-
Notifications
You must be signed in to change notification settings - Fork 11
/
STD_P5.pm6
2738 lines (2204 loc) · 67.1 KB
/
STD_P5.pm6
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
# STD_P5.pm
#
# Copyright 2009-2010, Larry Wall
#
# You may copy this software under the terms of the Artistic License,
# version 2.0 or later.
grammar STD5 is STD;
use DEBUG;
method TOP ($STOP?) {
if defined $STOP {
my $*GOAL ::= $STOP;
self.unitstop($STOP).comp_unit;
}
else {
self.comp_unit;
}
}
##############
# Precedence #
##############
# The internal precedence levels are *not* part of the public interface.
# The current values are mere implementation; they may change at any time.
# Users should specify precedence only in relation to existing levels.
constant %term = (:dba('term') , :prec<z=>);
constant %methodcall = (:dba('methodcall') , :prec<y=>, :assoc<unary>, :uassoc<left>, :fiddly);
constant %autoincrement = (:dba('autoincrement') , :prec<x=>, :assoc<unary>, :uassoc<non>);
constant %exponentiation = (:dba('exponentiation') , :prec<w=>, :assoc<right>);
constant %symbolic_unary = (:dba('symbolic unary') , :prec<v=>, :assoc<unary>, :uassoc<left>);
constant %binding = (:dba('binding') , :prec<u=>, :assoc<non>);
constant %multiplicative = (:dba('multiplicative') , :prec<t=>, :assoc<left>);
constant %additive = (:dba('additive') , :prec<s=>, :assoc<left>);
constant %shift = (:dba('shift') , :prec<r=>, :assoc<left>);
constant %named_unary = (:dba('named unary') , :prec<q=>, :assoc<unary>, :uassoc<left>);
constant %comparison = (:dba('comparison') , :prec<p=>, :assoc<non>, :diffy);
constant %equality = (:dba('equality') , :prec<o=>, :assoc<chain>, :diffy, :iffy);
constant %bitwise_and = (:dba('bitwise and') , :prec<n=>, :assoc<left>);
constant %bitwise_or = (:dba('bitwise or') , :prec<m=>, :assoc<left>);
constant %tight_and = (:dba('tight and') , :prec<l=>, :assoc<left>);
constant %tight_or = (:dba('tight or') , :prec<k=>, :assoc<left>);
constant %range = (:dba('range') , :prec<j=>, :assoc<right>, :fiddly);
constant %conditional = (:dba('conditional') , :prec<i=>, :assoc<right>, :fiddly);
constant %assignment = (:dba('assignment') , :prec<h=>, :assoc<right>);
constant %comma = (:dba('comma operator') , :prec<g=>, :assoc<left>, :nextterm<nulltermish>, :fiddly);
constant %listop = (:dba('list operator') , :prec<f=>, :assoc<unary>, :uassoc<left>);
constant %loose_not = (:dba('not operator') , :prec<e=>, :assoc<unary>, :uassoc<left>);
constant %loose_and = (:dba('loose and') , :prec<d=>, :assoc<left>);
constant %loose_or = (:dba('loose or') , :prec<c=>, :assoc<left>);
constant %LOOSEST = (:dba('LOOSEST') , :prec<a=!>);
constant %terminator = (:dba('terminator') , :prec<a=>, :assoc<list>);
# "epsilon" tighter than terminator
#constant $LOOSEST = %LOOSEST<prec>;
constant $LOOSEST = "a=!"; # XXX preceding line is busted
##############
# Categories #
##############
# Categories are designed to be easily extensible in derived grammars
# by merely adding more rules in the same category. The rules within
# a given category start with the category name followed by a differentiating
# adverbial qualifier to serve (along with the category) as the longer name.
# The endsym context, if specified, says what to implicitly check for in each
# rule right after the initial <sym>. Normally this is used to make sure
# there's appropriate whitespace. # Note that endsym isn't called if <sym>
# isn't called.
my $*endsym = "null";
proto token category {*}
token category:category { <sym> }
token category:p5sigil { <sym> }
proto token p5sigil {*}
token category:p5special_variable { <sym> }
proto token p5special_variable {*}
token category:p5comment { <sym> }
proto token p5comment {*}
token category:p5module_name { <sym> }
proto token p5module_name {*}
token category:p5value { <sym> }
proto token p5value {*}
token category:p5term { <sym> }
proto token p5term {*}
token category:p5number { <sym> }
proto token p5number {*}
token category:p5quote { <sym> }
proto token p5quote () {*}
token category:p5prefix { <sym> }
proto token p5prefix is unary is defequiv(%symbolic_unary) {*}
token category:p5infix { <sym> }
proto token p5infix is binary is defequiv(%additive) {*}
token category:p5postfix { <sym> }
proto token p5postfix is unary is defequiv(%autoincrement) {*}
token category:p5dotty { <sym> }
proto token p5dotty (:$*endsym = 'unspacey') {*}
token category:p5circumfix { <sym> }
proto token p5circumfix {*}
token category:p5postcircumfix { <sym> }
proto token p5postcircumfix is unary {*} # unary as far as EXPR knows...
token category:p5type_declarator { <sym> }
proto token p5type_declarator (:$*endsym = 'spacey') {*}
token category:p5scope_declarator { <sym> }
proto token p5scope_declarator (:$*endsym = 'nofun') {*}
token category:p5package_declarator { <sym> }
proto token p5package_declarator (:$*endsym = 'spacey') {*}
token category:p5routine_declarator { <sym> }
proto token p5routine_declarator (:$*endsym = 'nofun') {*}
token category:p5regex_declarator { <sym> }
proto token p5regex_declarator (:$*endsym = 'spacey') {*}
token category:p5statement_prefix { <sym> }
proto rule p5statement_prefix () {*}
token category:p5statement_control { <sym> }
proto rule p5statement_control (:$*endsym = 'spacey') {*}
token category:p5statement_mod_cond { <sym> }
proto rule p5statement_mod_cond (:$*endsym = 'nofun') {*}
token category:p5statement_mod_loop { <sym> }
proto rule p5statement_mod_loop (:$*endsym = 'nofun') {*}
token category:p5terminator { <sym> }
proto token p5terminator {*}
token unspacey { <.unsp>? }
token endid { <?before <-[ \- \' \w ]> > }
token spacey { <?before <[ \s \# ]> > }
token nofun { <!before '(' | '->(' | '\\' | '\'' | '-' | "'" | \w > }
##################
# Lexer routines #
##################
token ws {
:temp @*STUB = return self if @*MEMOS[self.pos]<ws> :exists;
:my $startpos = self.pos;
:dba('whitespace')
[
| \h+ <![\#\s\\]> { @*MEMOS[$¢.pos]<ws> = $startpos; } # common case
| <?before \w> <?after \w> :::
{ @*MEMOS[$startpos]<ws> :delete; }
<.panic: "Whitespace is required between alphanumeric tokens"> # must \s+ between words
]
||
[
| <.unsp>
| <.vws> <.heredoc>
| <.unv>
| $ { $¢.moreinput }
]*
{{
if ($¢.pos == $startpos) {
@*MEMOS[$¢.pos]<ws> :delete;
}
else {
@*MEMOS[$¢.pos]<ws> = $startpos;
@*MEMOS[$¢.pos]<endstmt> = @*MEMOS[$startpos]<endstmt>
if @*MEMOS[$startpos]<endstmt> :exists;
}
}}
}
token unsp {
<!>
}
token vws {
:dba('vertical whitespace')
\v
[ '#DEBUG -1' { say "DEBUG"; $STD::DEBUG = $*DEBUG = -1; } ]?
}
# We provide two mechanisms here:
# 1) define $*moreinput, or
# 2) override moreinput method
method moreinput () {
$*moreinput.() if $*moreinput;
}
token unv {
:dba('horizontal whitespace')
[
| \h+
| <?before \h* '=' [ \w | '\\'] > ^^ <.pod_comment>
| \h* <comment=p5comment>
]
}
token p5comment:sym<#> {
'#' {} \N*
}
token ident {
<.alpha> \w*
}
token identifier {
<.alpha> \w*
}
# XXX We need to parse the pod eventually to support $= variables.
token pod_comment {
^^ \h* '=' <.unsp>?
[
| 'begin' \h+ <identifier> ::
[
|| .*? "\n" \h* '=' <.unsp>? 'end' \h+ $<identifier> » \N*
|| <?{ $<identifier>.Str eq 'END'}> .*
|| { my $id = $<identifier>.Str; self.panic("=begin $id without matching =end $id"); }
]
| 'begin' » :: \h* [ $$ || '#' || <.panic: "Unrecognized token after =begin"> ]
[ .*? "\n" \h* '=' <.unsp>? 'end' » \N* || { self.panic("=begin without matching =end"); } ]
| 'for' » :: \h* [ <identifier> || $$ || '#' || <.panic: "Unrecognized token after =for"> ]
[.*? ^^ \h* $$ || .*]
| :: .*? ^^ '=cut' » \N*
]
}
###################
# Top-level rules #
###################
# Note: we only check for the stopper. We don't check for ^ because
# we might be embedded in something else.
rule comp_unit {
:my $*begin_compunit = 1;
:my %*LANG;
:my $*PKGDECL ::= "";
:my $*IN_DECL;
:my $*DECLARAND;
:my $*NEWPKG;
:my $*NEWLEX;
:my $*QSIGIL ::= '';
:my $*IN_META = 0;
:my $*QUASIMODO;
:my $*SCOPE = "";
:my $*LEFTSIGIL;
:my %*MYSTERY = ();
:my $*INVOCANT_OK;
:my $*INVOCANT_IS;
:my $*CURLEX;
:my $*MULTINESS = '';
:my $*CURPKG;
{{
%*LANG<MAIN> = ::STD5 ;
%*LANG<Q> = ::STD5::Q ;
%*LANG<Regex> = ::STD5::Regex ;
%*LANG<Trans> = ::STD5::Trans ;
@*WORRIES = ();
self.load_setting($*SETTINGNAME);
my $oid = $*SETTING.id;
my $id = 'MY:file<' ~ $*FILE<name> ~ '>';
$*CURLEX = Stash.new(
'OUTER::' => [$oid],
'!file' => $*FILE, '!line' => 0,
'!id' => [$id],
);
$STD::ALL.{$id} = $*CURLEX;
$*UNIT = $*CURLEX;
$STD::ALL.<UNIT> = $*UNIT;
self.finishlex;
}}
<statementlist>
[ <?unitstopper> || <.panic: "Confused"> ]
# "CHECK" time...
{{
if @*WORRIES {
warn "Potential difficulties:\n " ~ join( "\n ", @*WORRIES) ~ "\n";
}
my $m = $¢.explain_mystery();
warn $m if $m;
}}
}
method explain_mystery() {
my %post_types;
my %unk_types;
my %unk_routines;
my $m = '';
for keys(%*MYSTERY) {
my $p = %*MYSTERY{$_}.<lex>;
if self.is_name($_, $p) {
# types may not be post-declared
%post_types{$_} = %*MYSTERY{$_};
next;
}
next if self.is_known($_, $p) or self.is_known('&' ~ $_, $p);
# just a guess, but good enough to improve error reporting
if $_ lt 'a' {
%unk_types{$_} = %*MYSTERY{$_};
}
else {
%unk_routines{$_} = %*MYSTERY{$_};
}
}
if %post_types {
my @tmp = sort keys(%post_types);
$m ~= "Illegally post-declared type" ~ ('s' x (@tmp != 1)) ~ ":\n";
for @tmp {
$m ~= "\t$_ used at line " ~ %post_types{$_}.<line> ~ "\n";
}
}
if %unk_types {
my @tmp = sort keys(%unk_types);
$m ~= "Undeclared name" ~ ('s' x (@tmp != 1)) ~ ":\n";
for @tmp {
$m ~= "\t$_ used at line " ~ %unk_types{$_}.<line> ~ "\n";
}
}
if %unk_routines {
my @tmp = sort keys(%unk_routines);
$m ~= "Undeclared routine" ~ ('s' x (@tmp != 1)) ~ ":\n";
for @tmp {
$m ~= "\t$_ used at line " ~ %unk_routines{$_}.<line> ~ "\n";
}
}
$m;
}
# Look for an expression followed by a required lambda.
token xblock {
:my $*GOAL ::= '{';
:dba('block expression') '(' ~ ')' <EXPR>
<.ws>
<sblock>
}
token sblock {
:temp $*CURLEX;
:dba('statement block')
[ <?before '{' > || <.panic: "Missing block"> ]
<.newlex>
<blockoid>
{ @*MEMOS[$¢.pos]<endstmt> = 2; }
<.ws>
}
token block {
:temp $*CURLEX;
:dba('scoped block')
[ <?before '{' > || <.panic: "Missing block"> ]
<.newlex>
<blockoid>
<.ws>
}
token blockoid {
# encapsulate braided languages
:temp %*LANG;
<.finishlex>
[
| :dba('block') '{' ~ '}' <statementlist>
| <?terminator> <.panic: 'Missing block'>
| <?> <.panic: "Malformed block">
]
}
# statement semantics
rule statementlist {
:my $*INVOCANT_OK = 0;
:dba('statement list')
''
[
| $
| <?before <[\)\]\}]> >
| [<statement><eat_terminator> ]*
]
}
# embedded semis, context-dependent semantics
rule semilist {
:my $*INVOCANT_OK = 0;
:dba('semicolon list')
''
[
| <?before <[\)\]\}]> >
| [<statement><eat_terminator> ]*
]
}
token label {
:my $label;
<identifier> ':' <?before \s> <.ws>
[ <?{ $¢.is_name($label = $<identifier>.Str) }>
<.sorry("Illegal redeclaration of '$label'")>
]?
# add label as a pseudo type
{{ $¢.add_my_name($label); }}
}
token statement {
:my $*QSIGIL ::= 0;
<!before <[\)\]\}]> >
# this could either be a statement that follows a declaration
# or a statement that is within the block of a code declaration
<!!{ $¢ = %*LANG<MAIN>.bless($¢); }>
[
| <label> <statement>
| <statement_control=p5statement_control>
| <EXPR>
:dba('statement end')
<.ws>
:dba('statement modifier')
[
| <statement_mod_loop=p5statement_mod_loop>
| <statement_mod_cond=p5statement_mod_cond>
]?
| <?before ';'>
]
}
token eat_terminator {
[
|| ';' [ <?before $> { $*ORIG ~~ s/\;$/ /; } ]?
|| <?{ @*MEMOS[$¢.pos]<endstmt> }> <.ws>
|| <?terminator>
|| $
|| {{ if @*MEMOS[$¢.pos]<ws> { $¢.pos = @*MEMOS[$¢.pos]<ws>; } }} # undo any line transition
<.panic: "Confused">
]
}
#####################
# statement control #
#####################
rule p5statement_control:use {
:my $longname;
:my $*SCOPE = 'use';
<sym>
[
|| <version=p5versionish> [
|| <?{ substr($<version>[0].Str,0,2) eq 'v5' }>
|| <?{ substr($<version>[0].Str,0,2) eq 'v6' }> [
:my %*LANG;
{
%*LANG<MAIN> = ::STD::P6 ;
%*LANG<Regex> = ::STD::Regex ;
%*LANG<Q> = ::STD::Q ;
%*LANG<Trans> = ::STD::Trans ;
$¢ = %*LANG<MAIN>.bless($¢);
}
<.ws> ';'
[ <statementlist> || <.panic: "Bad P6 code"> ]
]
]
|| <module_name=p5module_name>
{
$longname = $<module_name><longname>;
}
<version=p5versionish>?
[
<arglist>?
# {
# $¢.do_use($longname, $<arglist>);
# }
# || {
# $¢.do_use($longname, '');
# }
]
]
}
rule p5statement_control:no {
<sym>
<module_name=p5module_name>[<.spacey><arglist>]?
}
rule p5statement_control:if {
$<sym>=['if'|'unless']
<xblock>
[
[ <!before 'else'\s*'if'> || <.panic: "Please use 'elsif'"> ]
'elsif'<?spacey> <elsif=xblock>
]*
[
'else'<?spacey> <else=sblock>
]?
}
rule p5statement_control:while {
<sym> <xblock>
}
rule p5statement_control:until {
<sym> <xblock>
}
rule p5statement_control:for {
['for'|'foreach']
[
|| '('
<e1=EXPR>? ';'
<e2=EXPR>? ';'
<e3=EXPR>?
')'
|| ['my'? <variable_declarator>]? '(' ~ ')' <EXPR>
|| <.panic: "Malformed loop spec">
]
<sblock>
}
rule p5statement_control:given {
<sym> <xblock>
}
rule p5statement_control:when {
<sym> <xblock>
}
rule p5statement_control:default {<sym> <sblock> }
rule p5statement_prefix:BEGIN {<sym> <sblock> }
rule p5statement_prefix:CHECK {<sym> <sblock> }
rule p5statement_prefix:INIT {<sym> <sblock> }
rule p5statement_control:END {<sym> <sblock> }
#######################
# statement modifiers #
#######################
rule modifier_expr { <EXPR> }
rule p5statement_mod_cond:if {<sym> <modifier_expr> }
rule p5statement_mod_cond:unless {<sym> <modifier_expr> }
rule p5statement_mod_cond:when {<sym> <modifier_expr> }
rule p5statement_mod_loop:while {<sym> <modifier_expr> }
rule p5statement_mod_loop:until {<sym> <modifier_expr> }
rule p5statement_mod_loop:for {<sym> <modifier_expr> }
rule p5statement_mod_loop:given {<sym> <modifier_expr> }
################
# module names #
################
token def_module_name {
<longname>
}
token p5module_name:normal {
<longname>
[ <?before '['> :dba('generic role') '[' ~ ']' <arglist> ]?
}
token vnum {
\d+
}
token p5versionish {
| <p5version>
| <?before \d+'.'\d+> <vnum> +% '.'
}
token p5version {
| 'v' <?before \d+ > :: <vnum> +% '.'
| <?before \d+'.'\d+'.'\d+> <vnum> +% '.'
}
###############
# Declarators #
###############
token variable_declarator {
:my $*IN_DECL = 1;
:my $*DECLARAND;
<variable>
{ $*IN_DECL = 0; $¢.add_variable($<variable>.Str) }
<.ws>
<trait>*
}
rule scoped($*SCOPE) {
:dba('scoped declarator')
[
| <declarator>
| <regex_declarator=p5regex_declarator>
| <package_declarator=p5package_declarator>
]
|| <?before <[A..Z]>><longname>{{
my $t = $<longname>.Str;
if not $¢.is_known($t) {
$¢.sorry("In \"$*SCOPE\" declaration, typename $t must be predeclared (or marked as declarative with :: prefix)");
}
}}
<!> # drop through
|| <.panic: "Malformed $*SCOPE">
}
rule p5scope_declarator:my { <sym> <scoped('my')> }
rule p5scope_declarator:our { <sym> <scoped('our')> }
rule p5scope_declarator:state { <sym> <scoped('state')> }
rule p5package_declarator:package {
:my $*PKGDECL ::= 'package';
<sym> <package_def>
}
rule p5package_declarator:require { # here because of declarational aspects
<sym>
[
|| <version=p5versionish>
|| <module_name=p5module_name> <EXPR>?
|| <EXPR>
]
}
rule package_def {
:my $longname;
:my $*IN_DECL = 'package';
:my $*HAS_SELF = '';
:my $*DECLARAND;
:my $*NEWPKG;
:my $*NEWLEX;
:my $outer = $*CURLEX;
:temp $*CURPKG;
:temp $*CURLEX;
{ $*SCOPE ||= 'our'; }
'' # XXX
[
[ <longname> { $longname = $<longname>[0]; $¢.add_name($longname<name>.Str); } ]?
<.newlex>
<trait>*
<.getdecl>
[
|| <?before '{'>
[
{
# figure out the actual full package name (nested in outer package)
if $longname and $*NEWPKG {
my $shortname = $longname.<name>.Str;
if $*SCOPE eq 'our' {
$*CURPKG = $*NEWPKG // $*CURPKG.{$shortname ~ '::'};
self.deb("added our " ~ $*CURPKG.id) if $*DEBUG +& DEBUG::symtab;
}
else {
$*CURPKG = $*NEWPKG // $*CURPKG.{$shortname ~ '::'};
self.deb("added my " ~ $*CURPKG.id) if $*DEBUG +& DEBUG::symtab;
}
}
$*begin_compunit = 0;
$*UNIT<$?LONGNAME> ||= $longname ?? $longname<name>.Str !! '';
}
{ $*IN_DECL = ''; }
<blockoid>
<.checkyada>
]
|| <?before ';'>
{
$longname orelse $¢.panic("Compilation unit cannot be anonymous");
my $shortname = $longname.<name>.Str;
$*CURPKG = $*NEWPKG // $*CURPKG.{$shortname ~ '::'};
$*begin_compunit = 0;
# XXX throws away any role sig above
$*CURLEX = $outer;
$*UNIT<$?LONGNAME> = $longname<name>.Str;
}
{ $*IN_DECL = ''; }
<statementlist> # whole rest of file, presumably
|| <.panic: "Unable to parse " ~ $*PKGDECL ~ " definition">
]
] || <.panic: "Malformed $*PKGDECL">
}
token declarator {
[
| <variable_declarator>
| '(' ~ ')' <signature> <trait>*
| <routine_declarator=p5routine_declarator>
| <regex_declarator=p5regex_declarator>
| <type_declarator=p5type_declarator>
]
}
token p5multi_declarator:null {
:my $*MULTINESS = '';
<declarator>
}
rule p5routine_declarator:sub { <sym> <routine_def> }
rule parensig {
:dba('signature')
'(' ~ ')' <signature(1)>
}
method checkyada {
try {
my $startsym = self.<blockoid><statementlist><statement>[0]<EXPR><term><sym> // '';
if $startsym eq '...' or $startsym eq '!!!' or $startsym eq '???' {
$*DECLARAND<stub> = 1;
}
};
return self;
}
rule routine_def () {
:temp $*CURLEX;
:my $*IN_DECL = 1;
:my $*DECLARAND;
[
|| <deflongname>
<.newlex(1)>
<parensig>?
<trait>*
<!{
$*IN_DECL = 0;
}>
<blockoid>:!s
{ @*MEMOS[$¢.pos]<endstmt> = 2; }
<.checkyada>
<.getsig>
|| <?before \W>
<.newlex(1)>
<parensig>?
<trait>*
<!{
$*IN_DECL = 0;
}>
<blockoid>:!s
<.checkyada>
<.getsig>
] || <.panic: "Malformed routine">
}
rule trait {
:my $*IN_DECL = 0;
':' <EXPR(item %comma)>
}
#########
# Nouns #
#########
# (for when you want to tell EXPR that infix already parsed the term)
token nullterm {
<?>
}
token nulltermish {
:dba('null term')
[
| <?stdstopper>
| <term=termish>
{
$¢.<PRE> = $<term><PRE>:delete;
$¢.<POST> = $<term><POST>:delete;
$¢.<~CAPS> = $<term><~CAPS>;
}
| <?>
]
}
token termish {
:my $*SCOPE = "";
:my $*VAR;
:dba('prefix or term')
[
| <PRE> [ <!{ my $p = $<PRE>; my @p = @$p; @p[*-1]<O><term> and $<term> = pop @$p }> <PRE> ]*
[ <?{ $<term> }> || <term=p5term> ]
| <term=p5term>
]
# also queue up any postfixes
:dba('postfix')
[
|| <?{ $*QSIGIL }>
[ <?before '[' | '{' > <POST> ]*!
|| <!{ $*QSIGIL }>
<POST>*
]
{
self.check_variable($*VAR) if $*VAR;
$¢.<~CAPS> = $<term><~CAPS>;
}
}
token p5term:fatkey { <fatkey> }
token p5term:variable { <variable>
[
|| <?{ $<variable><sigil>.Str ne '$' }>
|| <?before '['> { $<variable><really> = '@' }
|| <?before '{'> { $<variable><really> = '%' }
]?
{ $*VAR ||= $<variable> }
}
token p5term:package_declarator { <package_declarator=p5package_declarator> }
token p5term:scope_declarator { <scope_declarator=p5scope_declarator> }
token p5term:routine_declarator { <routine_declarator=p5routine_declarator> }
token p5term:circumfix { <circumfix=p5circumfix> }
token p5term:dotty { <dotty=p5dotty> }
token p5term:value { <value=p5value> }
token p5term:capterm { <capterm> }
token p5term:statement_prefix { <statement_prefix=p5statement_prefix> }
token fatkey {
'-'?<key=identifier> <?before \h* '=>' >
}
# Most of these special variable rules are there simply to catch old p5 brainos
token p5special_variable:sym<$!> { <sym> <!before \w> }
token p5special_variable:sym<$!{ }> {
'$!{' ~ '}' <EXPR>
}
token p5special_variable:sym<$/> {
<sym>
}
token p5special_variable:sym<$~> {
<sym>
}
token p5special_variable:sym<$`> {
<sym>
}
token p5special_variable:sym<$@> {
<sym>
}
token p5special_variable:sym<$#> {
<sym>
}
token p5special_variable:sym<$$> {
<sym> <!alpha>
}
token p5special_variable:sym<$%> {
<sym>
}
token p5special_variable:sym<$^X> {
<sigil=p5sigil> '^' $<letter> = [<[A..Z]>] <?before \W >
}
token p5special_variable:sym<$^> {
<sym>
}
token p5special_variable:sym<$&> {
<sym>
}
token p5special_variable:sym<$*> {
<sym>
}
token p5special_variable:sym<$)> {
<sym>
}
token p5special_variable:sym<$-> {
<sym>
}
token p5special_variable:sym<$=> {
<sym>
}
token p5special_variable:sym<@+> {
<sym>
}
token p5special_variable:sym<%+> {
<sym>
}
token p5special_variable:sym<$+[ ]> {
'$+['
}
token p5special_variable:sym<@+[ ]> {
'@+['
}
token p5special_variable:sym<@+{ }> {
'@+{'
}
token p5special_variable:sym<@-> {
<sym>
}
token p5special_variable:sym<%-> {
<sym>
}
token p5special_variable:sym<$-[ ]> {
'$-['
}
token p5special_variable:sym<@-[ ]> {
'@-['
}
token p5special_variable:sym<%-{ }> {
'@-{'
}
token p5special_variable:sym<$+> {
<sym>
}
token p5special_variable:sym<${^ }> {
<sigil=p5sigil> '{^' :: $<text>=[.*?] '}'
}
token p5special_variable:sym<::{ }> {
'::' <?before '{'>
}
token p5special_variable:sym<$[> {
<sym>
}
token p5special_variable:sym<$]> {
<sym>
}
token p5special_variable:sym<$\\> {
<sym>
}
token p5special_variable:sym<$|> {
<sym>
}
token p5special_variable:sym<$:> {
<sym>
}
token p5special_variable:sym<$;> {
<sym>
}
token p5special_variable:sym<$'> { #'
<sym>
}
token p5special_variable:sym<$"> {
<sym> <!{ $*QSIGIL }>
}
token p5special_variable:sym<$,> {
<sym>
}