// walk returns the last leaf / branch present in the trie and the index in the namespace that the last node exists.
// It is useful e.g. to locate the right place to add new metric type into tree with the given namespace
176787
func (mtt *mttNode) walk(ns []string) (*mttNode, int) {
	parent := mtt
	var pp *mttNode

	for i, n := range ns {
		if parent.children == nil {
			return parent, i
		}

		pp = parent
		parent = parent.children[n]

		if parent == nil {
			return pp, i
		}
	}
	return parent, len(ns)
}
1225103
func (n *ConstFetch) Walk(v walker.Visitor) {
	if v.EnterNode(n) == false {
		return
	}

	if n.Constant != nil {
		v.EnterChildNode("Constant", n)
		n.Constant.Walk(v)
		v.LeaveChildNode("Constant", n)
	}

	v.LeaveNode(n)
}
318407
func (n *Static) Walk(v walker.Visitor) {
	if v.EnterNode(n) == false {
		return
	}

	if n.Vars != nil {
		v.EnterChildList("Vars", n)
		for _, nn := range n.Vars {
			if nn != nil {
				nn.Walk(v)
			}
		}
		v.LeaveChildList("Vars", n)
	}

	v.LeaveNode(n)
}
407053
func (n *ClosureUse) Walk(v walker.Visitor) {
	if v.EnterNode(n) == false {
		return
	}

	if n.Uses != nil {
		v.EnterChildList("Uses", n)
		for _, nn := range n.Uses {
			if nn != nil {
				nn.Walk(v)
			}
		}
		v.LeaveChildList("Uses", n)
	}

	v.LeaveNode(n)
}
889519
func (s *state) walk(node ast.Node) {
	s.at(node)
	switch node := node.(type) {
	case *ast.SoyFileNode:
		s.visitSoyFile(node)
	case *ast.NamespaceNode:
		s.visitNamespace(node)
	case *ast.SoyDocNode:
		return
	case *ast.TemplateNode:
		s.visitTemplate(node)
	case *ast.ListNode:
		s.visitChildren(node)

		// Output nodes ----------
	case *ast.RawTextNode:
		s.writeRawText(node.Text)
	case *ast.PrintNode:
		s.visitPrint(node)
	case *ast.MsgNode:
		s.visitMsg(node)
	case *ast.MsgHtmlTagNode:
		s.writeRawText(node.Text)
	case *ast.CssNode:
		if node.Expr != nil {
			s.jsln(s.bufferName, " += ", node.Expr, " + '-';")
		}
		s.writeRawText([]byte(node.Suffix))
	case *ast.DebuggerNode:
		s.jsln("debugger;")
	case *ast.LogNode:
		s.bufferName += "_"
		s.jsln("var ", s.bufferName, " = '';")
		s.walk(node.Body)
		s.jsln("console.log(", s.bufferName, ");")
		s.bufferName = s.bufferName[:len(s.bufferName)-1]

	// Control flow ----------
	case *ast.IfNode:
		s.visitIf(node)
	case *ast.ForNode:
		s.visitFor(node)
	case *ast.SwitchNode:
		s.visitSwitch(node)
	case *ast.CallNode:
		s.visitCall(node)
	case *ast.LetValueNode:
		s.jsln("var ", s.scope.makevar(node.Name), " = ", node.Expr, ";")
	case *ast.LetContentNode:
		var oldBufferName = s.bufferName
		s.bufferName = s.scope.makevar(node.Name)
		s.jsln("var ", s.bufferName, " = '';")
		s.walk(node.Body)
		s.bufferName = oldBufferName

	// Values ----------
	case *ast.NullNode:
		s.js("null")
	case *ast.StringNode:
		s.js("'")
		template.JSEscape(s.wr, []byte(node.Value))
		s.js("'")
	case *ast.IntNode:
		s.js(node.String())
	case *ast.FloatNode:
		s.js(node.String())
	case *ast.BoolNode:
		s.js(node.String())
	case *ast.GlobalNode:
		s.visitGlobal(node)
	case *ast.ListLiteralNode:
		s.js("[")
		for i, item := range node.Items {
			if i != 0 {
				s.js(",")
			}
			s.walk(item)
		}
		s.js("]")
	case *ast.MapLiteralNode:
		s.js("{")
		var (
			first = true
			keys  = make([]string, len(node.Items))
			i     = 0
		)
		for k := range node.Items {
			keys[i] = k
			i++
		}
		sort.Strings(keys)
		for _, k := range keys {
			if !first {
				s.js(",")
			}
			first = false
			s.js("\"", k, "\"", ":")
			s.walk(node.Items[k])
		}
		s.js("}")
	case *ast.FunctionNode:
		s.visitFunction(node)
	case *ast.DataRefNode:
		s.visitDataRef(node)

	// Arithmetic operators ----------
	case *ast.NegateNode:
		s.js("(-", node.Arg, ")")
	case *ast.AddNode:
		s.op("+", node)
	case *ast.SubNode:
		s.op("-", node)
	case *ast.DivNode:
		s.op("/", node)
	case *ast.MulNode:
		s.op("*", node)
	case *ast.ModNode:
		s.op("%", node)

		// Arithmetic comparisons ----------
	case *ast.EqNode:
		s.op("==", node)
	case *ast.NotEqNode:
		s.op("!=", node)
	case *ast.LtNode:
		s.op("<", node)
	case *ast.LteNode:
		s.op("<=", node)
	case *ast.GtNode:
		s.op(">", node)
	case *ast.GteNode:
		s.op(">=", node)

	// Boolean operators ----------
	case *ast.NotNode:
		s.js("!(", node.Arg, ")")
	case *ast.AndNode:
		s.op("&&", node)
	case *ast.OrNode:
		s.op("||", node)
	case *ast.ElvisNode:
		// ?: is specified to check for null.
		s.js("((", node.Arg1, ") != null ? ", node.Arg1, " : ", node.Arg2, ")")
	case *ast.TernNode:
		s.js("((", node.Arg1, ") ?", node.Arg2, ":", node.Arg3, ")")

	default:
		s.errorf("unknown node (%T): %v", node, node)
	}
}
1266946
func (n *Array) Walk(v walker.Visitor) {
	if v.EnterNode(n) == false {
		return
	}

	if n.Items != nil {
		v.EnterChildList("Items", n)
		for _, nn := range n.Items {
			if nn != nil {
				nn.Walk(v)
			}
		}
		v.LeaveChildList("Items", n)
	}

	v.LeaveNode(n)
}
1240447
func (n *Try) Walk(v walker.Visitor) {
	if v.EnterNode(n) == false {
		return
	}

	if n.Stmts != nil {
		v.EnterChildList("Stmts", n)
		for _, nn := range n.Stmts {
			if nn != nil {
				nn.Walk(v)
			}
		}
		v.LeaveChildList("Stmts", n)
	}

	if n.Catches != nil {
		v.EnterChildList("Catches", n)
		for _, nn := range n.Catches {
			if nn != nil {
				nn.Walk(v)
			}
		}
		v.LeaveChildList("Catches", n)
	}

	if n.Finally != nil {
		v.EnterChildNode("Finally", n)
		n.Finally.Walk(v)
		v.LeaveChildNode("Finally", n)
	}

	v.LeaveNode(n)
}
692206
func (me *TLibraryType) Walk() (err error) {
	if fn := WalkHandlers.TLibraryType; me != nil {
		if fn != nil {
			if err = fn(me, true); xsdt.OnWalkError(&err, &WalkErrors, WalkContinueOnError, WalkOnError) {
				return
			}
		}
		if err = me.XsdGoPkgHasElem_DesignDESCRIPTIONsequenceLibraryTypeschema_DesignDescription_XsdtString_.Walk(); xsdt.OnWalkError(&err, &WalkErrors, WalkContinueOnError, WalkOnError) {
			return
		}
		if err = me.XsdGoPkgHasElem_SampleDESCRIPTORsequenceLibraryTypeschema_SampleDescriptor_TSampleDescriptorType_.Walk(); xsdt.OnWalkError(&err, &WalkErrors, WalkContinueOnError, WalkOnError) {
			return
		}
		if err = me.XsdGoPkgHasElem_LibraryDESCRIPTORsequenceLibraryTypeschema_LibraryDescriptor_TLibraryDescriptorType_.Walk(); xsdt.OnWalkError(&err, &WalkErrors, WalkContinueOnError, WalkOnError) {
			return
		}
		if err = me.XsdGoPkgHasElem_SpotDESCRIPTORsequenceLibraryTypeschema_SpotDescriptor_TSpotDescriptorType_.Walk(); xsdt.OnWalkError(&err, &WalkErrors, WalkContinueOnError, WalkOnError) {
			return
		}
		if fn != nil {
			if err = fn(me, false); xsdt.OnWalkError(&err, &WalkErrors, WalkContinueOnError, WalkOnError) {
				return
			}
		}
	}
	return
}
644753
func (n *Closure) Walk(v walker.Visitor) {
	if v.EnterNode(n) == false {
		return
	}

	if n.Params != nil {
		v.EnterChildList("Params", n)
		for _, nn := range n.Params {
			if nn != nil {
				nn.Walk(v)
			}
		}
		v.LeaveChildList("Params", n)
	}

	if n.ClosureUse != nil {
		v.EnterChildNode("ClosureUse", n)
		n.ClosureUse.Walk(v)
		v.LeaveChildNode("ClosureUse", n)
	}

	if n.ReturnType != nil {
		v.EnterChildNode("ReturnType", n)
		n.ReturnType.Walk(v)
		v.LeaveChildNode("ReturnType", n)
	}

	if n.Stmts != nil {
		v.EnterChildList("Stmts", n)
		for _, nn := range n.Stmts {
			if nn != nil {
				nn.Walk(v)
			}
		}
		v.LeaveChildList("Stmts", n)
	}

	v.LeaveNode(n)
}
1279402
func walk(n node, f func(node)) {
	f(n)
	switch n := n.(type) {
	case *arrayNode:
		for _, a := range n.Array {
			walk(a, f)
		}
	case *classNode:
		for _, a := range n.Members {
			walk(a, f)
		}
	case *numberNode, *stringNode:
		// Ignore.
	default:
		panic(fmt.Errorf("other type: %T", n))
	}
}
231577
func (n *TraitUse) Walk(v walker.Visitor) {
	if v.EnterNode(n) == false {
		return
	}

	if n.Traits != nil {
		v.EnterChildList("Traits", n)
		for _, nn := range n.Traits {
			if nn != nil {
				nn.Walk(v)
			}
		}
		v.LeaveChildList("Traits", n)
	}

	if n.TraitAdaptationList != nil {
		v.EnterChildNode("TraitAdaptationList", n)
		n.TraitAdaptationList.Walk(v)
		v.LeaveChildNode("TraitAdaptationList", n)
	}

	v.LeaveNode(n)
}
933235
func (n *Ternary) Walk(v walker.Visitor) {
	if v.EnterNode(n) == false {
		return
	}

	if n.Condition != nil {
		v.EnterChildNode("Condition", n)
		n.Condition.Walk(v)
		v.LeaveChildNode("Condition", n)
	}

	if n.IfTrue != nil {
		v.EnterChildNode("IfTrue", n)
		n.IfTrue.Walk(v)
		v.LeaveChildNode("IfTrue", n)
	}

	if n.IfFalse != nil {
		v.EnterChildNode("IfFalse", n)
		n.IfFalse.Walk(v)
		v.LeaveChildNode("IfFalse", n)
	}

	v.LeaveNode(n)
}
1058087
func Walk(path string, walkFn WalkFunc) error {
	root := strings.TrimSpace(path)
	_, err := os.Lstat(root)
	if err != nil {
		return walkFn(nil, err)
	}
	return walk(root, walkFn)
}
657751
func Walk(node Node, f func(Node) bool) {
	if !f(node) {
		return
	}

	switch x := node.(type) {
	case *File:
		walkStmts(x.Stmts, x.Last, f)
	case *Comment:
	case *Stmt:
		for _, c := range x.Comments {
			if !x.End().After(c.Pos()) {
				defer Walk(&c, f)
				break
			}
			Walk(&c, f)
		}
		if x.Cmd != nil {
			Walk(x.Cmd, 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)
		}
		if x.Index != nil {
			Walk(x.Index, f)
		}
		if x.Array != nil {
			Walk(x.Array, f)
		}
	case *Redirect:
		if x.N != nil {
			Walk(x.N, f)
		}
		Walk(x.Word, f)
		if x.Hdoc != nil {
			Walk(x.Hdoc, f)
		}
	case *CallExpr:
		for _, a := range x.Assigns {
			Walk(a, f)
		}
		walkWords(x.Args, f)
	case *Subshell:
		walkStmts(x.Stmts, x.Last, f)
	case *Block:
		walkStmts(x.Stmts, x.Last, f)
	case *IfClause:
		walkStmts(x.Cond, x.CondLast, f)
		walkStmts(x.Then, x.ThenLast, f)
		if x.Else != nil {
			Walk(x.Else, f)
		}
	case *WhileClause:
		walkStmts(x.Cond, x.CondLast, f)
		walkStmts(x.Do, x.DoLast, f)
	case *ForClause:
		Walk(x.Loop, f)
		walkStmts(x.Do, x.DoLast, f)
	case *WordIter:
		Walk(x.Name, f)
		walkWords(x.Items, f)
	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, x.Last, f)
	case *ParamExp:
		Walk(x.Param, f)
		if x.Index != nil {
			Walk(x.Index, f)
		}
		if x.Repl != nil {
			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 {
			Walk(x.Exp.Word, f)
		}
	case *ArithmExp:
		Walk(x.X, f)
	case *ArithmCmd:
		Walk(x.X, f)
	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)
		for _, ci := range x.Items {
			Walk(ci, f)
		}
		for _, c := range x.Last {
			Walk(&c, f)
		}
	case *CaseItem:
		for _, c := range x.Comments {
			if c.Pos().After(x.Pos()) {
				defer Walk(&c, f)
				break
			}
			Walk(&c, f)
		}
		walkWords(x.Patterns, f)
		walkStmts(x.Stmts, x.Last, f)
	case *TestClause:
		Walk(x.X, f)
	case *DeclClause:
		walkWords(x.Opts, f)
		for _, a := range x.Assigns {
			Walk(a, f)
		}
	case *ArrayExpr:
		for _, el := range x.Elems {
			Walk(el, f)
		}
		for _, c := range x.Last {
			Walk(&c, f)
		}
	case *ArrayElem:
		for _, c := range x.Comments {
			if c.Pos().After(x.Pos()) {
				defer Walk(&c, f)
				break
			}
			Walk(&c, f)
		}
		if x.Index != nil {
			Walk(x.Index, f)
		}
		if x.Value != nil {
			Walk(x.Value, f)
		}
	case *ExtGlob:
		Walk(x.Pattern, f)
	case *ProcSubst:
		walkStmts(x.Stmts, x.Last, f)
	case *TimeClause:
		if x.Stmt != nil {
			Walk(x.Stmt, f)
		}
	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)
}
620407
func (n *Default) Walk(v walker.Visitor) {
	if v.EnterNode(n) == false {
		return
	}

	if n.Stmts != nil {
		v.EnterChildList("Stmts", n)
		for _, nn := range n.Stmts {
			if nn != nil {
				nn.Walk(v)
			}
		}
		v.LeaveChildList("Stmts", n)
	}

	v.LeaveNode(n)
}
1374618
func (n *ClassConstFetch) Walk(v walker.Visitor) {
	if v.EnterNode(n) == false {
		return
	}

	if n.Class != nil {
		v.EnterChildNode("Class", n)
		n.Class.Walk(v)
		v.LeaveChildNode("Class", n)
	}

	if n.ConstantName != nil {
		v.EnterChildNode("ConstantName", n)
		n.ConstantName.Walk(v)
		v.LeaveChildNode("ConstantName", n)
	}

	v.LeaveNode(n)
}
606862
func (l *lcowfs) Walk(root string, walkFn filepath.WalkFunc) error {
	info, err := l.Lstat(root)
	if err != nil {
		err = walkFn(root, nil, err)
	} else {
		err = l.walk(root, info, walkFn)
	}
	if err == filepath.SkipDir {
		return nil
	}
	return err
}