2020-05-12 21:16:36 +00:00
|
|
|
package php7
|
|
|
|
|
|
|
|
import (
|
2021-07-30 18:01:34 +00:00
|
|
|
"github.com/VKCOM/php-parser/internal/position"
|
|
|
|
"github.com/VKCOM/php-parser/internal/scanner"
|
|
|
|
"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 {
|
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-29 19:23:22 +00:00
|
|
|
builder *position.Builder
|
2020-05-12 21:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewParser creates and returns new Parser
|
2021-02-13 21:54:34 +00:00
|
|
|
func NewParser(lexer *scanner.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-18 18:15:07 +00:00
|
|
|
}
|
2020-05-12 21:16:36 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 11:52:43 +00:00
|
|
|
func (p *Parser) Lex(lval *yySymType) int {
|
|
|
|
t := p.Lexer.Lex()
|
2020-05-17 22:01:35 +00:00
|
|
|
|
2020-06-29 11:52:43 +00:00
|
|
|
p.currentToken = t
|
2020-05-17 22:01:35 +00:00
|
|
|
lval.token = t
|
|
|
|
|
|
|
|
return int(t.ID)
|
2020-05-12 21:16:36 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 11:52:43 +00:00
|
|
|
func (p *Parser) Error(msg string) {
|
2020-07-04 15:04:12 +00:00
|
|
|
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
|
2020-06-29 11:52:43 +00:00
|
|
|
func (p *Parser) Parse() int {
|
|
|
|
p.rootNode = nil
|
2020-05-12 21:16:36 +00:00
|
|
|
|
2020-06-29 11:52:43 +00:00
|
|
|
return yyParse(p)
|
2020-05-12 21:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRootNode returns root node
|
2020-06-29 11:52:43 +00:00
|
|
|
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]
|
|
|
|
}
|