2 Copyright 2015 The Kubernetes Authors.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
21 // NodeType identifies the type of a parse tree node.
24 // Type returns itself and provides an easy default implementation
25 func (t NodeType) Type() NodeType {
29 func (t NodeType) String() string {
30 return NodeTypeName[t]
34 NodeText NodeType = iota
48 var NodeTypeName = map[NodeType]string{
50 NodeArray: "NodeArray",
52 NodeField: "NodeField",
53 NodeIdentifier: "NodeIdentifier",
54 NodeFilter: "NodeFilter",
56 NodeFloat: "NodeFloat",
57 NodeWildcard: "NodeWildcard",
58 NodeRecursive: "NodeRecursive",
59 NodeUnion: "NodeUnion",
68 // ListNode holds a sequence of nodes.
69 type ListNode struct {
71 Nodes []Node // The element nodes in lexical order.
74 func newList() *ListNode {
75 return &ListNode{NodeType: NodeList}
78 func (l *ListNode) append(n Node) {
79 l.Nodes = append(l.Nodes, n)
82 func (l *ListNode) String() string {
83 return l.Type().String()
86 // TextNode holds plain text.
87 type TextNode struct {
89 Text string // The text; may span newlines.
92 func newText(text string) *TextNode {
93 return &TextNode{NodeType: NodeText, Text: text}
96 func (t *TextNode) String() string {
97 return fmt.Sprintf("%s: %s", t.Type(), t.Text)
100 // FieldNode holds field of struct
101 type FieldNode struct {
106 func newField(value string) *FieldNode {
107 return &FieldNode{NodeType: NodeField, Value: value}
110 func (f *FieldNode) String() string {
111 return fmt.Sprintf("%s: %s", f.Type(), f.Value)
114 // IdentifierNode holds an identifier
115 type IdentifierNode struct {
120 func newIdentifier(value string) *IdentifierNode {
121 return &IdentifierNode{
122 NodeType: NodeIdentifier,
127 func (f *IdentifierNode) String() string {
128 return fmt.Sprintf("%s: %s", f.Type(), f.Name)
131 // ParamsEntry holds param information for ArrayNode
132 type ParamsEntry struct {
134 Known bool // whether the value is known when parse it
137 // ArrayNode holds start, end, step information for array index selection
138 type ArrayNode struct {
140 Params [3]ParamsEntry // start, end, step
143 func newArray(params [3]ParamsEntry) *ArrayNode {
150 func (a *ArrayNode) String() string {
151 return fmt.Sprintf("%s: %v", a.Type(), a.Params)
154 // FilterNode holds operand and operator information for filter
155 type FilterNode struct {
162 func newFilter(left, right *ListNode, operator string) *FilterNode {
164 NodeType: NodeFilter,
171 func (f *FilterNode) String() string {
172 return fmt.Sprintf("%s: %s %s %s", f.Type(), f.Left, f.Operator, f.Right)
175 // IntNode holds integer value
176 type IntNode struct {
181 func newInt(num int) *IntNode {
182 return &IntNode{NodeType: NodeInt, Value: num}
185 func (i *IntNode) String() string {
186 return fmt.Sprintf("%s: %d", i.Type(), i.Value)
189 // FloatNode holds float value
190 type FloatNode struct {
195 func newFloat(num float64) *FloatNode {
196 return &FloatNode{NodeType: NodeFloat, Value: num}
199 func (i *FloatNode) String() string {
200 return fmt.Sprintf("%s: %f", i.Type(), i.Value)
203 // WildcardNode means a wildcard
204 type WildcardNode struct {
208 func newWildcard() *WildcardNode {
209 return &WildcardNode{NodeType: NodeWildcard}
212 func (i *WildcardNode) String() string {
213 return i.Type().String()
216 // RecursiveNode means a recursive descent operator
217 type RecursiveNode struct {
221 func newRecursive() *RecursiveNode {
222 return &RecursiveNode{NodeType: NodeRecursive}
225 func (r *RecursiveNode) String() string {
226 return r.Type().String()
229 // UnionNode is union of ListNode
230 type UnionNode struct {
235 func newUnion(nodes []*ListNode) *UnionNode {
236 return &UnionNode{NodeType: NodeUnion, Nodes: nodes}
239 func (u *UnionNode) String() string {
240 return u.Type().String()
243 // BoolNode holds bool value
244 type BoolNode struct {
249 func newBool(value bool) *BoolNode {
250 return &BoolNode{NodeType: NodeBool, Value: value}
253 func (b *BoolNode) String() string {
254 return fmt.Sprintf("%s: %t", b.Type(), b.Value)