[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" "path/filepath"
"runtime" "runtime"
"sync" "sync"
"time"
"github.com/pkg/profile" "github.com/pkg/profile"
"github.com/yookoala/realpath" "github.com/yookoala/realpath"
@ -28,6 +29,7 @@ var withFreeFloating *bool
var showResolvedNs *bool var showResolvedNs *bool
var printBack *bool var printBack *bool
var printPath *bool var printPath *bool
var printExecTime *bool
type file struct { type file struct {
path string path string
@ -40,6 +42,9 @@ type result struct {
} }
func main() { func main() {
start := time.Now()
printExecTime = flag.Bool("time", false, "print execution time")
withFreeFloating = flag.Bool("ff", false, "parse and show free floating strings") withFreeFloating = flag.Bool("ff", false, "parse and show free floating strings")
showResolvedNs = flag.Bool("r", false, "resolve names") showResolvedNs = flag.Bool("r", false, "resolve names")
printBack = flag.Bool("pb", false, "print AST back into the parsed file") printBack = flag.Bool("pb", false, "print AST back into the parsed file")
@ -84,6 +89,11 @@ func main() {
wg.Wait() wg.Wait()
close(fileCh) close(fileCh)
close(resultCh) close(resultCh)
elapsed := time.Since(start)
if *printExecTime {
log.Printf("took: %s", elapsed)
}
} }
func processPath(pathList []string, fileCh chan<- *file) { 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 // Lex proxy to scanner Lex
func (l *Parser) Lex(lval *yySymType) int { func (p *Parser) Lex(lval *yySymType) int {
t := l.Lexer.Lex() t := p.Lexer.Lex()
l.currentToken = t p.currentToken = t
lval.token = t lval.token = t
return int(t.ID) return int(t.ID)
} }
func (l *Parser) Error(msg string) { func (p *Parser) Error(msg string) {
var pos = l.currentToken.Position 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 // GetErrors returns errors list
func (l *Parser) GetErrors() []*errors.Error { func (p *Parser) GetErrors() []*errors.Error {
return l.errors return p.errors
} }
// Parse the php7 Parser entrypoint // Parse the php7 Parser entrypoint
func (l *Parser) Parse() int { func (p *Parser) Parse() int {
// init // init
l.errors = nil p.errors = nil
l.rootNode = nil p.rootNode = nil
l.positionBuilder = &positionbuilder.PositionBuilder{} p.positionBuilder = &positionbuilder.PositionBuilder{}
// parse // parse
return yyParse(l) return yyParse(p)
} }
// GetRootNode returns root node // GetRootNode returns root node
func (l *Parser) GetRootNode() ast.Vertex { func (p *Parser) GetRootNode() ast.Vertex {
return l.rootNode return p.rootNode
} }
// helpers // helpers
@ -87,16 +87,12 @@ func lastNode(nn []ast.Vertex) ast.Vertex {
return nn[len(nn)-1] return nn[len(nn)-1]
} }
func firstNode(nn []ast.Vertex) ast.Vertex {
return nn[0]
}
func isDollar(r rune) bool { func isDollar(r rune) bool {
return r == '$' return r == '$'
} }
func (l *Parser) MoveFreeFloating(src ast.Vertex, dst ast.Vertex) { func (p *Parser) MoveFreeFloating(src ast.Vertex, dst ast.Vertex) {
if l.withTokens == false { if p.withTokens == false {
return return
} }
@ -104,12 +100,12 @@ func (l *Parser) MoveFreeFloating(src ast.Vertex, dst ast.Vertex) {
return 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) delete(src.GetNode().Tokens, token.Start)
} }
func (l *Parser) setFreeFloating(dst ast.Vertex, p token.Position, strings []token.Token) { func (p *Parser) setFreeFloating(dst ast.Vertex, pos token.Position, strings []token.Token) {
if l.withTokens == false { if p.withTokens == false {
return return
} }
@ -122,11 +118,11 @@ func (l *Parser) setFreeFloating(dst ast.Vertex, p token.Position, strings []tok
*dstCollection = make(token.Collection) *dstCollection = make(token.Collection)
} }
(*dstCollection)[p] = strings (*dstCollection)[pos] = strings
} }
func (l *Parser) GetFreeFloatingToken(t *scanner.Token) []token.Token { func (p *Parser) GetFreeFloatingToken(t *scanner.Token) []token.Token {
if l.withTokens == false { if p.withTokens == false {
return []token.Token{} return []token.Token{}
} }
@ -138,12 +134,12 @@ func (l *Parser) GetFreeFloatingToken(t *scanner.Token) []token.Token {
} }
} }
func (l *Parser) addDollarToken(v ast.Vertex) { func (p *Parser) addDollarToken(v ast.Vertex) {
if l.withTokens == false { if p.withTokens == false {
return return
} }
l.setFreeFloating(v, token.Dollar, []token.Token{ p.setFreeFloating(v, token.Dollar, []token.Token{
{ {
ID: token.ID('$'), ID: token.ID('$'),
Value: []byte("$"), Value: []byte("$"),
@ -151,8 +147,8 @@ func (l *Parser) addDollarToken(v ast.Vertex) {
}) })
} }
func (l *Parser) splitSemiColonAndPhpCloseTag(htmlNode ast.Vertex, prevNode ast.Vertex) { func (p *Parser) splitSemiColonAndPhpCloseTag(htmlNode ast.Vertex, prevNode ast.Vertex) {
if l.withTokens == false { if p.withTokens == false {
return return
} }
@ -163,7 +159,7 @@ func (l *Parser) splitSemiColonAndPhpCloseTag(htmlNode ast.Vertex, prevNode ast.
} }
if semiColon[0].Value[0] == ';' { if semiColon[0].Value[0] == ';' {
l.setFreeFloating(prevNode, token.SemiColon, []token.Token{ p.setFreeFloating(prevNode, token.SemiColon, []token.Token{
{ {
ID: token.ID(';'), ID: token.ID(';'),
Value: semiColon[0].Value[0:1], 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:], 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) { 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] return nn[len(nn)-1]
} }
func firstNode(nn []ast.Vertex) ast.Vertex {
return nn[0]
}
func isDollar(r rune) bool { func isDollar(r rune) bool {
return r == '$' return r == '$'
} }
@ -106,8 +102,8 @@ func (l *Parser) MoveFreeFloating(src ast.Vertex, dst ast.Vertex) {
delete(src.GetNode().Tokens, token.Start) delete(src.GetNode().Tokens, token.Start)
} }
func (l *Parser) setFreeFloating(dst ast.Vertex, p token.Position, strings []token.Token) { func (p *Parser) setFreeFloating(dst ast.Vertex, pos token.Position, strings []token.Token) {
if l.withTokens == false { if p.withTokens == false {
return return
} }
@ -120,7 +116,7 @@ func (l *Parser) setFreeFloating(dst ast.Vertex, p token.Position, strings []tok
*dstCollection = make(token.Collection) *dstCollection = make(token.Collection)
} }
(*dstCollection)[p] = strings (*dstCollection)[pos] = strings
} }
func (l *Parser) GetFreeFloatingToken(t *scanner.Token) []token.Token { func (l *Parser) GetFreeFloatingToken(t *scanner.Token) []token.Token {