github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/internal/parser/node.go (about) 1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Parse nodes. 6 7 package parse 8 9 import ( 10 "fmt" 11 "strings" 12 ) 13 14 var textFormat = "%s" // Changed to "%q" in tests for better error messages. 15 16 // A Node is an element in the parse tree. The interface is trivial. 17 // The interface contains an unexported method so that only 18 // types local to this package can satisfy it. 19 type Node interface { 20 Type() NodeType 21 String() string 22 // Copy does a deep copy of the Node and all its components. 23 // To avoid type assertions, some XxxNodes also have specialized 24 // CopyXxx methods that return *XxxNode. 25 Copy() Node 26 Position() Pos // byte position of start of node in full original input string 27 // tree returns the containing *Tree. 28 // It is unexported so all implementations of Node are in this package. 29 tree() *Tree 30 // writeTo writes the String output to the builder. 31 writeTo(*strings.Builder) 32 } 33 34 // NodeType identifies the type of a parse tree node. 35 type NodeType int 36 37 // Pos represents a byte position in the original input text from which 38 // this template was parsed. 39 type Pos int 40 41 func (p Pos) Position() Pos { 42 return p 43 } 44 45 // Type returns itself and provides an easy default implementation 46 // for embedding in a Node. Embedded in all non-trivial Nodes. 47 func (t NodeType) Type() NodeType { 48 return t 49 } 50 51 const ( 52 NodeList NodeType = iota // a list of Nodes 53 NodeText 54 NodeModel 55 NodeTable 56 NodeView 57 NodeCounter 58 ) 59 60 // Nodes. 61 62 // ListNode holds a sequence of nodes. 63 type ListNode struct { 64 NodeType 65 Pos 66 tr *Tree 67 Nodes []Node // The element nodes in lexical order. 68 } 69 70 func (t *Tree) newList(pos Pos) *ListNode { 71 return &ListNode{tr: t, NodeType: NodeList, Pos: pos} 72 } 73 74 func (l *ListNode) append(n Node) { 75 l.Nodes = append(l.Nodes, n) 76 } 77 78 func (l *ListNode) tree() *Tree { 79 return l.tr 80 } 81 82 func (l *ListNode) String() string { 83 var sb strings.Builder 84 l.writeTo(&sb) 85 86 return sb.String() 87 } 88 89 func (l *ListNode) writeTo(sb *strings.Builder) { 90 for _, n := range l.Nodes { 91 n.writeTo(sb) 92 } 93 } 94 95 func (l *ListNode) CopyList() *ListNode { 96 if l == nil { 97 return l 98 } 99 n := l.tr.newList(l.Pos) 100 for _, elem := range l.Nodes { 101 n.append(elem.Copy()) 102 } 103 104 return n 105 } 106 107 func (l *ListNode) Copy() Node { 108 return l.CopyList() 109 } 110 111 // TextNode holds plain text. 112 type TextNode struct { 113 NodeType 114 Pos 115 tr *Tree 116 Text string // The text; may span newlines. 117 } 118 119 func (t *Tree) newText(pos Pos, text string) *TextNode { 120 return &TextNode{tr: t, NodeType: NodeText, Pos: pos, Text: text} 121 } 122 123 func (t *TextNode) String() string { 124 return fmt.Sprintf(textFormat, t.Text) 125 } 126 127 func (t *TextNode) writeTo(sb *strings.Builder) { 128 sb.WriteString(t.String()) 129 } 130 131 func (t *TextNode) tree() *Tree { 132 return t.tr 133 } 134 135 func (t *TextNode) Copy() Node { 136 return &TextNode{tr: t.tr, NodeType: NodeText, Pos: t.Pos, Text: t.Text} 137 } 138 139 // ModelNode holds model 140 type ModelNode struct { 141 NodeType 142 Pos 143 tr *Tree 144 Text string // The text; may span newlines. 145 } 146 147 func (t *Tree) newModel(pos Pos, text string) *ModelNode { 148 return &ModelNode{tr: t, NodeType: NodeModel, Pos: pos, Text: text} 149 } 150 151 func (t *ModelNode) String() string { 152 return fmt.Sprintf(textFormat, t.Text) 153 } 154 155 func (t *ModelNode) writeTo(sb *strings.Builder) { 156 sb.WriteString(t.String()) 157 } 158 159 func (t *ModelNode) tree() *Tree { 160 return t.tr 161 } 162 163 func (t *ModelNode) Copy() Node { 164 return &ModelNode{tr: t.tr, NodeType: NodeModel, Pos: t.Pos, Text: t.Text} 165 } 166 167 // TableNode holds table 168 type TableNode struct { 169 NodeType 170 Pos 171 tr *Tree 172 PartitionKeys []string 173 ClusteringKeys []string 174 } 175 176 func (t *Tree) newTable(pos Pos, pks, cks []string) *TableNode { 177 return &TableNode{tr: t, NodeType: NodeTable, Pos: pos, PartitionKeys: pks, ClusteringKeys: cks} 178 } 179 180 func (t *TableNode) String() string { 181 return fmt.Sprintf("PKs: %v, CKs: %v", t.PartitionKeys, t.ClusteringKeys) 182 } 183 184 func (t *TableNode) writeTo(sb *strings.Builder) { 185 sb.WriteString(t.String()) 186 } 187 188 func (t *TableNode) tree() *Tree { 189 return t.tr 190 } 191 192 func (t *TableNode) Copy() Node { 193 return &TableNode{ 194 tr: t.tr, 195 NodeType: NodeTable, 196 Pos: t.Pos, 197 PartitionKeys: t.PartitionKeys, 198 ClusteringKeys: t.ClusteringKeys, 199 } 200 } 201 202 // ViewNode holds view 203 type ViewNode struct { 204 NodeType 205 Pos 206 tr *Tree 207 PartitionKeys []string 208 ClusteringKeys []string 209 } 210 211 func (t *Tree) newView(pos Pos, pks, cks []string) *ViewNode { 212 return &ViewNode{tr: t, NodeType: NodeView, Pos: pos, PartitionKeys: pks, ClusteringKeys: cks} 213 } 214 215 func (t *ViewNode) String() string { 216 return fmt.Sprintf("PKs: %v, CKs: %v", t.PartitionKeys, t.ClusteringKeys) 217 } 218 219 func (t *ViewNode) writeTo(sb *strings.Builder) { 220 sb.WriteString(t.String()) 221 } 222 223 func (t *ViewNode) tree() *Tree { 224 return t.tr 225 } 226 227 func (t *ViewNode) Copy() Node { 228 return &ViewNode{ 229 tr: t.tr, 230 NodeType: NodeView, 231 Pos: t.Pos, 232 PartitionKeys: t.PartitionKeys, 233 ClusteringKeys: t.ClusteringKeys, 234 } 235 } 236 237 // CounterNode holds counter 238 type CounterNode struct { 239 NodeType 240 Pos 241 tr *Tree 242 Text string // The text; may span newlines. 243 } 244 245 func (t *Tree) newCounter(pos Pos, text string) *CounterNode { 246 return &CounterNode{tr: t, NodeType: NodeCounter, Pos: pos, Text: text} 247 } 248 249 func (t *CounterNode) String() string { 250 return fmt.Sprintf(textFormat, t.Text) 251 } 252 253 func (t *CounterNode) writeTo(sb *strings.Builder) { 254 sb.WriteString(t.String()) 255 } 256 257 func (t *CounterNode) tree() *Tree { 258 return t.tr 259 } 260 261 func (t *CounterNode) Copy() Node { 262 return &CounterNode{tr: t.tr, NodeType: NodeCounter, Pos: t.Pos, Text: t.Text} 263 }