diff --git a/Makefile b/Makefile index db2c383..88be435 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,20 @@ PHPFILE=example.php -all: ./php5/php5.go ./php7/php7.go ./scanner/scanner.go - rm -f y.output - gofmt -l -s -w *.go +all: compile run + +build: + gofmt -l -s -w **/*.go go build -run: all +run: build ./php-parser $(PHPFILE) test: all go test ./... --cover +compile: ./php5/php5.go ./php7/php7.go ./scanner/scanner.go + rm -f y.output + ./scanner/scanner.go: ./scanner/scanner.l golex -o $@ $< diff --git a/main.go b/main.go index 35488fc..c24f112 100644 --- a/main.go +++ b/main.go @@ -8,11 +8,20 @@ import ( "path/filepath" "github.com/yookoala/realpath" + "github.com/z7zmey/php-parser/comment" + "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/php5" + "github.com/z7zmey/php-parser/php7" + "github.com/z7zmey/php-parser/position" "github.com/z7zmey/php-parser/visitor" ) func main() { + var nodes node.Node + var comments comment.Comments + var positions position.Positions + + usePhp5 := flag.Bool("php5", false, "use PHP5 parser") flag.Parse() for _, path := range flag.Args() { @@ -24,7 +33,11 @@ func main() { fmt.Printf("==> %s\n", path) src, _ := os.Open(string(path)) - nodes, comments, positions := php5.Parse(src, path) + if *usePhp5 { + nodes, comments, positions = php5.Parse(src, path) + } else { + nodes, comments, positions = php7.Parse(src, path) + } visitor := visitor.Dumper{ Indent: " | ", diff --git a/php5/lexer.go b/php5/lexer.go index fa55a16..0f848af 100644 --- a/php5/lexer.go +++ b/php5/lexer.go @@ -12,15 +12,15 @@ import ( ) type lexer struct { - scanner.Lexer + scanner.Lexer } func (l *lexer) Lex(lval *yySymType) int { - return l.Lexer.Lex(lval) + return l.Lexer.Lex(lval) } func (lval *yySymType) Token(t token.Token) { - lval.token = t + lval.token = t } func newLexer(src io.Reader, fName string) *lexer { diff --git a/php7/lexer.go b/php7/lexer.go index 02dfd5f..e452c93 100644 --- a/php7/lexer.go +++ b/php7/lexer.go @@ -12,15 +12,15 @@ import ( ) type lexer struct { - scanner.Lexer + scanner.Lexer } func (l *lexer) Lex(lval *yySymType) int { - return l.Lexer.Lex(lval) + return l.Lexer.Lex(lval) } func (lval *yySymType) Token(t token.Token) { - lval.token = t + lval.token = t } func newLexer(src io.Reader, fName string) *lexer { diff --git a/php7/parser.go b/php7/parser.go index ec04bb5..739bd18 100644 --- a/php7/parser.go +++ b/php7/parser.go @@ -16,14 +16,14 @@ var positions position.Positions var positionBuilder position.Builder func Parse(src io.Reader, fName string) (node.Node, comment.Comments, position.Positions) { - yyDebug = 0 - yyErrorVerbose = true - rootnode = stmt.NewStmtList([]node.Node{}) //reset - comments = comment.Comments{} - positions = position.Positions{} - positionBuilder = position.Builder{&positions} - yyParse(newLexer(src, fName)) - return rootnode, comments, positions + yyDebug = 0 + yyErrorVerbose = true + rootnode = stmt.NewStmtList([]node.Node{}) //reset + comments = comment.Comments{} + positions = position.Positions{} + positionBuilder = position.Builder{&positions} + yyParse(newLexer(src, fName)) + return rootnode, comments, positions } func ListGetFirstNodeComments(list []node.Node) []comment.Comment { @@ -37,16 +37,16 @@ func ListGetFirstNodeComments(list []node.Node) []comment.Comment { } type foreachVariable struct { - node node.Node - byRef bool + node node.Node + byRef bool } type nodesWithEndToken struct { - nodes []node.Node - endToken token.Token + nodes []node.Node + endToken token.Token } type boolWithToken struct { - value bool - token *token.Token -} \ No newline at end of file + value bool + token *token.Token +}