forked from tokay-lang/tokay
/
tokay.tok
349 lines (296 loc) · 7.73 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
# This is Tokay's own grammar expressed as a Tokay grammar
#
# This grammar was generated by the tokay_dump!()-macro from src/compiler/macros.rs,
# but some manual intervention was required, especially for the character-classes.
# It hopefully will serve as the source grammar in a future Tokay release.
#
# This parser can be executed by Tokay and parse itself with the call
#
# tokay examples/tokay.tok -- examples/tokay.tok
#
_ : @{
[\t ]+
'#' [^\n]*
'\\' '\n'
}
___ : @{
peek not [A-Z_a-z] _
EOF
}
T_EOL : @{
'\n' _
'\r' '\n'? _
';' _
EOF
peek '}'
}
T_OctDigit : @{
[0-7]
}
T_HexDigit : @{
[0-9A-Fa-f]
}
T_EscapeSequence : @{
'a' "\x07"
'b' "\x08"
'f' "\x0c"
'n' "\n"
'r' "\r"
't' "\t"
'v' "\x0b"
T_OctDigit T_OctDigit T_OctDigit
'x' T_HexDigit T_HexDigit
'u' T_HexDigit T_HexDigit T_HexDigit T_HexDigit
'U' T_HexDigit T_HexDigit T_HexDigit T_HexDigit T_HexDigit T_HexDigit T_HexDigit T_HexDigit
Any
}
T_Identifier : @{
ast("identifier", Ident)
}
T_Consumable : @{
[A-Z_] [0-9A-Z_a-z]* ast("identifier", $0)
}
T_Alias : @{
[A-Z_a-z] [0-9A-Z_a-z]* ast("value_string", $0)
}
T_String : @{
'"' {
'\\' T_EscapeSequence
[^\\\"]
EOF error("Unclosed string, expecting '\"'")
}* str_join("", $2) expect '"'
}
T_Touch : @{
'\'' {
'\\' T_EscapeSequence
[^\\\']
EOF error("Unclosed match, expecting '\''")
}* str_join("", $2) expect '\''
}
T_Integer : @{
ast("value_integer", Int)
}
T_Float : @{
ast("value_float", Float)
}
CclChar : @{
'\\' T_EscapeSequence
[^\]]
EOF error("Unclosed character-class, expecting ']'")
}
CclRange : @{
CclChar '-' CclChar ast("range", ($1, $3))
CclChar ast("char")
}
Ccl : @{
'^' CclRange* ast("ccl_neg")
CclRange* ast("ccl")
}
Subscript : @{
'[' _ Expression ']' _ ast("index")
}
Attribute : @{
'.' _ T_Alias ast("attribute")
}
Capture : @{
'$' T_Alias _ ast("capture_alias")
'$' T_Integer _ ast("capture_index")
'$' '(' _ (T_EOL _)* Expression ')' _ ast("capture_expr")
'$' error("'$': Expecting identifier, integer or (expression)")
}
Variable : @{
T_Identifier
Capture
}
Lvalue : @{
Variable _ {
Subscript
}* ast("lvalue")
}
Load : @{
Lvalue '++' ast("inplace_post_inc")
Lvalue '--' ast("inplace_post_dec")
'++' expect Lvalue ast("inplace_pre_inc")
'--' expect Lvalue ast("inplace_pre_dec")
Variable
}
CallParameter : @{
T_Identifier _ '=' _ expect Expression ast("param_named")
Expression ast("param")
}
CallParameters : @{
(CallParameter (',' _)? (T_EOL _)*)+
}
Literal : @{
'true' ___ ast("value_true")
'false' ___ ast("value_false")
'void' ___ ast("value_void")
'null' ___ ast("value_null")
T_String ast("value_string")
T_Float
T_Integer
}
CollectionItem : @{
T_Alias _ '=>' _ expect Expression ast("alias")
Expression '=>' _ expect Expression ast("alias")
Expression
}
Collection : @{
'(' _ (T_EOL _)* ')' ast("value_void", null)
'(' _ (T_EOL _)* (Expression (',' _)? (T_EOL _)*)+ ')' ast("sequence")
'(' _ (T_EOL _)* (CollectionItem (',' _)? (T_EOL _)*)+ expect ')' ast("sequence")
}
TokenLiteral : @{
'\'' T_Touch '\'' ast("value_token_match")
T_Touch ast("value_token_touch")
'.' ast("value_token_any")
'[' Ccl ']' ast("value_token_ccl")
}
TokenCall : @{
TokenLiteral
T_Consumable '(' _ (T_EOL _)* CallParameters? (T_EOL _)* expect ')' ast("call")
T_Consumable ast("call")
Parselet
Collection
Block
}
Token : @{
TokenCall '+' ast("op_mod_pos")
TokenCall '*' ast("op_mod_kle")
TokenCall '?' ast("op_mod_opt")
TokenCall
'peek' ___ expect Token ast("op_mod_peek")
'not' ___ expect Token ast("op_mod_not")
'expect' ___ expect Token ast("op_mod_expect")
}
Atomic : @{
'(' _ (T_EOL _)* Expression (T_EOL _)* ')'
Literal
Token
'if' ___ Expression (T_EOL _)* expect Statement ((T_EOL _)* 'else' ___ (T_EOL _)* expect Statement)? ast("op_if")
'for' ___ StatementOrEmpty ';' _ StatementOrEmpty ';' _ StatementOrEmpty StatementOrEmpty ast("op_for")
'for' ___ error("'for': Expecting start; condition; iter; statement")
'loop' ___ Expression _ Statement ast("op_loop")
'loop' ___ expect Statement ast("op_loop")
Load
}
Rvalue : @{
Rvalue '(' _ (T_EOL _)* CallParameters? 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_div")
Unary
}
AddSub : @{
AddSub '+' not '+' _ expect MulDiv ast("op_binary_add")
AddSub '-' not '-' _ expect MulDiv ast("op_binary_sub")
MulDiv
}
Compare : @{
Compare '==' _ expect AddSub ast("op_compare_eq")
Compare '!=' _ expect AddSub ast("op_compare_neq")
Compare '<=' _ expect AddSub ast("op_compare_lteq")
Compare '>=' _ expect AddSub ast("op_compare_gteq")
Compare '<' _ expect AddSub ast("op_compare_lt")
Compare '>' _ expect AddSub ast("op_compare_gt")
AddSub
}
LogicalAnd : @{
LogicalAnd '&&' _ expect Compare ast("op_logical_and")
Compare
}
LogicalOr : @{
LogicalOr '||' _ expect LogicalAnd ast("op_logical_or")
LogicalAnd
}
Expression : @{
Lvalue _ '=' not {
'>'
'='
} _ expect Expression ast("assign_hold")
Lvalue _ '+=' _ expect Expression ast("assign_add_hold")
Lvalue _ '-=' _ expect Expression ast("assign_sub_hold")
Lvalue _ '*=' _ expect Expression ast("assign_mul_hold")
Lvalue _ '/=' _ expect Expression ast("assign_div_hold")
LogicalOr
}
StatementOrEmpty : @{
Statement
ast("op_nop")
}
Statement : @{
'accept' ___ Expression? ast("op_accept")
'break' ___ Expression? ast("op_break")
'continue' ___ Expression? ast("op_continue")
'exit' ___ Expression? ast("op_exit")
'next' ___ ast("op_next")
'push' ___ Expression? ast("op_push")
'reject' ___ ast("op_reject")
'repeat' ___ Expression? ast("op_repeat")
'return' ___ Expression? ast("op_accept")
Lvalue _ '=' not {
'>'
'='
} _ expect Expression ast("assign")
Lvalue _ '+=' _ expect Expression ast("assign_add")
Lvalue _ '-=' _ expect Expression ast("assign_sub")
Lvalue _ '*=' _ expect Expression ast("assign_mul")
Lvalue _ '/=' _ expect Expression ast("assign_div")
Expression
}
Argument : @{
T_Identifier _ ('=' _ Expression?)? ast("arg")
}
Arguments : @{
(Argument (',' _)?)+
}
Parselet : @{
'@' _ Arguments? Block ast("value_parselet")
'@' _ Arguments? Token ast("value_parselet")
}
Block : @{
'{' _ (T_EOL _)* '}' 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
}
SequenceOrExpression : @{
Expression peek T_EOL
Sequences
}
Instruction : @{
'begin' ___ Sequence expect T_EOL ast("begin")
'end' ___ Sequence expect T_EOL ast("end")
T_Identifier _ ':' _ expect SequenceOrExpression expect T_EOL ast("constant")
Sequences
T_EOL
}
Tokay : @{
Instruction+
. error("Parse error, unexpected token", true)
}
# Original invocation
# _ Tokay expect EOF ast("main")
# fixme: The invocation is a little bit ugly currently.
ast_print(@{_ Tokay expect EOF ast("main")}()) # fixme...