forked from johnkerl/miller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast_build.go
278 lines (245 loc) · 8.02 KB
/
ast_build.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// ================================================================
// AST-build methods, for use by callbacks within the GOCC/BNF Miller
// DSL grammar in mlr.bnf.
// ================================================================
package dsl
import (
"fmt"
"github.com/johnkerl/miller/pkg/lib"
"github.com/johnkerl/miller/pkg/parsing/token"
)
// ----------------------------------------------------------------
// xxx comment interface{} everywhere vs. true types due to gocc polymorphism API.
// and, line-count for casts here vs in the BNF:
//
// Statement :
// md_token_field_name md_token_assign md_token_number
//
// Statement :
// md_token_field_name md_token_assign md_token_number
// << dsl.NewASTNodeTernary("foo", $0, $1, $2) >> ;
// This is for the GOCC/BNF parser, which produces an AST
func NewAST(iroot interface{}) (*AST, error) {
return &AST{
RootNode: iroot.(*ASTNode),
}, nil
}
// ----------------------------------------------------------------
func NewASTNode(itok interface{}, nodeType TNodeType) (*ASTNode, error) {
return NewASTNodeNestable(itok, nodeType), nil
}
// For handling empty expressions.
func NewASTNodeEmptyNestable(nodeType TNodeType) *ASTNode {
return &ASTNode{
Token: nil,
Type: nodeType,
Children: nil,
}
}
// For handling empty expressions.
func NewASTNodeEmpty(nodeType TNodeType) (*ASTNode, error) {
return NewASTNodeEmptyNestable(nodeType), nil
}
// Strips the leading '$' from field names, or '@' from oosvar names. Not done
// in the parser itself due to LR-1 conflicts.
func NewASTNodeStripDollarOrAtSign(itok interface{}, nodeType TNodeType) (*ASTNode, error) {
oldToken := itok.(*token.Token)
lib.InternalCodingErrorIf(len(oldToken.Lit) < 2)
lib.InternalCodingErrorIf(oldToken.Lit[0] != '$' && oldToken.Lit[0] != '@')
newToken := &token.Token{
Type: oldToken.Type,
Lit: oldToken.Lit[1:],
Pos: oldToken.Pos,
}
return NewASTNodeNestable(newToken, nodeType), nil
}
// Strips the leading '${' and trailing '}' from braced field names, or '@{'
// and '}' from oosvar names. Not done in the parser itself due to LR-1
// conflicts.
func NewASTNodeStripDollarOrAtSignAndCurlyBraces(
itok interface{},
nodeType TNodeType,
) (*ASTNode, error) {
oldToken := itok.(*token.Token)
n := len(oldToken.Lit)
lib.InternalCodingErrorIf(n < 4)
lib.InternalCodingErrorIf(oldToken.Lit[0] != '$' && oldToken.Lit[0] != '@')
lib.InternalCodingErrorIf(oldToken.Lit[1] != '{')
lib.InternalCodingErrorIf(oldToken.Lit[n-1] != '}')
newToken := &token.Token{
Type: oldToken.Type,
Lit: oldToken.Lit[2 : n-1],
Pos: oldToken.Pos,
}
return NewASTNodeNestable(newToken, nodeType), nil
}
// Likewise for the leading/trailing double quotes on string literals. Also,
// since string literals can have backslash-escaped double-quotes like
// "...\"...\"...", we also unbackslash here.
func NewASTNodeStripDoubleQuotePair(
itok interface{},
nodeType TNodeType,
) (*ASTNode, error) {
oldToken := itok.(*token.Token)
n := len(oldToken.Lit)
contents := string(oldToken.Lit[1 : n-1])
newToken := &token.Token{
Type: oldToken.Type,
Lit: []byte(contents),
Pos: oldToken.Pos,
}
return NewASTNodeNestable(newToken, nodeType), nil
}
// xxx comment why grammar use
func NewASTNodeNestable(itok interface{}, nodeType TNodeType) *ASTNode {
var tok *token.Token = nil
if itok != nil {
tok = itok.(*token.Token)
}
return &ASTNode{
Token: tok,
Type: nodeType,
Children: nil,
}
}
func NewASTNodeZary(itok interface{}, nodeType TNodeType) (*ASTNode, error) {
parent := NewASTNodeNestable(itok, nodeType)
convertToZary(parent)
return parent, nil
}
func NewASTNodeUnaryNestable(itok, childA interface{}, nodeType TNodeType) *ASTNode {
parent := NewASTNodeNestable(itok, nodeType)
convertToUnary(parent, childA)
return parent
}
func NewASTNodeUnary(itok, childA interface{}, nodeType TNodeType) (*ASTNode, error) {
return NewASTNodeUnaryNestable(itok, childA, nodeType), nil
}
// Signature: Token Node Node Type
func NewASTNodeBinaryNestable(itok, childA, childB interface{}, nodeType TNodeType) *ASTNode {
parent := NewASTNodeNestable(itok, nodeType)
convertToBinary(parent, childA, childB)
return parent
}
// Signature: Token Node Node Type
func NewASTNodeBinary(
itok, childA, childB interface{}, nodeType TNodeType,
) (*ASTNode, error) {
return NewASTNodeBinaryNestable(itok, childA, childB, nodeType), nil
}
func NewASTNodeTernary(itok, childA, childB, childC interface{}, nodeType TNodeType) (*ASTNode, error) {
parent := NewASTNodeNestable(itok, nodeType)
convertToTernary(parent, childA, childB, childC)
return parent, nil
}
func NewASTNodeQuaternary(
itok, childA, childB, childC, childD interface{}, nodeType TNodeType,
) (*ASTNode, error) {
parent := NewASTNodeNestable(itok, nodeType)
convertToQuaternary(parent, childA, childB, childC, childD)
return parent, nil
}
// Pass-through expressions in the grammar sometimes need to be turned from
// (ASTNode) to (ASTNode, error)
func Nestable(iparent interface{}) (*ASTNode, error) {
return iparent.(*ASTNode), nil
}
func convertToZary(iparent interface{}) {
parent := iparent.(*ASTNode)
children := make([]*ASTNode, 0)
parent.Children = children
}
// xxx inline this. can be a one-liner.
func convertToUnary(iparent interface{}, childA interface{}) {
parent := iparent.(*ASTNode)
children := make([]*ASTNode, 1)
children[0] = childA.(*ASTNode)
parent.Children = children
}
func convertToBinary(iparent interface{}, childA, childB interface{}) {
parent := iparent.(*ASTNode)
children := make([]*ASTNode, 2)
children[0] = childA.(*ASTNode)
children[1] = childB.(*ASTNode)
parent.Children = children
}
func convertToTernary(iparent interface{}, childA, childB, childC interface{}) {
parent := iparent.(*ASTNode)
children := make([]*ASTNode, 3)
children[0] = childA.(*ASTNode)
children[1] = childB.(*ASTNode)
children[2] = childC.(*ASTNode)
parent.Children = children
}
func convertToQuaternary(iparent interface{}, childA, childB, childC, childD interface{}) {
parent := iparent.(*ASTNode)
children := make([]*ASTNode, 4)
children[0] = childA.(*ASTNode)
children[1] = childB.(*ASTNode)
children[2] = childC.(*ASTNode)
children[3] = childD.(*ASTNode)
parent.Children = children
}
func PrependChild(iparent interface{}, ichild interface{}) (*ASTNode, error) {
parent := iparent.(*ASTNode)
child := ichild.(*ASTNode)
if parent.Children == nil {
convertToUnary(iparent, ichild)
} else {
parent.Children = append([]*ASTNode{child}, parent.Children...)
}
return parent, nil
}
func PrependTwoChildren(iparent interface{}, ichildA, ichildB interface{}) (*ASTNode, error) {
parent := iparent.(*ASTNode)
childA := ichildA.(*ASTNode)
childB := ichildB.(*ASTNode)
if parent.Children == nil {
convertToBinary(iparent, ichildA, ichildB)
} else {
parent.Children = append([]*ASTNode{childA, childB}, parent.Children...)
}
return parent, nil
}
func AppendChild(iparent interface{}, child interface{}) (*ASTNode, error) {
parent := iparent.(*ASTNode)
if parent.Children == nil {
convertToUnary(iparent, child)
} else {
parent.Children = append(parent.Children, child.(*ASTNode))
}
return parent, nil
}
func AdoptChildren(iparent interface{}, ichild interface{}) (*ASTNode, error) {
parent := iparent.(*ASTNode)
child := ichild.(*ASTNode)
parent.Children = child.Children
child.Children = nil
return parent, nil
}
// TODO: comment
func Wrap(inode interface{}) (*ASTNode, error) {
node := inode.(*ASTNode)
return node, nil
}
func (node *ASTNode) CheckArity(
arity int,
) error {
if len(node.Children) != arity {
return fmt.Errorf("expected AST node arity %d, got %d", arity, len(node.Children))
} else {
return nil
}
}
// Tokens are produced by GOCC. However there is an exception: for the ternary
// operator I want the AST to have a "?:" token, which GOCC doesn't produce
// since nothing is actually spelled like that in the DSL.
func NewASTToken(iliteral interface{}, iclonee interface{}) *token.Token {
literal := iliteral.(string)
clonee := iclonee.(*token.Token)
return &token.Token{
Type: clonee.Type,
Lit: []byte(literal),
Pos: clonee.Pos,
}
}