refactoring: update traverser

This commit is contained in:
Vadym Slizov 2020-12-28 21:13:08 +02:00
parent c0465f9605
commit bd479007be
No known key found for this signature in database
GPG Key ID: AEA2A9388EF42A4A
16 changed files with 2050 additions and 3451 deletions

View File

@ -3,6 +3,10 @@ package main
import ( import (
"bytes" "bytes"
"flag" "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"
"io/ioutil" "io/ioutil"
"log" "log"
@ -17,8 +21,6 @@ import (
"github.com/yookoala/realpath" "github.com/yookoala/realpath"
"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/errors" "github.com/z7zmey/php-parser/pkg/errors"
"github.com/z7zmey/php-parser/pkg/parser" "github.com/z7zmey/php-parser/pkg/parser"
) )
@ -165,7 +167,7 @@ func printerWorker(r <-chan result) {
if *printBack { if *printBack {
o := bytes.NewBuffer([]byte{}) o := bytes.NewBuffer([]byte{})
p := visitor.NewPrinter(o) p := printer.NewPrinter(o)
res.rootNode.Accept(p) res.rootNode.Accept(p)
err := ioutil.WriteFile(res.path, o.Bytes(), 0644) err := ioutil.WriteFile(res.path, o.Bytes(), 0644)
@ -173,16 +175,15 @@ func printerWorker(r <-chan result) {
} }
if *showResolvedNs { if *showResolvedNs {
v := visitor.NewNamespaceResolver() v := nsresolver.NewNamespaceResolver()
t := traverser.NewDFS(v) traverser.NewTraverser(v).Traverse(res.rootNode)
t.Traverse(res.rootNode)
for _, n := range v.ResolvedNames { for _, n := range v.ResolvedNames {
_, _ = io.WriteString(os.Stderr, "===> "+n+"\n") _, _ = io.WriteString(os.Stderr, "===> "+n+"\n")
} }
} }
if *dump == true { if *dump == true {
visitor.NewDumper(os.Stdout).WithPositions().WithTokens().Dump(res.rootNode) dumper.NewDumper(os.Stdout).WithPositions().WithTokens().Dump(res.rootNode)
} }
wg.Done() wg.Done()

View File

@ -3,23 +3,11 @@ package ast
import "github.com/z7zmey/php-parser/pkg/position" import "github.com/z7zmey/php-parser/pkg/position"
type Vertex interface { type Vertex interface {
Accept(v NodeVisitor) Accept(v Visitor)
GetPosition() *position.Position GetPosition() *position.Position
} }
type Traverser interface {
Traverse(n Vertex)
}
type Visitor interface { 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) Root(n *Root)
Nullable(n *Nullable) Nullable(n *Nullable)
Parameter(n *Parameter) Parameter(n *Parameter)

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
package visitor package dumper
import ( import (
"github.com/z7zmey/php-parser/pkg/position" "github.com/z7zmey/php-parser/pkg/position"

View File

@ -1,19 +1,19 @@
package visitor_test package dumper_test
import ( import (
"bytes" "bytes"
"github.com/z7zmey/php-parser/pkg/position" "github.com/z7zmey/php-parser/pkg/position"
"github.com/z7zmey/php-parser/pkg/token" "github.com/z7zmey/php-parser/pkg/token"
"github.com/z7zmey/php-parser/pkg/visitor/dumper"
"testing" "testing"
"github.com/z7zmey/php-parser/pkg/ast" "github.com/z7zmey/php-parser/pkg/ast"
"github.com/z7zmey/php-parser/pkg/ast/visitor"
) )
func TestDumper_root(t *testing.T) { func TestDumper_root(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := visitor.NewDumper(o).WithTokens().WithPositions() p := dumper.NewDumper(o).WithTokens().WithPositions()
n := &ast.Root{ n := &ast.Root{
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,

View File

@ -1,4 +1,4 @@
package visitor package formatter
import ( import (
"bytes" "bytes"

View File

@ -1,15 +1,16 @@
// Package visitor contains walker.visitor implementations // Package visitor contains walker.visitor implementations
package visitor package nsresolver
import ( import (
"errors" "errors"
"github.com/z7zmey/php-parser/pkg/ast" "github.com/z7zmey/php-parser/pkg/ast"
"github.com/z7zmey/php-parser/pkg/visitor"
"strings" "strings"
) )
// NamespaceResolver visitor // NamespaceResolver visitor
type NamespaceResolver struct { type NamespaceResolver struct {
Null visitor.Null
Namespace *Namespace Namespace *Namespace
ResolvedNames map[ast.Vertex]string ResolvedNames map[ast.Vertex]string

View File

@ -1,13 +1,13 @@
package visitor_test package nsresolver_test
import ( import (
"github.com/z7zmey/php-parser/pkg/visitor/nsresolver"
"github.com/z7zmey/php-parser/pkg/visitor/traverser"
"testing" "testing"
"gotest.tools/assert" "gotest.tools/assert"
"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"
) )
func TestResolveStaticCall(t *testing.T) { func TestResolveStaticCall(t *testing.T) {
@ -34,9 +34,8 @@ func TestResolveStaticCall(t *testing.T) {
nameBC: "A\\B\\C", nameBC: "A\\B\\C",
} }
nsResolver := visitor.NewNamespaceResolver() nsResolver := nsresolver.NewNamespaceResolver()
dfsTraverser := traverser.NewDFS(nsResolver) traverser.NewTraverser(nsResolver).Traverse(stxTree)
dfsTraverser.Traverse(stxTree)
assert.DeepEqual(t, expected, nsResolver.ResolvedNames) assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
} }
@ -65,9 +64,8 @@ func TestResolveStaticPropertyFetch(t *testing.T) {
nameBC: "A\\B\\C", nameBC: "A\\B\\C",
} }
nsResolver := visitor.NewNamespaceResolver() nsResolver := nsresolver.NewNamespaceResolver()
dfsTraverser := traverser.NewDFS(nsResolver) traverser.NewTraverser(nsResolver).Traverse(stxTree)
dfsTraverser.Traverse(stxTree)
assert.DeepEqual(t, expected, nsResolver.ResolvedNames) assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
} }
@ -96,9 +94,8 @@ func TestResolveClassConstFetch(t *testing.T) {
nameBC: "A\\B\\C", nameBC: "A\\B\\C",
} }
nsResolver := visitor.NewNamespaceResolver() nsResolver := nsresolver.NewNamespaceResolver()
dfsTraverser := traverser.NewDFS(nsResolver) traverser.NewTraverser(nsResolver).Traverse(stxTree)
dfsTraverser.Traverse(stxTree)
assert.DeepEqual(t, expected, nsResolver.ResolvedNames) assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
} }
@ -126,9 +123,8 @@ func TestResolveNew(t *testing.T) {
nameBC: "A\\B\\C", nameBC: "A\\B\\C",
} }
nsResolver := visitor.NewNamespaceResolver() nsResolver := nsresolver.NewNamespaceResolver()
dfsTraverser := traverser.NewDFS(nsResolver) traverser.NewTraverser(nsResolver).Traverse(stxTree)
dfsTraverser.Traverse(stxTree)
assert.DeepEqual(t, expected, nsResolver.ResolvedNames) assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
} }
@ -157,9 +153,8 @@ func TestResolveInstanceOf(t *testing.T) {
nameBC: "A\\B\\C", nameBC: "A\\B\\C",
} }
nsResolver := visitor.NewNamespaceResolver() nsResolver := nsresolver.NewNamespaceResolver()
dfsTraverser := traverser.NewDFS(nsResolver) traverser.NewTraverser(nsResolver).Traverse(stxTree)
dfsTraverser.Traverse(stxTree)
assert.DeepEqual(t, expected, nsResolver.ResolvedNames) assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
} }
@ -205,9 +200,8 @@ func TestResolveInstanceCatch(t *testing.T) {
nameF: "D\\E", nameF: "D\\E",
} }
nsResolver := visitor.NewNamespaceResolver() nsResolver := nsresolver.NewNamespaceResolver()
dfsTraverser := traverser.NewDFS(nsResolver) traverser.NewTraverser(nsResolver).Traverse(stxTree)
dfsTraverser.Traverse(stxTree)
assert.DeepEqual(t, expected, nsResolver.ResolvedNames) assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
} }
@ -236,9 +230,8 @@ func TestResolveFunctionCall(t *testing.T) {
nameB: "A\\B", nameB: "A\\B",
} }
nsResolver := visitor.NewNamespaceResolver() nsResolver := nsresolver.NewNamespaceResolver()
dfsTraverser := traverser.NewDFS(nsResolver) traverser.NewTraverser(nsResolver).Traverse(stxTree)
dfsTraverser.Traverse(stxTree)
assert.DeepEqual(t, expected, nsResolver.ResolvedNames) assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
} }
@ -267,9 +260,8 @@ func TestResolveConstFetch(t *testing.T) {
nameB: "A\\B", nameB: "A\\B",
} }
nsResolver := visitor.NewNamespaceResolver() nsResolver := nsresolver.NewNamespaceResolver()
dfsTraverser := traverser.NewDFS(nsResolver) traverser.NewTraverser(nsResolver).Traverse(stxTree)
dfsTraverser.Traverse(stxTree)
assert.DeepEqual(t, expected, nsResolver.ResolvedNames) assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
} }
@ -323,9 +315,8 @@ func TestResolveGroupUse(t *testing.T) {
nameE: "B\\D\\E", nameE: "B\\D\\E",
} }
nsResolver := visitor.NewNamespaceResolver() nsResolver := nsresolver.NewNamespaceResolver()
dfsTraverser := traverser.NewDFS(nsResolver) traverser.NewTraverser(nsResolver).Traverse(stxTree)
dfsTraverser.Traverse(stxTree)
assert.DeepEqual(t, expected, nsResolver.ResolvedNames) assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
} }
@ -384,9 +375,8 @@ func TestResolveTraitUse(t *testing.T) {
relativeNameBC: "B\\C", relativeNameBC: "B\\C",
} }
nsResolver := visitor.NewNamespaceResolver() nsResolver := nsresolver.NewNamespaceResolver()
dfsTraverser := traverser.NewDFS(nsResolver) traverser.NewTraverser(nsResolver).Traverse(stxTree)
dfsTraverser.Traverse(stxTree)
assert.DeepEqual(t, expected, nsResolver.ResolvedNames) assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
} }
@ -415,9 +405,8 @@ func TestResolveClassName(t *testing.T) {
nameBC: "B\\C", nameBC: "B\\C",
} }
nsResolver := visitor.NewNamespaceResolver() nsResolver := nsresolver.NewNamespaceResolver()
dfsTraverser := traverser.NewDFS(nsResolver) traverser.NewTraverser(nsResolver).Traverse(stxTree)
dfsTraverser.Traverse(stxTree)
assert.DeepEqual(t, expected, nsResolver.ResolvedNames) assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
} }
@ -446,9 +435,8 @@ func TestResolveInterfaceName(t *testing.T) {
nameBC: "B\\C", nameBC: "B\\C",
} }
nsResolver := visitor.NewNamespaceResolver() nsResolver := nsresolver.NewNamespaceResolver()
dfsTraverser := traverser.NewDFS(nsResolver) traverser.NewTraverser(nsResolver).Traverse(stxTree)
dfsTraverser.Traverse(stxTree)
assert.DeepEqual(t, expected, nsResolver.ResolvedNames) assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
} }
@ -469,9 +457,8 @@ func TestResolveTraitName(t *testing.T) {
traitNode: "A", traitNode: "A",
} }
nsResolver := visitor.NewNamespaceResolver() nsResolver := nsresolver.NewNamespaceResolver()
dfsTraverser := traverser.NewDFS(nsResolver) traverser.NewTraverser(nsResolver).Traverse(stxTree)
dfsTraverser.Traverse(stxTree)
assert.DeepEqual(t, expected, nsResolver.ResolvedNames) assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
} }
@ -504,9 +491,8 @@ func TestResolveFunctionName(t *testing.T) {
nameBC: "B\\C", nameBC: "B\\C",
} }
nsResolver := visitor.NewNamespaceResolver() nsResolver := nsresolver.NewNamespaceResolver()
dfsTraverser := traverser.NewDFS(nsResolver) traverser.NewTraverser(nsResolver).Traverse(stxTree)
dfsTraverser.Traverse(stxTree)
assert.DeepEqual(t, expected, nsResolver.ResolvedNames) assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
} }
@ -534,9 +520,8 @@ func TestResolveMethodName(t *testing.T) {
nameBC: "B\\C", nameBC: "B\\C",
} }
nsResolver := visitor.NewNamespaceResolver() nsResolver := nsresolver.NewNamespaceResolver()
dfsTraverser := traverser.NewDFS(nsResolver) traverser.NewTraverser(nsResolver).Traverse(methodNode)
dfsTraverser.Traverse(methodNode)
assert.DeepEqual(t, expected, nsResolver.ResolvedNames) assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
} }
@ -561,9 +546,8 @@ func TestResolveClosureName(t *testing.T) {
nameBC: "B\\C", nameBC: "B\\C",
} }
nsResolver := visitor.NewNamespaceResolver() nsResolver := nsresolver.NewNamespaceResolver()
dfsTraverser := traverser.NewDFS(nsResolver) traverser.NewTraverser(nsResolver).Traverse(closureNode)
dfsTraverser.Traverse(closureNode)
assert.DeepEqual(t, expected, nsResolver.ResolvedNames) assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
} }
@ -599,9 +583,8 @@ func TestResolveConstantsName(t *testing.T) {
constantC: "A\\B\\C", constantC: "A\\B\\C",
} }
nsResolver := visitor.NewNamespaceResolver() nsResolver := nsresolver.NewNamespaceResolver()
dfsTraverser := traverser.NewDFS(nsResolver) traverser.NewTraverser(nsResolver).Traverse(stxTree)
dfsTraverser.Traverse(stxTree)
assert.DeepEqual(t, expected, nsResolver.ResolvedNames) assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
} }
@ -673,9 +656,8 @@ func TestResolveNamespaces(t *testing.T) {
nameCF: "A\\C\\F", nameCF: "A\\C\\F",
} }
nsResolver := visitor.NewNamespaceResolver() nsResolver := nsresolver.NewNamespaceResolver()
dfsTraverser := traverser.NewDFS(nsResolver) traverser.NewTraverser(nsResolver).Traverse(stxTree)
dfsTraverser.Traverse(stxTree)
assert.DeepEqual(t, expected, nsResolver.ResolvedNames) assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
} }
@ -692,9 +674,8 @@ func TestResolveStaticCallDinamicClassName(t *testing.T) {
expected := map[ast.Vertex]string{} expected := map[ast.Vertex]string{}
nsResolver := visitor.NewNamespaceResolver() nsResolver := nsresolver.NewNamespaceResolver()
dfsTraverser := traverser.NewDFS(nsResolver) traverser.NewTraverser(nsResolver).Traverse(stxTree)
dfsTraverser.Traverse(stxTree)
assert.DeepEqual(t, expected, nsResolver.ResolvedNames) assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
} }
@ -749,9 +730,8 @@ func TestDoNotResolveReservedConstants(t *testing.T) {
constantNull: "null", constantNull: "null",
} }
nsResolver := visitor.NewNamespaceResolver() nsResolver := nsresolver.NewNamespaceResolver()
dfsTraverser := traverser.NewDFS(nsResolver) traverser.NewTraverser(nsResolver).Traverse(stxTree)
dfsTraverser.Traverse(stxTree)
assert.DeepEqual(t, expected, nsResolver.ResolvedNames) assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
} }
@ -872,9 +852,8 @@ func TestDoNotResolveReservedNames(t *testing.T) {
nameObject: "object", nameObject: "object",
} }
nsResolver := visitor.NewNamespaceResolver() nsResolver := nsresolver.NewNamespaceResolver()
dfsTraverser := traverser.NewDFS(nsResolver) traverser.NewTraverser(nsResolver).Traverse(stxTree)
dfsTraverser.Traverse(stxTree)
assert.DeepEqual(t, expected, nsResolver.ResolvedNames) assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
} }
@ -943,9 +922,8 @@ func TestDoNotResolveReservedSpecialNames(t *testing.T) {
nameParent: "parent", nameParent: "parent",
} }
nsResolver := visitor.NewNamespaceResolver() nsResolver := nsresolver.NewNamespaceResolver()
dfsTraverser := traverser.NewDFS(nsResolver) traverser.NewTraverser(nsResolver).Traverse(stxTree)
dfsTraverser.Traverse(stxTree)
assert.DeepEqual(t, expected, nsResolver.ResolvedNames) assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
} }
@ -995,9 +973,8 @@ func TestResolvePropertyTypeName(t *testing.T) {
classNode: "Foo\\Bar", classNode: "Foo\\Bar",
} }
nsResolver := visitor.NewNamespaceResolver() nsResolver := nsresolver.NewNamespaceResolver()
dfsTraverser := traverser.NewDFS(nsResolver) traverser.NewTraverser(nsResolver).Traverse(stmts)
dfsTraverser.Traverse(stmts)
assert.DeepEqual(t, expected, nsResolver.ResolvedNames) assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
} }

