php-parser/php5/parser.go

109 lines
2.0 KiB
Go
Raw Normal View History

2018-01-26 15:24:56 +02:00
package php5
import (
"io"
"github.com/z7zmey/php-parser/comment"
"github.com/z7zmey/php-parser/errors"
2018-01-26 15:24:56 +02:00
"github.com/z7zmey/php-parser/node"
2018-04-15 21:39:26 +03:00
"github.com/z7zmey/php-parser/parser"
"github.com/z7zmey/php-parser/scanner"
2018-01-26 15:24:56 +02:00
)
func (lval *yySymType) Token(t *scanner.Token) {
lval.token = t
}
// Parser structure
type Parser struct {
2018-04-10 16:19:47 +03:00
*scanner.Lexer
2018-04-10 20:15:15 +03:00
path string
2018-04-15 23:04:24 +03:00
currentToken *scanner.Token
2018-04-15 22:06:02 +03:00
positionBuilder *parser.PositionBuilder
errors []*errors.Error
rootNode node.Node
comments parser.Comments
positions parser.Positions
}
2018-01-26 15:24:56 +02:00
// NewParser creates and returns new Parser
2018-04-10 20:15:15 +03:00
func NewParser(src io.Reader, path string) *Parser {
lexer := scanner.NewLexer(src, path)
return &Parser{
2018-04-10 16:19:47 +03:00
lexer,
2018-04-10 20:15:15 +03:00
path,
nil,
nil,
nil,
nil,
nil,
nil,
}
}
2018-02-04 18:51:44 +02:00
// Lex proxy to lexer Lex
func (l *Parser) Lex(lval *yySymType) int {
t := l.Lexer.Lex(lval)
2018-04-15 23:04:24 +03:00
l.currentToken = lval.token
return t
}
func (l *Parser) Error(msg string) {
2018-04-15 23:04:24 +03:00
l.errors = append(l.errors, errors.NewError(msg, l.currentToken))
}
// Parse the php7 Parser entrypoint
func (l *Parser) Parse() int {
2018-02-04 18:51:44 +02:00
yyDebug = 0
yyErrorVerbose = true
// init
l.errors = nil
l.rootNode = nil
l.comments = parser.Comments{}
l.positions = parser.Positions{}
2018-04-15 22:06:02 +03:00
l.positionBuilder = &parser.PositionBuilder{
Positions: &l.positions,
}
// parse
return yyParse(l)
2018-01-26 15:24:56 +02:00
}
2018-04-15 15:55:33 +03:00
func (l *Parser) listGetFirstNodeComments(list []node.Node) []*comment.Comment {
2018-01-26 15:24:56 +02:00
if len(list) == 0 {
return nil
}
node := list[0]
return l.comments[node]
2018-01-26 15:24:56 +02:00
}
2018-04-10 20:15:15 +03:00
// GetPath return path to file
func (l *Parser) GetPath() string {
return l.path
}
// GetRootNode returns root node
func (l *Parser) GetRootNode() node.Node {
return l.rootNode
2018-01-26 15:24:56 +02:00
}
// GetErrors returns errors list
func (l *Parser) GetErrors() []*errors.Error {
return l.errors
2018-01-29 21:12:12 +02:00
}
// GetComments returns comments list
func (l *Parser) GetComments() parser.Comments {
return l.comments
2018-02-01 16:07:18 +02:00
}
2018-02-18 19:44:17 +02:00
// GetPositions returns positions list
func (l *Parser) GetPositions() parser.Positions {
return l.positions
2018-02-18 19:44:17 +02:00
}