-
-
Notifications
You must be signed in to change notification settings - Fork 376
/
Grammar.nqp
5505 lines (4945 loc) · 199 KB
/
Grammar.nqp
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use QRegex;
use NQPP6QRegex;
use NQPP5QRegex;
use Perl6::Actions;
use Perl6::World;
use Perl6::Pod;
role startstops[$start, $stop1, $stop2] {
token starter { $start }
token stopper { $stop1 | $stop2 }
}
role startstop[$start, $stop] {
token starter { $start }
token stopper { $stop }
}
role stop[$stop] {
token starter { <!> }
token stopper { $stop }
}
# This role captures things that STD factors out from any individual grammar,
# but that don't make sense to go in HLL::Grammar.
role STD {
token opener {
<[
\x0028 \x003C \x005B \x007B \x00AB \x0F3A \x0F3C \x169B \x2018 \x201A \x201B
\x201C \x201E \x201F \x2039 \x2045 \x207D \x208D \x2208 \x2209 \x220A \x2215
\x223C \x2243 \x2252 \x2254 \x2264 \x2266 \x2268 \x226A \x226E \x2270 \x2272
\x2274 \x2276 \x2278 \x227A \x227C \x227E \x2280 \x2282 \x2284 \x2286 \x2288
\x228A \x228F \x2291 \x2298 \x22A2 \x22A6 \x22A8 \x22A9 \x22AB \x22B0 \x22B2
\x22B4 \x22B6 \x22C9 \x22CB \x22D0 \x22D6 \x22D8 \x22DA \x22DC \x22DE \x22E0
\x22E2 \x22E4 \x22E6 \x22E8 \x22EA \x22EC \x22F0 \x22F2 \x22F3 \x22F4 \x22F6
\x22F7 \x2308 \x230A \x2329 \x23B4 \x2768 \x276A \x276C \x276E \x2770 \x2772
\x2774 \x27C3 \x27C5 \x27D5 \x27DD \x27E2 \x27E4 \x27E6 \x27E8 \x27EA \x2983
\x2985 \x2987 \x2989 \x298B \x298D \x298F \x2991 \x2993 \x2995 \x2997 \x29C0
\x29C4 \x29CF \x29D1 \x29D4 \x29D8 \x29DA \x29F8 \x29FC \x2A2B \x2A2D \x2A34
\x2A3C \x2A64 \x2A79 \x2A7D \x2A7F \x2A81 \x2A83 \x2A8B \x2A91 \x2A93 \x2A95
\x2A97 \x2A99 \x2A9B \x2AA1 \x2AA6 \x2AA8 \x2AAA \x2AAC \x2AAF \x2AB3 \x2ABB
\x2ABD \x2ABF \x2AC1 \x2AC3 \x2AC5 \x2ACD \x2ACF \x2AD1 \x2AD3 \x2AD5 \x2AEC
\x2AF7 \x2AF9 \x2E02 \x2E04 \x2E09 \x2E0C \x2E1C \x2E20 \x2E28 \x3008 \x300A
\x300C \x300E \x3010 \x3014 \x3016 \x3018 \x301A \x301D \xFD3E \xFE17 \xFE35
\xFE37 \xFE39 \xFE3B \xFE3D \xFE3F \xFE41 \xFE43 \xFE47 \xFE59 \xFE5B \xFE5D
\xFF08 \xFF1C \xFF3B \xFF5B \xFF5F \xFF62
]>
}
method balanced($start, $stop) {
if nqp::istype($stop, VMArray) {
self.HOW.mixin(self, startstops.HOW.curry(startstops, $start, $stop[0], $stop[1]));
}
else {
self.HOW.mixin(self, startstop.HOW.curry(startstop, $start, $stop));
}
}
method unbalanced($stop) {
self.HOW.mixin(self, stop.HOW.curry(stop, $stop));
}
token starter { <!> }
token stopper { <!> }
my %quote_lang_cache := nqp::hash();
method quote_lang($l, $start, $stop, @base_tweaks?, @extra_tweaks?) {
sub lang_key() {
my $stopstr := nqp::istype($stop,VMArray) ?? nqp::join(' ',$stop) !! $stop;
my @keybits := [$l.HOW.name($l), $start, $stopstr];
for @base_tweaks {
@keybits.push($_);
}
for @extra_tweaks {
if $_[0] eq 'to' {
return 'NOCACHE';
}
@keybits.push($_[0] ~ '=' ~ $_[1]);
}
nqp::join("\0", @keybits)
}
sub con_lang() {
my $lang := $l.'!cursor_init'(self.orig(), :p(self.pos()), :shared(self.'!shared'()));
$lang.clone_braid_from(self);
for @base_tweaks {
$lang := $lang."tweak_$_"(1);
}
for @extra_tweaks {
my $t := $_[0];
if nqp::can($lang, "tweak_$t") {
$lang := $lang."tweak_$t"($_[1]);
}
else {
self.sorry("Unrecognized adverb: :$t");
}
}
for self.slangs {
if nqp::istype($lang, $_.value) {
$lang.set_actions(self.slang_actions($_.key));
last;
}
}
$lang.set_pragma("STOPPER",$stop);
nqp::istype($stop,VMArray) ||
$start ne $stop ?? $lang.balanced($start, $stop)
!! $lang.unbalanced($stop);
}
# Get language from cache or derive it.
my $key := lang_key();
my $quote_lang := nqp::existskey(%quote_lang_cache, $key) && $key ne 'NOCACHE'
?? %quote_lang_cache{$key}
!! (%quote_lang_cache{$key} := con_lang());
$quote_lang.set_package(self.package);
$quote_lang;
}
token babble($l, @base_tweaks?) {
:my @extra_tweaks;
[ <quotepair> <.ws>
{
my $kv := $<quotepair>[-1].ast;
my $k := $kv.named;
if nqp::istype($kv, QAST::Stmts) || nqp::istype($kv, QAST::Stmt) && +@($kv) == 1 {
$kv := $kv[0];
}
my $v := nqp::istype($kv, QAST::IVal)
?? $kv.value
!! $kv.has_compile_time_value
?? $kv.compile_time_value
!! self.panic("Invalid adverb value for " ~ $<quotepair>[-1].Str);
nqp::push(@extra_tweaks, [$k, $v]);
}
]*
$<B>=[<?before .>]
{
# Work out the delimiters.
my $c := $/;
my @delims := $c.peek_delimiters($c.target, $c.pos);
my $start := @delims[0];
my $stop := @delims[1];
# Get the language.
my $lang := self.quote_lang($l, $start, $stop, @base_tweaks, @extra_tweaks);
$<B>.make([$lang, $start, $stop]);
}
}
my @herestub_queue;
my class Herestub {
has $!delim;
has $!orignode;
has $!grammar;
method delim() { $!delim }
method orignode() { $!orignode }
method grammar() { $!grammar }
}
role herestop {
token starter { <!> }
token stopper { ^^ {} $<ws>=(\h*) $*DELIM \h* $$ [\r\n | \v]? }
method parsing_heredoc() { 1 }
}
method heredoc () {
my $actions := self.actions;
if @herestub_queue {
my $here := self.'!cursor_start_cur'();
$here.'!cursor_pos'(self.pos);
while @herestub_queue {
my $herestub := nqp::shift(@herestub_queue);
my $*DELIM := $herestub.delim;
my $lang := $herestub.grammar.HOW.mixin($herestub.grammar, herestop);
for self.slangs {
if nqp::istype($lang, $_.value) {
$lang.set_actions(self.slang_actions($_.key));
last;
}
}
my $doc := $here.nibble($lang);
if $doc {
# Match stopper.
my $stop := $lang.'!cursor_init'(self.orig(), :p($doc.pos), :shared(self.'!shared'())).stopper();
$stop.clone_braid_from(self);
unless $stop {
self.panic("Ending delimiter $*DELIM not found");
}
$here.'!cursor_pos'($stop.pos);
# Get it trimmed and AST updated.
$actions.trim_heredoc(self, $doc, $stop, $herestub.orignode.MATCH.ast);
}
else {
self.panic("Ending delimiter $*DELIM not found");
}
}
$here.'!cursor_pass'($here.pos);
$here.set_actions($actions);
$here
}
else {
self
}
}
token cheat_heredoc {
<?{ +@herestub_queue }> \h* <[ ; } ]> \h* <?before \n | '#'> <.ws> <?MARKER('endstmt')>
}
method queue_heredoc($delim, $grammar) {
nqp::ifnull(@herestub_queue, @herestub_queue := []);
nqp::push(@herestub_queue, Herestub.new(:$delim, :$grammar, :orignode(self)));
return self;
}
token quibble($l, *@base_tweaks) {
:my $lang;
:my $start;
:my $stop;
<babble($l, @base_tweaks)>
{ my $B := $<babble><B>.ast; $lang := $B[0]; $start := $B[1]; $stop := $B[2]; }
$start <nibble($lang)> [ $stop || { $/.typed_panic('X::Comp::AdHoc', payload => "Couldn't find terminator $stop (corresponding $start was at line {HLL::Compiler.lineof($<babble><B>.orig(), $<babble><B>.from(), :cache(1))})", expected => [$stop] ) } ]
{
nqp::can($lang, 'herelang') && self.queue_heredoc(
$*W.nibble_to_str($/, $<nibble>.ast[1], -> { "Stopper '" ~ $<nibble> ~ "' too complex for heredoc" }),
$lang.herelang)
}
}
# Note, $lang must carry its own actions by the time we call this.
method nibble($lang) {
$lang.'!cursor_init'(self.orig(), :p(self.pos()), :shared(self.'!shared'())).nibbler().set_braid_from(self)
}
token obsbrace { <.obs('curlies around escape argument','square brackets')> }
method FAILGOAL($goal, $dba?) {
my $stopper;
unless $dba {
$dba := nqp::getcodename(nqp::callercode());
# Handle special case to conceal variable name leaked by core grammar
if ~$goal eq '$stopper ' {
my $ch := $dba ~~ /[post]?circumfix\:sym[\<|\«]\S+\s+(\S+)[\>|\»]/;
$ch := ~$ch[0];
if nqp::chars($ch) {
$stopper := "'" ~ $ch ~ "'";
}
}
}
# core grammar also has a penchant for sending us trailing .ws contents
$stopper := $stopper // $goal;
$stopper := $stopper ~~ /(.*\S)\s*/;
$stopper := ~$stopper[0];
self.typed_panic('X::Comp::FailGoal', :$dba, :goal($stopper),
:line-real(HLL::Compiler.lineof(self.orig(), self.from(),
:cache(1))));
}
method panic(*@args) {
self.typed_panic('X::Comp::AdHoc', payload => nqp::join('', @args))
}
method sorry(*@args) {
self.typed_sorry('X::Comp::AdHoc', payload => nqp::join('', @args))
}
method worry(*@args) {
self.typed_worry('X::Comp::AdHoc', payload => nqp::join('', @args))
}
method typed_panic($type_str, *%opts) {
$*W.throw(self.MATCH(), nqp::split('::', $type_str), |%opts);
}
method typed_sorry($type_str, *%opts) {
if +@*SORROWS + 1 == $*SORRY_LIMIT {
$*W.throw(self.MATCH(), nqp::split('::', $type_str), |%opts);
}
else {
@*SORROWS.push($*W.typed_exception(self.MATCH(), nqp::split('::', $type_str), |%opts));
}
self
}
method typed_worry($type_str, *%opts) {
unless self.pragma('no-worries') {
self.pragma('fatal')
?? self.typed_sorry($type_str, |%opts)
!! @*WORRIES.push($*W.typed_exception(
self.MATCH(), nqp::split('::', $type_str), |%opts));
}
self
}
method security($payload) {
self.typed_panic('X::SecurityPolicy::Eval', :$payload);
}
method malformed($what) {
self.typed_panic('X::Syntax::Malformed', :$what);
}
method missing_block($borg, $has_mystery) {
my $marked := self.MARKED('ws');
my $pos := $marked ?? $marked.from !! self.pos;
if $borg<block> {
self.'!clear_highwater'();
self.'!cursor_pos'($borg<block>.pos);
self.typed_sorry('X::Syntax::BlockGobbled', what => ($borg<name> // ''));
self.'!cursor_pos'($pos);
self.missing("block (apparently claimed by " ~ ($borg<name> ?? "'" ~ $borg<name> ~ "'" !! "expression") ~ ")");
} elsif $pos > 0 && nqp::eqat(self.orig(), '}', $pos - 1) {
self.missing("block (whitespace needed before curlies taken as a hash subscript?)");
} elsif $has_mystery {
self.missing("block (taken by some undeclared routine?)");
} else {
self.missing("block");
}
}
method missing($what) {
self.typed_panic('X::Syntax::Missing', :$what);
}
method NYI($feature) {
self.typed_panic('X::Comp::NYI', :$feature)
}
token experimental($feature) {
<?{ try $*W.find_symbol(['EXPERIMENTAL-' ~ nqp::uc($feature)]) }>
|| <.typed_panic('X::Experimental', :$feature)>
}
method EXPR_nonassoc($cur, $left, $right) {
self.typed_panic('X::Syntax::NonAssociative', :left(~$left), :right(~$right));
}
method EXPR_nonlistassoc($cur, $left, $right) {
self.typed_panic('X::Syntax::NonListAssociative', :left(~$left), :right(~$right));
}
# "when" arg assumes more things will become obsolete after Perl 6 comes out...
method obs($old, $new, $when = 'in Perl 6') {
$*W.throw(self.MATCH(), ['X', 'Obsolete'],
old => $old,
replacement => $new,
when => $when,
);
}
method obsvar($name) {
$*W.throw(self.MATCH(), ['X', 'Syntax', 'Perl5Var'], :$name);
}
method sorryobs($old, $new, $when = 'in Perl 6') {
$*W.throw(self.MATCH(), ['X', 'Obsolete'],
old => $old,
replacement => $new,
when => $when,
);
}
method worryobs($old, $new, $when = 'in Perl 6') {
self.typed_worry('X::Obsolete',
old => $old,
replacement => $new,
when => $when,
);
}
method dupprefix($prefixes) {
self.typed_panic('X::Syntax::DuplicatedPrefix', :$prefixes);
}
method mark_variable_used($name) {
my $lex := $*W.cur_lexpad();
my %sym := $lex.symbol($name);
if %sym {
%sym<used> := 1;
}
else {
# Add mention-only record (used to poison outer
# usages and disambiguate hashes/blocks by use of
# $_ when $*IMPLICIT is in force).
my $au := $lex.ann('also_uses');
$lex.annotate('also_uses', $au := {}) unless $au;
$au{$name} := 1;
}
}
method check_variable($var) {
my $varast := $var.ast;
if nqp::istype($varast, QAST::Op) && $varast.op eq 'ifnull' {
$varast := $varast[0];
}
if !$*IN_DECL && nqp::istype($varast, QAST::Var) && $varast.scope eq 'lexical' {
my $name := $varast.name;
if $name ne '%_' && $name ne '@_' && !$*W.is_lexical($name) {
my $sigil := $var<sigil> || nqp::substr($name,0,1);
if $sigil ne '&' {
if !$*STRICT {
$*W.auto_declare_var($var);
}
else {
my @suggestions := $*W.suggest_lexicals($name);
my $package := self.package;
if nqp::can($package.HOW, 'get_attribute_for_usage') {
my $sigil := nqp::substr($name, 0, 1);
my $twigil := nqp::concat($sigil, '!');
my $basename := nqp::substr($name, 1, nqp::chars($name) - 1);
my $attrname := nqp::concat($twigil, $basename);
my $attribute := $package.HOW.get_attribute_for_usage($package, $attrname);
nqp::push(@suggestions, $attrname);
CATCH {}
}
$*W.throw($var, ['X', 'Undeclared'], symbol => $name, suggestions => @suggestions, precursor => '1');
}
}
else {
$var.add_mystery($name, $var.to, 'var');
}
}
else {
self.mark_variable_used($name);
}
}
if !$*IN_DECL && nqp::istype($varast, QAST::Op) && $varast.name eq '&DYNAMIC' {
my $lex := $*W.cur_lexpad();
if nqp::istype($varast[0], QAST::Want) && nqp::istype($varast[0][2], QAST::SVal) {
my $au := $lex.ann('also_uses');
$lex.annotate('also_uses', $au := {}) unless $au;
$au{$varast[0][2].value} := 1;
}
}
self
}
token RESTRICTED {
:my $r := $*RESTRICTED || "(not)";
[ <?{ $*RESTRICTED }> [ $ || <.security($*RESTRICTED)> ] ]?
<!>
}
}
grammar Perl6::Grammar is HLL::Grammar does STD {
my $sc_id := 0;
method TOP() {
# Language braid.
my $*LANG := self;
my $*LEAF := self; # the leaf cursor, workaround for when we can't pass via $/ into world
self.define_slang('MAIN', self.WHAT, self.actions);
self.define_slang('Quote', Perl6::QGrammar, Perl6::QActions);
self.define_slang('Regex', Perl6::RegexGrammar, Perl6::RegexActions);
self.define_slang('P5Regex', Perl6::P5RegexGrammar, Perl6::P5RegexActions);
# Old language braid, going away eventually
# XXX TODO: if these are going out, be sure to make similar change
# to src/perl6-debug.nqp and ensure it still works.
my %*LANG;
%*LANG<Regex> := Perl6::RegexGrammar;
%*LANG<Regex-actions> := Perl6::RegexActions;
%*LANG<P5Regex> := Perl6::P5RegexGrammar;
%*LANG<P5Regex-actions> := Perl6::P5RegexActions;
%*LANG<Quote> := Perl6::QGrammar;
%*LANG<Quote-actions> := Perl6::QActions;
%*LANG<MAIN> := self.WHAT;
%*LANG<MAIN-actions> := self.actions;
# We could start out TOP with a fatalizing language in self, conceivably...
my $*FATAL := self.pragma('fatal'); # also set if somebody calls 'use fatal' in mainline
# A cacheable false dynvar value.
my $*WANTEDOUTERBLOCK := 0;
# Package declarator to meta-package mapping. Starts pretty much empty;
# we get the mappings either imported or supplied by the setting. One
# issue is that we may have no setting to provide them, e.g. when we
# compile the setting, but it still wants some kinda package. We just
# fudge in knowhow for that.
self.set_how('knowhow', nqp::knowhow());
self.set_how('package', nqp::knowhow());
# Will we use the result of this? (Yes for EVAL and REPL).
my $*NEED_RESULT := nqp::existskey(%*COMPILING<%?OPTIONS>, 'outer_ctx');
# Symbol table and serialization context builder - keeps track of
# objects that cross the compile-time/run-time boundary that are
# associated with this compilation unit.
my $file := nqp::getlexdyn('$?FILES');
my $source_id := nqp::sha1($file ~ (
nqp::defined(%*COMPILING<%?OPTIONS><outer_ctx>)
?? self.target() ~ $sc_id++
!! self.target()));
my $outer_world := nqp::getlexdyn('$*W');
my $is_nested := (
$outer_world
&& nqp::defined(%*COMPILING<%?OPTIONS><outer_ctx>)
&& $outer_world.is_precompilation_mode()
);
my $*W := $is_nested
?? $outer_world.create_nested()
!! nqp::isnull($file)
?? Perl6::World.new(:handle($source_id))
!! Perl6::World.new(:handle($source_id), :description($file));
unless $is_nested {
$*W.add_initializations();
}
my $cursor := self.comp_unit;
$*W.pop_lexpad(); # UNIT
$*W.pop_lexpad(); # UNIT_OUTER
$cursor;
}
## Lexer stuff
token apostrophe {
<[ ' \- ]>
}
token identifier {
<.ident> [ <.apostrophe> <.ident> ]*
}
token name {
[
| <identifier> <morename>*
| <morename>+
]
}
token morename {
:my $*QSIGIL := '';
'::'
[
|| <?before '(' | <.alpha> >
[
| <identifier>
| :dba('indirect name') '(' ~ ')' [ <.ws> <EXPR> ]
]
|| <?before '::'> <.typed_panic: "X::Syntax::Name::Null">
|| $<bad>=[<.sigil><.identifier>] { my str $b := $<bad>; self.malformed("lookup of ::$b; please use ::('$b'), ::\{'$b'\}, or ::<$b>") }
]?
}
token longname {
<name> {} [ <?before ':' <.+alpha+[\< \[ \« ]>> <!RESTRICTED> <colonpair> ]*
}
token deflongname {
:dba('new name to be defined')
<name> <colonpair>*
}
token subshortname {
<desigilname>
}
token sublongname {
<subshortname> <sigterm>?
}
token deftermnow { <defterm> }
token defterm { # XXX this is probably too general
:dba('new term to be defined')
<identifier>
[
| <colonpair>+
{
if $<colonpair>[0]<coloncircumfix> -> $cf {
my $category := $<identifier>.Str;
my $opname := $cf<circumfix>
?? $*W.colonpair_nibble_to_str($/, $cf<circumfix><nibble>)
!! '';
my $canname := $category ~ $*W.canonicalize_pair('sym', $opname);
my $termname := $category ~ $*W.canonicalize_pair('', $opname);
$/.add_categorical($category, $opname, $canname, $termname, :defterm);
}
}
| <?>
]
}
token module_name {
<longname>
[ <?[[]> :dba('generic role') '[' ~ ']' <arglist> ]?
}
token end_keyword {
» <!before <.[ \( \\ ' \- ]> || \h* '=>'>
}
token end_prefix {
<.end_keyword> \s*
}
token spacey { <?[\s#]> }
token kok {
<.end_keyword>
[
|| <?before <.[ \s \# ]> > <.ws>
|| <?{
my $n := nqp::substr(self.orig, self.from, self.pos - self.from);
$*W.is_name([$n]) || $*W.is_name(['&' ~ $n])
?? False
!! self.panic("Whitespace required after keyword '$n'")
}>
]
}
token tok {
<.end_keyword>
<!{
my $n := nqp::substr(self.orig, self.from, self.pos - self.from);
$*W.is_name([$n]) || $*W.is_name(['&' ~ $n])
}>
}
token ENDSTMT {
[
| \h* $$ <.ws> <?MARKER('endstmt')>
| <.unv>? $$ <.ws> <?MARKER('endstmt')>
]?
}
# ws is highly performance sensitive. So, we check if we already marked it
# at this point with a simple method, and only if that is not the case do
# we bother doing any pattern matching.
method ws() {
self.MARKED('ws') ?? self !! self._ws()
}
token _ws {
:my $old_highexpect := self.'!fresh_highexpect'();
:dba('whitespace')
<!ww>
[
| [\r\n || \v] <.heredoc>
| <.unv>
| <.unsp>
]*
<?MARKER('ws')>
:my $stub := self.'!fresh_highexpect'();
}
token unsp {
\\ <?before \s | '#'>
:dba('unspace')
[
| <.vws>
| <.unv>
| <.unsp>
]*
}
token vws {
:dba('vertical whitespace')
[
[
| \v
| '<<<<<<<' {} <?before [.*? \v '=======']: .*? \v '>>>>>>>' > <.sorry: 'Found a version control conflict marker'> \V* \v
| '=======' {} .*? \v '>>>>>>>' \V* \v # ignore second half
]
]+
}
token unv {
:dba('horizontal whitespace')
[
| \h+
| \h* <.comment>
| <?before \h* '=' [ \w | '\\'] > ^^ <.pod_content_toplevel>
]
}
proto token comment { <...> }
token comment:sym<#> {
'#' {} \N*
}
token comment:sym<#`(...)> {
'#`' <?opener> {}
[ <.quibble(self.slang_grammar('Quote'))> || <.typed_panic: 'X::Syntax::Comment::Embedded'> ]
}
token comment:sym<#|(...)> {
'#|' <?opener> <attachment=.quibble(self.slang_grammar('Quote'))>
{
unless $*POD_BLOCKS_SEEN{ self.from() } {
$*POD_BLOCKS_SEEN{ self.from() } := 1;
if $*DECLARATOR_DOCS eq '' {
$*DECLARATOR_DOCS := $<attachment><nibble>;
} else {
$*DECLARATOR_DOCS := nqp::concat($*DECLARATOR_DOCS, nqp::concat("\n", $<attachment><nibble>));
}
}
}
}
token comment:sym<#|> {
'#|' \h+ $<attachment>=[\N*]
{
unless $*POD_BLOCKS_SEEN{ self.from() } {
$*POD_BLOCKS_SEEN{ self.from() } := 1;
if $*DECLARATOR_DOCS eq '' {
$*DECLARATOR_DOCS := $<attachment>;
} else {
$*DECLARATOR_DOCS := nqp::concat($*DECLARATOR_DOCS, nqp::concat("\n", $<attachment>));
}
}
}
}
token comment:sym<#=(...)> {
'#=' <?opener> <attachment=.quibble(self.slang_grammar('Quote'))>
{
self.attach_trailing_docs(~$<attachment><nibble>);
}
}
token comment:sym<#=> {
'#=' \h+ $<attachment>=[\N*]
{
self.attach_trailing_docs(~$<attachment>);
}
}
method attach_leading_docs() {
if ~$*DOC ne '' {
my $cont := Perl6::Pod::serialize_aos(
[Perl6::Pod::normalize_text(~$*DOC)]
).compile_time_value;
my $block := $*W.add_constant(
'Pod::Block::Declarator', 'type_new',
:nocache, :leading([$cont]),
);
$*POD_BLOCK := $block.compile_time_value;
$*POD_BLOCKS.push($*POD_BLOCK);
}
self
}
method attach_trailing_docs($doc) {
unless $*POD_BLOCKS_SEEN{ self.from() } {
$*POD_BLOCKS_SEEN{ self.from() } := 1;
my $pod_block;
if $doc ne '' {
my $cont := Perl6::Pod::serialize_aos(
[Perl6::Pod::normalize_text($doc)]
).compile_time_value;
my $block := $*W.add_constant(
'Pod::Block::Declarator', 'type_new',
:nocache, :trailing([$cont]),
);
$pod_block := $block.compile_time_value;
}
unless $*PRECEDING_DECL =:= Mu {
Perl6::Pod::document(self.MATCH, $*PRECEDING_DECL, $pod_block, :trailing);
}
}
}
token pod_content_toplevel {
<pod_block>
}
proto token pod_content { <...> }
token pod_content:sym<block> {
<pod_newline>*
<pod_block>
<pod_newline>*
}
# any number of paragraphs of text
token pod_content:sym<text> {
<pod_newline>*
<pod_textcontent>+ % <pod_newline>+
<pod_newline>*
}
# not a block, just a directive
token pod_content:sym<config> {
<pod_newline>*
^^ \h* '=config' \h+ $<type>=\S+ <pod_configuration>
<pod_newline>+
}
proto token pod_textcontent { <...> }
# text not being code
token pod_textcontent:sym<regular> {
$<spaces>=[ \h* ]
<?{ $*POD_IN_CODE_BLOCK
|| !$*ALLOW_INLINE_CODE
|| ($<spaces>.to - $<spaces>.from) <= $*VMARGIN }>
$<text> = [
\h* <!before '=' \w> <pod_string> [ <pod_newline> | $ ]
] +
}
token pod_textcontent:sym<code> {
$<spaces>=[ \h* ]
<?{ !$*POD_IN_CODE_BLOCK
&& $*ALLOW_INLINE_CODE
&& ($<spaces>.to - $<spaces>.from) > $*VMARGIN }>
$<text> = [
[<!before '=' \w> \N+]+ % [<pod_newline>+ $<spaces>]
]
}
token pod_formatting_code {
:my $*POD_ALLOW_FCODES := nqp::getlexdyn('$*POD_ALLOW_FCODES');
:my $*POD_IN_FORMATTINGCODE := nqp::getlexdyn('$*POD_IN_FORMATTINGCODE');
:my $*POD_ANGLE_COUNT := nqp::getlexdyn('$*POD_ANGLE_COUNT');
<?{ $*POD_ALLOW_FCODES }>
:my $endtag;
<code=[A..Z]>
$<begin-tag>=['<'+ <![<]> | '«'] { $*POD_IN_FORMATTINGCODE := 1 }
<?{
my $codenum := nqp::ord($<code>.Str) - nqp::ord("A");
if !($*POD_ALLOW_FCODES +& (2 ** $codenum)) {
0
} elsif ~$<begin-tag> eq '«' {
$endtag := "»";
$*POD_ANGLE_COUNT := -1;
1
} else {
my int $ct := nqp::chars($<begin-tag>);
$endtag := nqp::x(">", $ct);
my $rv := $*POD_ANGLE_COUNT <= 0 || $*POD_ANGLE_COUNT >= $ct;
$*POD_ANGLE_COUNT := $ct;
$rv;
}
}>
{
if $<code>.Str eq "V" || $<code>.Str eq "C" {
$*POD_ALLOW_FCODES := 0;
}
}
[ <!{$<code> eq 'E'}>
$<contents>=[
<!before $endtag>
[ <?{$<code> ne 'L' && $<code> ne 'D' && $<code> ne 'X' }> || <!before \s* \| > ]
<pod_string_character>
]*
]?
[
| <?{$<code> eq 'L'}> \s* \| \s* $<meta>=[<!before $endtag>.]+
| <?{$<code> eq 'X'}> \s* \| \s* ( [$<meta>=[<!before $endtag | <[,;]> >.]+] +%% \, ) +%% \;
| <?{$<code> eq 'D'}> \s* \| \s* [$<meta>=[<!before $endtag | \; >.]+] +%% \;
| <?{$<code> eq 'E'}> ( <integer> | $<uni_name>=<[A..Z\s]>+ <![a..z]> || $<html_ref>=<[A..Za..z]>+ ) +%% \;
]?
[ $endtag || <.worry: "Pod formatting code $<code> missing endtag '$endtag'."> ]
}
token pod_balanced_braces {
<?{ $*POD_IN_FORMATTINGCODE }>
:my $endtag;
[
$<braces>=[
|| '<'+ <![<]>
|| '>'+ <![>]>
]
<?{ nqp::chars($<braces>) < $*POD_ANGLE_COUNT || $*POD_ANGLE_COUNT < 0 }>
||
<?{ $*POD_ANGLE_COUNT >= 1 }>
$<start>=['<'+] <![<]>
<?{ nqp::chars($<start>) == $*POD_ANGLE_COUNT || $*POD_ANGLE_COUNT < 0 }>
{
$endtag := nqp::x(">", nqp::chars($<start>));
}
$<contents>=[ <pod_string_character>*?]
<!after '>'> $<endtag>=[$endtag]
]
}
token pod_string {
<pod_string_character>+
}
token pod_string_character {
<pod_balanced_braces> || <pod_formatting_code> || $<char>=[ \N || [
<?{ $*POD_IN_FORMATTINGCODE }> \n [
<?{ $*POD_DELIMITED_CODE_BLOCK }> <!before \h* '=end' \h+ code> ||
<!before \h* '=' \w>
]
]
]
}
proto token pod_block { <...> }
token pod_configuration($spaces = '') {
[ [\n $spaces '=']? \h+ <colonpair> ]*
}
token pod_block:sym<delimited_comment> {
^^
$<spaces> = [ \h* ]
'=begin' \h+ 'comment' {}
<pod_configuration($<spaces>)> <pod_newline>+
[
$<pod_content> = [ .*? ]
^^ $<spaces> '=end' \h+
[
'comment' [ <pod_newline> | $ ]
|| $<instead>=<identifier>? {$/.typed_panic: 'X::Syntax::Pod::BeginWithoutEnd', type => 'comment', spaces => ~$<spaces>, instead => $<instead> ?? ~$<instead> !! ''}
]
]
}
regex pod_block:sym<delimited> {
^^
$<spaces> = [ \h* ]
'=begin'
[ <?pod_newline>
<.typed_panic('X::Syntax::Pod::BeginWithoutIdentifier')>
]?
\h+ <!before 'finish'>
{
$*VMARGIN := $<spaces>.to - $<spaces>.from;
}
:my $*ALLOW_INLINE_CODE := 0;
$<type> = [
<pod_code_parent> { $*ALLOW_INLINE_CODE := 1 }
|| <identifier>
]
:my $*POD_ALLOW_FCODES := nqp::getlexdyn('$*POD_ALLOW_FCODES');
<pod_configuration($<spaces>)> <pod_newline>+
[
<pod_content> *
^^ $<spaces> '=end' \h+
[
$<type> [ <pod_newline> | $ ]
|| $<instead>=<identifier>? {$/.typed_panic: 'X::Syntax::Pod::BeginWithoutEnd', type => ~$<type>, spaces => ~$<spaces>, instead => $<instead> ?? ~$<instead> !! ''}
]
]
}
token pod_block:sym<delimited_table> {
^^
$<spaces> = [ \h* ]
'=begin' \h+ 'table' {}
:my $*POD_ALLOW_FCODES := nqp::getlexdyn('$*POD_ALLOW_FCODES');
<pod_configuration($<spaces>)> <pod_newline>+
[
[ $<table_row>=<.table_row_or_blank> ]*
^^ \h* '=end' \h+
[
'table' [ <pod_newline> | $ ]
|| $<instead>=<identifier>? {$/.typed_panic: 'X::Syntax::Pod::BeginWithoutEnd', type => 'table', spaces => ~$<spaces>, instead => $<instead> ?? ~$<instead> !! ''}
]
]
}
token pod_block:sym<delimited_code> {
^^
$<spaces> = [ \h* ]
'=begin' \h+ 'code' {}
:my $*POD_ALLOW_FCODES := 0;
:my $*POD_IN_CODE_BLOCK := 1;
:my $*POD_DELIMITED_CODE_BLOCK := 1;
<pod_configuration($<spaces>)> <pod_newline>+
[
|| <delimited_code_content($<spaces>)> $<spaces> '=end' \h+
[ 'code' [ <pod_newline> | $ ]
|| $<instead>=<identifier>? {$/.typed_panic: 'X::Syntax::Pod::BeginWithoutEnd', type => 'code', spaces => ~$<spaces>, instead => $<instead> ?? ~$<instead> !! ''}
]
]
}
token delimited_code_content($spaces = '') {
^^
(
| $spaces
<!before '=end' \h+ 'code' [ <pod_newline> | $ ]>
<pod_string>**0..1 <pod_newline>
| <pod_newline>
)*
}
token table_row {
\h* <!before '=' \w> \N+ [ \n | $ ]
}
token table_row_or_blank {
<.table_row> | [\h* <!before '=' \w> \n ]
}
token pod_block:sym<finish> {
^^ \h*
[
| '=begin' \h+ 'finish' <pod_newline>
| '=for' \h+ 'finish' <pod_newline>