| 1 |
// Copyright 2017 The Bazel Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package syntax |
| 6 |
|
| 7 |
// Walk traverses a syntax tree in depth-first order. |
| 8 |
// It starts by calling f(n); n must not be nil. |
| 9 |
// If f returns true, Walk calls itself |
| 10 |
// recursively for each non-nil child of n. |
| 11 |
// Walk then calls f(nil). |
| 12 |
func Walk(n Node, f func(Node) bool) { |
| 13 |
if n == nil { |
| 14 |
panic("nil") |
| 15 |
} |
| 16 |
if !f(n) { |
| 17 |
return |
| 18 |
} |
| 19 |
|
| 20 |
// TODO(adonovan): opt: order cases using profile data. |
| 21 |
switch n := n.(type) { |
| 22 |
case *File: |
| 23 |
walkStmts(n.Stmts, f) |
| 24 |
|
| 25 |
case *ExprStmt: |
| 26 |
Walk(n.X, f) |
| 27 |
|
| 28 |
case *BranchStmt: |
| 29 |
// no-op |
| 30 |
|
| 31 |
case *IfStmt: |
| 32 |
Walk(n.Cond, f) |
| 33 |
walkStmts(n.True, f) |
| 34 |
walkStmts(n.False, f) |
| 35 |
|
| 36 |
case *AssignStmt: |
| 37 |
Walk(n.LHS, f) |
| 38 |
Walk(n.RHS, f) |
| 39 |
|
| 40 |
case *DefStmt: |
| 41 |
Walk(n.Name, f) |
| 42 |
for _, param := range n.Params { |
| 43 |
Walk(param, f) |
| 44 |
} |
| 45 |
walkStmts(n.Body, f) |
| 46 |
|
| 47 |
case *ForStmt: |
| 48 |
Walk(n.Vars, f) |
| 49 |
Walk(n.X, f) |
| 50 |
walkStmts(n.Body, f) |
| 51 |
|
| 52 |
case *WhileStmt: |
| 53 |
Walk(n.Cond, f) |
| 54 |
walkStmts(n.Body, f) |
| 55 |
|
| 56 |
case *ReturnStmt: |
| 57 |
if n.Result != nil { |
| 58 |
Walk(n.Result, f) |
| 59 |
} |
| 60 |
|
| 61 |
case *LoadStmt: |
| 62 |
Walk(n.Module, f) |
| 63 |
for _, from := range n.From { |
| 64 |
Walk(from, f) |
| 65 |
} |
| 66 |
for _, to := range n.To { |
| 67 |
Walk(to, f) |
| 68 |
} |
| 69 |
|
| 70 |
case *Ident, *Literal: |
| 71 |
// no-op |
| 72 |
|
| 73 |
case *ListExpr: |
| 74 |
for _, x := range n.List { |
| 75 |
Walk(x, f) |
| 76 |
} |
| 77 |
|
| 78 |
case *ParenExpr: |
| 79 |
Walk(n.X, f) |
| 80 |
|
| 81 |
case *CondExpr: |
| 82 |
Walk(n.Cond, f) |
| 83 |
Walk(n.True, f) |
| 84 |
Walk(n.False, f) |
| 85 |
|
| 86 |
case *IndexExpr: |
| 87 |
Walk(n.X, f) |
| 88 |
Walk(n.Y, f) |
| 89 |
|
| 90 |
case *DictEntry: |
| 91 |
Walk(n.Key, f) |
| 92 |
Walk(n.Value, f) |
| 93 |
|
| 94 |
case *SliceExpr: |
| 95 |
Walk(n.X, f) |
| 96 |
if n.Lo != nil { |
| 97 |
Walk(n.Lo, f) |
| 98 |
} |
| 99 |
if n.Hi != nil { |
| 100 |
Walk(n.Hi, f) |
| 101 |
} |
| 102 |
if n.Step != nil { |
| 103 |
Walk(n.Step, f) |
| 104 |
} |
| 105 |
|
| 106 |
case *Comprehension: |
| 107 |
Walk(n.Body, f) |
| 108 |
for _, clause := range n.Clauses { |
| 109 |
Walk(clause, f) |
| 110 |
} |
| 111 |
|
| 112 |
case *IfClause: |
| 113 |
Walk(n.Cond, f) |
| 114 |
|
| 115 |
case *ForClause: |
| 116 |
Walk(n.Vars, f) |
| 117 |
Walk(n.X, f) |
| 118 |
|
| 119 |
case *TupleExpr: |
| 120 |
for _, x := range n.List { |
| 121 |
Walk(x, f) |
| 122 |
} |
| 123 |
|
| 124 |
case *DictExpr: |
| 125 |
for _, entry := range n.List { |
| 126 |
Walk(entry, f) |
| 127 |
} |
| 128 |
|
| 129 |
case *UnaryExpr: |
| 130 |
if n.X != nil { |
| 131 |
Walk(n.X, f) |
| 132 |
} |
| 133 |
|
| 134 |
case *BinaryExpr: |
| 135 |
Walk(n.X, f) |
| 136 |
Walk(n.Y, f) |
| 137 |
|
| 138 |
case *DotExpr: |
| 139 |
Walk(n.X, f) |
| 140 |
Walk(n.Name, f) |
| 141 |
|
| 142 |
case *CallExpr: |
| 143 |
Walk(n.Fn, f) |
| 144 |
for _, arg := range n.Args { |
| 145 |
Walk(arg, f) |
| 146 |
} |
| 147 |
|
| 148 |
case *LambdaExpr: |
| 149 |
for _, param := range n.Params { |
| 150 |
Walk(param, f) |
| 151 |
} |
| 152 |
Walk(n.Body, f) |
| 153 |
|
| 154 |
default: |
| 155 |
panic(n) |
| 156 |
} |
| 157 |
|
| 158 |
f(nil) |
| 159 |
} |
| 160 |
|
| 161 |
func walkStmts(stmts []Stmt, f func(Node) bool) { |
| 162 |
for _, stmt := range stmts { |
| 163 |
Walk(stmt, f) |
| 164 |
} |
| 165 |
} |