• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2017 syzkaller project authors. All rights reserved.
2// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
3
4package ast
5
6// Walk calls callback cb for every top-level node in description.
7// Note: it's not recursive. Use Recursive helper for recursive walk.
8func (desc *Description) Walk(cb func(Node)) {
9	for _, n := range desc.Nodes {
10		cb(n)
11	}
12}
13
14func Recursive(cb func(Node)) func(Node) {
15	var rec func(Node)
16	rec = func(n Node) {
17		cb(n)
18		n.Walk(rec)
19	}
20	return rec
21}
22
23func (n *NewLine) Walk(cb func(Node)) {}
24func (n *Comment) Walk(cb func(Node)) {}
25func (n *Ident) Walk(cb func(Node))   {}
26func (n *String) Walk(cb func(Node))  {}
27func (n *Int) Walk(cb func(Node))     {}
28
29func (n *Include) Walk(cb func(Node)) {
30	cb(n.File)
31}
32
33func (n *Incdir) Walk(cb func(Node)) {
34	cb(n.Dir)
35}
36
37func (n *Define) Walk(cb func(Node)) {
38	cb(n.Name)
39	cb(n.Value)
40}
41
42func (n *Resource) Walk(cb func(Node)) {
43	cb(n.Name)
44	cb(n.Base)
45	for _, v := range n.Values {
46		cb(v)
47	}
48}
49
50func (n *TypeDef) Walk(cb func(Node)) {
51	cb(n.Name)
52	for _, a := range n.Args {
53		cb(a)
54	}
55	if n.Type != nil {
56		cb(n.Type)
57	}
58	if n.Struct != nil {
59		cb(n.Struct)
60	}
61}
62
63func (n *Call) Walk(cb func(Node)) {
64	cb(n.Name)
65	for _, f := range n.Args {
66		cb(f)
67	}
68	if n.Ret != nil {
69		cb(n.Ret)
70	}
71}
72
73func (n *Struct) Walk(cb func(Node)) {
74	cb(n.Name)
75	for _, f := range n.Fields {
76		cb(f)
77	}
78	for _, a := range n.Attrs {
79		cb(a)
80	}
81	for _, c := range n.Comments {
82		cb(c)
83	}
84}
85
86func (n *IntFlags) Walk(cb func(Node)) {
87	cb(n.Name)
88	for _, v := range n.Values {
89		cb(v)
90	}
91}
92
93func (n *StrFlags) Walk(cb func(Node)) {
94	cb(n.Name)
95	for _, v := range n.Values {
96		cb(v)
97	}
98}
99
100func (n *Type) Walk(cb func(Node)) {
101	for _, t := range n.Args {
102		cb(t)
103	}
104}
105
106func (n *Field) Walk(cb func(Node)) {
107	cb(n.Name)
108	cb(n.Type)
109	for _, c := range n.Comments {
110		cb(c)
111	}
112}
113