Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ast: walk add Go+ expr and stmt #1480

Merged
merged 1 commit into from
Oct 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions ast/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ func walkDeclList(v Visitor, list []Decl) {
// v.Visit(node) is not nil, Walk is invoked recursively with visitor
// w for each of the non-nil children of node, followed by a call of
// w.Visit(nil).
//
func Walk(v Visitor, node Node) {
if v = v.Visit(node); v == nil {
return
Expand Down Expand Up @@ -372,6 +371,62 @@ func Walk(v Visitor, node Node) {
Walk(v, f)
}

// Go+ expr and stmt
case *SliceLit:
walkExprList(v, n.Elts)

case *ErrWrapExpr:
Walk(v, n.X)
if n.Default != nil {
Walk(v, n.Default)
}

case *LambdaExpr:
walkIdentList(v, n.Lhs)
walkExprList(v, n.Rhs)

case *LambdaExpr2:
walkIdentList(v, n.Lhs)
Walk(v, n.Body)

case *ForPhrase:
if n.Key != nil {
Walk(v, n.Key)
}
if n.Value != nil {
Walk(v, n.Value)
}
if n.Init != nil {
Walk(v, n.Init)
}
if n.Cond != nil {
Walk(v, n.Cond)
}
Walk(v, n.X)

case *ComprehensionExpr:
if n.Elt != nil {
Walk(v, n.Elt)
}
for _, x := range n.Fors {
Walk(v, x)
}

case *ForPhraseStmt:
Walk(v, n.ForPhrase)
Walk(v, n.Body)

case *RangeExpr:
if n.First != nil {
Walk(v, n.First)
}
if n.Last != nil {
Walk(v, n.Last)
}
if n.Expr3 != nil {
Walk(v, n.Expr3)
}

default:
panic(fmt.Sprintf("ast.Walk: unexpected node type %T", n))
}
Expand All @@ -392,7 +447,6 @@ func (f inspector) Visit(node Node) Visitor {
// f(node); node must not be nil. If f returns true, Inspect invokes f
// recursively for each of the non-nil children of node, followed by a
// call of f(nil).
//
func Inspect(node Node, f func(Node) bool) {
Walk(inspector(f), node)
}