php-parser/internal/php8/parser.go

58 lines
1.1 KiB
Go
Raw Permalink Normal View History

package php8
import (
2023-12-09 21:36:19 +00:00
"git.maride.cc/maride/php-parser/internal/position"
"git.maride.cc/maride/php-parser/pkg/ast"
"git.maride.cc/maride/php-parser/pkg/conf"
"git.maride.cc/maride/php-parser/pkg/errors"
"git.maride.cc/maride/php-parser/pkg/token"
)
// Parser structure
type Parser struct {
Lexer *Lexer
currentToken *token.Token
rootNode ast.Vertex
errHandlerFunc func(*errors.Error)
builder *Builder
}
// NewParser creates and returns new Parser
func NewParser(lexer *Lexer, config conf.Config) *Parser {
p := &Parser{
Lexer: lexer,
errHandlerFunc: config.ErrorHandlerFunc,
}
p.builder = NewBuilder(position.NewBuilder(), p)
return p
}
func (p *Parser) Lex(lval *yySymType) int {
t := p.Lexer.Lex()
p.currentToken = t
lval.token = t
return int(t.ID)
}
func (p *Parser) Error(msg string) {
if p.errHandlerFunc == nil {
return
}
p.errHandlerFunc(errors.NewError(msg, p.currentToken.Position))
}
// Parse the php7 Parser entrypoint
func (p *Parser) Parse() int {
p.rootNode = nil
return yyParse(p)
}
// GetRootNode returns root node
func (p *Parser) GetRootNode() ast.Vertex {
return p.rootNode
}