php-parser/internal/php7/parser.go

66 lines
1.3 KiB
Go
Raw Normal View History

2020-05-12 21:16:36 +00:00
package php7
import (
2020-12-08 00:08:59 +00:00
builder "github.com/z7zmey/php-parser/internal/position"
2020-05-12 21:16:36 +00:00
"github.com/z7zmey/php-parser/internal/scanner"
"github.com/z7zmey/php-parser/pkg/ast"
"github.com/z7zmey/php-parser/pkg/errors"
"github.com/z7zmey/php-parser/pkg/token"
)
// Parser structure
type Parser struct {
2020-06-29 20:00:56 +00:00
Lexer *scanner.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-08 00:08:59 +00:00
builder *builder.Builder
2020-05-12 21:16:36 +00:00
}
// NewParser creates and returns new Parser
func NewParser(lexer *scanner.Lexer, errHandlerFunc func(*errors.Error)) *Parser {
2020-06-29 20:00:56 +00:00
return &Parser{
Lexer: lexer,
errHandlerFunc: errHandlerFunc,
2020-12-08 00:08:59 +00:00
builder: builder.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
}
// helpers
func lastNode(nn []ast.Vertex) ast.Vertex {
if len(nn) == 0 {
return nil
}
return nn[len(nn)-1]
}