php-parser/internal/php7/parser.go

57 lines
1.1 KiB
Go
Raw Normal View History

2020-05-12 21:16:36 +00:00
package php7
import (
"github.com/VKCOM/php-parser/internal/position"
"github.com/VKCOM/php-parser/pkg/ast"
"github.com/VKCOM/php-parser/pkg/conf"
"github.com/VKCOM/php-parser/pkg/errors"
"github.com/VKCOM/php-parser/pkg/token"
2020-05-12 21:16:36 +00:00
)
// Parser structure
type Parser struct {
Lexer *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)
2020-12-29 19:23:22 +00:00
builder *position.Builder
2020-05-12 21:16:36 +00:00
}
// NewParser creates and returns new Parser
func NewParser(lexer *Lexer, config conf.Config) *Parser {
2020-06-29 20:00:56 +00:00
return &Parser{
Lexer: lexer,
2020-12-29 19:23:22 +00:00
errHandlerFunc: config.ErrorHandlerFunc,
builder: position.NewBuilder(),
}
2020-05-12 21:16:36 +00:00
}
func (p *Parser) Lex(lval *yySymType) int {
t := p.Lexer.Lex()
p.currentToken = t
lval.token = t
return int(t.ID)
2020-05-12 21:16:36 +00:00
}
func (p *Parser) Error(msg string) {
if p.errHandlerFunc == nil {
return
}
2020-07-04 15:41:12 +00:00
2020-08-17 17:31:04 +00:00
p.errHandlerFunc(errors.NewError(msg, p.currentToken.Position))
2020-05-12 21:16:36 +00:00
}
// Parse the php7 Parser entrypoint
func (p *Parser) Parse() int {
p.rootNode = nil
2020-05-12 21:16:36 +00:00
return yyParse(p)
2020-05-12 21:16:36 +00:00
}
// GetRootNode returns root node
func (p *Parser) GetRootNode() ast.Vertex {
return p.rootNode
2020-05-12 21:16:36 +00:00
}