-
Notifications
You must be signed in to change notification settings - Fork 1
/
bicep.pegjs
330 lines (278 loc) · 8.17 KB
/
bicep.pegjs
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
324
325
326
327
328
329
{{
function extractList(list, index) {
return list.map(function(element) { return element[index]; });
}
function buildList(head, tail, index) {
return [head].concat(extractList(tail, index));
}
function buildBinaryExpression(head, tail) {
return tail.reduce(function(result, element) {
return {
type: "BinaryExpression",
operator: element[1],
left: result,
right: element[3]
};
}, head);
}
function optionalList(value) {
return value !== null ? value : [];
}
}}
BICEP = __ head:statement tail:(__ statement)* __ {
return { type: 'BICEP', body: buildList(head, tail, 1) }
}
statement
= targetScopeDecl
/ importDecl
/ parameterDecl
/ variableDecl
/ resourceDecl
/ moduleDecl
/ outputDecl
/ ( _ NL ) / NL
// Target scope
targetScopeDecl
= "targetScope" _ "=" __ "'" value:targetScopeValue "'" { return {
type: 'TargetScopeDeclaration',
scope: value
}}
targetScopeValue
= "subscription"
/ "tenant"
/ "managementGroup"
/ "resourceGroup"
importDecl = (decorator __)* "import" identifier "as" identifier object? NL
// Params
parameterDecl
= decs:(decorator __)* "param" _ name:identifier _ dataType:DataType __ val:paramValue {
return {
type: 'ParameterDeclaration',
name: name.name,
decorators: extractList(decs,0),
dataType,
expression: val
}
}
paramValue = exp:("=" __ expression __) { return exp[2] } / __
variableDecl = decorators:(decorator __)* "var" _ id:identifier __ "=" __ exp:expression __ {
return {
type: 'VariableDeclaration',
name: id.name,
decorators: extractList(decorators, 0),
expression: exp
}
}
resourceDecl = decorators:(decorator __)* "resource" _ id:identifier _ mod:interpString ext:(_ "existing")? __ "=" __ expr:(ifCondition / object / forExpression) __ {
return {
type: 'ResourceDeclaration',
name: id.name,
decorators: extractList(decorators, 0),
module: mod,
existing: ext,
expression: expr
}
}
moduleDecl = decorators:(decorator __)* "module" _ id:identifier _ mod:interpString __ "=" __ expr:(ifCondition / object / forExpression) __ {
return {
type: 'ModuleDeclaration',
decorators: extractList(decorators, 0),
name: id.name,
module: mod,
expression: expr
}
}
outputDecl = decorators:(decorator __)* "output" _ id:identifier _ dataType:DataType __"=" __ expression:expression __ {
return {
type: "OutputDeclaration",
id: id.name,
decorators,
dataType,
expression,
}
}
identifier = id:([a-zA-Z_0-9]+) {
return {
type: 'Identifier',
name: id.join('')
}
}
ifCondition = "if" ___ test:parenthesizedExpression ___ object {
return {
type: 'IfCondition',
test
}
}
parenthesizedExpression = "(" expression ")"
forExpression = "[" ___ "for" _ (identifier / forVariableBlock) _ "in" _ expression ___ ":" ___ forBody "]"
forVariableBlock = "(" ___ identifier ___ "," ___ identifier ___ ")"
forBody = __ expression __ / ifCondition
decorator = "@" exp:decoratorExpression { return { type: 'Decorator', expression: exp }}
decoratorExpression = functionCall / (memberExpression "." functionCall)
// Types
DataType
= "string"
/ "int"
/ "bool"
/ "object"
/ "array"
// / "any" __ "=" __ primaryExpression
expression
= head:binaryExpression ___
tail:("?" ___ expression ___":"___ expression)* {
return buildBinaryExpression(head, tail)
}
binaryOperator = "&&" / "||" / "??"
binaryExpression
= head:equalityExpression
tail:( ___ binaryOperator ___ binaryExpression)* {
return buildBinaryExpression(head, tail)
}
equalityOperator = "==" / "!="
equalityExpression
= head:relationalExpression
tail:( ___ equalityOperator ___ equalityExpression)* {
return buildBinaryExpression(head, tail)
}
relationalOperator = ">" / ">=" / "<" / "<="
relationalExpression =
head:additiveExpression
tail:( ___ relationalOperator ___ relationalExpression)* {
return buildBinaryExpression(head, tail)
}
additiveOperator = "+" / "-"
additiveExpression =
head:MultiplicativeExpression
tail:(___ additiveOperator ___ additiveExpression)* {
return buildBinaryExpression(head, tail)
}
MultiplicativeOperator = "*" / "/" / "%"
MultiplicativeExpression =
head:unaryExpression
tail:(___ MultiplicativeOperator ___ MultiplicativeExpression)* {
return buildBinaryExpression(head, tail)
}
unaryOperator = "!" / "-" / "+"
unaryExpression =
head:memberExpression
tail:(___ unaryOperator ___ unaryExpression)* {
return buildBinaryExpression(head, tail)
}
memberExpression
= head:primaryExpression
tail:(
"[" property:(expression) "]" { return { type: 'IndexedProperty', property }}
/ "." property:functionCall { return { type: 'FunctionCallProperty', property }}
/ "." property:identifier { return { type: 'DotIdentifierProperty', property }}
/ ":" property:identifier { return { type: 'ColonProperty', property }}
)* {
return tail.reduce((result, element) => {
return {
type: "MemberExpression",
object: result,
property: element
};
}, head);
}
char "char"
= unescaped
/ escape sequence:(
"'"
/ "\\"
/ "/"
/ "b" { return "\b"; }
/ "f" { return "\f"; }
/ "n" { return "\n"; }
/ "r" { return "\r"; }
/ "t" { return "\t"; }
) { return sequence; }
unescaped
= [^\0-\x1F\x27\x5C]
escape
= "\\"
interpString
= left:stringLeftPiece middle:( expression stringMiddlePiece )* expRight:expression right:stringRightPiece {
return {
type: 'TemplateLiteral',
expressions: extractList(middle,0).concat([expRight]),
quasis: [left, ...extractList(middle,1), right]
}
}
/ str:stringComplete {
return str
}
stringLeftPiece = "'" str:char* "${" { return {type: 'TemplateElement', value: str.join(''), tail:false }}
stringMiddlePiece = "}" str:char* "${" { return {type: 'TemplateElement', value: str.join(''), tail:false }}
stringRightPiece = "}" str:char* "'" { return {type: 'TemplateElement', value: str.join(''), tail:true }}
stringComplete = "'" str:char* "'" { return { type: 'String', value: str.join('') } }
multilineString = "'''" str:(char / NL)* "'''" { return { type: 'MultilineString', value: str.join('') } }
primaryExpression
= multilineString
/ interpString
/ array
/ object
/ functionCall
/ literalValue
/ identifier
/ parenthesizedExpression
/ forExpression
literalValue
= num:( "-" [0-9]+ / [0-9]+ ) { return { type: 'Number', value: parseInt(num.join('')), raw: num.join('') }}
/ "true" { return { type: 'Boolean', value: true, raw: 'true'}}
/ "false" { return { type: 'Boolean', value: false, raw: 'false'}}
/ "null" { return { type: 'Null', value: null, raw: 'null'}}
array = "[" ___ line:arrayLine? NL* ___ "]" {
return {
type: 'ArrayExpression',
elements: line
}
}
arrayLine = NL+ els:arrayItem+ { return [...els] }
arrayItem = ___ exp:expression ___ NL+ { return exp }
object = "{" __ props:( objectProperty NL+ )* __ "}" {
return {
type: 'Object',
properties: extractList(props,0)
}
}
objectProperty = __ key:( identifier / interpString ) ___ ":" ___ exp:expression ___ {
return {
key,
value: exp
}
}
functionCall = id:identifier "(" args:argumentList? ")" { return {
type: 'FunctionCall',
name: id.name,
args: optionalList(args)
}}
argumentList =
head:expression
tail:("," __ expression)* {
return [head].concat(extractList(tail,2))
}
// argumentList = ""
// Whitespace
___ "optional no new line" = ([ \t])*
_ "mandatory whitespace" = ([ \t])+
__ = (WhiteSpace / LineTerminatorSequence / Comment)*
WhiteSpace "whitespace" = [ \t]
NL "NewLine" = ( Comment* [\n\r]+)
LineTerminator = [\n\r\u2028\u2029]
LineTerminatorSequence "end of line"
= "\n"
/ "\r\n"
/ "\r"
Comment "comment"
= MultiLineComment
/ SingleLineComment
MultiLineComment
= "/*" comm:(!"*/" char)* "*/" {
return { value: extractList(comm, 1)}
}
MultiLineCommentNoLineTerminator
= "/*" (!("*/" / LineTerminator) char)* "*/"
SingleLineComment
= "//" comm:(!LineTerminator (char / "'"))* {
return { value: extractList(comm, 1)}
}