refactoring: update traverser
This commit is contained in:
parent
c0465f9605
commit
bd479007be
@ -3,6 +3,10 @@ package main
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"github.com/z7zmey/php-parser/pkg/visitor/dumper"
|
||||
"github.com/z7zmey/php-parser/pkg/visitor/nsresolver"
|
||||
"github.com/z7zmey/php-parser/pkg/visitor/printer"
|
||||
"github.com/z7zmey/php-parser/pkg/visitor/traverser"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
@ -17,8 +21,6 @@ import (
|
||||
"github.com/yookoala/realpath"
|
||||
|
||||
"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/errors"
|
||||
"github.com/z7zmey/php-parser/pkg/parser"
|
||||
)
|
||||
@ -165,7 +167,7 @@ func printerWorker(r <-chan result) {
|
||||
|
||||
if *printBack {
|
||||
o := bytes.NewBuffer([]byte{})
|
||||
p := visitor.NewPrinter(o)
|
||||
p := printer.NewPrinter(o)
|
||||
res.rootNode.Accept(p)
|
||||
|
||||
err := ioutil.WriteFile(res.path, o.Bytes(), 0644)
|
||||
@ -173,16 +175,15 @@ func printerWorker(r <-chan result) {
|
||||
}
|
||||
|
||||
if *showResolvedNs {
|
||||
v := visitor.NewNamespaceResolver()
|
||||
t := traverser.NewDFS(v)
|
||||
t.Traverse(res.rootNode)
|
||||
v := nsresolver.NewNamespaceResolver()
|
||||
traverser.NewTraverser(v).Traverse(res.rootNode)
|
||||
for _, n := range v.ResolvedNames {
|
||||
_, _ = io.WriteString(os.Stderr, "===> "+n+"\n")
|
||||
}
|
||||
}
|
||||
|
||||
if *dump == true {
|
||||
visitor.NewDumper(os.Stdout).WithPositions().WithTokens().Dump(res.rootNode)
|
||||
dumper.NewDumper(os.Stdout).WithPositions().WithTokens().Dump(res.rootNode)
|
||||
}
|
||||
|
||||
wg.Done()
|
||||
|
@ -3,23 +3,11 @@ package ast
|
||||
import "github.com/z7zmey/php-parser/pkg/position"
|
||||
|
||||
type Vertex interface {
|
||||
Accept(v NodeVisitor)
|
||||
Accept(v Visitor)
|
||||
GetPosition() *position.Position
|
||||
}
|
||||
|
||||
type Traverser interface {
|
||||
Traverse(n Vertex)
|
||||
}
|
||||
|
||||
type Visitor interface {
|
||||
Enter(key string, singleNode bool)
|
||||
Leave(key string, singleNode bool)
|
||||
|
||||
EnterNode(n Vertex) bool
|
||||
LeaveNode(n Vertex)
|
||||
}
|
||||
|
||||
type NodeVisitor interface {
|
||||
Root(n *Root)
|
||||
Nullable(n *Nullable)
|
||||
Parameter(n *Parameter)
|
||||
|
322
pkg/ast/node.go
322
pkg/ast/node.go
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,4 +1,4 @@
|
||||
package visitor
|
||||
package dumper
|
||||
|
||||
import (
|
||||
"github.com/z7zmey/php-parser/pkg/position"
|
@ -1,19 +1,19 @@
|
||||
package visitor_test
|
||||
package dumper_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/z7zmey/php-parser/pkg/position"
|
||||
"github.com/z7zmey/php-parser/pkg/token"
|
||||
"github.com/z7zmey/php-parser/pkg/visitor/dumper"
|
||||
"testing"
|
||||
|
||||
"github.com/z7zmey/php-parser/pkg/ast"
|
||||
"github.com/z7zmey/php-parser/pkg/ast/visitor"
|
||||
)
|
||||
|
||||
func TestDumper_root(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
p := visitor.NewDumper(o).WithTokens().WithPositions()
|
||||
p := dumper.NewDumper(o).WithTokens().WithPositions()
|
||||
n := &ast.Root{
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
@ -1,4 +1,4 @@
|
||||
package visitor
|
||||
package formatter
|
||||
|
||||
import (
|
||||
"bytes"
|
File diff suppressed because it is too large
Load Diff
@ -1,15 +1,16 @@
|
||||
// Package visitor contains walker.visitor implementations
|
||||
package visitor
|
||||
package nsresolver
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/z7zmey/php-parser/pkg/ast"
|
||||
"github.com/z7zmey/php-parser/pkg/visitor"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// NamespaceResolver visitor
|
||||
type NamespaceResolver struct {
|
||||
Null
|
||||
visitor.Null
|
||||
Namespace *Namespace
|
||||
ResolvedNames map[ast.Vertex]string
|
||||
|
@ -1,13 +1,13 @@
|
||||
package visitor_test
|
||||
package nsresolver_test
|
||||
|
||||
import (
|
||||
"github.com/z7zmey/php-parser/pkg/visitor/nsresolver"
|
||||
"github.com/z7zmey/php-parser/pkg/visitor/traverser"
|
||||
"testing"
|
||||
|
||||
"gotest.tools/assert"
|
||||
|
||||
"github.com/z7zmey/php-parser/pkg/ast"
|
||||
"github.com/z7zmey/php-parser/pkg/ast/traverser"
|
||||
"github.com/z7zmey/php-parser/pkg/ast/visitor"
|
||||
)
|
||||
|
||||
func TestResolveStaticCall(t *testing.T) {
|
||||
@ -34,9 +34,8 @@ func TestResolveStaticCall(t *testing.T) {
|
||||
nameBC: "A\\B\\C",
|
||||
}
|
||||
|
||||
nsResolver := visitor.NewNamespaceResolver()
|
||||
dfsTraverser := traverser.NewDFS(nsResolver)
|
||||
dfsTraverser.Traverse(stxTree)
|
||||
nsResolver := nsresolver.NewNamespaceResolver()
|
||||
traverser.NewTraverser(nsResolver).Traverse(stxTree)
|
||||
|
||||
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
|
||||
}
|
||||
@ -65,9 +64,8 @@ func TestResolveStaticPropertyFetch(t *testing.T) {
|
||||
nameBC: "A\\B\\C",
|
||||
}
|
||||
|
||||
nsResolver := visitor.NewNamespaceResolver()
|
||||
dfsTraverser := traverser.NewDFS(nsResolver)
|
||||
dfsTraverser.Traverse(stxTree)
|
||||
nsResolver := nsresolver.NewNamespaceResolver()
|
||||
traverser.NewTraverser(nsResolver).Traverse(stxTree)
|
||||
|
||||
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
|
||||
}
|
||||
@ -96,9 +94,8 @@ func TestResolveClassConstFetch(t *testing.T) {
|
||||
nameBC: "A\\B\\C",
|
||||
}
|
||||
|
||||
nsResolver := visitor.NewNamespaceResolver()
|
||||
dfsTraverser := traverser.NewDFS(nsResolver)
|
||||
dfsTraverser.Traverse(stxTree)
|
||||
nsResolver := nsresolver.NewNamespaceResolver()
|
||||
traverser.NewTraverser(nsResolver).Traverse(stxTree)
|
||||
|
||||
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
|
||||
}
|
||||
@ -126,9 +123,8 @@ func TestResolveNew(t *testing.T) {
|
||||
nameBC: "A\\B\\C",
|
||||
}
|
||||
|
||||
nsResolver := visitor.NewNamespaceResolver()
|
||||
dfsTraverser := traverser.NewDFS(nsResolver)
|
||||
dfsTraverser.Traverse(stxTree)
|
||||
nsResolver := nsresolver.NewNamespaceResolver()
|
||||
traverser.NewTraverser(nsResolver).Traverse(stxTree)
|
||||
|
||||
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
|
||||
}
|
||||
@ -157,9 +153,8 @@ func TestResolveInstanceOf(t *testing.T) {
|
||||
nameBC: "A\\B\\C",
|
||||
}
|
||||
|
||||
nsResolver := visitor.NewNamespaceResolver()
|
||||
dfsTraverser := traverser.NewDFS(nsResolver)
|
||||
dfsTraverser.Traverse(stxTree)
|
||||
nsResolver := nsresolver.NewNamespaceResolver()
|
||||
traverser.NewTraverser(nsResolver).Traverse(stxTree)
|
||||
|
||||
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
|
||||
}
|
||||
@ -205,9 +200,8 @@ func TestResolveInstanceCatch(t *testing.T) {
|
||||
nameF: "D\\E",
|
||||
}
|
||||
|
||||
nsResolver := visitor.NewNamespaceResolver()
|
||||
dfsTraverser := traverser.NewDFS(nsResolver)
|
||||
dfsTraverser.Traverse(stxTree)
|
||||
nsResolver := nsresolver.NewNamespaceResolver()
|
||||
traverser.NewTraverser(nsResolver).Traverse(stxTree)
|
||||
|
||||
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
|
||||
}
|
||||
@ -236,9 +230,8 @@ func TestResolveFunctionCall(t *testing.T) {
|
||||
nameB: "A\\B",
|
||||
}
|
||||
|
||||
nsResolver := visitor.NewNamespaceResolver()
|
||||
dfsTraverser := traverser.NewDFS(nsResolver)
|
||||
dfsTraverser.Traverse(stxTree)
|
||||
nsResolver := nsresolver.NewNamespaceResolver()
|
||||
traverser.NewTraverser(nsResolver).Traverse(stxTree)
|
||||
|
||||
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
|
||||
}
|
||||
@ -267,9 +260,8 @@ func TestResolveConstFetch(t *testing.T) {
|
||||
nameB: "A\\B",
|
||||
}
|
||||
|
||||
nsResolver := visitor.NewNamespaceResolver()
|
||||
dfsTraverser := traverser.NewDFS(nsResolver)
|
||||
dfsTraverser.Traverse(stxTree)
|
||||
nsResolver := nsresolver.NewNamespaceResolver()
|
||||
traverser.NewTraverser(nsResolver).Traverse(stxTree)
|
||||
|
||||
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
|
||||
}
|
||||
@ -323,9 +315,8 @@ func TestResolveGroupUse(t *testing.T) {
|
||||
nameE: "B\\D\\E",
|
||||
}
|
||||
|
||||
nsResolver := visitor.NewNamespaceResolver()
|
||||
dfsTraverser := traverser.NewDFS(nsResolver)
|
||||
dfsTraverser.Traverse(stxTree)
|
||||
nsResolver := nsresolver.NewNamespaceResolver()
|
||||
traverser.NewTraverser(nsResolver).Traverse(stxTree)
|
||||
|
||||
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
|
||||
}
|
||||
@ -384,9 +375,8 @@ func TestResolveTraitUse(t *testing.T) {
|
||||
relativeNameBC: "B\\C",
|
||||
}
|
||||
|
||||
nsResolver := visitor.NewNamespaceResolver()
|
||||
dfsTraverser := traverser.NewDFS(nsResolver)
|
||||
dfsTraverser.Traverse(stxTree)
|
||||
nsResolver := nsresolver.NewNamespaceResolver()
|
||||
traverser.NewTraverser(nsResolver).Traverse(stxTree)
|
||||
|
||||
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
|
||||
}
|
||||
@ -415,9 +405,8 @@ func TestResolveClassName(t *testing.T) {
|
||||
nameBC: "B\\C",
|
||||
}
|
||||
|
||||
nsResolver := visitor.NewNamespaceResolver()
|
||||
dfsTraverser := traverser.NewDFS(nsResolver)
|
||||
dfsTraverser.Traverse(stxTree)
|
||||
nsResolver := nsresolver.NewNamespaceResolver()
|
||||
traverser.NewTraverser(nsResolver).Traverse(stxTree)
|
||||
|
||||
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
|
||||
}
|
||||
@ -446,9 +435,8 @@ func TestResolveInterfaceName(t *testing.T) {
|
||||
nameBC: "B\\C",
|
||||
}
|
||||
|
||||
nsResolver := visitor.NewNamespaceResolver()
|
||||
dfsTraverser := traverser.NewDFS(nsResolver)
|
||||
dfsTraverser.Traverse(stxTree)
|
||||
nsResolver := nsresolver.NewNamespaceResolver()
|
||||
traverser.NewTraverser(nsResolver).Traverse(stxTree)
|
||||
|
||||
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
|
||||
}
|
||||
@ -469,9 +457,8 @@ func TestResolveTraitName(t *testing.T) {
|
||||
traitNode: "A",
|
||||
}
|
||||
|
||||
nsResolver := visitor.NewNamespaceResolver()
|
||||
dfsTraverser := traverser.NewDFS(nsResolver)
|
||||
dfsTraverser.Traverse(stxTree)
|
||||
nsResolver := nsresolver.NewNamespaceResolver()
|
||||
traverser.NewTraverser(nsResolver).Traverse(stxTree)
|
||||
|
||||
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
|
||||
}
|
||||
@ -504,9 +491,8 @@ func TestResolveFunctionName(t *testing.T) {
|
||||
nameBC: "B\\C",
|
||||
}
|
||||
|
||||
nsResolver := visitor.NewNamespaceResolver()
|
||||
dfsTraverser := traverser.NewDFS(nsResolver)
|
||||
dfsTraverser.Traverse(stxTree)
|
||||
nsResolver := nsresolver.NewNamespaceResolver()
|
||||
traverser.NewTraverser(nsResolver).Traverse(stxTree)
|
||||
|
||||
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
|
||||
}
|
||||
@ -534,9 +520,8 @@ func TestResolveMethodName(t *testing.T) {
|
||||
nameBC: "B\\C",
|
||||
}
|
||||
|
||||
nsResolver := visitor.NewNamespaceResolver()
|
||||
dfsTraverser := traverser.NewDFS(nsResolver)
|
||||
dfsTraverser.Traverse(methodNode)
|
||||
nsResolver := nsresolver.NewNamespaceResolver()
|
||||
traverser.NewTraverser(nsResolver).Traverse(methodNode)
|
||||
|
||||
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
|
||||
}
|
||||
@ -561,9 +546,8 @@ func TestResolveClosureName(t *testing.T) {
|
||||
nameBC: "B\\C",
|
||||
}
|
||||
|
||||
nsResolver := visitor.NewNamespaceResolver()
|
||||
dfsTraverser := traverser.NewDFS(nsResolver)
|
||||
dfsTraverser.Traverse(closureNode)
|
||||
nsResolver := nsresolver.NewNamespaceResolver()
|
||||
traverser.NewTraverser(nsResolver).Traverse(closureNode)
|
||||
|
||||
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
|
||||
}
|
||||
@ -599,9 +583,8 @@ func TestResolveConstantsName(t *testing.T) {
|
||||
constantC: "A\\B\\C",
|
||||
}
|
||||
|
||||
nsResolver := visitor.NewNamespaceResolver()
|
||||
dfsTraverser := traverser.NewDFS(nsResolver)
|
||||
dfsTraverser.Traverse(stxTree)
|
||||
nsResolver := nsresolver.NewNamespaceResolver()
|
||||
traverser.NewTraverser(nsResolver).Traverse(stxTree)
|
||||
|
||||
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
|
||||
}
|
||||
@ -673,9 +656,8 @@ func TestResolveNamespaces(t *testing.T) {
|
||||
nameCF: "A\\C\\F",
|
||||
}
|
||||
|
||||
nsResolver := visitor.NewNamespaceResolver()
|
||||
dfsTraverser := traverser.NewDFS(nsResolver)
|
||||
dfsTraverser.Traverse(stxTree)
|
||||
nsResolver := nsresolver.NewNamespaceResolver()
|
||||
traverser.NewTraverser(nsResolver).Traverse(stxTree)
|
||||
|
||||
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
|
||||
}
|
||||
@ -692,9 +674,8 @@ func TestResolveStaticCallDinamicClassName(t *testing.T) {
|
||||
|
||||
expected := map[ast.Vertex]string{}
|
||||
|
||||
nsResolver := visitor.NewNamespaceResolver()
|
||||
dfsTraverser := traverser.NewDFS(nsResolver)
|
||||
dfsTraverser.Traverse(stxTree)
|
||||
nsResolver := nsresolver.NewNamespaceResolver()
|
||||
traverser.NewTraverser(nsResolver).Traverse(stxTree)
|
||||
|
||||
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
|
||||
}
|
||||
@ -749,9 +730,8 @@ func TestDoNotResolveReservedConstants(t *testing.T) {
|
||||
constantNull: "null",
|
||||
}
|
||||
|
||||
nsResolver := visitor.NewNamespaceResolver()
|
||||
dfsTraverser := traverser.NewDFS(nsResolver)
|
||||
dfsTraverser.Traverse(stxTree)
|
||||
nsResolver := nsresolver.NewNamespaceResolver()
|
||||
traverser.NewTraverser(nsResolver).Traverse(stxTree)
|
||||
|
||||
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
|
||||
}
|
||||
@ -872,9 +852,8 @@ func TestDoNotResolveReservedNames(t *testing.T) {
|
||||
nameObject: "object",
|
||||
}
|
||||
|
||||
nsResolver := visitor.NewNamespaceResolver()
|
||||
dfsTraverser := traverser.NewDFS(nsResolver)
|
||||
dfsTraverser.Traverse(stxTree)
|
||||
nsResolver := nsresolver.NewNamespaceResolver()
|
||||
traverser.NewTraverser(nsResolver).Traverse(stxTree)
|
||||
|
||||
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
|
||||
}
|
||||
@ -943,9 +922,8 @@ func TestDoNotResolveReservedSpecialNames(t *testing.T) {
|
||||
nameParent: "parent",
|
||||
}
|
||||
|
||||
nsResolver := visitor.NewNamespaceResolver()
|
||||
dfsTraverser := traverser.NewDFS(nsResolver)
|
||||
dfsTraverser.Traverse(stxTree)
|
||||
nsResolver := nsresolver.NewNamespaceResolver()
|
||||
traverser.NewTraverser(nsResolver).Traverse(stxTree)
|
||||
|
||||
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
|
||||
}
|
||||
@ -995,9 +973,8 @@ func TestResolvePropertyTypeName(t *testing.T) {
|
||||
classNode: "Foo\\Bar",
|
||||
}
|
||||
|
||||
nsResolver := visitor.NewNamespaceResolver()
|
||||
dfsTraverser := traverser.NewDFS(nsResolver)
|
||||
dfsTraverser.Traverse(stmts)
|
||||
nsResolver := nsresolver.NewNamespaceResolver()
|
||||
traverser.NewTraverser(nsResolver).Traverse(stmts)
|
||||
|
||||
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package visitor
|
||||
package printer
|
||||
|
||||
import (
|
||||
"bytes"
|
@ -1,8 +1,8 @@
|
||||
package visitor_test
|
||||
package printer_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/z7zmey/php-parser/pkg/ast/visitor"
|
||||
printer2 "github.com/z7zmey/php-parser/pkg/visitor/printer"
|
||||
"testing"
|
||||
|
||||
"github.com/z7zmey/php-parser/pkg/ast"
|
||||
@ -22,7 +22,7 @@ func parsePhp5(src string) ast.Vertex {
|
||||
func printPhp5(n ast.Vertex) string {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
printer := visitor.NewPrinter(o)
|
||||
printer := printer2.NewPrinter(o)
|
||||
n.Accept(printer)
|
||||
|
||||
return o.String()
|
@ -1,14 +1,14 @@
|
||||
package visitor_test
|
||||
package printer_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
printer2 "github.com/z7zmey/php-parser/pkg/visitor/printer"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/z7zmey/php-parser/internal/php7"
|
||||
"github.com/z7zmey/php-parser/internal/scanner"
|
||||
"github.com/z7zmey/php-parser/pkg/ast"
|
||||
"github.com/z7zmey/php-parser/pkg/ast/visitor"
|
||||
)
|
||||
|
||||
func ExamplePrinter() {
|
||||
@ -41,7 +41,7 @@ abstract class Bar extends Baz
|
||||
|
||||
// print
|
||||
|
||||
printer := visitor.NewPrinter(os.Stdout)
|
||||
printer := printer2.NewPrinter(os.Stdout)
|
||||
rootNode.Accept(printer)
|
||||
|
||||
// Output:
|
||||
@ -70,7 +70,7 @@ func parse(src string) ast.Vertex {
|
||||
func print(n ast.Vertex) string {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
printer := visitor.NewPrinter(o)
|
||||
printer := printer2.NewPrinter(o)
|
||||
n.Accept(printer)
|
||||
|
||||
return o.String()
|
File diff suppressed because it is too large
Load Diff
1164
pkg/visitor/traverser/traverser.go
Normal file
1164
pkg/visitor/traverser/traverser.go
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user