-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathion.peg
391 lines (290 loc) · 10.4 KB
/
ion.peg
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
* Ion Grammar
*/
{
const TYPES_TO_PROPERTY_NAMES = { Call: "callee", Member: "object"}
const map = (fn, array) => {
var result = new Array(array.length), i
for (i = 0; i < array.length; i++)
result[i] = fn(array[i])
return result
}
const list = (head, tail, index) =>
[head].concat(map((item) => item[index], tail))
const reduce = (fn) => (init, array) => {
var result = init, i;
for (i = 0; i < array.length; i++)
result = fn(result, array[i])
return result
}
const member = reduce((result, element) => ({
type: "Member",
object: result,
property: element.property,
computed: element.computed
}))
const memberOrCall = reduce((result, element) => {
element[TYPES_TO_PROPERTY_NAMES[element.type]] = result
return element
})
const binary = reduce((result, element) => ({
type: "Binary",
operator: element[1],
left: result,
right: element[3]
}))
function r(type, location, rest) {
return Object.assign({ type, location: location() }, rest)
}
}
Start = __ program:Program __ { return program; }
Program =
body:Statements?
{ return r("Program", location, { body: body || [] }) }
Statements =
head:Statement tail:(__ Statement)*
{ return list(head, tail, 1) }
Statement "statement" =
Header / WrongHeader / Export / Call / Assignment / If
IonPath =
"./bin/ion" / "./bin/dion" / "ion"
Header =
"#!/usr/bin/env " path:IonPath { return r("Header", location, { path }) }
WrongHeader =
"#!/usr/bin/env " path:([a-z/.] +) { return r("WrongHeader", location, { path: path.join('') }) }
Export =
ExportToken __ expression:Expression
{ return r("Export", location, { expression }) }
ExportToken = "export" !IdChar
Call =
head:(
callee:Member args:Arguments
{ return { type: "Call", callee, args } }
)
tail:(
args:Arguments
{ return { type: "Call", args } }
/ __ "[" __ property:Expression __ "]"
{ return { type: "Member", property, computed: true } }
/ __ "." __ property:IdName
{ return { type: "Member", property, computed: false } }
)*
{ return memberOrCall(head, tail) }
Assignment =
left:Member __ "=" !"=" __ right:Expression
{ return r("Assignment", location, { left, right }) }
// Value must precede Op, 'cause of / (RegExp, division)
Expression =
ExpressionWithWhere / ExpressionWithoutWhere
ExpressionWithoutWhere = If / Lambda / Ternary / Testable
Testable = Binary / Unary / Operable
Operable = Call / OpCall / Member / Value
ExpressionWithWhere =
expression:(ExpressionWithoutWhere) __ WhereToken __ assignments:AssignmentList {
return r("Where", location, {expression, assignments})
}
AssignmentList =
head:Assignment tail:(__ "," __ Assignment)*
{ return list(head, tail, 3) }
// Params = "(" __ params:ParamList? __ ")" { return params || [] }
// ParamList = head:Id tail:(__ "," __ Id)* { return list(head, tail, 3); }
WhereToken = "where"
Ternary =
test:Binary __ '?' __ consequent:Expression __ ':' __ alternate:Expression
{ return r("Ternary", location, { test, consequent, alternate }) }
OpCall =
op:Op args:Arguments
{ return r("OpCall", location, { op, args }) }
Binary =
head:BinaryUnit tail:(Ss Op Ss BinaryUnit)*
{ return binary(head, tail) }
BinaryUnit =
"(" __ expr:Binary __ ")" { return Object.assign({ parens: true }, expr) }
/ Unary
Unary =
Operable / operator:UnaryOp __ argument:Unary
{ return r('Unary', location, { operator, argument }) }
/* Member */
Member =
head:Id
tail:(
__ "[" __ property:Expression __ "]"
{ return { property, computed: true } }
/ __ "." __ property:IdName
{ return { property, computed: false } }
)*
{ return member(head, tail) }
Arguments =
"(" __ args:ArgumentList? __ ")"
{ return args || [] }
ArgumentList =
head:Expression tail:(__ "," __ Expression)*
{ return list(head, tail, 3); }
SourceChar = .
S "whitespace" = "\t" / "\v" / "\f" / " " / "\u00A0" / "\uFEFF"
LineTerminator = [\n\r\u2028\u2029]
LineTerminatorSequence "EOL" = "\n" / "\r\n" / "\r" / "\u2028" / "\u2029"
Comment "comment" = MultiLineComment / SingleLineComment
MultiLineComment = "/*" (!"*/" SourceChar)* "*/"
MultiLineCommentNoLineTerminator = "/*" (!("*/" / LineTerminator) SourceChar)* "*/"
SingleLineComment = "//" (!LineTerminator SourceChar)*
Id = !ReservedWord name:IdName { return name; }
IdName "identifier" =
head:IdChar tail:IdRest*
{ return r("Id", location, { name: head + tail.join("") }) }
IdChar = [A-Za-z_]
IdRest = IdChar / [0-9]
ReservedWord = ElseToken / IfToken / ExportToken / NullLiteral / BooleanLiteral
/* Null */
NullLiteral = NullToken { return r("Null", location) }
NullToken = "null" !IdChar
/* Bool */
BooleanLiteral = TrueLiteral / FalseLiteral
FalseLiteral = FalseToken { return r("False", location) }
FalseToken = "false" !IdChar
TrueLiteral = TrueToken { return r("True", location) }
TrueToken = "true" !IdChar
/*
* The "!(IdChar / DecimalDigit)" predicate is not part of the official
* grammar, it comes from text in section 7.8.3.
*/
NumericLiteral "number" =
literal:(HexIntLiteral / OctalIntLiteral / DecimalLiteral) !(IdChar / DecimalDigit)
{ return literal }
OctalIntLiteral =
"0"+ OctalDigit+
{ return r("Octal", location, { value: text() }) }
// here it is diff from JS, doesn't allow '123.' or '.123'
// in JS you can't have 123.func(), weird. Write about that!
DecimalLiteral =
DecimalIntLiteral ("." DecimalDigit+)? ExponentPart?
{ return r("Decimal", location, { value: text() }) }
DecimalIntLiteral =
"0" / NonZeroDigit DecimalDigit* { return text() }
DecimalDigit = [0-9]
NonZeroDigit = [1-9]
OctalDigit = [0-7]
ExponentPart = ExponentIndicator SignedInt
ExponentIndicator = "e"i
SignedInt = [+-]? DecimalDigit+
HexIntLiteral = "0x"i digits:$HexDigit+ { return r("Hex", location, { value: text() }) }
HexDigit = [0-9a-f]i
StringLiteral "string" =
'`' chars:BacktickStringChar* '`'
{ return r("String", location, { value: chars.join("") }) }
/ '"' chars:DoubleStringChar* '"'
{ return r("String", location, { value: chars.join("") }) }
/ "'" chars:SingleStringChar* "'"
{ return r("String", location, { value: chars.join("") }) }
BacktickStringChar =
"\\" sequence:EscapeSequence { return sequence }
/ !'`' SourceChar { return text() }
/ LineContinuation
DoubleStringChar =
"\\" sequence:EscapeSequence { return sequence }
/ !'"' SourceChar { return text() }
/ LineContinuation
SingleStringChar =
"\\" sequence:EscapeSequence { return sequence }
/ !"'" SourceChar { return text() }
/ LineContinuation
LineContinuation = "\\" LineTerminatorSequence { return "" }
EscapeSequence =
CharEscapeSequence
/ "0" !DecimalDigit { return "\0" }
/ HexEscapeSequence
/ UnicodeEscapeSequence
CharEscapeSequence = SingleEscapeChar / NonEscapeChar
SingleEscapeChar =
"'"
/ '"'
/ "\\"
/ "b" { return "\b" }
/ "f" { return "\f" }
/ "n" { return "\n" }
/ "r" { return "\r" }
/ "t" { return "\t" }
/ "v" { return "\x0B" } // IE does not recognize "\v".
HexEscapeSequence =
"x" digits:$(HexDigit HexDigit)
{ return String.fromCharCode(parseInt(digits, 16)) }
UnicodeEscapeSequence =
"u" digits:$(HexDigit HexDigit HexDigit HexDigit)
{ return String.fromCharCode(parseInt(digits, 16)) }
NonEscapeChar = !(EscapeChar / LineTerminator) SourceChar { return text() }
EscapeChar = SingleEscapeChar / DecimalDigit / "x" / "u"
/* RegExp */
RegExp "regular expression" =
"/" pattern:$RegExpBody "/" flags:$RegExpFlags
{ return r("RegExp", location, { value: text() }) }
RegExpBody = RegExpFirstChar RegExpChar*
RegExpFirstChar = ![*\\/[] RegExpNonTerminator / RegExpBackslashSequence / RegExpClass
RegExpChar = ![\\/[] RegExpNonTerminator / RegExpBackslashSequence / RegExpClass
RegExpBackslashSequence = "\\" RegExpNonTerminator
RegExpNonTerminator = !LineTerminator SourceChar
RegExpClass = "[" RegExpClassChar* "]"
RegExpClassChar = ![\]\\] RegExpNonTerminator / RegExpBackslashSequence
RegExpFlags = IdChar*
__ = (S / LineTerminatorSequence / Comment)*
Ss = (S / LineTerminatorSequence)+
/* Value literals */
Value =
Id / NullLiteral / BooleanLiteral / NumericLiteral / StringLiteral
/ RegExp / Range / ArrayLiteral / ObjectLiteral
/* Range */
Range "range" =
"[" from:DecimalIntLiteral ".." to:DecimalIntLiteral "]"
{ return { type: "Range", from, to } }
/* Array */
ArrayLiteral =
"[" __ elements:ElementList? __ "]"
{ return { type: "Array", elements: elements || [] } }
ExpressionOrSpread =
Expression / SpreadArray
SpreadArray = "..." spreaded:(Id / ArrayLiteral) { return r("Spread", location, {spreaded}) }
ElementList =
head:( element:ExpressionOrSpread { return element } )
tail:( __ "," __ element:ExpressionOrSpread { return element } )*
{ return Array.prototype.concat.apply(head, tail) }
/* Object */
ObjectLiteral =
"{" __ properties:PropertyKeyAndValueList? __ "}"
{ return { type: "Object", properties: properties || [] } }
PropertyAssignmentOrSpread =
PropertyAssignment / SpreadObject
SpreadObject = "..." spreaded:(Id / ObjectLiteral) { return r("Spread", location, {spreaded}) }
PropertyKeyAndValueList =
head:PropertyAssignmentOrSpread tail:(__ "," __ PropertyAssignmentOrSpread)*
{ return list(head, tail, 3) }
PropertyAssignment =
key:PropertyName __ ":" __ value:Expression
{ return { key, value, kind: "init" } }
PropertyName = IdName / StringLiteral
/* Ops */
UnaryOp = "!"
FunctionalOp = $("|" ![>|]) / "@?" / "!@" / "@s" / "@=" / $("@" ![?]) / "->" / "<-"
MathOp = "**" / "*" / "/" / "%" / "+" / "-"
RelationalOp = "<=" / ">=" / "<" / ">"
EqualityOp = "==" / "!="
// TODO: rethink bitwise ops, specially OR
BitwiseOp = ">>" / $("&" ![&=]) / "^"
LogicalOp = "&&" / "||"
Op = FunctionalOp / MathOp / RelationalOp / EqualityOp / BitwiseOp / LogicalOp
/* If */
If
= IfToken __ "(" __ test:Testable __ ")" __ then:Expression __ ElseToken __ else_:Expression
{ return { type: "If", test, then, else: else_ } }
/ IfToken __ "(" __ test:Testable __ ")" __ then:Expression
{ return { type: "If", test, then, else: null } }
IfExpression
= IfToken __ "(" __ test:Testable __ ")" __ then:Expression __ ElseToken __ else_:Expression
{ return { type: "If", test, then, else: else_ } }
IfToken = "if" !IdChar
ElseToken = "else" !IdChar
/* Lambda */
Lambda =
params:Params __ ArrowToken __ body:Expression
{ return { type: "Lambda", params, body } }
Params = "(" __ params:ParamList? __ ")" { return params || [] }
ParamList = head:Id tail:(__ "," __ Id)* { return list(head, tail, 3); }
ArrowToken = "=>"