php-parser/pkg/visitor/traverser/traverser.go
Laytan Laats 25877adda4 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
2023-03-25 22:03:02 +01:00

46 lines
675 B
Go

package traverser
import (
"github.com/VKCOM/php-parser/pkg/ast"
)
//go:generate go run traverser_gen.go
type CheckEntrance interface {
EnterNode(ast.Vertex) bool
}
type NotifyLeave interface {
LeaveNode(ast.Vertex)
}
type Traverser struct {
v ast.Visitor
}
func NewTraverser(v ast.Visitor) *Traverser {
return &Traverser{
v: v,
}
}
func (t *Traverser) Traverse(n ast.Vertex) {
if n != nil {
n.Accept(t)
}
}
func (t *Traverser) checkEntrance(n ast.Vertex) bool {
if ssv, ok := t.v.(CheckEntrance); ok {
return ssv.EnterNode(n)
}
return true
}
func (t *Traverser) leave(n ast.Vertex) {
if ssv, ok := t.v.(NotifyLeave); ok {
ssv.LeaveNode(n)
}
}