[refactoring] update dumper

This commit is contained in:
Vadym Slizov 2020-12-11 13:28:16 +02:00
parent 60433615a9
commit 632146f98e
No known key found for this signature in database
GPG Key ID: AEA2A9388EF42A4A
6 changed files with 1937 additions and 536 deletions

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"bytes"
"flag" "flag"
"io" "io"
"io/ioutil" "io/ioutil"
@ -163,12 +164,12 @@ func printerWorker(r <-chan result) {
} }
if *printBack { if *printBack {
//o := bytes.NewBuffer([]byte{}) o := bytes.NewBuffer([]byte{})
//p := printer.NewPrinter(o) p := visitor.NewPrinter(o)
//p.Print(res.rootNode) res.rootNode.Accept(p)
//
//err := ioutil.WriteFile(res.path, o.Bytes(), 0644) err := ioutil.WriteFile(res.path, o.Bytes(), 0644)
//checkErr(err) checkErr(err)
} }
if *showResolvedNs { if *showResolvedNs {
@ -181,9 +182,7 @@ func printerWorker(r <-chan result) {
} }
if *dump == true { if *dump == true {
v := visitor.NewDump(os.Stdout) visitor.NewDump(os.Stdout).WithPositions().WithTokens().Dump(res.rootNode)
t := traverser.NewDFS(v)
t.Traverse(res.rootNode)
} }
wg.Done() wg.Done()

Binary file not shown.

View File

@ -255,7 +255,7 @@ func (lex *Lexer) Lex() *token.Token {
'use'i => {lex.setTokenPosition(tkn); tok = token.T_USE; fbreak;}; 'use'i => {lex.setTokenPosition(tkn); tok = token.T_USE; fbreak;};
'var'i => {lex.setTokenPosition(tkn); tok = token.T_VAR; fbreak;}; 'var'i => {lex.setTokenPosition(tkn); tok = token.T_VAR; fbreak;};
'while'i => {lex.setTokenPosition(tkn); tok = token.T_WHILE; fbreak;}; 'while'i => {lex.setTokenPosition(tkn); tok = token.T_WHILE; fbreak;};
'yield'i whitespace_line* 'from'i => {lex.setTokenPosition(tkn); tok = token.T_YIELD_FROM; fbreak;}; 'yield'i whitespace_line+ 'from'i => {lex.setTokenPosition(tkn); tok = token.T_YIELD_FROM; fbreak;};
'yield'i => {lex.setTokenPosition(tkn); tok = token.T_YIELD; fbreak;}; 'yield'i => {lex.setTokenPosition(tkn); tok = token.T_YIELD; fbreak;};
'include'i => {lex.setTokenPosition(tkn); tok = token.T_INCLUDE; fbreak;}; 'include'i => {lex.setTokenPosition(tkn); tok = token.T_INCLUDE; fbreak;};
'include_once'i => {lex.setTokenPosition(tkn); tok = token.T_INCLUDE_ONCE; fbreak;}; 'include_once'i => {lex.setTokenPosition(tkn); tok = token.T_INCLUDE_ONCE; fbreak;};

File diff suppressed because it is too large Load Diff

View File

@ -1,62 +1,76 @@
package visitor_test package visitor_test
import ( import (
"os" "bytes"
"github.com/z7zmey/php-parser/pkg/position"
"github.com/z7zmey/php-parser/pkg/token"
"testing"
"github.com/z7zmey/php-parser/pkg/ast" "github.com/z7zmey/php-parser/pkg/ast"
"github.com/z7zmey/php-parser/pkg/ast/traverser"
"github.com/z7zmey/php-parser/pkg/ast/visitor" "github.com/z7zmey/php-parser/pkg/ast/visitor"
"github.com/z7zmey/php-parser/pkg/token"
) )
func ExampleDump() { func TestDumper_root(t *testing.T) {
stxTree := &ast.Root{ o := bytes.NewBufferString("")
p := visitor.NewDump(o)
n := &ast.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 2,
StartPos: 3,
EndPos: 4,
},
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.Identifier{}, &ast.StmtNop{},
&ast.Parameter{
Var: &ast.ExprVariable{},
},
&ast.StmtInlineHtml{
Value: []byte("foo"),
},
}, },
EndTkn: &token.Token{ EndTkn: &token.Token{
ID: token.T_WHITESPACE, FreeFloating: []*token.Token{
Value: []byte(" "), {
ID: token.T_WHITESPACE,
Value: []byte(" "),
Position: &position.Position{
StartLine: 1,
EndLine: 2,
StartPos: 3,
EndPos: 4,
},
},
},
}, },
} }
n.Accept(p)
traverser.NewDFS(visitor.NewDump(os.Stdout)).Traverse(stxTree) expected := `&ast.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 2,
StartPos: 3,
EndPos: 4,
},
Stmts: []ast.Vertex{
&ast.StmtNop{
},
},
EndTkn: &token.Token{
FreeFloating: []*token.Token{
{
ID: token.T_WHITESPACE,
Value: []byte(" "),
Position: &position.Position{
StartLine: 1,
EndLine: 2,
StartPos: 3,
EndPos: 4,
},
},
},
},
},
`
actual := o.String()
//output: if expected != actual {
//&ast.Root{ t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
// Node: ast.Node{ }
// Tokens: token.Collection{
// token.Start: []*token.Token{
// {
// ID: token.T_WHITESPACE,
// Value: []byte(" "),
// },
// },
// },
// Position: &position.Position{
// StartLine: 1,
// EndLine: 1,
// StartPos: 0,
// EndPos: 1,
// },
// },
// Stmts: []ast.Vertex{
// &ast.Identifier{
// Value: []byte(""),
// },
// &ast.Parameter{
// Var: &ast.ExprVariable{
// },
// },
// &ast.StmtInlineHtml{
// Value: []byte("foo"),
// },
// },
//}
} }

