php-parser/php7/parser.go

110 lines
2.0 KiB
Go
Raw Normal View History

2018-01-26 13:24:56 +00:00
package php7
import (
"io"
"github.com/z7zmey/php-parser/comment"
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"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/scanner"
2018-01-26 13:24:56 +00:00
"github.com/z7zmey/php-parser/token"
)
func (lval *yySymType) Token(t token.Token) {
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
lastToken *token.Token
positionBuilder *position.Builder
errors []*errors.Error
rootNode node.Node
comments comment.Comments
positions position.Positions
}
// 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)
return &Parser{
2018-04-10 13:19:47 +00:00
lexer,
2018-04-10 17:15:15 +00:00
path,
nil,
nil,
nil,
nil,
nil,
nil,
}
}
2018-01-26 13:24:56 +00:00
// Lex proxy to lexer Lex
func (l *Parser) Lex(lval *yySymType) int {
t := l.Lexer.Lex(lval)
l.lastToken = &lval.token
return t
}
func (l *Parser) Error(msg string) {
l.errors = append(l.errors, errors.NewError(msg, *l.lastToken))
}
// Parse the php7 Parser entrypoint
func (l *Parser) Parse() int {
2018-02-06 10:39:42 +00:00
yyDebug = 0
yyErrorVerbose = true
2018-04-09 20:08:29 +00:00
// init
l.errors = nil
l.rootNode = nil
l.comments = comment.Comments{}
l.positions = position.Positions{}
l.positionBuilder = &position.Builder{
Positions: &l.positions,
}
// parse
return yyParse(l)
2018-01-26 13:24:56 +00:00
}
func (l *Parser) listGetFirstNodeComments(list []node.Node) []comment.Comment {
2018-01-26 13:24:56 +00:00
if len(list) == 0 {
return nil
}
node := list[0]
return l.comments[node]
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
}
// 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 {
return l.errors
2018-01-26 13:24:56 +00:00
}
// GetComments returns comments list
func (l *Parser) GetComments() comment.Comments {
return l.comments
2018-02-06 10:39:42 +00:00
}
2018-02-18 17:44:17 +00:00
// GetPositions returns positions list
func (l *Parser) GetPositions() position.Positions {
return l.positions
2018-02-18 17:44:17 +00:00
}