php-parser/internal/php5/parser.go

172 lines
3.6 KiB
Go
Raw Normal View History

2018-01-26 13:24:56 +00:00
package php5
import (
2020-05-13 17:18:53 +00:00
"bytes"
2020-05-12 21:16:36 +00:00
2020-05-13 17:18:53 +00:00
"github.com/z7zmey/php-parser/internal/scanner"
2020-05-12 21:16:36 +00:00
"github.com/z7zmey/php-parser/pkg/ast"
"github.com/z7zmey/php-parser/pkg/errors"
2020-05-13 17:18:53 +00:00
"github.com/z7zmey/php-parser/pkg/token"
2018-01-26 13:24:56 +00:00
)
// Parser structure
type Parser struct {
2020-06-29 20:00:56 +00:00
Lexer *scanner.Lexer
2020-08-17 17:31:04 +00:00
currentToken *token.Token
2020-06-29 20:00:56 +00:00
rootNode ast.Vertex
errHandlerFunc func(*errors.Error)
}
2018-01-26 13:24:56 +00:00
// NewParser creates and returns new Parser
func NewParser(lexer *scanner.Lexer, errHandlerFunc func(*errors.Error)) *Parser {
2020-06-29 20:00:56 +00:00
return &Parser{
Lexer: lexer,
errHandlerFunc: errHandlerFunc,
}
}
2018-02-04 16:51:44 +00:00
// Lex proxy to scanner Lex
2020-06-29 11:15:58 +00:00
func (p *Parser) Lex(lval *yySymType) int {
t := p.Lexer.Lex()
2020-06-29 11:15:58 +00:00
p.currentToken = t
lval.token = t
return int(t.ID)
}
2020-06-29 11:15:58 +00:00
func (p *Parser) Error(msg string) {
if p.errHandlerFunc == nil {
return
}
2020-08-17 17:31:04 +00:00
p.errHandlerFunc(errors.NewError(msg, p.currentToken.Position))
}
// Parse the php7 Parser entrypoint
2020-06-29 11:15:58 +00:00
func (p *Parser) Parse() int {
p.rootNode = nil
return yyParse(p)
2018-01-26 13:24:56 +00:00
}
// GetRootNode returns root node
2020-06-29 11:15:58 +00:00
func (p *Parser) GetRootNode() ast.Vertex {
return p.rootNode
2018-01-26 13:24:56 +00:00
}
2018-06-06 23:25:27 +00:00
// helpers
2020-05-13 17:18:53 +00:00
func lastNode(nn []ast.Vertex) ast.Vertex {
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]
}
2020-06-29 11:15:58 +00:00
func (p *Parser) MoveFreeFloating(src ast.Vertex, dst ast.Vertex) {
if _, ok := src.GetNode().Tokens[token.Start]; !ok {
return
}
2020-05-13 17:18:53 +00:00
if src.GetNode().Tokens == nil {
return
}
dstCollection := &dst.GetNode().Tokens
if *dstCollection == nil {
*dstCollection = make(token.Collection)
}
(*dstCollection)[token.Start] = src.GetNode().Tokens[token.Start]
2020-05-13 17:18:53 +00:00
delete(src.GetNode().Tokens, token.Start)
}
2020-08-17 17:31:04 +00:00
func (p *Parser) setFreeFloating(dst ast.Vertex, pos token.Position, tokens []*token.Token) {
if len(tokens) == 0 {
return
}
dstCollection := &dst.GetNode().Tokens
if *dstCollection == nil {
*dstCollection = make(token.Collection)
}
l := len(tokens)
for _, v := range tokens[0 : l-1] {
(*dstCollection)[pos] = append((*dstCollection)[pos], v)
}
}
2020-08-17 17:31:04 +00:00
func (p *Parser) setFreeFloatingTokens(dst ast.Vertex, pos token.Position, tokens []*token.Token) {
if len(tokens) == 0 {
return
}
2020-05-13 17:18:53 +00:00
dstCollection := &dst.GetNode().Tokens
if *dstCollection == nil {
2020-05-13 17:18:53 +00:00
*dstCollection = make(token.Collection)
}
2020-08-17 17:31:04 +00:00
(*dstCollection)[pos] = make([]*token.Token, 0)
for _, v := range tokens {
(*dstCollection)[pos] = append((*dstCollection)[pos], v)
}
}
2020-08-17 17:31:04 +00:00
func (p *Parser) setToken(dst ast.Vertex, pos token.Position, tokens []*token.Token) {
if len(tokens) == 0 {
return
}
dstCollection := &dst.GetNode().Tokens
if *dstCollection == nil {
*dstCollection = make(token.Collection)
2020-05-17 19:56:32 +00:00
}
l := len(tokens)
(*dstCollection)[pos] = append((*dstCollection)[pos], tokens[l-1])
2020-05-17 19:56:32 +00:00
}
2020-06-29 11:15:58 +00:00
func (p *Parser) splitSemiColonAndPhpCloseTag(htmlNode ast.Vertex, prevNode ast.Vertex) {
if _, ok := prevNode.GetNode().Tokens[token.SemiColon]; !ok {
return
}
2020-05-13 17:18:53 +00:00
semiColon := prevNode.GetNode().Tokens[token.SemiColon]
delete(prevNode.GetNode().Tokens, token.SemiColon)
if len(semiColon) == 0 {
return
}
if semiColon[0].Value[0] == ';' {
2020-08-17 17:31:04 +00:00
p.setFreeFloatingTokens(prevNode, token.SemiColon, []*token.Token{
{
2020-05-13 17:18:53 +00:00
ID: token.ID(';'),
Value: semiColon[0].Value[0:1],
},
})
}
vlen := len(semiColon[0].Value)
tlen := 2
2020-05-13 17:18:53 +00:00
if bytes.HasSuffix(semiColon[0].Value, []byte("?>\n")) {
tlen = 3
}
2020-08-17 17:31:04 +00:00
phpCloseTag := []*token.Token{}
if vlen-tlen > 1 {
2020-08-17 17:31:04 +00:00
phpCloseTag = append(phpCloseTag, &token.Token{
2020-05-13 17:18:53 +00:00
ID: token.T_WHITESPACE,
Value: semiColon[0].Value[1 : vlen-tlen],
})
}
2020-08-17 17:31:04 +00:00
phpCloseTag = append(phpCloseTag, &token.Token{
2020-05-13 17:18:53 +00:00
ID: T_CLOSE_TAG,
Value: semiColon[0].Value[vlen-tlen:],
})
p.setFreeFloatingTokens(htmlNode, token.Start, append(phpCloseTag, htmlNode.GetNode().Tokens[token.Start]...))
}