-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtl.pegjs
369 lines (322 loc) · 9.66 KB
/
tl.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
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
{
const makeNode = (type, body = {}) => ({
type,
...location(),
...body,
})
const makeFinalDecl = (finalization, id) =>
makeNode('FinalDeclaration', { finalization, id })
const makeDeclarationsNode = (type, declarations, initial = []) =>
makeNode(type, {
declarations: declarations
.filter(e => e.type === type)
.reduce((acc, { declarations }) => {
acc.push(...declarations)
return acc
}, initial)
})
// [[a,b],[c,d]] -> [a,c]
const extractFirst = list =>
list.map(list2 => list2 && list2[0])
// [[a,b],[c,d]] -> [b,d]
const extractLast = list =>
list.map(list2 => list2 && list2[list2.length - 1])
const safeFirst = list => list ? list[0] : null
const safeLast = list => list ? list[list.length - 1] : null
}
Start
= __ program:TLProgram __ { return program }
// ---Character classes---
LcLetter
= letter:[a-z] { return letter }
UcLetter
= letter:[A-Z] { return letter }
Digit
= digit:[0-9] { return Number(digit) }
HexDigit
= hexDigit:[0-9a-f] { return hexDigit }
Letter = LcLetter / UcLetter
IdentChar = Letter / Digit / "_"
// ---Simple identifiers and keywords---
LcIdent = LcLetter IdentChar* { return text() }
UcIdent = UcLetter IdentChar* { return text() }
NamespaceIdent = LcIdent
LcIdentNs = (NamespaceIdent ".")? LcIdent { return text() }
UcIdentNs = (NamespaceIdent ".")? UcIdent { return text() }
LcIdentFull =
LcIdentNs ("#"
// 4 - 8 hex digits
HexDigit HexDigit HexDigit HexDigit
HexDigit? HexDigit? HexDigit? HexDigit?
)? { return text() }
// ---Other tokens---
FinalKw = "Final" !IdentChar
NewKw = "New" !IdentChar
EmptyKw = "Empty" !IdentChar
NatConst = Digit+ !IdentChar { return Number(text()) }
// ---General syntax of a TL program---
TLProgram
= head:ConstrDeclarations tail:(
__ "---" "functions" "---" __ FunDeclarations
/ __ "---" "types" "---" __ ConstrDeclarations
)* {
const declarations = extractLast(tail)
const constructors = makeDeclarationsNode(
'ConstructorDeclarations', declarations, head.declarations)
const functions = makeDeclarationsNode(
'FunctionDeclarations', declarations)
return makeNode('TLProgram', {
constructors,
functions
})
}
ConstrDeclarations
= decls:(Declaration __)*
{ return makeNode('ConstructorDeclarations', {
declarations: extractFirst(decls) }) }
FunDeclarations
= decls:(Declaration __)*
{ return makeNode('FunctionDeclarations', {
declarations: extractFirst(decls) }) }
Declaration
= FinalDecl
/ CombinatorDecl
/ BuiltinCombinatorDecl
/ PartialAppDecl
// ---Syntactical categories and constructions---
ENat
= value:NatConst
{ return makeNode('ENat', { value }) }
// TypeExpr
// = expression:Expr
// { return makeNode('TypeExpression', { expression }) }
// NatExpr
// = expression:Expr
// { return makeNode('NatExpression', { expression }) }
Expr
= subexprs:(__ Subexpr)* {
return makeNode('EExpression', {
subexpressions: extractLast(subexprs)
})
}
Subexpr
= Term
/ natexpr:ENat __ "+" __ subexpr:Subexpr {
return makeNode('EOperator', {
kind: '+',
expression: makeNode('EExpression', {
subexpressions: [natexpr, subexpr]
})
})
}
// / Subexpr "+" NatConst
// Possible infinite loop when parsing
// (left recursion: Expr -> Subexpr -> Subexpr).
ETypeIdent
= id:TypeIdent
{ return makeNode('ETypeIdentifier', { id }) }
Term
= "(" __ expr:Expr __ ")" { return expr }
// / id:ETypeIdent __ "<" __ head:Expr tail:(__ "," __ Expr)* __ ">" {
/ id:ETypeIdent __ "<" __ head:Subexpr tail:(__ "," __ Subexpr)* __ ">" {
return makeNode('EExpression', {
subexpressions: [id, head].concat(extractLast(tail))
})
}
/ ETypeIdent
// / VarIdent //?
/ ENat
/ "%" terms:(__ Term)+ {
const subexpressions = extractLast(terms)
return makeNode('EOperator', {
kind: '%',
expression: subexpressions.length > 1
? makeNode('EExpression', { subexpressions })
: subexpressions[0]
})
}
SimpleTypeIdent
= name:LcIdentNs
{ return makeNode('SimpleTypeIdentifier', { name }) }
HashTypeIdent
= name:"#"
{ return makeNode('HashTypeIdentifier', { name }) }
TypeIdent
= BoxedTypeIdent / SimpleTypeIdent / HashTypeIdent
BoxedTypeIdent
= name:UcIdentNs
{ return makeNode('BoxedTypeIdentifier', { name }) }
VarIdent
= name:(LcIdent / UcIdent)
{ return makeNode('VariableIdentifier', { name }) }
TypeTerm
= bang:"!"? __ expr:Term {
const expression = bang === '!'
? makeNode('EOperator', { kind: '!', expression: expr })
: expr
return makeNode('TypeExpression', { expression })
}
NatTerm
= expression:Term
{ return makeNode('NatExpression', { expression }) }
// ---Combinator declarations---
FullCombName
= ident:LcIdentFull {
const [name, magic] = ident.split('#')
if (!magic) return makeNode('ShortCombinatorName', { name })
return makeNode('FullCombinatorName', { name, magic })
}
ShortCombName
= name:LcIdentNs
{ return makeNode('ShortCombinatorName', { name }) }
EmptyCombName
= name:"_"
{ return makeNode('EmptyCombinatorName', { name }) }
VarIdentEmpty
= name:"_"
{ return makeNode('EmptyVariableIdentifier', { name }) }
CombinatorDecl
= id:FullCombinatorId __
optionalArgs:(OptArgs __)*
args:(Args __)*
"=" __ excl:"!"? __
resultType:ResultType __ ";" {
return makeNode('CombinatorDeclaration', {
id,
optionalArgs: [].concat(...extractFirst(optionalArgs)),
args: [].concat(...extractFirst(args)),
bang: excl === "!",
resultType
})
}
FullCombinatorId
= FullCombName / EmptyCombName
CombinatorId
= ShortCombName / EmptyCombName
OptArgs
// = "{" ids:(__ VarIdent)+ __ ":" __ "!"? __ type:TypeExpr __ "}"
= "{" ids:(__ VarIdent)+ __ ":" __ argType:TypeTerm __ "}" {
return extractLast(ids).map(id =>
makeNode('OptionalArgument', { id, argType }))
}
Args
= idOrNull:(VarIdentOpt __ ":")? __
mult:(Multiplicity "*")? __
"[" __ subargs:(__ Args)* __ "]" {
const id = safeFirst(idOrNull)
|| makeNode('EmptyVariableIdentifier', { name: '_' })
const argType = makeNode('TypeExpression', {
expression: makeNode('EMultiArg', {
multiplicity: safeFirst(mult),
subargs: [].concat(...extractLast(subargs))
})
})
return [makeNode('Argument', {
id,
conditionalDef: null,
argType
})]
}
/ id:VarIdentOpt __ ":" __ cond:ConditionalDef? __ argType:TypeTerm
{ return [makeNode('Argument', { id, conditionalDef: cond, argType })] }
/ id:VarIdentOpt __ ":" __ "(" __ cond:ConditionalDef? __ argType:TypeTerm __ ")"
{ return [makeNode('Argument', { id, conditionalDef: cond, argType })] }
/ "(" ids:(__ VarIdentOpt)+ __ ":" __ argType:TypeTerm __ ")" {
return extractLast(ids).map(id =>
makeNode('Argument', { id, conditionalDef: null, argType }))
}
/ argType:TypeTerm {
return [makeNode('Argument', {
id: makeNode('EmptyVariableIdentifier', { name: '_' }),
conditionalDef: null,
argType
})]
}
Multiplicity
= NatTerm
VarIdentOpt
= VarIdent / VarIdentEmpty
ConditionalDef
= id:VarIdent __ nat:("." NatConst)? __ "?" {
return makeNode('ConditionalDefinition', {
id,
nat: safeLast(nat)
})
}
ResultType
= id:BoxedTypeIdent __ "<" __ head:Subexpr tail:(__ "," __ Subexpr)* __ ">" {
return makeNode('ResultType', {
id,
expression: makeNode('EExpression', {
subexpressions: [head].concat(extractLast(tail))
})
})
}
/ id:BoxedTypeIdent subexprs:(__ Subexpr)* {
return makeNode('ResultType', {
id,
expression: makeNode('EExpression', {
subexpressions: extractLast(subexprs)
})
})
}
BuiltinCombinatorDecl
= id:FullCombinatorId __
"?" __ "=" __
result:BoxedTypeIdent __ ";"
{ return makeNode('BuiltinCombinatorDeclaration', { id, result }) }
// ---Partial applications (patterns)---
PartialAppDecl
= PartialTypeAppDecl
/ PartialCombAppDecl
PartialTypeAppDecl
// = id:BoxedTypeIdent __ "<" __ head:Expr tail:(__ "," __ Expr)* __ ">" __ ";" {
= id:BoxedTypeIdent __ "<" __ head:Subexpr tail:(__ "," __ Subexpr)* __ ">" __ ";" {
return makeNode('PartialTypeApplicationDeclaration', {
id,
expression: makeNode('EExpression', {
subexpressions: [head].concat(extractLast(tail))
})
})
}
/ id:BoxedTypeIdent subexprs:(__ Subexpr)+ __ ";" {
return makeNode('PartialTypeApplicationDeclaration', {
id,
expression: makeNode('EExpression', {
subexpressions: extractLast(subexprs)
})
})
}
PartialCombAppDecl
= id:CombinatorId subexprs:(__ Subexpr)+ __ ";" {
return makeNode('PartialCombinatorApplicationDeclaration', {
id,
expression: makeNode('EExpression', {
subexpressions: extractLast(subexprs)
})
})
}
// ---Type finalization---
FinalDecl
= NewKw __ ident:BoxedTypeIdent __ ";"
{ return makeFinalDecl('New', ident) }
/ FinalKw __ ident:BoxedTypeIdent __ ";"
{ return makeFinalDecl('Final', ident) }
/ EmptyKw __ ident:BoxedTypeIdent __ ";"
{ return makeFinalDecl('Empty', ident) }
// --- ---
Comment
= "//" comment:[^\r\n]* ([\r\n] / EOF)
// { return makeNode('Comment', { value: comment.join('') }) }
// --- ---
Ws "whitespace"
= " "
/ "\t"
/ "\r"
/ "\n"
__ "skip whitespace and comments"
= (Ws / Comment)*
// _ "one or more whitespace"
// = Ws+
EOF
= !.