2020-12-28 19:13:08 +00:00
|
|
|
package traverser
|
|
|
|
|
|
|
|
import (
|
2021-07-30 18:01:34 +00:00
|
|
|
"github.com/VKCOM/php-parser/pkg/ast"
|
2020-12-28 19:13:08 +00:00
|
|
|
)
|
|
|
|
|
2023-03-25 20:55:24 +00:00
|
|
|
//go:generate go run traverser_gen.go
|
|
|
|
|
|
|
|
type CheckEntrance interface {
|
|
|
|
EnterNode(ast.Vertex) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type NotifyLeave interface {
|
|
|
|
LeaveNode(ast.Vertex)
|
|
|
|
}
|
|
|
|
|
2020-12-28 19:13:08 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-25 20:55:24 +00:00
|
|
|
func (t *Traverser) checkEntrance(n ast.Vertex) bool {
|
|
|
|
if ssv, ok := t.v.(CheckEntrance); ok {
|
|
|
|
return ssv.EnterNode(n)
|
2020-12-28 19:13:08 +00:00
|
|
|
}
|
|
|
|
|
2023-03-25 20:55:24 +00:00
|
|
|
return true
|
2020-12-28 19:13:08 +00:00
|
|
|
}
|
|
|
|
|
2023-03-25 20:55:24 +00:00
|
|
|
func (t *Traverser) leave(n ast.Vertex) {
|
|
|
|
if ssv, ok := t.v.(NotifyLeave); ok {
|
|
|
|
ssv.LeaveNode(n)
|
2020-12-28 19:13:08 +00:00
|
|
|
}
|
|
|
|
}
|