2018-01-26 13:24:56 +00:00
|
|
|
package php7
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
2018-11-05 15:14:09 +00:00
|
|
|
"github.com/z7zmey/php-parser/position"
|
|
|
|
|
2018-06-29 21:51:11 +00:00
|
|
|
"github.com/z7zmey/php-parser/meta"
|
|
|
|
|
2018-04-09 20:08:29 +00:00
|
|
|
"github.com/z7zmey/php-parser/errors"
|
2018-01-26 13:24:56 +00:00
|
|
|
"github.com/z7zmey/php-parser/node"
|
2018-04-15 18:39:26 +00:00
|
|
|
"github.com/z7zmey/php-parser/parser"
|
2018-04-09 21:52:38 +00:00
|
|
|
"github.com/z7zmey/php-parser/scanner"
|
2018-01-26 13:24:56 +00:00
|
|
|
)
|
|
|
|
|
2018-04-15 19:56:20 +00:00
|
|
|
func (lval *yySymType) Token(t *scanner.Token) {
|
2018-04-09 21:52:38 +00:00
|
|
|
lval.token = t
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parser structure
|
|
|
|
type Parser struct {
|
2018-04-10 13:19:47 +00:00
|
|
|
*scanner.Lexer
|
2018-04-10 17:15:15 +00:00
|
|
|
path string
|
2018-04-15 20:04:24 +00:00
|
|
|
currentToken *scanner.Token
|
2018-04-15 19:06:02 +00:00
|
|
|
positionBuilder *parser.PositionBuilder
|
2018-04-09 21:52:38 +00:00
|
|
|
rootNode node.Node
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewParser creates and returns new Parser
|
2018-04-10 17:15:15 +00:00
|
|
|
func NewParser(src io.Reader, path string) *Parser {
|
|
|
|
lexer := scanner.NewLexer(src, path)
|
2018-04-09 21:52:38 +00:00
|
|
|
|
|
|
|
return &Parser{
|
2018-04-10 13:19:47 +00:00
|
|
|
lexer,
|
2018-04-10 17:15:15 +00:00
|
|
|
path,
|
2018-04-09 21:52:38 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
}
|
|
|
|
}
|
2018-01-26 13:24:56 +00:00
|
|
|
|
2018-04-09 21:52:38 +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
|
2018-04-09 21:52:38 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
l.Lexer.Errors = append(l.Lexer.Errors, errors.NewError(msg, pos))
|
2018-04-09 21:52:38 +00:00
|
|
|
}
|
|
|
|
|
2018-06-29 21:51:11 +00:00
|
|
|
func (l *Parser) WithMeta() {
|
|
|
|
l.Lexer.WithMeta = true
|
|
|
|
}
|
|
|
|
|
2018-04-09 21:52:38 +00:00
|
|
|
// Parse the php7 Parser entrypoint
|
|
|
|
func (l *Parser) Parse() int {
|
|
|
|
// init
|
2018-11-05 14:56:27 +00:00
|
|
|
l.Lexer.Errors = nil
|
2018-04-09 21:52:38 +00:00
|
|
|
l.rootNode = nil
|
2018-06-24 07:19:44 +00:00
|
|
|
l.positionBuilder = &parser.PositionBuilder{}
|
2018-04-09 21:52:38 +00:00
|
|
|
|
|
|
|
// parse
|
|
|
|
|
|
|
|
return yyParse(l)
|
2018-01-26 13:24:56 +00:00
|
|
|
}
|
|
|
|
|
2018-04-10 17:15:15 +00:00
|
|
|
// GetPath return path to file
|
|
|
|
func (l *Parser) GetPath() string {
|
|
|
|
return l.path
|
|
|
|
}
|
|
|
|
|
2018-04-09 21:52:38 +00:00
|
|
|
// GetRootNode returns root node
|
|
|
|
func (l *Parser) GetRootNode() node.Node {
|
|
|
|
return l.rootNode
|
2018-01-26 13:24:56 +00:00
|
|
|
}
|
|
|
|
|
2018-04-09 21:52:38 +00:00
|
|
|
// GetErrors returns errors list
|
|
|
|
func (l *Parser) GetErrors() []*errors.Error {
|
2018-11-05 14:56:27 +00:00
|
|
|
return l.Lexer.Errors
|
2018-01-26 13:24:56 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 15:02:58 +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-05-27 15:02:58 +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 == '$'
|
|
|
|
}
|
2018-06-10 23:41:12 +00:00
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func (l *Parser) appendMetaToken(n node.Node, t *scanner.Token, tn meta.TokenName) {
|
|
|
|
if !l.Lexer.WithMeta {
|
|
|
|
return
|
2018-06-29 21:51:11 +00:00
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
m := &meta.Data{
|
|
|
|
Value: t.Value,
|
|
|
|
Type: meta.TokenType,
|
|
|
|
Position: l.positionBuilder.NewTokenPosition(t),
|
|
|
|
TokenName: tn,
|
|
|
|
}
|
|
|
|
|
|
|
|
n.GetMeta().Push(m)
|
|
|
|
}
|
|
|
|
|
2018-10-24 14:04:13 +00:00
|
|
|
func (l *Parser) appendMeta(n node.Node, m *meta.Data, tn meta.TokenName) {
|
|
|
|
if !l.Lexer.WithMeta {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
n.GetMeta().Push(m)
|
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
func (l *Parser) prependMetaToken(n node.Node, t *scanner.Token, tn meta.TokenName) {
|
|
|
|
if !l.Lexer.WithMeta {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m := &meta.Data{
|
|
|
|
Value: t.Value,
|
|
|
|
Type: meta.TokenType,
|
|
|
|
Position: l.positionBuilder.NewTokenPosition(t),
|
|
|
|
TokenName: tn,
|
|
|
|
}
|
|
|
|
|
|
|
|
n.GetMeta().Unshift(m)
|
2018-06-29 21:51:11 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:41:12 +00:00
|
|
|
func (p *Parser) returnTokenToPool(yyDollar []yySymType, yyVAL *yySymType) {
|
|
|
|
for i := 1; i < len(yyDollar); i++ {
|
|
|
|
if yyDollar[i].token != nil {
|
|
|
|
p.TokenPool.Put(yyDollar[i].token)
|
|
|
|
}
|
|
|
|
yyDollar[i].token = nil
|
|
|
|
}
|
|
|
|
yyVAL.token = nil
|
|
|
|
}
|