-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathvalidate.go
323 lines (293 loc) · 8.74 KB
/
validate.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// ================================================================
// Checks for things that are syntax errors but not done in the AST for
// pragmatic reasons. For example, $anything in begin/end blocks;
// begin/end/func not at top level; etc.
// ================================================================
package cst
import (
"fmt"
"github.com/johnkerl/miller/v6/pkg/dsl"
"github.com/johnkerl/miller/v6/pkg/lib"
)
// ----------------------------------------------------------------
func ValidateAST(
ast *dsl.AST,
dslInstanceType DSLInstanceType, // mlr put, mlr filter, mlr repl
) error {
atTopLevel := true
inLoop := false
inBeginOrEnd := false
inUDF := false
inUDS := false
isMainBlockLastStatement := false
isAssignmentLHS := false
isUnset := false
// They can do mlr put '': there are simply zero statements.
// But filter '' is an error.
if len(ast.RootNode.Children) == 0 {
if dslInstanceType == DSLInstanceTypeFilter {
return fmt.Errorf("mlr: filter statement must not be empty")
}
}
for _, astChild := range ast.RootNode.Children {
err := validateASTAux(
astChild,
dslInstanceType,
atTopLevel,
inLoop,
inBeginOrEnd,
inUDF,
inUDS,
isMainBlockLastStatement,
isAssignmentLHS,
isUnset,
)
if err != nil {
return err
}
}
return nil
}
// ----------------------------------------------------------------
func validateASTAux(
astNode *dsl.ASTNode,
dslInstanceType DSLInstanceType, // mlr put, mlr filter, mlr repl
atTopLevel bool,
inLoop bool,
inBeginOrEnd bool,
inUDF bool,
inUDS bool,
isMainBlockLastStatement bool, // TODO -- keep this or not ...
isAssignmentLHS bool,
isUnset bool,
) error {
nextLevelAtTopLevel := false
nextLevelInLoop := inLoop
nextLevelInBeginOrEnd := inBeginOrEnd
nextLevelInUDF := inUDF
nextLevelInUDS := inUDS
nextLevelIsAssignmentLHS := isAssignmentLHS
nextLevelIsUnset := isUnset
if astNode.Type == dsl.NodeTypeFilterStatement {
if dslInstanceType == DSLInstanceTypeFilter {
return fmt.Errorf(
`mlr: filter expressions must not also contain the "filter" keyword`,
)
}
}
// Check: begin/end/func/subr must be at top-level
if astNode.Type == dsl.NodeTypeBeginBlock {
if !atTopLevel {
return fmt.Errorf(
"mlr: begin blocks can only be at top level",
)
}
nextLevelInBeginOrEnd = true
} else if astNode.Type == dsl.NodeTypeEndBlock {
if !atTopLevel {
return fmt.Errorf(
"mlr: end blocks can only be at top level",
)
}
nextLevelInBeginOrEnd = true
} else if astNode.Type == dsl.NodeTypeNamedFunctionDefinition {
if !atTopLevel {
return fmt.Errorf(
"mlr: func blocks can only be at top level",
)
}
nextLevelInUDF = true
} else if astNode.Type == dsl.NodeTypeUnnamedFunctionDefinition {
nextLevelInUDF = true
} else if astNode.Type == dsl.NodeTypeSubroutineDefinition {
if !atTopLevel {
return fmt.Errorf(
"mlr: subr blocks can only be at top level",
)
}
nextLevelInUDS = true
} else if astNode.Type == dsl.NodeTypeForLoopTwoVariable {
err := validateForLoopTwoVariableUniqueNames(astNode)
if err != nil {
return err
}
} else if astNode.Type == dsl.NodeTypeForLoopMultivariable {
err := validateForLoopMultivariableUniqueNames(astNode)
if err != nil {
return err
}
}
// Check: $-anything cannot be in begin/end
if inBeginOrEnd {
if astNode.Type == dsl.NodeTypeDirectFieldValue ||
astNode.Type == dsl.NodeTypeIndirectFieldValue ||
astNode.Type == dsl.NodeTypeFullSrec {
return fmt.Errorf(
"mlr: begin/end blocks cannot refer to records via $x, $*, etc",
)
}
}
// Check: break/continue outside of loop
if !inLoop {
if astNode.Type == dsl.NodeTypeBreak {
return fmt.Errorf(
"mlr: break statements are only valid within for/do/while loops",
)
}
}
if !inLoop {
if astNode.Type == dsl.NodeTypeContinue {
return fmt.Errorf(
"mlr: break statements are only valid within for/do/while loops",
)
}
}
if astNode.Type == dsl.NodeTypeWhileLoop ||
astNode.Type == dsl.NodeTypeDoWhileLoop ||
astNode.Type == dsl.NodeTypeForLoopOneVariable ||
astNode.Type == dsl.NodeTypeForLoopTwoVariable ||
astNode.Type == dsl.NodeTypeForLoopMultivariable ||
astNode.Type == dsl.NodeTypeTripleForLoop {
nextLevelInLoop = true
}
// Check: return outside of func/subr
if !inUDF && !inUDS {
if astNode.Type == dsl.NodeTypeReturn {
return fmt.Errorf(
"mlr: return statements are only valid within func/subr blocks",
)
}
}
// Check: enforce return-value iff in a function; return-void iff in a subroutine
if astNode.Type == dsl.NodeTypeReturn {
if inUDF {
if len(astNode.Children) != 1 {
return fmt.Errorf(
"mlr: return statements in func blocks must return a value",
)
}
}
if inUDS {
if len(astNode.Children) != 0 {
return fmt.Errorf(
"mlr: return statements in subr blocks must not return a value",
)
}
}
}
// Check: prohibit NR etc at LHS; 1+2=3+4; etc
if isAssignmentLHS {
ok := VALID_LHS_NODE_TYPES[astNode.Type]
if !ok {
return fmt.Errorf(
"mlr: %s is not valid on the left-hand side of an assignment",
astNode.Type,
)
}
}
// Check: prohibit NR etc at LHS; 1+2=3+4; etc
if isUnset {
ok := VALID_LHS_NODE_TYPES[astNode.Type]
if !ok {
return fmt.Errorf(
"mlr: %s is not valid for unset statement",
astNode.Type,
)
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Treewalk
for i, astChild := range astNode.Children {
nextLevelIsAssignmentLHS = astNode.Type == dsl.NodeTypeAssignment && i == 0
nextLevelIsUnset = astNode.Type == dsl.NodeTypeUnset
err := validateASTAux(
astChild,
dslInstanceType,
nextLevelAtTopLevel,
nextLevelInLoop,
nextLevelInBeginOrEnd,
nextLevelInUDF,
nextLevelInUDS,
isMainBlockLastStatement,
nextLevelIsAssignmentLHS,
nextLevelIsUnset,
)
if err != nil {
return err
}
}
return nil
}
// Check against 'for (a, a in $*)' -- repeated 'a'.
// AST:
// * statement block
// * double-variable for-loop "for"
// * local variable "a"
// * local variable "a"
// * full record "$*"
// * statement block
func validateForLoopTwoVariableUniqueNames(astNode *dsl.ASTNode) error {
lib.InternalCodingErrorIf(astNode.Type != dsl.NodeTypeForLoopTwoVariable)
lib.InternalCodingErrorIf(len(astNode.Children) != 4)
keyVarNode := astNode.Children[0]
valVarNode := astNode.Children[1]
lib.InternalCodingErrorIf(keyVarNode.Type != dsl.NodeTypeLocalVariable)
lib.InternalCodingErrorIf(valVarNode.Type != dsl.NodeTypeLocalVariable)
keyVarName := string(keyVarNode.Token.Lit)
valVarName := string(valVarNode.Token.Lit)
if keyVarName == valVarName {
return fmt.Errorf("mlr: redefinition of variable %s in the same scope", keyVarName)
} else {
return nil
}
}
// Check against 'for ((a,a), b in $*)' or 'for ((a,b), a in $*)' -- repeated 'a'.
// AST:
// * statement block
// - multi-variable for-loop "for"
// - parameter list
// - local variable "a"
// - local variable "b"
// - local variable "a"
// - full record "$*"
// - statement block
func validateForLoopMultivariableUniqueNames(astNode *dsl.ASTNode) error {
lib.InternalCodingErrorIf(astNode.Type != dsl.NodeTypeForLoopMultivariable)
keyVarsNode := astNode.Children[0]
valVarNode := astNode.Children[1]
lib.InternalCodingErrorIf(keyVarsNode.Type != dsl.NodeTypeParameterList)
lib.InternalCodingErrorIf(valVarNode.Type != dsl.NodeTypeLocalVariable)
seen := make(map[string]bool)
for _, keyVarNode := range keyVarsNode.Children {
lib.InternalCodingErrorIf(keyVarNode.Type != dsl.NodeTypeLocalVariable)
name := string(keyVarNode.Token.Lit)
_, present := seen[name]
if present {
return fmt.Errorf("mlr: redefinition of variable %s in the same scope", name)
}
seen[name] = true
}
valVarName := string(valVarNode.Token.Lit)
if seen[valVarName] {
return fmt.Errorf("mlr: redefinition of variable %s in the same scope", valVarName)
}
return nil
}
// ================================================================
var VALID_LHS_NODE_TYPES = map[dsl.TNodeType]bool{
dsl.NodeTypeArrayOrMapIndexAccess: true,
dsl.NodeTypeDotOperator: true,
dsl.NodeTypeArrayOrMapPositionalNameAccess: true,
dsl.NodeTypeArrayOrMapPositionalValueAccess: true,
dsl.NodeTypeArraySliceAccess: true,
dsl.NodeTypeDirectFieldValue: true,
dsl.NodeTypeDirectOosvarValue: true,
dsl.NodeTypeEnvironmentVariable: true,
dsl.NodeTypeFullOosvar: true,
dsl.NodeTypeFullSrec: true,
dsl.NodeTypeIndirectFieldValue: true,
dsl.NodeTypeIndirectOosvarValue: true,
dsl.NodeTypeLocalVariable: true,
dsl.NodeTypePositionalFieldName: true,
dsl.NodeTypePositionalFieldValue: true,
}