[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
import (
"bytes"
"flag"
"io"
"io/ioutil"
@ -163,12 +164,12 @@ func printerWorker(r <-chan result) {
}
if *printBack {
//o := bytes.NewBuffer([]byte{})
//p := printer.NewPrinter(o)
//p.Print(res.rootNode)
//
//err := ioutil.WriteFile(res.path, o.Bytes(), 0644)
//checkErr(err)
o := bytes.NewBuffer([]byte{})
p := visitor.NewPrinter(o)
res.rootNode.Accept(p)
err := ioutil.WriteFile(res.path, o.Bytes(), 0644)
checkErr(err)
}
if *showResolvedNs {
@ -181,9 +182,7 @@ func printerWorker(r <-chan result) {
}
if *dump == true {
v := visitor.NewDump(os.Stdout)
t := traverser.NewDFS(v)
t.Traverse(res.rootNode)
visitor.NewDump(os.Stdout).WithPositions().WithTokens().Dump(res.rootNode)
}
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;};
'var'i => {lex.setTokenPosition(tkn); tok = token.T_VAR; 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;};
'include'i => {lex.setTokenPosition(tkn); tok = token.T_INCLUDE; 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
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/traverser"
"github.com/z7zmey/php-parser/pkg/ast/visitor"
"github.com/z7zmey/php-parser/pkg/token"
)
func ExampleDump() {
stxTree := &ast.Root{
func TestDumper_root(t *testing.T) {
o := bytes.NewBufferString("")
p := visitor.NewDump(o)
n := &ast.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 2,
StartPos: 3,
EndPos: 4,
},
Stmts: []ast.Vertex{
&ast.Identifier{},
&ast.Parameter{
Var: &ast.ExprVariable{},
},
&ast.StmtInlineHtml{
Value: []byte("foo"),
},
&ast.StmtNop{},
},
EndTkn: &token.Token{
ID: token.T_WHITESPACE,
Value: []byte(" "),
FreeFloating: []*token.Token{
{
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:
//&ast.Root{
// 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"),
// },
// },
//}
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}

View File

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