View File

@ -1,4 +1,4 @@
package visitor package printer
import ( import (
"bytes" "bytes"

View File

@ -1,8 +1,8 @@
package visitor_test package printer_test
import ( import (
"bytes" "bytes"
"github.com/z7zmey/php-parser/pkg/ast/visitor" printer2 "github.com/z7zmey/php-parser/pkg/visitor/printer"
"testing" "testing"
"github.com/z7zmey/php-parser/pkg/ast" "github.com/z7zmey/php-parser/pkg/ast"
@ -22,7 +22,7 @@ func parsePhp5(src string) ast.Vertex {
func printPhp5(n ast.Vertex) string { func printPhp5(n ast.Vertex) string {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
printer := visitor.NewPrinter(o) printer := printer2.NewPrinter(o)
n.Accept(printer) n.Accept(printer)
return o.String() return o.String()

View File

@ -1,14 +1,14 @@
package visitor_test package printer_test
import ( import (
"bytes" "bytes"
printer2 "github.com/z7zmey/php-parser/pkg/visitor/printer"
"os" "os"
"testing" "testing"
"github.com/z7zmey/php-parser/internal/php7" "github.com/z7zmey/php-parser/internal/php7"
"github.com/z7zmey/php-parser/internal/scanner" "github.com/z7zmey/php-parser/internal/scanner"
"github.com/z7zmey/php-parser/pkg/ast" "github.com/z7zmey/php-parser/pkg/ast"
"github.com/z7zmey/php-parser/pkg/ast/visitor"
) )
func ExamplePrinter() { func ExamplePrinter() {
@ -41,7 +41,7 @@ abstract class Bar extends Baz
// print // print
printer := visitor.NewPrinter(os.Stdout) printer := printer2.NewPrinter(os.Stdout)
rootNode.Accept(printer) rootNode.Accept(printer)
// Output: // Output:
@ -70,7 +70,7 @@ func parse(src string) ast.Vertex {
func print(n ast.Vertex) string { func print(n ast.Vertex) string {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
printer := visitor.NewPrinter(o) printer := printer2.NewPrinter(o)
n.Accept(printer) n.Accept(printer)
return o.String() return o.String()

File diff suppressed because it is too large Load Diff