feat: add enter and leave checks for traversers

- Improved performance (can short circuit/not continue when done)
- Backwards compatible because added interfaces
- Now generating the implementation for easier updates
This commit is contained in:
Laytan Laats 2023-03-25 21:55:24 +01:00
parent b934f01c67
commit 25877adda4
6 changed files with 2722 additions and 1273 deletions

1
go.mod
View File

@ -11,4 +11,5 @@ require (
require ( require (
github.com/google/go-cmp v0.4.0 // indirect github.com/google/go-cmp v0.4.0 // indirect
github.com/pkg/errors v0.9.1 // indirect github.com/pkg/errors v0.9.1 // indirect
golang.org/x/tools v0.7.0
) )

40
go.sum
View File

@ -6,6 +6,46 @@ github.com/pkg/profile v1.4.0 h1:uCmaf4vVbWAOZz36k1hrQD7ijGRzLwaME8Am/7a4jZI=
github.com/pkg/profile v1.4.0/go.mod h1:NWz/XGvpEW1FyYQ7fCx4dqYBLlfTcE+A9FLAkNKqjFE= github.com/pkg/profile v1.4.0/go.mod h1:NWz/XGvpEW1FyYQ7fCx4dqYBLlfTcE+A9FLAkNKqjFE=
github.com/yookoala/realpath v1.0.0 h1:7OA9pj4FZd+oZDsyvXWQvjn5oBdcHRTV44PpdMSuImQ= github.com/yookoala/realpath v1.0.0 h1:7OA9pj4FZd+oZDsyvXWQvjn5oBdcHRTV44PpdMSuImQ=
github.com/yookoala/realpath v1.0.0/go.mod h1:gJJMA9wuX7AcqLy1+ffPatSCySA1FQ2S8Ya9AIoYBpE= github.com/yookoala/realpath v1.0.0/go.mod h1:gJJMA9wuX7AcqLy1+ffPatSCySA1FQ2S8Ya9AIoYBpE=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=
golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4=
golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,173 @@
//go:build ignore
// +build ignore
package main
import (
"fmt"
"go/ast"
"log"
"os"
"path/filepath"
"text/template"
phpast "github.com/VKCOM/php-parser/pkg/ast"
"golang.org/x/tools/go/packages"
)
type nodeType struct {
// The type name.
Name string
// The function name.
FuncName string
// Map of field names to whether the field is an array or not.
Traversable map[string]bool
}
type templData struct {
Types []nodeType
}
var fileTempl = template.Must(
template.New("").Parse(`// Code generated by "go generate go run traverser_gen.go"; DO NOT EDIT.
package traverser
import (
"github.com/VKCOM/php-parser/pkg/ast"
// Importing packages here, so that go mod tidy does not remove the dependency on it.
// It is used in traverser_gen.go but that is ignored with go mod tidy.
"golang.org/x/tools/go/packages"
)
{{range $typ := .Types}}
func (t *Traverser) {{$typ.FuncName}}(n *ast.{{$typ.Name}}) {
if !t.checkEntrance(n) {
return
}
n.Accept(t.v)
{{range $name, $isArray := $typ.Traversable}}
{{- if $isArray}}
for _, nn := range n.{{$name}} {
nn.Accept(t)
}
{{else}}
t.Traverse(n.{{$name}})
{{end}}
{{- end}}
t.leave(n)
}
{{end}}`),
)
func main() {
ast := astOfFile("../../ast/node.go")
nodeTypes := getNodeTypes(ast)
file, err := os.Create("traverser_impl.go")
if err != nil {
panic(fmt.Errorf("open 'traverser_impl.go': %w", err))
}
fileTempl.Execute(file, templData{Types: nodeTypes})
}
func astOfFile(path string) *ast.File {
nodesFile, err := filepath.Abs(path)
if err != nil {
panic(fmt.Errorf("getting '%s' absolute path: %w", path, err))
}
cfg := &packages.Config{
Mode: packages.NeedName | packages.NeedFiles | packages.NeedTypes | packages.NeedTypesInfo | packages.NeedSyntax,
Tests: false,
}
pkgs, err := packages.Load(cfg, fmt.Sprintf("file=%s", nodesFile))
if err != nil {
panic(fmt.Errorf("loading '%s' package: %w", nodesFile, err))
}
if len(pkgs) == 0 {
panic(fmt.Errorf("could not get any package for file '%s'", nodesFile))
}
pkg := pkgs[0]
var syntax *ast.File
for i, fn := range pkg.GoFiles {
if fn == nodesFile {
syntax = pkg.Syntax[i]
break
}
}
if syntax == nil {
panic(fmt.Errorf("could not get ast of file: '%s'", nodesFile))
}
return syntax
}
func getNodeTypes(syntax *ast.File) []nodeType {
types := []nodeType{}
for _, decl := range syntax.Decls {
typedDecl, ok := decl.(*ast.GenDecl)
if !ok {
log.Printf("%T not *ast.GenDecl", decl)
continue
}
for _, spec := range typedDecl.Specs {
typedSpec, ok := spec.(*ast.TypeSpec)
if !ok {
if _, ok := spec.(*ast.ImportSpec); !ok {
log.Printf("%T not *ast.TypeSpec", spec)
}
continue
}
structType, ok := typedSpec.Type.(*ast.StructType)
if !ok {
log.Printf("%T not *ast.StructType", spec)
continue
}
nType := nodeType{
Name: typedSpec.Name.String(),
FuncName: typedSpec.Name.String(),
Traversable: map[string]bool{},
}
if funcName, ok := phpast.TypeToVisitorNameMap[typedSpec.Name.String()]; ok {
nType.FuncName = funcName
}
for _, field := range structType.Fields.List {
if traversable, isArray := checkType(field.Type); traversable {
for _, n := range field.Names {
nType.Traversable[n.String()] = isArray
}
}
}
types = append(types, nType)
}
}
return types
}
func checkType(t ast.Expr) (traversable bool, isArray bool) {
switch ft := t.(type) {
case *ast.ArrayType:
ok, _ := checkType(ft.Elt)
return ok, true
case *ast.Ident:
switch ft.Name {
case "Vertex":
return true, false
default:
return false, false
}
default:
return false, false
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,67 @@
package traverser_test
import (
"testing"
"github.com/VKCOM/php-parser/pkg/ast"
"github.com/VKCOM/php-parser/pkg/visitor"
"github.com/VKCOM/php-parser/pkg/visitor/traverser"
)
// testCase is a traverser that does not go into any class statement or its children.
// If it does, the test fails.
type testCase struct {
t *testing.T
visitor.Null
traversedFunction bool
}
var _ ast.Visitor = &testCase{}
func (t *testCase) EnterNode(n ast.Vertex) bool {
t.t.Logf("EnterNode: %T", n)
if _, ok := n.(*ast.StmtClass); ok {
return false
}
return true
}
func (t *testCase) LeaveNode(n ast.Vertex) {
t.t.Logf("LeaveNode: %T", n)
if _, ok := n.(*ast.Root); ok {
if !t.traversedFunction {
t.t.Error("traverser did not traverse function")
}
}
}
func (t *testCase) StmtClass(n *ast.StmtClass) {
t.t.Errorf("traverser got to class")
}
func (t *testCase) StmtClassMethod(n *ast.StmtClassMethod) {
t.t.Errorf("traverser got to method")
}
func (t *testCase) StmtFunction(n *ast.StmtFunction) {
t.traversedFunction = true
}
func TestEnterNodeIsRespected(t *testing.T) {
tc := &testCase{t: t}
tv := traverser.NewTraverser(tc)
root := &ast.Root{
Stmts: []ast.Vertex{
&ast.StmtFunction{},
&ast.StmtClass{
Stmts: []ast.Vertex{
&ast.StmtClassMethod{},
},
},
},
}
root.Accept(tv)
}