tokay 0.6.6

Tokay is a programming language designed for ad-hoc parsing.
Documentation
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
422
# The Tokay programming language
# Copyright © 2023 by Jan Max Meyer, Phorward Software Technologies.
# Licensed under the MIT license. See LICENSE for more information.
#
# This Tokay program expresses Tokay's grammar in itself.
# It is used to modify and build Tokays own language parser.
#
# See `build/README.md` for details.
#

#ExpectAndRecover : @<P> msg=void {
#    accept P
#    print(msg || "Expecting " + *P + ", but got " + repr(Peek<(Token | Char | "end-of-file")>)) Char<^\n;>*
#}

# 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

T_EOL : @{
    '\n' _
    '\r' '\n'? _
    ';' _
    accept Peek<'}'>
    accept Peek<EOF>
}

# 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("body")>  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
}

# Inlined stuff - here comes everything which happens in brackets (...)

InlineAssignment : Assignment<Expression>("copy")

InlineSequenceItem : @{
    T_Alias _ '=>' _ Expect<InlineAssignment>  ast("alias")
    LogicalOr '=>' _ Expect<InlineAssignment>  ast("alias")
    InlineAssignment
}

InlineSequence : @{
    (InlineSequenceItem ___)+  if type($1) == "list" && $1.len > 1 || $1["emit"] == "alias" ast("sequence")
}

InlineSequences : @{
    InlineSequence (___ '|' _ ___ Expect<InlineSequence>)+  ast("block")
    InlineSequence
}

InlineList : @{
    InlineAssignment ___ (',' _ InlineAssignment ___)+ (',' _)? ___  ast("list")
    InlineAssignment? ___ (',' _) ___  ast("list")
}

# Call parameters (used by calls and rvalues)

CallArgument : @{
    T_Identifier _ '=' Not<Char<\>=>> _ Expect<InlineSequences>  ast("callarg_named")
    InlineSequences  ast("callarg")
}

CallArguments : @{
    CallArgument + Repeat<((',' _) ___ CallArgument), min:0, blur:false> (',' _)? ___
    # List<CallArgument, Separator: ((',' _)? ___)>  # Stack overflow :-(
}

# Token

TokenLiteral : @{
    '\'' T_Touch '\''  ast("value_token_match")
    T_Touch  ast("value_token_touch")
    Keyword<'Chars'> '<' Ccl '>'  ast("value_token_ccls")
    Keyword<'Chars'>  ast("value_token_anys")
    Keyword<'Char'> '<' Ccl '>'  ast("value_token_ccl")
    Keyword<'Char'>  ast("value_token_any")
    Keyword<'Self'>  ast("value_token_self")
    Keyword<'Void'>  ast("value_token_void")
}

Token : @{
    '(' _ ___ ')'  ast("dict")  # defines an empty dict
    '(' _ ___ (InlineList | InlineSequences) ___ Expect<')'>
    '@' _ '(' _ ___ (InlineList | InlineSequences) ___ Expect<')'>  ast("area")
    Block
    TokenLiteral
    ParseletInstance '(' _ ___ CallArguments? ___ Expect<')'>  ast("call")
    ParseletInstance
}

TokenModifier : @{
    Token '+'  ast("op_mod_pos")
    Token '*'  ast("op_mod_kle")
    Token '?'  ast("op_mod_opt")
    Token
}

# Expression & Flow

## Literals

Literal : @{
    Keyword<'true'> _  ast("value_true")
    Keyword<'false'> _  ast("value_false")
    Keyword<'void'> _  ast("value_void")
    Keyword<'null'> _  ast("value_null")
    Keyword<'self'> _  ast("value_self")
    T_String  ast("value_string")
    T_Float
    T_Integer
}

## Atomic elements, including if and loops as they are atomic part of expressions

Atomic : @{
    Literal
    TokenModifier
    Keyword<'if'> _ Expect<Expression> ___ Expect<Statement> \
        (___ Keyword<'else'> _ ___ Expect<Statement>)?  ast("op_if")
    Keyword<'for'> _ Expect<Lvalue> Keyword<Expect<'in'>> _ Expect<Expression> \
         ___ Expect<Statement>  ast("op_for")
    Keyword<'loop'> _ Expression ___ Block  ast("op_loop")
    Keyword<'loop'> _ 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
}

