php-parser/php5/parser.go

227 lines
4.9 KiB
Go
Raw Normal View History

2018-01-26 13:24:56 +00:00
package php5
import (
"strings"
2018-01-26 13:24:56 +00:00
"github.com/z7zmey/php-parser/errors"
2019-03-10 21:37:01 +00:00
"github.com/z7zmey/php-parser/freefloating"
2018-01-26 13:24:56 +00:00
"github.com/z7zmey/php-parser/node"
2018-11-05 15:14:09 +00:00
"github.com/z7zmey/php-parser/position"
2019-12-26 15:57:56 +00:00
"github.com/z7zmey/php-parser/positionbuilder"
"github.com/z7zmey/php-parser/scanner"
2018-01-26 13:24:56 +00:00
)
func (lval *yySymType) Token(t *scanner.Token) {
lval.token = t
}
// Parser structure
type Parser struct {
2019-03-10 21:37:01 +00:00
Lexer scanner.Scanner
2018-04-15 20:04:24 +00:00
currentToken *scanner.Token
2019-12-26 15:57:56 +00:00
positionBuilder *positionbuilder.PositionBuilder
rootNode node.Node
}
2018-01-26 13:24:56 +00:00
// NewParser creates and returns new Parser
2019-12-26 15:57:56 +00:00
func NewParser(src []byte, v string) *Parser {
2019-03-10 21:37:01 +00:00
lexer := scanner.NewLexer(src)
2019-12-26 15:57:56 +00:00
lexer.PHPVersion = v
return &Parser{
2018-04-10 13:19:47 +00:00
lexer,
nil,
nil,
nil,
}
}
2018-02-04 16:51:44 +00:00
// Lex proxy to lexer Lex
func (l *Parser) Lex(lval *yySymType) int {
t := l.Lexer.Lex(lval)
2018-04-15 20:04:24 +00:00
l.currentToken = lval.token
return t
}
func (l *Parser) Error(msg string) {
2018-11-05 15:14:09 +00:00
pos := &position.Position{
StartLine: l.currentToken.StartLine,
EndLine: l.currentToken.EndLine,
StartPos: l.currentToken.StartPos,
EndPos: l.currentToken.EndPos,
}
2019-03-10 21:37:01 +00:00
l.Lexer.AddError(errors.NewError(msg, pos))
}
2019-02-25 14:52:47 +00:00
func (l *Parser) WithFreeFloating() {
2019-03-10 21:37:01 +00:00
l.Lexer.SetWithFreeFloating(true)
}
// Parse the php7 Parser entrypoint
func (l *Parser) Parse() int {
// init
2019-03-10 21:37:01 +00:00
l.Lexer.SetErrors(nil)
l.rootNode = nil
2019-12-26 15:57:56 +00:00
l.positionBuilder = &positionbuilder.PositionBuilder{}
// parse
return yyParse(l)
2018-01-26 13:24:56 +00:00
}
// GetRootNode returns root node
func (l *Parser) GetRootNode() node.Node {
return l.rootNode
2018-01-26 13:24:56 +00:00
}
// GetErrors returns errors list
func (l *Parser) GetErrors() []*errors.Error {
2019-03-10 21:37:01 +00:00
return l.Lexer.GetErrors()
2018-01-29 19:12:12 +00:00
}
2018-06-06 23:25:27 +00:00
// helpers
func lastNode(nn []node.Node) node.Node {
2018-06-26 08:57:17 +00:00
if len(nn) == 0 {
return nil
}
2018-06-06 23:25:27 +00:00
return nn[len(nn)-1]
}
func firstNode(nn []node.Node) node.Node {
return nn[0]
}
2018-06-10 11:53:10 +00:00
func isDollar(r rune) bool {
return r == '$'
}
func (l *Parser) MoveFreeFloating(src node.Node, dst node.Node) {
2019-03-10 21:37:01 +00:00
if l.Lexer.GetWithFreeFloating() == false {
return
}
if src.GetFreeFloating() == nil {
return
}
l.setFreeFloating(dst, freefloating.Start, (*src.GetFreeFloating())[freefloating.Start])
delete((*src.GetFreeFloating()), freefloating.Start)
}
func (l *Parser) setFreeFloating(dst node.Node, p freefloating.Position, strings []freefloating.String) {
2019-03-10 21:37:01 +00:00
if l.Lexer.GetWithFreeFloating() == false {
return
}
if len(strings) == 0 {
return
}
dstCollection := dst.GetFreeFloating()
if *dstCollection == nil {
*dstCollection = make(freefloating.Collection)
}
(*dstCollection)[p] = strings
}
func (l *Parser) GetFreeFloatingToken(t *scanner.Token) []freefloating.String {
2019-03-10 21:37:01 +00:00
if l.Lexer.GetWithFreeFloating() == false {
return []freefloating.String{}
}
return t.GetFreeFloatingToken()
}
func (l *Parser) addDollarToken(v node.Node) {
2019-03-10 21:37:01 +00:00
if l.Lexer.GetWithFreeFloating() == false {
return
}
l.setFreeFloating(v, freefloating.Dollar, []freefloating.String{
{
StringType: freefloating.TokenType,
Value: "$",
Position: &position.Position{
StartLine: v.GetPosition().StartLine,
EndLine: v.GetPosition().StartLine,
StartPos: v.GetPosition().StartPos,
EndPos: v.GetPosition().StartPos + 1,
},
},
})
}
func (l *Parser) splitSemiColonAndPhpCloseTag(htmlNode node.Node, prevNode node.Node) {
2019-03-10 21:37:01 +00:00
if l.Lexer.GetWithFreeFloating() == false {
return
}
semiColon := (*prevNode.GetFreeFloating())[freefloating.SemiColon]
delete((*prevNode.GetFreeFloating()), freefloating.SemiColon)
if len(semiColon) == 0 {
return
}
p := semiColon[0].Position
if semiColon[0].Value[0] == ';' {
l.setFreeFloating(prevNode, freefloating.SemiColon, []freefloating.String{
{
StringType: freefloating.TokenType,
Value: ";",
Position: &position.Position{
StartLine: p.StartLine,
EndLine: p.StartLine,
StartPos: p.StartPos,
EndPos: p.StartPos + 1,
},
},
})
}
vlen := len(semiColon[0].Value)
tlen := 2
if strings.HasSuffix(semiColon[0].Value, "?>\n") {
tlen = 3
}
phpCloseTag := []freefloating.String{}
if vlen-tlen > 1 {
phpCloseTag = append(phpCloseTag, freefloating.String{
StringType: freefloating.WhiteSpaceType,
Value: semiColon[0].Value[1 : vlen-tlen],
Position: &position.Position{
StartLine: p.StartLine,
EndLine: p.EndLine,
StartPos: p.StartPos + 1,
EndPos: p.EndPos - tlen,
},
})
}
phpCloseTag = append(phpCloseTag, freefloating.String{
StringType: freefloating.WhiteSpaceType,
Value: semiColon[0].Value[vlen-tlen:],
Position: &position.Position{
StartLine: p.EndLine,
EndLine: p.EndLine,
StartPos: p.EndPos - tlen,
EndPos: p.EndPos,
},
})
l.setFreeFloating(htmlNode, freefloating.Start, append(phpCloseTag, (*htmlNode.GetFreeFloating())[freefloating.Start]...))
}
func (p *Parser) returnTokenToPool(yyDollar []yySymType, yyVAL *yySymType) {
for i := 1; i < len(yyDollar); i++ {
if yyDollar[i].token != nil {
2019-03-10 21:37:01 +00:00
p.Lexer.ReturnTokenToPool(yyDollar[i].token)
}
yyDollar[i].token = nil
}
yyVAL.token = nil
}