ExceedsDepth determines whether the AST contains expressions nested deeper than the specified maxDepth. The root expression has depth 0, so a maxDepth of 250 permits expressions nested up to and including 250 levels deep. The traversal is bounded: it descends at most maxDepth+1 levels, so it remain
(a *AST, maxDepth int)
| 189 | // adversarially deep inputs that could otherwise exhaust the Go stack during later checking or |
| 190 | // planning. A non-positive maxDepth disables the check and returns false. |
| 191 | func ExceedsDepth(a *AST, maxDepth int) bool { |
| 192 | if a == nil || maxDepth <= 0 { |
| 193 | return false |
| 194 | } |
| 195 | exceedsDepth := false |
| 196 | visitor := NewExprVisitor(func(e Expr) { |
| 197 | if nav, ok := e.(NavigableExpr); ok && nav.Depth() >= maxDepth { |
| 198 | exceedsDepth = true |
| 199 | } |
| 200 | }) |
| 201 | // Bound the walk to maxDepth+1 levels so it never recurses past the first level that exceeds |
| 202 | // the limit, keeping the check itself safe on the deep inputs it guards against. |
| 203 | visit(NavigateAST(a), visitor, postOrder, 0, maxDepth+1) |
| 204 | return exceedsDepth |
| 205 | } |
| 206 | |
| 207 | type visitOrder int |
| 208 |