[refactoring] update internal parser

This commit is contained in:
Vadym Slizov 2020-06-29 14:15:58 +03:00
parent 009d638ee2
commit ee673e5667
3 changed files with 43 additions and 41 deletions

View File

@ -10,6 +10,7 @@ import (
"path/filepath"
"runtime"
"sync"
"time"
"github.com/pkg/profile"
"github.com/yookoala/realpath"
@ -28,6 +29,7 @@ var withFreeFloating *bool
var showResolvedNs *bool
var printBack *bool
var printPath *bool
var printExecTime *bool
type file struct {
path string
@ -40,6 +42,9 @@ type result struct {
}
func main() {
start := time.Now()
printExecTime = flag.Bool("time", false, "print execution time")
withFreeFloating = flag.Bool("ff", false, "parse and show free floating strings")
showResolvedNs = flag.Bool("r", false, "resolve names")
printBack = flag.Bool("pb", false, "print AST back into the parsed file")
@ -84,6 +89,11 @@ func main() {
wg.Wait()
close(fileCh)
close(resultCh)
elapsed := time.Since(start)
if *printExecTime {
log.Printf("took: %s", elapsed)
}
}
func processPath(pathList []string, fileCh chan<- *file) {

View File

@ -41,41 +41,41 @@ func NewParser(src []byte, v string, withTokens bool) *Parser {
}
// Lex proxy to scanner Lex
func (l *Parser) Lex(lval *yySymType) int {
t := l.Lexer.Lex()
func (p *Parser) Lex(lval *yySymType) int {
t := p.Lexer.Lex()
l.currentToken = t
p.currentToken = t
lval.token = t
return int(t.ID)
}
func (l *Parser) Error(msg string) {
var pos = l.currentToken.Position
func (p *Parser) Error(msg string) {
var pos = p.currentToken.Position
l.errors = append(l.errors, errors.NewError(msg, &pos))
p.errors = append(p.errors, errors.NewError(msg, &pos))
}
// GetErrors returns errors list
func (l *Parser) GetErrors() []*errors.Error {
return l.errors
func (p *Parser) GetErrors() []*errors.Error {
return p.errors
}
// Parse the php7 Parser entrypoint
func (l *Parser) Parse() int {
func (p *Parser) Parse() int {
// init
l.errors = nil
l.rootNode = nil
l.positionBuilder = &positionbuilder.PositionBuilder{}
p.errors = nil
p.rootNode = nil
p.positionBuilder = &positionbuilder.PositionBuilder{}
// parse
return yyParse(l)
return yyParse(p)
}
// GetRootNode returns root node
func (l *Parser) GetRootNode() ast.Vertex {
return l.rootNode
func (p *Parser) GetRootNode() ast.Vertex {
return p.rootNode
}
// helpers
@ -87,16 +87,12 @@ func lastNode(nn []ast.Vertex) ast.Vertex {
return nn[len(nn)-1]
}
func firstNode(nn []ast.Vertex) ast.Vertex {
return nn[0]
}
func isDollar(r rune) bool {
return r == '$'
}
func (l *Parser) MoveFreeFloating(src ast.Vertex, dst ast.Vertex) {
if l.withTokens == false {
func (p *Parser) MoveFreeFloating(src ast.Vertex, dst ast.Vertex) {
if p.withTokens == false {
return
}
@ -104,12 +100,12 @@ func (l *Parser) MoveFreeFloating(src ast.Vertex, dst ast.Vertex) {
return
}
l.setFreeFloating(dst, token.Start, src.GetNode().Tokens[token.Start])
p.setFreeFloating(dst, token.Start, src.GetNode().Tokens[token.Start])
delete(src.GetNode().Tokens, token.Start)
}
func (l *Parser) setFreeFloating(dst ast.Vertex, p token.Position, strings []token.Token) {
if l.withTokens == false {
func (p *Parser) setFreeFloating(dst ast.Vertex, pos token.Position, strings []token.Token) {
if p.withTokens == false {
return
}
@ -122,11 +118,11 @@ func (l *Parser) setFreeFloating(dst ast.Vertex, p token.Position, strings []tok
*dstCollection = make(token.Collection)
}
(*dstCollection)[p] = strings
(*dstCollection)[pos] = strings
}
func (l *Parser) GetFreeFloatingToken(t *scanner.Token) []token.Token {
if l.withTokens == false {
func (p *Parser) GetFreeFloatingToken(t *scanner.Token) []token.Token {
if p.withTokens == false {
return []token.Token{}
}
@ -138,12 +134,12 @@ func (l *Parser) GetFreeFloatingToken(t *scanner.Token) []token.Token {
}
}
func (l *Parser) addDollarToken(v ast.Vertex) {
if l.withTokens == false {
func (p *Parser) addDollarToken(v ast.Vertex) {
if p.withTokens == false {
return
}
l.setFreeFloating(v, token.Dollar, []token.Token{
p.setFreeFloating(v, token.Dollar, []token.Token{
{
ID: token.ID('$'),
Value: []byte("$"),
@ -151,8 +147,8 @@ func (l *Parser) addDollarToken(v ast.Vertex) {
})
}
func (l *Parser) splitSemiColonAndPhpCloseTag(htmlNode ast.Vertex, prevNode ast.Vertex) {
if l.withTokens == false {
func (p *Parser) splitSemiColonAndPhpCloseTag(htmlNode ast.Vertex, prevNode ast.Vertex) {
if p.withTokens == false {
return
}
@ -163,7 +159,7 @@ func (l *Parser) splitSemiColonAndPhpCloseTag(htmlNode ast.Vertex, prevNode ast.
}
if semiColon[0].Value[0] == ';' {
l.setFreeFloating(prevNode, token.SemiColon, []token.Token{
p.setFreeFloating(prevNode, token.SemiColon, []token.Token{
{
ID: token.ID(';'),
Value: semiColon[0].Value[0:1],
@ -192,7 +188,7 @@ func (l *Parser) splitSemiColonAndPhpCloseTag(htmlNode ast.Vertex, prevNode ast.
Value: semiColon[0].Value[vlen-tlen:],
})
l.setFreeFloating(htmlNode, token.Start, append(phpCloseTag, htmlNode.GetNode().Tokens[token.Start]...))
p.setFreeFloating(htmlNode, token.Start, append(phpCloseTag, htmlNode.GetNode().Tokens[token.Start]...))
}
func (p *Parser) returnTokenToPool(yyDollar []yySymType, yyVAL *yySymType) {

View File

@ -85,10 +85,6 @@ func lastNode(nn []ast.Vertex) ast.Vertex {
return nn[len(nn)-1]
}
func firstNode(nn []ast.Vertex) ast.Vertex {
return nn[0]
}
func isDollar(r rune) bool {
return r == '$'
}
@ -106,8 +102,8 @@ func (l *Parser) MoveFreeFloating(src ast.Vertex, dst ast.Vertex) {
delete(src.GetNode().Tokens, token.Start)
}
func (l *Parser) setFreeFloating(dst ast.Vertex, p token.Position, strings []token.Token) {
if l.withTokens == false {
func (p *Parser) setFreeFloating(dst ast.Vertex, pos token.Position, strings []token.Token) {
if p.withTokens == false {
return
}
@ -120,7 +116,7 @@ func (l *Parser) setFreeFloating(dst ast.Vertex, p token.Position, strings []tok
*dstCollection = make(token.Collection)
}
(*dstCollection)[p] = strings
(*dstCollection)[pos] = strings
}
func (l *Parser) GetFreeFloatingToken(t *scanner.Token) []token.Token {