# Expressional syntax

Unary : @{
    '-' Not<'-'> _ Unary  ast("op_unary_neg")
    '!' _ Unary  ast("op_unary_not")
    '*' _ Unary  ast("op_deref")
    Rvalue _
}

MulDiv : @{
    MulDiv '*' Not<Char<=>> _ Expect<Unary>  ast("op_binary_mul")
    MulDiv '//' Not<Char<=>> _ Expect<Unary>  ast("op_binary_divi")
    MulDiv '/' Not<Char<=>> _ Expect<Unary>  ast("op_binary_div")
    MulDiv '%' Not<Char<=>> _ Expect<Unary>  ast("op_binary_mod")
    Unary
}

AddSub : @{
    AddSub '+' Not<Char<+=>> _ Expect<MulDiv>  ast("op_binary_add")
    AddSub '-' Not<Char<-=>> _ 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
}

Expression : LogicalOr

ExpressionList : @{
    Expression (',' _ Expression)+ (',' _)?  ast("list")
    Expression? (',' _)  ast("list")
    Expression
}

# Assignments

Assignment : @<Source> mode = "hold" {
    Lvalue _ '+=' _ Expect<Self>  ast("assign_add_" + mode)
    Lvalue _ '-=' _ Expect<Self>  ast("assign_sub_" + mode)
    Lvalue _ '*=' _ Expect<Self>  ast("assign_mul_" + mode)
    Lvalue _ '/=' _ Expect<Self>  ast("assign_div_" + mode)
    Lvalue _ '//=' _ Expect<Self>  ast("assign_divi_" + mode)
    Lvalue _ '%=' _ Expect<Self>  ast("assign_mod_" + mode)
    Lvalue _ '=' Not<Char<\>=>> _ Expect<Self>  ast("assign_" + mode)
    Source
}

# Blocks and Sequences

Statement : @{
    Keyword<'accept'> _ Expression?  ast("op_accept")
    Keyword<'break'> _ Expression?  ast("op_break")
    Keyword<'continue'> _ Expression?  ast("op_continue")
    Keyword<'exit'> _ Expression?  ast("op_exit")
    Keyword<'next'> _  ast("op_next")
    Keyword<'push'> _ Expression?  ast("op_push")
    Keyword<'reject'> _  ast("op_reject")
    Keyword<'repeat'> _  ast("op_repeat")
    Keyword<'reset'> _  ast("op_reset")
    Keyword<'return'> _ Expression?  ast("op_accept")
    Assignment<ExpressionList>("drop")
}

Block : @ emit = "block" {
    '{' _ ___ '}'  ast("value_void")
    '{' _ Tokay* _ Expect<'}'>   ast(emit)
}

SequenceItem : @{
    T_Alias _ '=>' _ Expect<ExpressionList>  ast("alias")
    Expression '=>' _ Expect<ExpressionList>  ast("alias")
    Statement
}

Sequence : @{
    SequenceItem+  if type($1) == "list" && $1.len > 1 || $1["emit"] == "alias" ast("sequence")
}

Sequences : @{
    Sequence ('|' _ Expect<Sequence>)+  ast("block")
    Sequence
}

# Main

Tokay : @{
    T_EOL
    Keyword<'begin'> _ Expect<Sequences> Expect<T_EOL>  ast("begin")
    Keyword<'end'> _ Expect<Sequences> Expect<T_EOL>  ast("end")
    T_Identifier _ ':' _ {
        Literal _ Peek<T_EOL>
        Token _ Peek<T_EOL>
        Sequences
    } Expect<T_EOL>  ast("constant")
    Sequences T_EOL?
}

_ Tokay* Expect<EOF>  ast("main")
#_ Tokay* Expect<EOF>  ast2rust(ast("main"))
#_ Tokay* Expect<EOF>  ast_print(ast("main"))