1
0
mirror of https://github.com/go-task/task.git synced 2024-12-16 10:59:23 +02:00
task/vendor/github.com/mvdan/sh/syntax/walk.go

192 lines
3.4 KiB
Go
Raw Normal View History

2017-04-24 14:47:10 +02:00
// Copyright (c) 2016, Daniel Martí <mvdan@mvdan.cc>
// See LICENSE for licensing information
package syntax
import "fmt"
func walkStmts(stmts []*Stmt, f func(Node) bool) {
for _, s := range stmts {
Walk(s, f)
}
}
func walkWords(words []*Word, f func(Node) bool) {
for _, w := range words {
Walk(w, f)
}
}
// Walk traverses an AST in depth-first order: It starts by calling
// f(node); node must not be nil. If f returns true, Walk invokes f
// recursively for each of the non-nil children of node, followed by
// f(nil).
func Walk(node Node, f func(Node) bool) {
if !f(node) {
return
}
switch x := node.(type) {
case *File:
walkStmts(x.Stmts, f)
case *Stmt:
if x.Cmd != nil {
Walk(x.Cmd, f)
}
for _, a := range x.Assigns {
Walk(a, f)
}
for _, r := range x.Redirs {
Walk(r, f)
}
case *Assign:
if x.Name != nil {
Walk(x.Name, f)
}
if x.Value != nil {
Walk(x.Value, f)
}
2017-05-17 19:49:27 +02:00
if x.Array != nil {
Walk(x.Array, f)
}
2017-04-24 14:47:10 +02:00
case *Redirect:
if x.N != nil {
Walk(x.N, f)
}
Walk(x.Word, f)
if x.Hdoc != nil {
Walk(x.Hdoc, f)
}
case *CallExpr:
walkWords(x.Args, f)
case *Subshell:
walkStmts(x.Stmts, f)
case *Block:
walkStmts(x.Stmts, f)
case *IfClause:
walkStmts(x.CondStmts, f)
walkStmts(x.ThenStmts, f)
for _, elif := range x.Elifs {
walkStmts(elif.CondStmts, f)
walkStmts(elif.ThenStmts, f)
}
walkStmts(x.ElseStmts, f)
case *WhileClause:
walkStmts(x.CondStmts, f)
walkStmts(x.DoStmts, f)
case *ForClause:
Walk(x.Loop, f)
walkStmts(x.DoStmts, f)
case *WordIter:
Walk(x.Name, f)
2017-05-27 16:17:49 +02:00
walkWords(x.Items, f)
2017-04-24 14:47:10 +02:00
case *CStyleLoop:
if x.Init != nil {
Walk(x.Init, f)
}
if x.Cond != nil {
Walk(x.Cond, f)
}
if x.Post != nil {
Walk(x.Post, f)
}
case *BinaryCmd:
Walk(x.X, f)
Walk(x.Y, f)
case *FuncDecl:
Walk(x.Name, f)
Walk(x.Body, f)
case *Word:
for _, wp := range x.Parts {
Walk(wp, f)
}
case *Lit:
case *SglQuoted:
case *DblQuoted:
for _, wp := range x.Parts {
Walk(wp, f)
}
case *CmdSubst:
walkStmts(x.Stmts, f)
case *ParamExp:
if x.Param != nil {
Walk(x.Param, f)
}
2017-05-17 19:49:27 +02:00
if x.Index != nil {
Walk(x.Index, f)
2017-04-24 14:47:10 +02:00
}
if x.Repl != nil {
2017-05-27 16:17:49 +02:00
if x.Repl.Orig != nil {
Walk(x.Repl.Orig, f)
}
if x.Repl.With != nil {
Walk(x.Repl.With, f)
}
}
if x.Exp != nil && x.Exp.Word != nil {
2017-04-24 14:47:10 +02:00
Walk(x.Exp.Word, f)
}
case *ArithmExp:
2017-05-27 16:17:49 +02:00
Walk(x.X, f)
2017-04-24 14:47:10 +02:00
case *ArithmCmd:
2017-05-27 16:17:49 +02:00
Walk(x.X, f)
2017-04-24 14:47:10 +02:00
case *BinaryArithm:
Walk(x.X, f)
Walk(x.Y, f)
case *BinaryTest:
Walk(x.X, f)
Walk(x.Y, f)
case *UnaryArithm:
Walk(x.X, f)
case *UnaryTest:
Walk(x.X, f)
case *ParenArithm:
Walk(x.X, f)
case *ParenTest:
Walk(x.X, f)
case *CaseClause:
Walk(x.Word, f)
2017-05-27 16:17:49 +02:00
for _, ci := range x.Items {
walkWords(ci.Patterns, f)
walkStmts(ci.Stmts, f)
2017-04-24 14:47:10 +02:00
}
case *TestClause:
Walk(x.X, f)
case *DeclClause:
walkWords(x.Opts, f)
for _, a := range x.Assigns {
Walk(a, f)
}
case *ArrayExpr:
2017-05-27 16:17:49 +02:00
for _, el := range x.Elems {
Walk(el, f)
}
case *ArrayElem:
if x.Index != nil {
Walk(x.Index, f)
}
Walk(x.Value, f)
2017-04-24 14:47:10 +02:00
case *ExtGlob:
Walk(x.Pattern, f)
case *ProcSubst:
walkStmts(x.Stmts, f)
2017-05-27 16:17:49 +02:00
case *TimeClause:
if x.Stmt != nil {
Walk(x.Stmt, f)
}
2017-04-24 14:47:10 +02:00
case *CoprocClause:
if x.Name != nil {
Walk(x.Name, f)
}
Walk(x.Stmt, f)
case *LetClause:
for _, expr := range x.Exprs {
Walk(expr, f)
}
default:
panic(fmt.Sprintf("syntax.Walk: unexpected node type %T", x))
}
f(nil)
}