This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Grammar.pm
587 lines (472 loc) · 14.2 KB
/
Grammar.pm
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
grammar NQP::Grammar is HLL::Grammar;
method TOP() {
my %*LANG;
%*LANG<Regex> := NQP::Regex;
%*LANG<Regex-actions> := NQP::RegexActions;
%*LANG<MAIN> := NQP::Grammar;
%*LANG<MAIN-actions> := NQP::Actions;
my $*SCOPE := '';
self.comp_unit;
}
## Lexer stuff
token identifier { <ident> }
token name { <identifier> ** '::' }
token deflongname {
<identifier> <colonpair>?
}
token ENDSTMT {
[
| \h* $$ <.ws> <?MARKER('endstmt')>
| <.unv>? $$ <.ws> <?MARKER('endstmt')>
]?
}
token ws {
|| <?MARKED('ws')>
|| <!ww>
[ \v+
| '#' \N*
| ^^ <.pod_comment>
| \h+
]*
<?MARKER('ws')>
}
token unv {
# :dba('horizontal whitespace')
[
| ^^ <?before \h* '=' [ \w | '\\'] > <.pod_comment>
| \h* '#' \N*
| \h+
]
}
token pod_comment {
^^ \h* '='
[
| 'begin' \h+ 'END' >>
[ .*? \n \h* '=' 'end' \h+ 'END' » \N* || .* ]
| 'begin' \h+ <identifier>
[
|| .*? \n \h* '=' 'end' \h+ $<identifier> » \N*
|| <.panic: '=begin without matching =end'>
]
| 'begin' » \h*
[ $$ || '#' || <.panic: 'Unrecognized token after =begin'> ]
[
|| .*? \n \h* '=' 'end' » \N*
|| <.panic: '=begin without matching =end'>
]
| <identifier>
.*? ^^ <?before \h* [
'='
[ 'cut' »
<.panic: 'Obsolete pod format, please use =begin/=end instead'> ]?
| \n ]>
|
[ \s || <.panic: 'Illegal pod directive'> ]
\N*
]
}
## Top-level rules
token comp_unit {
<.newpad>
<statementlist>
[ $ || <.panic: 'Confused'> ]
}
rule statementlist {
| $
| [<statement><.eat_terminator> ]*
}
token statement {
<!before <[\])}]> | $ >
[
| <statement_control>
| <EXPR> <.ws>
[
|| <?MARKED('endstmt')>
|| <statement_mod_cond> <statement_mod_loop>?
|| <statement_mod_loop>
]?
]
}
token eat_terminator {
| ';'
| <?MARKED('endstmt')>
| <?terminator>
| $
}
token xblock {
<EXPR> <.ws> <pblock>
}
token pblock {
| <.lambda>
<.newpad>
<signature>
<blockoid>
| <?[{]>
<.newpad>
<blockoid>
| <.panic: 'Missing block'>
}
token lambda { '->' | '<->' }
token block {
[ <?[{]> || <.panic: 'Missing block'> ]
<.newpad>
<blockoid>
}
token blockoid {
<.finishpad>
'{' ~ '}' <statementlist>
<?ENDSTMT>
}
token newpad { <?> }
token finishpad { <?> }
proto token terminator { <...> }
token terminator:sym<;> { <?[;]> }
token terminator:sym<}> { <?[}]> }
## Statement control
proto token statement_control { <...> }
token statement_control:sym<if> {
<sym> \s :s
<xblock>
[ 'elsif'\s <xblock> ]*
[ 'else'\s <else=.pblock> ]?
}
token statement_control:sym<unless> {
<sym> \s :s
<xblock>
[ <!before 'else'> || <.panic: 'unless does not take "else", please rewrite using "if"'> ]
}
token statement_control:sym<while> {
$<sym>=[while|until] \s :s
<xblock>
}
token statement_control:sym<repeat> {
<sym> \s :s
[
| $<wu>=[while|until]\s <xblock>
| <pblock> $<wu>=[while|until]\s <EXPR>
]
}
token statement_control:sym<for> {
<sym> \s :s
<xblock>
}
token statement_control:sym<CATCH> {
<sym> \s :s
<block>
}
token statement_control:sym<CONTROL> {
<sym> \s :s
<block>
}
proto token statement_prefix { <...> }
token statement_prefix:sym<INIT> { <sym> <blorst> }
token statement_prefix:sym<try> {
<sym>
<blorst>
}
token blorst {
\s <.ws> [ <?[{]> <block> | <statement> ]
}
## Statement modifiers
proto token statement_mod_cond { <...> }
token statement_mod_cond:sym<if> { <sym> :s <cond=.EXPR> }
token statement_mod_cond:sym<unless> { <sym> :s <cond=.EXPR> }
proto token statement_mod_loop { <...> }
token statement_mod_loop:sym<while> { <sym> :s <cond=.EXPR> }
token statement_mod_loop:sym<until> { <sym> :s <cond=.EXPR> }
## Terms
token term:sym<fatarrow> { <fatarrow> }
token term:sym<colonpair> { <colonpair> }
token term:sym<variable> { <variable> }
token term:sym<package_declarator> { <package_declarator> }
token term:sym<scope_declarator> { <scope_declarator> }
token term:sym<routine_declarator> { <routine_declarator> }
token term:sym<regex_declarator> { <regex_declarator> }
token term:sym<statement_prefix> { <statement_prefix> }
token term:sym<lambda> { <?lambda> <pblock> }
token fatarrow {
<key=.identifier> \h* '=>' <.ws> <val=.EXPR('i=')>
}
token colonpair {
':'
[
| $<not>='!' <identifier>
| <identifier> <circumfix>?
| <circumfix>
]
}
token variable {
| <sigil> <twigil>? <desigilname=.name>
| <sigil> <?[<[]> <postcircumfix>
| $<sigil>=['$'] $<desigilname>=[<[/_!]>]
}
token sigil { <[$@%&]> }
token twigil { <[*!?]> }
proto token package_declarator { <...> }
token package_declarator:sym<module> { <sym> <package_def> }
token package_declarator:sym<class> { $<sym>=[class|grammar] <package_def> }
rule package_def {
<name>
[ 'is' <parent=.name> ]?
[
|| ';' <comp_unit>
|| <?[{]> <block>
|| <.panic: 'Malformed package declaration'>
]
}
proto token scope_declarator { <...> }
token scope_declarator:sym<my> { <sym> <scoped('my')> }
token scope_declarator:sym<our> { <sym> <scoped('our')> }
token scope_declarator:sym<has> { <sym> <scoped('has')> }
rule scoped($*SCOPE) {
| <declarator>
| <typename>+ <declarator> # eventually <multi_declarator>
}
token typename { <name> }
token declarator {
| <variable_declarator>
| <routine_declarator>
}
token variable_declarator { <variable> }
proto token routine_declarator { <...> }
token routine_declarator:sym<sub> { <sym> <routine_def> }
token routine_declarator:sym<method> { <sym> <method_def> }
rule routine_def {
[ $<sigil>=['&'?]<deflongname> ]?
<.newpad>
[ '(' <signature> ')'
|| <.panic: 'Routine declaration requires a signature'> ]
<blockoid>
}
rule method_def {
<deflongname>?
<.newpad>
[ '(' <signature> ')'
|| <.panic: 'Routine declaration requires a signature'> ]
<blockoid>
}
token signature { [ [<.ws><parameter><.ws>] ** ',' ]? }
token parameter {
[ <typename> <.ws> ]* # <type_constraint>
[
| $<quant>=['*'] <param_var>
| [ <param_var> | <named_param> ] $<quant>=['?'|'!'|<?>]
]
<default_value>?
}
token param_var {
<sigil> <twigil>?
[ <name=.ident> | $<name>=[<[/!]>] ]
}
token named_param {
':' <param_var>
}
rule default_value { '=' <EXPR('i=')> }
rule regex_declarator {
[
| $<proto>=[proto] [regex|token|rule]
<deflongname>
[
|| '{' '<...>' '}'<?ENDSTMT>
|| <.panic: "Proto regex body must be <...>">
]
| $<sym>=[regex|token|rule]
<deflongname>
<.newpad>
[ '(' <signature> ')' ]?
{*} #= open
'{'<p6regex=.LANG('Regex','nibbler')>'}'<?ENDSTMT>
]
}
token dotty {
'.'
[ <longname=identifier>
| <?['"]> <quote>
[ <?[(]> || <.panic: "Quoted method name requires parenthesized arguments"> ]
]
[
| <?[(]> <args>
| ':' \s <args=.arglist>
]?
}
proto token term { <...> }
token term:sym<self> { <sym> » }
token term:sym<identifier> {
<identifier> <?[(]> <args>
}
token term:sym<name> {
<name> <args>?
}
token term:sym<pir::op> {
'pir::' $<op>=[\w+] <args>?
}
token args {
| '(' <arglist> ')'
}
token arglist {
<.ws>
[
| <EXPR('f=')>
| <?>
]
}
token term:sym<value> { <value> }
token value {
| <quote>
| <number>
}
token number {
$<sign>=[<[+\-]>?]
[ <dec_number> | <integer> ]
}
proto token quote { <...> }
token quote:sym<apos> { <?[']> <quote_EXPR: ':q'> }
token quote:sym<dblq> { <?["]> <quote_EXPR: ':qq'> }
token quote:sym<q> { 'q' >> <![(]> <.ws> <quote_EXPR: ':q'> }
token quote:sym<qq> { 'qq' >> <![(]> <.ws> <quote_EXPR: ':qq'> }
token quote:sym<Q> { 'Q' >> <![(]> <.ws> <quote_EXPR> }
token quote:sym<Q:PIR> { 'Q:PIR' <.ws> <quote_EXPR> }
token quote:sym</ /> {
'/'
<.newpad>
{*} #= open
<p6regex=.LANG('Regex','nibbler')>
'/'
}
token quote_escape:sym<$> { <?[$]> <?quotemod_check('s')> <variable> }
token quote_escape:sym<{ }> { <?[{]> <?quotemod_check('c')> <block> }
token quote_escape:sym<esc> { \\ e <?quotemod_check('b')> }
token circumfix:sym<( )> { '(' <.ws> <EXPR>? ')' }
token circumfix:sym<[ ]> { '[' <.ws> <EXPR>? ']' }
token circumfix:sym<ang> { <?[<]> <quote_EXPR: ':q', ':w'> }
token circumfix:sym<« »> { <?[«]> <quote_EXPR: ':qq', ':w'> }
token circumfix:sym<{ }> { <?[{]> <pblock> }
token circumfix:sym<sigil> { <sigil> '(' ~ ')' <semilist> }
rule semilist { <statement> }
## Operators
INIT {
NQP::Grammar.O(':prec<y=>, :assoc<unary>', '%methodop');
NQP::Grammar.O(':prec<x=>, :assoc<unary>', '%autoincrement');
NQP::Grammar.O(':prec<w=>, :assoc<left>', '%exponentiation');
NQP::Grammar.O(':prec<v=>, :assoc<unary>', '%symbolic_unary');
NQP::Grammar.O(':prec<u=>, :assoc<left>', '%multiplicative');
NQP::Grammar.O(':prec<t=>, :assoc<left>', '%additive');
NQP::Grammar.O(':prec<r=>, :assoc<left>', '%concatenation');
NQP::Grammar.O(':prec<m=>, :assoc<left>', '%relational');
NQP::Grammar.O(':prec<l=>, :assoc<left>', '%tight_and');
NQP::Grammar.O(':prec<k=>, :assoc<left>', '%tight_or');
NQP::Grammar.O(':prec<j=>, :assoc<right>', '%conditional');
NQP::Grammar.O(':prec<i=>, :assoc<right>', '%assignment');
NQP::Grammar.O(':prec<g=>, :assoc<list>, :nextterm<nulltermish>', '%comma');
NQP::Grammar.O(':prec<f=>, :assoc<list>', '%list_infix');
NQP::Grammar.O(':prec<e=>, :assoc<unary>', '%list_prefix');
}
token infixish { <!infixstopper> <OPER=infix> }
token infixstopper { <?lambda> }
token postcircumfix:sym<[ ]> {
'[' <.ws> <EXPR> ']'
<O('%methodop')>
}
token postcircumfix:sym<{ }> {
'{' <.ws> <EXPR> '}'
<O('%methodop')>
}
token postcircumfix:sym<ang> {
<?[<]> <quote_EXPR: ':q'>
<O('%methodop')>
}
token postcircumfix:sym<( )> {
'(' <.ws> <arglist> ')'
<O('%methodop')>
}
token postfix:sym<.> { <dotty> <O('%methodop')> }
token prefix:sym<++> { <sym> <O('%autoincrement, :pirop<inc>')> }
token prefix:sym<--> { <sym> <O('%autoincrement, :pirop<dec>')> }
# see Actions.pm for postfix:<++> and postfix:<-->
token postfix:sym<++> { <sym> <O('%autoincrement')> }
token postfix:sym<--> { <sym> <O('%autoincrement')> }
token infix:sym<**> { <sym> <O('%exponentiation, :pirop<pow>')> }
token prefix:sym<+> { <sym> <O('%symbolic_unary, :pirop<set N*>')> }
token prefix:sym<~> { <sym> <O('%symbolic_unary, :pirop<set S*>')> }
token prefix:sym<-> { <sym> <![>]> <!number> <O('%symbolic_unary, :pirop<neg>')> }
token prefix:sym<?> { <sym> <O('%symbolic_unary, :pirop<istrue>')> }
token prefix:sym<!> { <sym> <O('%symbolic_unary, :pirop<isfalse>')> }
token prefix:sym<|> { <sym> <O('%symbolic_unary')> }
token infix:sym<*> { <sym> <O('%multiplicative, :pirop<mul>')> }
token infix:sym</> { <sym> <O('%multiplicative, :pirop<div>')> }
token infix:sym<%> { <sym> <O('%multiplicative, :pirop<mod>')> }
token infix:sym<+> { <sym> <O('%additive, :pirop<add>')> }
token infix:sym<-> { <sym> <O('%additive, :pirop<sub>')> }
token infix:sym<~> { <sym> <O('%concatenation , :pirop<concat>')> }
token infix:sym«==» { <sym> <O('%relational, :pirop<iseq INn>')> }
token infix:sym«!=» { <sym> <O('%relational, :pirop<isne INn>')> }
token infix:sym«<=» { <sym> <O('%relational, :pirop<isle INn>')> }
token infix:sym«>=» { <sym> <O('%relational, :pirop<isge INn>')> }
token infix:sym«<» { <sym> <O('%relational, :pirop<islt INn>')> }
token infix:sym«>» { <sym> <O('%relational, :pirop<isgt INn>')> }
token infix:sym«eq» { <sym> <O('%relational, :pirop<iseq ISs>')> }
token infix:sym«ne» { <sym> <O('%relational, :pirop<isne ISs>')> }
token infix:sym«le» { <sym> <O('%relational, :pirop<isle ISs>')> }
token infix:sym«ge» { <sym> <O('%relational, :pirop<isge ISs>')> }
token infix:sym«lt» { <sym> <O('%relational, :pirop<islt ISs>')> }
token infix:sym«gt» { <sym> <O('%relational, :pirop<isgt ISs>')> }
token infix:sym«=:=» { <sym> <O('%relational, :pirop<issame>')> }
token infix:sym<~~> { <sym> <O('%relational, :reducecheck<smartmatch>')> }
token infix:sym<&&> { <sym> <O('%tight_and, :pasttype<if>')> }
token infix:sym<||> { <sym> <O('%tight_or, :pasttype<unless>')> }
token infix:sym<//> { <sym> <O('%tight_or, :pasttype<def_or>')> }
token infix:sym<?? !!> {
'??'
<.ws>
<EXPR('i=')>
'!!'
<O('%conditional, :reducecheck<ternary>, :pasttype<if>')>
}
token infix:sym<=> {
<sym> <.panic: 'Assignment ("=") not supported in NQP, use ":=" instead'>
}
token infix:sym<:=> { <sym> <O('%assignment, :pasttype<bind>')> }
token infix:sym<::=> { <sym> <O('%assignment, :pasttype<bind>')> }
token infix:sym<,> { <sym> <O('%comma, :pasttype<list>')> }
token prefix:sym<return> { <sym> \s <O('%list_prefix, :pasttype<return>')> }
token prefix:sym<make> { <sym> \s <O('%list_prefix')> }
token term:sym<last> { <sym> }
token term:sym<next> { <sym> }
token term:sym<redo> { <sym> }
method smartmatch($/) {
# swap rhs into invocant position
my $t := $/[0]; $/[0] := $/[1]; $/[1] := $t;
}
grammar NQP::Regex is Regex::P6Regex::Grammar {
token metachar:sym<:my> {
':' <?before 'my'> <statement=.LANG('MAIN', 'statement')> <.ws> ';'
}
token metachar:sym<{ }> {
<?[{]> <codeblock>
}
token metachar:sym<nqpvar> {
<?[$@]> <var=.LANG('MAIN', 'variable')>
}
token assertion:sym<{ }> {
<?[{]> <codeblock>
}
token assertion:sym<?{ }> {
$<zw>=[ <[?!]> <?before '{'> ] <codeblock>
}
token assertion:sym<name> {
$<longname>=[\w+]
[
| <?before '>'>
| '=' <assertion>
| ':' <arglist>
| '(' <arglist=.LANG('MAIN','arglist')> ')'
| <.normspace> <nibbler>
]?
}
token assertion:sym<var> {
<?[$@]> <var=.LANG('MAIN', 'variable')>
}
token codeblock {
<block=.LANG('MAIN','pblock')>
}
}