-
Notifications
You must be signed in to change notification settings - Fork 15
/
Actions.pm
2370 lines (2051 loc) · 68.9 KB
/
Actions.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
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
package Niecza::Actions;
use 5.010;
use strict;
use warnings;
use Scalar::Util 'blessed';
use Op;
use RxOp;
use Body;
use Unit;
use Sig;
use CClass;
use Try::Tiny;
# I actually prefer this to the official AUTOLOAD solution
{
package CursorBase;
no warnings 'redefine';
sub _REDUCE { my $self = shift;
my $S = shift;
my $meth = shift;
my $key = $meth;
$key .= ' ' . $_[0] if @_;
$self->{_reduced} = $key;
$self->{_from} = $S;
if ($::ACTIONS) {
$::ACTIONS->REDUCE($meth, $self, @_);
}
$self->deb("REDUCE $key from " . $S . " to " . $self->{_pos}) if &CursorBase::DEBUG() & &DEBUG::matchers();
$self;
}
}
my %carped;
sub REDUCE {
my ($cl, $meth, $M) = @_;
eval {
my ($snd, $spec);
if ($meth =~ /^(.*)__S_\d\d\d(.*)$/) {
$meth = "$1__S_$2";
$snd = "$1__S_ANY";
$spec = $2;
}
if ($cl->can($meth)) {
return $cl->$meth($M);
} elsif ($snd && $cl->can($snd)) {
return $cl->$snd($M, $spec);
} elsif (!( $carped{$meth}++ )) {
die("Action method $meth not yet implemented");
}
};
if ($@) {
my $foo = $@;
$foo =~ s/^(?:[^\n]*\n){5}\K.*//s;
$M->sorry($foo);
}
}
sub node { my ($M) = @_;
file => $::FILE->{name}, line => $M->lineof($M->pos)
}
sub ws { }
sub is_ok { }
sub dumbsmart { }
sub normspace { }
sub vws { }
sub unv { }
sub begid { }
sub comment { }
sub comment__S_Sharp { }
sub spacey { }
sub unspacey { }
sub unsp { }
sub nofun { }
sub curlycheck { }
sub pod_comment { }
sub infixstopper { }
sub category { }
sub category__S_category { }
sub category__S_sigil { }
sub category__S_twigil { }
sub category__S_special_variable { }
sub category__S_comment { }
sub category__S_version { }
sub category__S_module_name { }
sub category__S_value { }
sub category__S_term { }
sub category__S_strtonum { }
sub category__S_quote { }
sub category__S_prefix { }
sub category__S_infix { }
sub category__S_postfix { }
sub category__S_dotty { }
sub category__S_circumfix { }
sub category__S_postcircumfix { }
sub category__S_quote_mod { }
sub category__S_trait_mod { }
sub category__S_type_declarator { }
sub category__S_scope_declarator { }
sub category__S_package_declarator { }
sub category__S_multi_declarator { }
sub category__S_routine_declarator { }
sub category__S_regex_declarator { }
sub category__S_statement_prefix { }
sub category__S_statement_control { }
sub category__S_statement_mod_cond { }
sub category__S_statement_mod_loop { }
sub category__S_infix_prefix_meta_operator { }
sub category__S_infix_postfix_meta_operator { }
sub category__S_infix_circumfix_meta_operator { }
sub category__S_postfix_prefix_meta_operator { }
sub category__S_prefix_postfix_meta_operator { }
sub category__S_prefix_circumfix_meta_operator { }
sub category__S_terminator { }
sub category__S_metachar { }
sub category__S_backslash { }
sub category__S_assertion { }
sub category__S_quantifier { }
sub category__S_mod_internal { }
sub sign { }
sub decint { my ($cl, $M) = @_;
$M->{_ast} = eval $M->Str; # XXX use a real string parser
}
sub hexint { my ($cl, $M) = @_;
my $s = $M->Str;
$s =~ s/_//g;
$M->{_ast} = hex $M->Str;
}
sub octint { my ($cl, $M) = @_;
my $s = $M->Str;
$s =~ s/_//g;
$M->{_ast} = oct $M->Str;
}
sub decints { my ($cl, $M) = @_;
$M->{_ast} = [ map { $_->{_ast} } @{ $M->{decint} } ];
}
sub hexints { my ($cl, $M) = @_;
$M->{_ast} = [ map { $_->{_ast} } @{ $M->{hexint} } ];
}
sub octints { my ($cl, $M) = @_;
$M->{_ast} = [ map { $_->{_ast} } @{ $M->{octint} } ];
}
sub integer { my ($cl, $M) = @_;
$M->{_ast} =
($M->{decint} // $M->{octint} // $M->{hexint} // $M->{binint})->{_ast};
}
sub number { my ($cl, $M) = @_;
my $child = $M->{integer} // $M->{dec_number} // $M->{rad_number};
$M->{_ast} = $child ? $child->{_ast} :
($M->Str eq 'NaN') ? (1e99999/1e99999) : (1e99999);
}
# Value :: Op
sub value { }
sub value__S_number { my ($cl, $M) = @_;
# TODO: Implement the rest of the numeric hierarchy once MMD exists
$M->{_ast} = Op::Num->new(node($M), value => $M->{number}{_ast});
}
sub value__S_quote { my ($cl, $M) = @_;
$M->{_ast} = $M->{quote}{_ast};
}
sub ident { my ($cl, $M) = @_;
$M->{_ast} = $M->Str;
}
sub identifier { my ($cl, $M) = @_;
$M->{_ast} = $M->Str;
}
# Either String Op
sub morename { my ($cl, $M) = @_;
$M->{_ast} = $M->{identifier}[0] ? $M->{identifier}[0]{_ast} :
$M->{EXPR}[0]{_ast};
}
# { dc: Bool, names: [Either String Op] }
sub name { my ($cl, $M) = @_;
my @names = map { $_->{_ast} } @{ $M->{morename} };
unshift @names, $M->{identifier}{_ast} if $M->{identifier};
$M->{_ast} = { dc => !($M->{identifier}), names => \@names };
}
sub longname {} # look at the children yourself
sub deflongname {}
# Turns a name like ::Foo::Bar:sym[ 'x' ] into
# { name => 'Bar:sym<x>', path => [ 'Foo '] }
# path can be undefined for a simple name like $x, which goes straight to pad
# pass $clean if you want to ignore adverbs entirely - currently needed for
# package names
sub unqual_longname { my ($cl, $M, $what, $clean) = @_;
my $h = $cl->mangle_longname($M, $clean);
if ($h->{path}) {
$M->sorry($what);
return;
}
return $h->{name};
}
sub mangle_longname { my ($cl, $M, $clean) = @_;
my @ns = @{ $M->{name}{_ast}{names} };
my $n = pop @ns;
unless ($clean) {
$n .= $_->{_ast}{ext} // do {
$M->sorry("Invalid colonpair for name extension");
"";
} for @{ $M->{colonpair} };
}
my @path = ($M->{name}{_ast}{dc} || @ns) ? (path => \@ns) : ();
return { name => $n, @path };
}
sub subshortname { my ($cl, $M) = @_;
if (@{ $M->{colonpair} }) {
my $n = $M->{category}->Str;
$n .= $_->{_ast}{ext} // do {
$M->sorry("Invalid colonpair for name extension");
"";
} for @{ $M->{colonpair} };
$M->{_ast} = { name => $n };
} else {
$M->{_ast} = $M->{desigilname}{_ast};
}
}
sub sublongname { my ($cl, $M) = @_;
if (@{ $M->{sigterm} }) {
$M->sorry("Sigterm sublongnames NYI");
return;
}
$M->{_ast} = $M->{subshortname}{_ast};
}
sub desigilname { my ($cl, $M) = @_;
if ($M->{variable}) {
$M->sorry("Truncated contextualizer syntax NYI");
return;
}
$M->{_ast} = $cl->mangle_longname($M->{longname});
}
sub stopper { }
# quote :: Op
sub quote {}
sub quote__S_Double_Double { my ($cl, $M) = @_;
$M->{_ast} = $M->{nibble}{_ast};
}
sub quote__S_Single_Single { my ($cl, $M) = @_;
$M->{_ast} = $M->{nibble}{_ast};
}
sub quote__S_qq { my ($cl, $M) = @_;
$M->{_ast} = $M->{quibble}{_ast};
}
sub quote__S_q { my ($cl, $M) = @_;
$M->{_ast} = $M->{quibble}{_ast};
}
sub quote__S_Q { my ($cl, $M) = @_;
$M->{_ast} = $M->{quibble}{_ast};
}
sub quote__S_Slash_Slash { my ($cl, $M) = @_;
my $slot = $cl->gensym;
# TODO should be a real pass.
local $::parenid = 0;
local $::symtext;
$M->{_ast} = $M->{nibble}{_ast}->close_rx;
}
sub regex_block { my ($cl, $M) = @_;
if (@{ $M->{quotepair} }) {
$M->sorry('Regex adverbs NYI');
return;
}
$M->{_ast} = $M->{nibble}{_ast};
}
sub regex_def { my ($cl, $M) = @_;
my ($name, $path) = $M->{deflongname}[0] ?
@{ $cl->mangle_longname($M->{deflongname}[0]) }{'name', 'path'} : ();
my $scope = (!defined($name)) ? "anon" : ($::SCOPE || "has");
if (@{ $M->{signature} } > 1) {
$M->sorry("Multiple signatures on a regex NYI");
return;
}
my $isproto;
local $::symtext =
!defined($name) ? undef :
($name =~ /:sym<(.*)>/) ? $1 :
($name =~ /:(\w+)/) ? $1 :
undef; #XXX
my $unsymtext =
!defined($name) ? undef :
($name =~ /(.*):sym<.*>/) ? $1 :
($name =~ /(.*):\w+/) ? $1 :
undef;
if ($::MULTINESS eq 'proto') {
if ($M->{signature}[0] || !$M->{regex_block}{onlystar} || $scope ne 'has') {
$M->sorry("Only simple {*} protoregexes with no parameters are supported");
return;
}
$isproto = 1;
} else {
my $m2 = defined($::symtext) ? 'multi' : 'only';
if ($::MULTINESS && $::MULTINESS ne $m2) {
$M->sorry("Inferred multiness disagrees with explicit");
return;
}
}
if ($path && $scope ne 'our') {
$M->sorry("Putting a regex in a package requires using the our scope.");
return;
}
my $sig = $M->{signature}[0] ? $M->{signature}[0]{_ast}
: $cl->get_placeholder_sig($M);
if ($scope =~ /state|augment|supercede/) {
$M->sorry("Nonsensical scope $scope for regex");
return;
}
if ($scope eq 'our') {
$M->sorry("our regexes NYI");
return;
}
my $var = ($scope eq 'anon' || $scope eq 'has') ? $cl->gensym
: '&' . $name;
local $::parenid = 0;
my $ast = $M->{regex_block}{_ast};
if ($isproto) {
$ast = RxOp::ProtoRedis->new(name => $name);
}
my ($cn, $op) = $ast->term_rx;
$M->{_ast} = Op::SubDef->new(
var => $var,
method_too => ($scope eq 'has' ? $name : undef),
proto_too => ($scope eq 'has' ? $unsymtext : undef),
body => Body->new(
ltm => $ast->lad,
class => 'Regex',
type => 'regex',
signature => $sig->for_regex($cn),
do => $op));
}
sub regex_declarator { my ($cl, $M) = @_;
$M->{_ast} = $M->{regex_def}{_ast};
}
sub regex_declarator__S_regex {}
sub regex_declarator__S_rule {}
sub regex_declarator__S_token {}
# :: RxOp
sub atom { my ($cl, $M) = @_;
if ($M->{metachar}) {
$M->{_ast} = $M->{metachar}{_ast};
} else {
$M->{_ast} = RxOp::String->new(text => $M->Str,
igcase => $::RX{i}, igmark => $::RX{a});
}
}
sub quantified_atom { my ($cl, $M) = @_; # :: RxOp
my $atom = $M->{atom}{_ast};
my $ns = $M->{normspace}[0];
my $q = $M->{quantifier}[0] ? $M->{quantifier}[0]{_ast} : undef;
if ($::RX{r}) {
# no quantifier at all? treat it as :
$q //= { mod => '' };
# quantifier without explicit :? / :! gets :
$q->{mod} //= '';
}
if ($q->{simple}) {
$atom = RxOp::Quantifier->new(type => $q->{simple}, zyg => [$atom],
minimal => ($q->{mod} && $q->{mod} eq '?'));
}
if (defined $q->{mod} && $q->{mod} eq '') {
$atom = RxOp::Cut->new(zyg => [$atom]);
}
$M->{_ast} = $atom;
}
# :: Context hash interpreted by quantified_atom
sub quantifier {}
sub quantifier__S_Star { my ($cl, $M) = @_;
$M->{_ast} = { simple => '*', mod => $M->{quantmod}{_ast} };
}
sub quantifier__S_Plus { my ($cl, $M) = @_;
$M->{_ast} = { simple => '+', mod => $M->{quantmod}{_ast} };
}
sub quantifier__S_Question { my ($cl, $M) = @_;
$M->{_ast} = { simple => '?', mod => $M->{quantmod}{_ast} };
}
sub quantifier__S_Colon { my ($cl, $M) = @_;
$M->{_ast} = { mod => '' };
}
sub quantmod { my ($cl, $M) = @_;
my $t = $M->Str;
return if ($t eq '');
$t =~ s/://;
if ($t eq '+') {
$M->sorry('STD parses + as a quantmod but there is nothing at all in S05 to explain what it should _do_'); #XXX
return;
}
$M->{_ast} = $t;
}
sub quant_atom_list { my ($cl, $M) = @_;
$M->{_ast} = RxOp::Sequence->new(zyg =>
[ map { $_->{_ast} } @{ $M->{quantified_atom} } ]);
}
my %LISTrx_types = (
'&' => 'RxOp::Conj',
'|' => 'RxOp::Alt',
'&&' => 'RxOp::SeqConj',
'||' => 'RxOp::SeqAlt',
);
sub LISTrx { my ($cl, $M) = @_;
$M->{_ast} = $LISTrx_types{$M->{delims}[0]{sym}}->new(zyg =>
[ map { $_->{_ast} } @{ $M->{list} } ]);
}
sub regex_infix {}
sub regex_infix__S_Vert {}
sub regex_infix__S_VertVert {}
sub regex_infix__S_Amp {}
sub regex_infix__S_AmpAmp {}
sub metachar {}
sub metachar__S_sigwhite { my ($cl, $M) = @_;
$M->{_ast} = $::RX{s} ? RxOp::Sigspace->new : RxOp::Sequence->new;
}
sub metachar__S_unsp { my ($cl, $M) = @_;
$M->{_ast} = RxOp::Sequence->new;
}
sub metachar__S_Cur_Ly { my ($cl, $M) = @_;
$M->{_ast} = RxOp::VoidBlock->new(block => $M->{embeddedblock}{_ast});
}
sub metachar__S_mod { my ($cl, $M) = @_;
# most of these have only parse-time effects
$M->{_ast} = $M->{mod_internal}{_ast} // RxOp::Sequence->new;
}
sub metachar__S_ColonColon { my ($cl, $M) = @_;
$M->{_ast} = RxOp::CutLTM->new;
}
sub metachar__S_ColonColonColon { my ($cl, $M) = @_;
$M->{_ast} = RxOp::CutRule->new;
}
sub metachar__S_Bra_Ket { my ($cl, $M) = @_;
$M->{_ast} = RxOp::ConfineLang->new(zyg => [$M->{nibbler}{_ast}]);
}
sub metachar__S_Paren_Thesis { my ($cl, $M) = @_;
$M->{_ast} = RxOp::Capture->new(names => [undef], zyg => [
RxOp::ConfineLang->new(zyg => [$M->{nibbler}{_ast}])]);
}
sub metachar__S_LtParen { my ($cl, $M) = @_;
$M->{_ast} = RxOp::MarkFrom->new;
}
sub metachar__S_ThesisGt { my ($cl, $M) = @_;
$M->{_ast} = RxOp::MarkTo->new;
}
sub metachar__S_LtLt { my ($cl, $M) = @_;
$M->{_ast} = RxOp::LWB->new;
}
sub metachar__S_GtGt { my ($cl, $M) = @_;
$M->{_ast} = RxOp::RWB->new;
}
sub metachar__S_Fre { my ($cl, $M) = @_;
$M->{_ast} = RxOp::LWB->new;
}
sub metachar__S_Nch { my ($cl, $M) = @_;
$M->{_ast} = RxOp::RWB->new;
}
sub metachar__S_qw { my ($cl, $M) = @_;
$M->sorry("< > splitting NYI");
}
sub metachar__S_Lt_Gt { my ($cl, $M) = @_;
$M->{_ast} = $M->{assertion}{_ast};
}
sub metachar__S_Back { my ($cl, $M) = @_;
my $cc = $M->{backslash}{_ast};
$M->{_ast} = ref($cc) ?
RxOp::CClass->new(cc => CClass->build(@$cc),
igcase => $::RX{i}, igmark => $::RX{a}) :
RxOp::String->new(text => $cc,
igcase => $::RX{i}, igmark => $::RX{a});
}
sub metachar__S_Dot { my ($cl, $M) = @_;
$M->{_ast} = RxOp::Any->new;
}
sub metachar__S_Caret { my ($cl, $M) = @_;
$M->{_ast} = RxOp::StrStart->new;
}
sub metachar__S_CaretCaret { my ($cl, $M) = @_;
$M->{_ast} = RxOp::LineStart->new;
}
sub metachar__S_Dollar { my ($cl, $M) = @_;
$M->{_ast} = RxOp::StrEnd->new;
}
sub metachar__S_DollarDollar { my ($cl, $M) = @_;
$M->{_ast} = RxOp::LineEnd->new;
}
sub metachar__S_Single_Single { my ($cl, $M) = @_;
if (! $M->{quote}{_ast}->isa('Op::StringLiteral')) {
$M->sorry("Interpolating strings in regexes NYI");
return;
}
$M->{_ast} = RxOp::String->new(text => $M->{quote}{_ast}->text,
igcase => $::RX{i}, igmark => $::RX{a});
}
sub metachar__S_Double_Double { my ($cl, $M) = @_;
if (! $M->{quote}{_ast}->isa('Op::StringLiteral')) {
$M->sorry("Interpolating strings in regexes NYI");
return;
}
$M->{_ast} = RxOp::String->new(text => $M->{quote}{_ast}->text,
igcase => $::RX{i}, igmark => $::RX{a});
}
sub rxcapturize { my ($cl, $name, $rxop) = @_;
if (!$rxop->isa('RxOp::Capture')) {
# $<foo>=[ ] or ( ) or <foo>
return RxOp::Capture->new(names => [$name], zyg => [$rxop]);
}
# $<foo>=(...)
if (@{ $rxop->names } == 1 && !defined($rxop->names->[0])) {
return RxOp::Capture->new(names => [$name], zyg => $rxop->zyg);
}
return RxOp::Capture->new(names => [ $name, @{ $rxop->names } ],
zyg => $rxop->zyg);
}
sub do_cclass { my ($cl, $M) = @_;
my @cce = @{ $M->{cclass_elem} };
my $rxop;
for (@cce) {
my $sign = $_->{sign}->Str ne '-';
my $exp = $_->{quibble} ?
RxOp::CClassElem->new(cc => $_->{quibble}{_ast}) :
RxOp::CallMethod->new(name => $_->{name}->Str); # assumes no capture
if ($sign) {
$rxop = $rxop ? RxOp::SeqAlt->new(zyg => [ $exp, $rxop ]) : $rxop;
} else {
$rxop = RxOp::Sequence->new(zyg => [
RxOp::NotBefore->new(zyg => [ $exp ]),
$rxop // RxOp::Any->new]);
}
}
$M->{_ast} = $rxop;
}
sub cclass_elem {}
sub assertion {}
# This needs to be deconstructed by :method, so it needs a regular structure
sub assertion__S_name { my ($cl, $M) = @_;
my $name = $cl->unqual_longname($M->{longname},
"Qualified method calls NYI");
if ($M->{assertion}[0]) {
$M->{_ast} = $M->{assertion}[0]{_ast};
} else {
$M->{_ast} = RxOp::CallMethod->new(arglist =>
($M->{arglist}[0] ? $M->{arglist}[0]{_ast} :
$M->{nibbler}[0] ? [$M->{nibbler}[0]{_ast}] : []), name => $name);
}
$M->{_ast} = $cl->rxcapturize($name, $M->{_ast});
}
sub assertion__S_method { my ($cl, $M) = @_;
if ($M->{dottyop}) {
$M->sorry("Dottyop assertions NYI");
return;
}
if ($M->{assertion}{assertion}[0]) {
$M->sorry("Binding to a method doesn't work like that");
return;
}
if (!$M->{assertion}{_ast}->isa('RxOp::Capture')) {
$M->sorry("Internal error in assertion:method parse");
return;
}
$M->{_ast} = RxOp::Capture->new(names => [],
zyg => $M->{assertion}{_ast}->zyg);
}
sub assertion__S_Question { my ($cl, $M) = @_;
if ($M->{assertion}) {
$M->{_ast} = RxOp::Before->new(zyg => [$M->{assertion}{_ast}]);
} else {
$M->{_ast} = RxOp::Sequence->new;
}
}
sub assertion__S_Bang { my ($cl, $M) = @_;
if ($M->{assertion}) {
$M->{_ast} = RxOp::Before->new(zyg => [$M->{assertion}{_ast}]);
} else {
$M->{_ast} = RxOp::None->new;
}
}
sub assertion__S_Cur_Ly { my ($cl, $M) = @_;
$M->{_ast} = RxOp::CheckBlock->new(block => $M->{embeddedblock}{_ast});
}
*assertion__S_Bra = \&do_cclass;
*assertion__S_Minus = \&do_cclass;
*assertion__S_Plus = \&do_cclass;
# These have effects only in the parser, so undef ast is correct.
sub mod_value {}
sub mod_internal {}
sub mod_internal__S_Coloni {}
sub mod_internal__S_ColonBangi {}
sub mod_internal__S_ColoniParen_Thesis {}
sub mod_internal__S_Colon0i {}
sub mod_internal__S_Colons {}
sub mod_internal__S_ColonBangs {}
sub mod_internal__S_ColonsParen_Thesis {}
sub mod_internal__S_Colon0s {}
sub mod_internal__S_Colonr {}
sub mod_internal__S_ColonBangr {}
sub mod_internal__S_ColonrParen_Thesis {}
sub mod_internal__S_Colon0r {}
sub mod_internal__S_Colona {}
sub mod_internal__S_ColonBanga {}
sub mod_internal__S_ColonaParen_Thesis {}
sub mod_internal__S_Colon0a {}
sub backslash { my ($cl, $M) = @_;
if ($M->Str =~ /^[A-Z]$/) {
if (!ref($M->{_ast}) && length($M->{_ast}) != 1) {
$M->sorry("Improper attempt to negate a string");
return;
}
$M->{_ast} = CClass->enum($M->{_ast}) unless blessed $M->{_ast};
$M->{_ast} = $M->{_ast}->negate;
}
}
sub backslash__S_x { my ($cl, $M) = @_;
if ($M->{hexint}) {
$M->{_ast} = chr($M->{hexint}{_ast});
} else {
$M->{_ast} = join "", map { chr } @{ $M->{hexints}{_ast} };
}
}
sub backslash__S_o { my ($cl, $M) = @_;
if ($M->{octint}) {
$M->{_ast} = chr($M->{octint}{_ast});
} else {
$M->{_ast} = join "", map { chr } @{ $M->{octints}{_ast} };
}
}
sub backslash__S_Back { my ($cl, $M) = @_;
$M->{_ast} = $M->{text}->Str;
}
sub backslash__S_stopper { my ($cl, $M) = @_;
$M->{_ast} = $M->{text}->Str;
}
sub backslash__S_unspace { my ($cl, $M) = @_;
$M->{_ast} = "";
}
sub backslash__S_misc { my ($cl, $M) = @_;
$M->{_ast} = $M->{text} // $M->{litchar}->Str;
}
# XXX h, v, s, needs spec clarification
sub backslash__S_0 { my ($cl, $M) = @_; $M->{_ast} = "\0" }
sub backslash__S_a { my ($cl, $M) = @_; $M->{_ast} = "\a" }
sub backslash__S_b { my ($cl, $M) = @_; $M->{_ast} = "\b" }
sub backslash__S_d { my ($cl, $M) = @_; $M->{_ast} = $CClass::Digit }
sub backslash__S_e { my ($cl, $M) = @_; $M->{_ast} = "\e" }
sub backslash__S_f { my ($cl, $M) = @_; $M->{_ast} = "\f" }
sub backslash__S_h { my ($cl, $M) = @_; $M->{_ast} = $CClass::HSpace }
sub backslash__S_n { my ($cl, $M) = @_; $M->{_ast} = "\n" }
sub backslash__S_r { my ($cl, $M) = @_; $M->{_ast} = "\r" }
sub backslash__S_s { my ($cl, $M) = @_; $M->{_ast} = $CClass::Space }
sub backslash__S_t { my ($cl, $M) = @_; $M->{_ast} = "\t" }
sub backslash__S_v { my ($cl, $M) = @_; $M->{_ast} = $CClass::VSpace }
sub backslash__S_w { my ($cl, $M) = @_; $M->{_ast} = $CClass::Word }
sub escape {}
sub escape__S_Back { my ($cl, $M) = @_;
$M->{_ast} = $M->{item}{_ast};
}
sub escape__S_Cur_Ly { my ($cl, $M) = @_;
$M->{_ast} = $M->{embeddedblock}{_ast};
}
sub escape__S_Dollar { my ($cl, $M) = @_;
$M->{_ast} = $M->{EXPR}{_ast};
}
sub escape__S_At { my ($cl, $M) = @_;
$M->{_ast} = $M->{EXPR}{_ast};
}
sub escape__S_Percent { my ($cl, $M) = @_;
$M->{_ast} = $M->{EXPR}{_ast};
}
sub escape__S_ch { my ($cl, $M) = @_;
$M->{_ast} = $M->{ch}->Str;
}
sub escape__S_ws { my ($cl, $M) = @_;
$M->{_ast} = "";
}
sub escape__S_DotDot { my ($cl, $M) = @_;
$M->{_ast} = \"DotDot"; #yuck
}
sub nibbler { my ($cl, $M) = @_;
if ($M->isa('STD::Regex')) {
$M->{_ast} = $M->{EXPR}{_ast};
} elsif ($M->isa('Niecza::Grammar::CgOp')) {
# XXX We don't interpret the code, so we can't tell if it's actually
# using variables, but still, it probably is.
if ($::SAFEMODE) {
$M->sorry('Q:CgOp not allowed in safe mode');
return;
}
for my $k (keys %$::CURLEX) {
$::CURLEX->{$k}{used} = 1 if $k =~ /^[\@\%\&\$]\w/;
}
$M->{_ast} = Op::CgOp->new(node($M), optree => $M->{cgexp}{_ast});
} elsif ($M->can('ccstate')) { #XXX XXX try to catch cclasses
my @nib = @{ $M->{nibbles} };
my @bits = map { $_->{_ast} } @nib;
for (my $i = 0; $i < @bits; $i++) {
my $t = ref($bits[$i]);
if (!$t) {
if (length($bits[$i]) > 1) {
$nib[$i]->sorry("Cannot use a string in a character class");
$bits[$i] = "";
}
} elsif ($t eq 'SCALAR') {
# .. hack
} elsif ($t eq 'CClass') {
# also ok
} else {
$nib[$i]->sorry("Cannot use an interpolation in a character class");
$bits[$i] = "";
}
if ($bits[$i] eq "") {
splice @bits, $i, 1;
splice @nib, $i, 1;
$i--;
}
}
for (my $i = 0; $i < @bits; $i++) {
next unless ref($bits[$i]) && ref($bits[$i]) eq 'SCALAR';
for ($i-1, $i + 1) {
if (ref($bits[$_])) {
$nib[$_]->sorry("Bad range endpoint");
$bits[$_] = "\0";
}
}
splice @bits, $i-1, 3, CClass->range($bits[$i-1], $bits[$i+1]);
splice @nib, $i-1, 2;
$i--;
}
$M->{_ast} = $CClass::Empty;
$M->{_ast} = $M->{_ast}->plus($_) for @bits;
say(YAML::XS::Dump($M->{_ast}));
} else {
# garden variety nibbler
my @bits;
for my $n (@{ $M->{nibbles} }) {
my $bit = $n->isa('Str') ? $n->{TEXT} : $n->{_ast};
if (ref($bit) && ref($bit) eq 'CClass') {
$n->sorry("Tried to use a character class in a string");
$bit = "";
}
# this *might* belong in an optimization pass
if (!blessed($bit) && @bits && !blessed($bits[-1])) {
$bits[-1] .= $bit;
} else {
push @bits, $bit;
}
}
push @bits, '' unless @bits;
@bits = map { blessed($_) ? $_ : Op::StringLiteral->new(node($M),
text => $_) } @bits;
$M->{_ast} = (@bits == 1) ? $bits[0] :
Op::CallSub->new(node($M),
invocant => Op::Lexical->new(name => '&infix:<~>'),
positionals => \@bits);
}
}
sub circumfix { }
sub circumfix__S_Lt_Gt { my ($cl, $M) = @_;
my $sl = $M->{nibble}{_ast};
if (!$sl->isa('Op::StringLiteral')) {
$M->sorry("Runtime word splitting NYI");
return;
}
my @tok = split ' ', $sl->text;
@tok = map { Op::StringLiteral->new(node($M), text => $_) } @tok;
$M->{_ast} = (@tok == 1) ? $tok[0] :
Op::CallSub->new(node($M),
invocant => Op::Lexical->new(name => '&infix:<,>'),
positionals => \@tok);
$M->{qpvalue} = '<' . join(" ", map { $_->text } @tok) . '>'; # XXX what if there are spaces or >
}
sub circumfix__S_LtLt_GtGt { goto &circumfix__S_Lt_Gt }
sub circumfix__S_Paren_Thesis { my ($cl, $M) = @_;
my @kids = grep { defined } @{ $M->{semilist}{_ast} };
if (@kids == 1 && $kids[0]->isa('Op::WhateverCode')) {
# XXX in cases like * > (2 + *), we *don't* want the parens to disable
# syntactic specialization, since they're required for grouping
$M->{_ast} = $kids[0];
} else {
$M->{_ast} = Op::StatementList->new(node($M), children =>
[ map { Op::Paren->new(inside => $_) } @kids ]);
}
}
sub circumfix__S_Cur_Ly { my ($cl, $M) = @_;
$M->{pblock}{_ast}->type('bare');
$M->{_ast} = Op::BareBlock->new(node($M), var => $cl->gensym,
body => $M->{pblock}{_ast});
}
sub infixish { my ($cl, $M) = @_;
$M->sorry("Metaoperators NYI") if $M->{infix_postfix_meta_operator}[0];
}
sub INFIX { my ($cl, $M) = @_;
my $s = '&infix:<' . $M->{infix}{sym} . '>';
my ($st,$l,$r) = $cl->whatever_precheck($s, $M->{left}{_ast},
$M->{right}{_ast});
if ($s eq '&infix:<?? !!>') { # XXX macro
$M->{_ast} = Op::Conditional->new(node($M), check => $l,
true => $M->{middle}{_ast}, false => $r);
} elsif ($s eq '&infix:<,>') {
#XXX STD bug causes , in setting to be parsed as left assoc
my @r;
push @r, $l->isa('Op::SimpleParcel') ? @{ $l->items } : ($l);
push @r, $r->isa('Op::SimpleParcel') ? @{ $r->items } : ($r);
$M->{_ast} = Op::SimpleParcel->new(items => \@r);
} else {
$M->{_ast} = Op::CallSub->new(node($M),
invocant => Op::Lexical->new(node($M), name => $s),
positionals => [ $l, $r ]);
if ($s eq '&infix:<=>' && $l->isa('Op::Lexical') && $l->state_decl) {
# Assignments (and assign metaops, but we don't do that yet) to has
# and state declarators are rewritten into an appropriate phaser
my $cv = $cl->gensym;
$M->{_ast} = Op::StatementList->new(node($M), children => [
Op::Start->new(condvar => $cv, body => $M->{_ast}),
Op::Lexical->new(name => $l->name)]);
}
}
$M->{_ast} = $cl->whatever_postcheck($M, $st, $M->{_ast});
}
sub CHAIN { my ($cl, $M) = @_;
my $op = '&infix:<' . $M->{chain}[1]{sym} . '>';
my @args;
for my $i (0 .. scalar @{ $M->{chain} }) {
if (($i % 2) == 0) {
push @args, $M->{chain}[$i]{_ast};
}
}
my ($st, @vargs) = $cl->whatever_precheck($op, @args);
my @pairwise;
while (@vargs >= 2) {
push @pairwise, Op::CallSub->new(node($M),
invocant => Op::Lexical->new(name => $op),
positionals => [ $vargs[0], $vargs[1] ]);
shift @vargs;
}
$M->{_ast} = (@pairwise > 1) ? Op::ShortCircuit->new(node($M),
kind => '&&', args => \@pairwise) : $pairwise[0];
$M->{_ast} = $cl->whatever_postcheck($M, $st, $M->{_ast});
}
my %loose2tight = (
'&&' => '&&', '||' => '||', '//' => '//', 'andthen' => 'andthen',
'orelse' => '//', 'and' => '&&', 'or' => '||',
);
sub LIST { my ($cl, $M) = @_;
if ($M->isa('STD::Regex')) {
goto &LISTrx;
}
# STD guarantees that all elements of delims have the same sym
# the last item may have an ast of undef due to nulltermish
my $op = $M->{delims}[0]{sym};
my ($st, @pos) = $cl->whatever_precheck("&infix:<$op>",
grep { defined } map { $_->{_ast} } @{ $M->{list} });
if ($op eq ',') {
$M->{_ast} = Op::SimpleParcel->new(node($M), items => \@pos);
} elsif ($loose2tight{$op}) {
$M->{_ast} = Op::ShortCircuit->new(node($M), kind => $loose2tight{$op},
args => \@pos);
} else {
$M->{_ast} = Op::CallSub->new(node($M),
invocant => Op::Lexical->new(name => "&infix:<$op>"),
positionals => \@pos);
}
$M->{_ast} = $cl->whatever_postcheck($M, $st, $M->{_ast});
}
sub POSTFIX { my ($cl, $M) = @_;
my $op = $M->{_ast};
my ($st, $arg) = $cl->whatever_precheck('', $M->{arg}{_ast});
if ($op->{postfix}) {
$M->{_ast} = Op::CallSub->new(node($M),
invocant => Op::Lexical->new(name => '&postfix:<' . $op->{postfix} . '>'),
positionals => [ $arg ]);
} elsif ($op->{postcircumfix}) {
$M->{_ast} = Op::CallSub->new(node($M),
invocant => Op::Lexical->new(name => '&postcircumfix:<' .
$op->{postcircumfix} . '>'),
positionals => [ $arg, @{ $op->{args} } ]);
} elsif ($op->{name} && $op->{name} =~ /^(?:HOW|WHAT)$/) {
if ($op->{args}) {
$M->sorry("Interrogative operator " . $op->{name} .