View File

@ -117,7 +117,7 @@ func TestResolveNew(t *testing.T) {
}, },
}, },
&ast.ExprNew{ &ast.ExprNew{
Class: nameBC, Class: nameBC,
}, },
}, },
} }
@ -227,7 +227,7 @@ func TestResolveFunctionCall(t *testing.T) {
}, },
}, },
&ast.ExprFunctionCall{ &ast.ExprFunctionCall{
Function: nameB, Function: nameB,
}, },
}, },
} }
@ -309,10 +309,10 @@ func TestResolveGroupUse(t *testing.T) {
Const: nameC, Const: nameC,
}, },
&ast.ExprFunctionCall{ &ast.ExprFunctionCall{
Function: nameF, Function: nameF,
}, },
&ast.ExprFunctionCall{ &ast.ExprFunctionCall{
Function: nameE, Function: nameE,
}, },
}, },
} }
@ -649,8 +649,8 @@ func TestResolveNamespaces(t *testing.T) {
}, },
}, },
&ast.ExprStaticCall{ &ast.ExprStaticCall{
Class: nameFG, Class: nameFG,
Call: &ast.Identifier{Value: []byte("foo")}, Call: &ast.Identifier{Value: []byte("foo")},
}, },
&ast.StmtNamespace{ &ast.StmtNamespace{
Stmts: []ast.Vertex{}, Stmts: []ast.Vertex{},
@ -666,12 +666,12 @@ func TestResolveNamespaces(t *testing.T) {
}, },
}, },
&ast.ExprStaticCall{ &ast.ExprStaticCall{
Class: relativeNameCE, Class: relativeNameCE,
Call: &ast.Identifier{Value: []byte("foo")}, Call: &ast.Identifier{Value: []byte("foo")},
}, },
&ast.ExprStaticCall{ &ast.ExprStaticCall{
Class: nameCF, Class: nameCF,
Call: &ast.Identifier{Value: []byte("foo")}, Call: &ast.Identifier{Value: []byte("foo")},
}, },
}, },
}, },
@ -697,8 +697,8 @@ func TestResolveStaticCallDinamicClassName(t *testing.T) {
stxTree := &ast.StmtStmtList{ stxTree := &ast.StmtStmtList{
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.ExprStaticCall{ &ast.ExprStaticCall{
Class: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("foo")}}, Class: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("foo")}},
Call: &ast.Identifier{Value: []byte("foo")}, Call: &ast.Identifier{Value: []byte("foo")},
}, },
}, },
} }
@ -917,20 +917,20 @@ func TestDoNotResolveReservedSpecialNames(t *testing.T) {
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtExpression{ &ast.StmtExpression{
Expr: &ast.ExprStaticCall{ Expr: &ast.ExprStaticCall{
Class: nameSelf, Class: nameSelf,
Call: &ast.Identifier{Value: []byte("func")}, Call: &ast.Identifier{Value: []byte("func")},
}, },
}, },
&ast.StmtExpression{ &ast.StmtExpression{
Expr: &ast.ExprStaticCall{ Expr: &ast.ExprStaticCall{
Class: nameStatic, Class: nameStatic,
Call: &ast.Identifier{Value: []byte("func")}, Call: &ast.Identifier{Value: []byte("func")},
}, },
}, },
&ast.StmtExpression{ &ast.StmtExpression{
Expr: &ast.ExprStaticCall{ Expr: &ast.ExprStaticCall{
Class: nameParent, Class: nameParent,
Call: &ast.Identifier{Value: []byte("func")}, Call: &ast.Identifier{Value: []byte("func")},
}, },
}, },
}, },