-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtokay.tok
421 lines (335 loc) · 10.3 KB
/
tokay.tok
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# Tokay grammar / parser
# This program is used to generate Tokays own parser, and is written in Tokay itself.
# Use the tooling in `gen_grammar/` for now.
# Whitespace & EOL
_ : @{ # true whitespace is made of comments and escaped line-breaks as well
Char<\t >+
'#' Char<^\n>*
'\\' '\r'? '\n'
}
___ : (T_EOL _)* # optional line-breaks followed by whitespace
_standalone_ : @{
# helper parselet to ensure that identifiers stand alone
# fixme: When generic parselets are available, this can be replaced by a Standalone<K> invocation
peek not Char<A-Z_a-z> _
_
}
T_EOL : @{
'\n' _
'\r' '\n'? _
';' _
peek EOF accept # accept is used to bypass bug #64 here
peek '}'
}
# Prime Tokens
T_OctDigit : Char<0-7>
T_HexDigit : Char<0-9A-Fa-f>
T_EscapeSequence : @{
# Named escape sequences
'a' "\x07"
'b' "\x08"
'f' "\x0c"
'n' "\n"
'r' "\r"
't' "\t"
'v' "\x0b"
# Encoded escape sequences
# fixme: This can be resolved better as soon as the Repeat generic builtin is ready
# ASCII Octal (8-Bit)
T_OctDigit T_OctDigit T_OctDigit chr(int($1) * 64 + int($2) * 8 + int($3))
# ASCII Hex (8-Bit)
'x' T_HexDigit T_HexDigit chr(int("0x" + $0.substr(1)))
# Unicode (32-Bit)
'u' T_HexDigit T_HexDigit T_HexDigit T_HexDigit chr(int("0x" + $0.substr(1)))
# Unicode (64-Bit)
'U' T_HexDigit T_HexDigit T_HexDigit T_HexDigit \
T_HexDigit T_HexDigit T_HexDigit T_HexDigit \
chr(int("0x" + $0.substr(1)))
# fixme: In case when odd amount of digits is provided, a syntax error shall occur.
# This is like in Python: "\x2" SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-2: truncated \xXX escape
Char
}
T_Identifier : @{
ast("identifier", Ident)
}
T_Consumable : @{
Char<A-Z_> Char<0-9A-Z_a-z>* ast("identifier", $0)
}
T_Alias : @{
Char<A-Z_a-z> Char<0-9A-Z_a-z>* ast("value_string", $0)
}
T_String : @{
'"' {
'\\' T_EscapeSequence
Char<^\\\">
EOF error("Unclosed string, expecting '\"'")
}* str_join("", $2) expect '"'
}
T_Touch : @{
'\'' {
'\\' T_EscapeSequence
Char<^\\\'>
EOF error("Unclosed match, expecting '\''")
}* str_join("", $2) expect '\''
}
T_Integer : @{
ast("value_integer", Int)
}
T_Float : @{
ast("value_float", Float)
}
# Character-classes
CclChar : @{
'\\' T_EscapeSequence
Char<^\>>
EOF error("Unclosed character-class, expecting ']'")
}
CclRange : @{
CclChar '-' CclChar ast("range", $1 + $3)
CclChar ast("char")
}
Ccl : @{
'^' CclRange* ast("ccl_neg")
CclRange* ast("ccl")
}
# Statics, Variables, Loads
Subscript : @{
'[' _ Expression ']' _ ast("item")
}
Attribute : @{
'.' _ T_Alias ast("attribute")
}
Capture : @{
'$' T_Alias _ ast("capture_alias")
'$' T_Integer _ ast("capture_index")
'$' '(' _ ___ Expression ')' _ ast("capture_expr")
'$' error("'$...': Expecting identifier, integer or (expression)")
}
Variable : @{
T_Identifier
Capture
}
Lvalue : @{
Variable _ Subscript* ast("lvalue") # Lvalue currently doesn't allow attribute assignment!
}
Load : @{
Lvalue '++' ast("inplace_post_inc")
Lvalue '--' ast("inplace_post_dec")
'++' expect Lvalue ast("inplace_pre_inc")
'--' expect Lvalue ast("inplace_pre_dec")
Variable
}
# Parselet
Parselet : @{
'@' _ ParseletGenerics? _ ParseletArguments? expect Block ast("value_parselet")
}
## Parselet: Generics
ParseletGeneric : @{
T_Identifier _ (':' _ expect Atomic)? ast("gen")
}
ParseletGenerics : @{
'<' _ (ParseletGeneric (',' _)?)* _ expect '>' _
}
## Parselet: Arguments
ParseletArgument : @{
T_Identifier _ ('=' _ expect Expression?)? ast("arg")
}
ParseletArguments : @{
(ParseletArgument (',' _)?)+
}
# Parselet: Instance
StaticParseletInstance : T_Consumable | Parselet
ParseletInstanceArgument : @{
T_Identifier _ ':' _ expect Atomic _ ast("genarg_named")
Atomic _ ast("genarg")
}
ParseletInstance : @{
StaticParseletInstance '<' _ (ParseletInstanceArgument (',' _)?)+ _ expect '>' _ ast("value_generic")
StaticParseletInstance
}
# Inline Blocks and Sequences
InlineSequenceItem : @{
T_Alias _ '=>' _ expect Expression ast("alias")
Expression '=>' _ expect Expression ast("alias")
Expression
}
InlineSequence : @{
# Special case: Expression followed by "," is considered as a list with a single item (syntactic sugar)
Expression ___ ',' _ ___ peek ')' ast("list")
# A sequence is a list of items optionally separated by ","
(InlineSequenceItem ___ (',' _)? ___)+ ast("inline_sequence")
# The empty sequences generates an empty list
Void ast("list")
}
InlineBlock : @{
'(' _ ___ InlineSequence {___ '|' _ ___ InlineSequence}+ ___ expect ')' ast("block")
'(' _ ___ InlineSequence ___ expect ')'
}
# Call parameters (used by calls and rvalues)
CallArgument : @{
T_Identifier _ '=' _ expect Expression ast("callarg_named")
Expression ast("callarg")
}
CallArguments : @{
(CallArgument (',' _)? ___)+
}
# Token
TokenLiteral : @{
'\'' T_Touch '\'' ast("value_token_match")
T_Touch ast("value_token_touch")
'Chars' '<' Ccl '>' ast("value_token_ccls")
'Chars' ast("value_token_anys")
'Char' '<' Ccl '>' ast("value_token_ccl")
'Char' ast("value_token_any")
}
TokenAtom : @{
TokenLiteral
InlineBlock
'@' _ InlineBlock ast("area")
Block
ParseletInstance '(' _ ___ CallArguments? ___ expect ')' ast("call")
ParseletInstance
}
Token1 : @{ # todo: Token1 can be renamed back to Token again when #31 is fixed and Self is used.
TokenAtom '+' ast("op_mod_pos")
TokenAtom '*' ast("op_mod_kle")
TokenAtom '?' ast("op_mod_opt")
TokenAtom
'peek' _standalone_ expect Token1 ast("op_mod_peek")
'not' _standalone_ expect Token1 ast("op_mod_not")
'expect' _standalone_ expect Token1 ast("op_mod_expect")
}
# Expression & Flow
## Literals
Literal : @{
'true' _standalone_ ast("value_true")
'false' _standalone_ ast("value_false")
'void' _standalone_ ast("value_void")
'null' _standalone_ ast("value_null")
T_String ast("value_string")
T_Float
T_Integer
}
## Atomic elements, including if and loops as they are atomic part of expressions
Atomic : @{
'(' _ ___ HoldExpression ___ ')'
Literal
Token1
'if' _standalone_ expect HoldExpression ___ expect Statement (___ 'else' _standalone_ ___ expect Statement)? ast("op_if")
'for' _standalone_ expect Lvalue _ expect 'in' _standalone_ expect Expression ___ expect Statement ast("op_for")
'loop' _standalone_ HoldExpression ___ Block ast("op_loop")
'loop' _standalone_ expect Block ast("op_loop")
Load
}
# Rvalue can be a function call or value attribute/subscript
Rvalue : @{
Rvalue '(' _ ___ CallArguments? expect ')' ast("call")
Rvalue (Attribute | Subscript)* ast("rvalue")
Atomic
}
Unary : @{
'-' not '-' _ Unary ast("op_unary_neg")
'!' _ Unary ast("op_unary_not")
Rvalue _
}
MulDiv : @{
MulDiv '*' _ expect Unary ast("op_binary_mul")
MulDiv '//' _ expect Unary ast("op_binary_divi")
MulDiv '/' _ expect Unary ast("op_binary_div")
MulDiv '%' _ expect Unary ast("op_binary_mod")
Unary
}
AddSub : @{
AddSub '+' not '+' _ expect MulDiv ast("op_binary_add")
AddSub '-' not '-' _ expect MulDiv ast("op_binary_sub")
MulDiv
}
Comparison : @{
AddSub {
'==' _ expect AddSub ast("cmp_eq")
'!=' _ expect AddSub ast("cmp_neq")
'<=' _ expect AddSub ast("cmp_lteq")
'>=' _ expect AddSub ast("cmp_gteq")
'<' _ expect AddSub ast("cmp_lt")
'>' _ expect AddSub ast("cmp_gt")
}+ ast("comparison")
AddSub
}
LogicalAnd : @{
LogicalAnd '&&' _ expect Comparison ast("op_logical_and")
Comparison
}
LogicalOr : @{
LogicalOr '||' _ expect LogicalAnd ast("op_logical_or")
LogicalAnd
}
HoldExpression : @{
Lvalue _ '+=' _ expect HoldExpression ast("assign_add_hold")
Lvalue _ '-=' _ expect HoldExpression ast("assign_sub_hold")
Lvalue _ '*=' _ expect HoldExpression ast("assign_mul_hold")
Lvalue _ '/=' _ expect HoldExpression ast("assign_div_hold")
Lvalue _ '//=' _ expect HoldExpression ast("assign_divi_hold")
Lvalue _ '%=' _ expect HoldExpression ast("assign_mod_hold")
Lvalue _ '=' not ('>' | '=') _ expect HoldExpression ast("assign_hold")
LogicalOr
}
Expression : @{
Lvalue _ '+=' _ expect HoldExpression ast("assign_add")
Lvalue _ '-=' _ expect HoldExpression ast("assign_sub")
Lvalue _ '*=' _ expect HoldExpression ast("assign_mul")
Lvalue _ '/=' _ expect HoldExpression ast("assign_div")
Lvalue _ '//=' _ expect HoldExpression ast("assign_divi")
Lvalue _ '%=' _ expect HoldExpression ast("assign_mod")
Lvalue _ '=' not ('>' | '=') _ expect HoldExpression ast("assign")
LogicalOr
}
# Statement and Assignment
Statement : @{
'accept' _standalone_ Expression? ast("op_accept")
'break' _standalone_ Expression? ast("op_break")
'continue' _standalone_ Expression? ast("op_continue")
'exit' _standalone_ Expression? ast("op_exit")
'next' _standalone_ ast("op_next")
'push' _standalone_ Expression? ast("op_push")
'reject' _standalone_ ast("op_reject")
'repeat' _standalone_ Expression? ast("op_repeat")
'return' _standalone_ Expression? ast("op_accept")
Expression
}
# Blocks and Sequences
Block : @{
'{' _ ___ '}' ast("value_void")
'{' _ Instruction* _ expect '}' ast("block")
}
SequenceItem : @{
T_Alias _ '=>' _ expect Expression ast("alias")
Expression '=>' _ expect Expression ast("alias")
Statement
}
Sequence : @{
(SequenceItem (',' _)?)+ ast("sequence")
}
Sequences : @{
Sequence {'|' _ Sequence}+ ast("block")
Sequence
}
Instruction : @{
'begin' _standalone_ Sequences expect T_EOL ast("begin")
'end' _standalone_ Sequences expect T_EOL ast("end")
T_Identifier _ ':' _ {
Literal _ peek T_EOL
Token1 _ peek T_EOL
Sequences
} expect T_EOL ast("constant")
Statement T_EOL
Sequences expect T_EOL
T_EOL
}
Nop : Void ast("op_nop")
# Main
Tokay : @{
Instruction+
Char error("Parse error, unexpected token", true)
}
_ Tokay? expect EOF ast("main")
#_ Tokay? expect EOF ast_print(ast("main"))