-
Notifications
You must be signed in to change notification settings - Fork 3
/
Factory.pm6
7600 lines (7126 loc) · 169 KB
/
Factory.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
=begin pod
=begin NAME
Perl6::Parser::Factory - Builds client-ready Perl 6 data tree
=end NAME
=begin DESCRIPTION
Generates the complete tree of Perl6-ready objects, shielding the client from the ugly details of the internal L<nqp> representation of the object. None of the elements, hash values or children should have L<NQPMatch> objects associated with them, as trying to view them can cause nasty crashes.
Objects are divided into two general categories, C<.textual> and C<.structural>. Structural items don't appear in the program text, because they just group things like blocks in C<grep { }> or argument lists in C<foo( 1, 2 )>.
The metaclasses look like this:
L<Perl6::Element>
L<Perl6::Visible> - Anything that's not whitespace
L<Perl6::Documentation> - Documentation
L<Perl6::Pod>
L<Perl6::Comment>
L<Perl6::Invisible> - Whitespace
L<Perl6::WS> - Horizontal whitespace
L<Perl6::Newline> - Vertical whitespace
(note: I'm using C<class Perl6::Foo is Perl6::Bar> to denote classes whose parents are empty, "virtual".)
Walking a list of objects is as simple as:
my @token = Seq.new( $parser.iterator( $source ) );
for grep { .textual }, @token {
print $_.content;
}
Stripping comments and POD:
my @token = Seq.new( $parser.iterator( $source ) );
for grep { .textual and not Perl6::Documentation }, @token {
print $_.content;
}
See the section OBJECT_TREE for the full hierarchy.
=end DESCRIPTION
=begin OBJECT_TREE
L<Perl6::Element>
L<Perl6::Visible> - Anything that's not whitespace
L<Perl6::Invisible> - Whitespace
L<Perl6::WS> - Horizontal whitespace
L<Perl6::Newline> - Vertical whitespace
=end OBJECT_TREE
=begin GENERAL_NOTES
The L<Perl6::Variable> classes all have a required C<.sigil> method which returns the sigil associated with that variable (and an C<.twigil> method should that be needed.) These are methods rather than attributes to keep the clutter down when using C<.dump> or C<.perl> debugging methods.
Keeping in mind that clients of the L<Perl6::Element> hierarchy may want to create or edit these subclasses, I'm restricting constant methods to just those things that users shouldn't need to change, for instance you shouldn't need to create a L<Perl6::Number::Binary> with a base of 3.
=end GENERAL_NOTES
=begin CLASSES
=item L<Perl6::Element>
Please see the module's documentation for the full scoop, but here's a terse summary of what's available.
=item C<is-root>
Is the element the root? Usually this is a L<Perl6::Document>.
=cut
=item C<is-start>, C<is-end>
Are we at the start or end of the stream?
=cut
=item C<is-start-leaf>, C<is-end-leaf>
Are we at the start or end B<leaf> 0f the stream?
=item C<is-leaf>, C<is-twig>
Is the element a leaf (a single token) or a composite of multiple tokens? (a twig)
Tokens are things like C<12,3>, C<'foo'> or C<+>. Everything else is a C<twig>, which is a list of multiple tokens surrounded by some sort of delimiter, whether it be a statement boundary or a here-doc.
=cut
=item C<next>, C<previous>, C<parent>, C<child($n)>
Go from one element to the next in sequence. There are no "end of stream" or "beginning of stream" markers, for that please use the C<is-end> and C<is-start> methods. This is mainly so that I can keep the guts statically-typed to help catch developer errors. Otherwise I'd have to use a compound type or constraints, and I want to keep the guts as simple as possible.
=item C<first-child>, C<last-child>
Go from a node to its first child, or last child.
=cut
=item C<remove-node>, C<insert-node-before>, C<insert-node-after>
Remove the node and children, insert a node before the element, or after.
=cut
=item C<replace-node-with>
Replace the current node with the contents.
=cut
=item L<Perl6::Number>
All numbers, whether decimal, rational, radix-32 or complex, fall under this class. You should be able to compare C<$x> to L<Perl6::Number> to do a quick check. Under this lies the teeming complexity of the full Perl 6 numeric lattice.
Binary, octal, hex and radix numbers have an additional C<$.headless> attribute, which gives you the binary value without the leading C<0b>. Radix numbers (C<:13(19a)>) have an additional C<$.radix> attribute specifying the radix, and its C<$.headless> attribute works as you'd expect, delivering C<'19a'> without the surrounding C<':13(..)'>.
Imaginary numbers have an alternative C<$.tailless> attribute which gives you the decimal value without the trailing C<i> marker.
Rather than spelling out a huge list, here's how the hierarchy looks:
L<Perl6::Number>
L<Perl6::Number::Binary>
L<Perl6::Number::Octal>
L<Perl6::Number::Decimal>
L<Perl6::Number::Decimal::FloatingPoint>
L<Perl6::Number::Hexadecimal>
L<Perl6::Number::Radix>
L<Perl6::Number::Imaginary>
There likely won't be a L<Perl6::Number::Complex>. While it's relatively easy to figure out that C<my $z = 3+2i;> is a complex number, who's to say what the intet behind C<my $z = 3*$a+2i> is, or a more complex high-order polynomial. Best to just assert that C<2i> is an imaginary number, and leave it to the client to form the proper interpretation.
=cut
=item L<Perl6::Variable>
The catch-all for Perl 6 variable types.
Scalar, Hash, Array and Callable subtypes have C<$.headless> attributes with the variable's name minus the sigil and optional twigil. They also have the C<.sigil> method, and C<.twigil> optional method for classes that have them.
L<Perl6::Variable>
L<Perl6::Variable::Scalar>
L<Perl6::Variable::Scalar::Dynamic>
L<Perl6::Variable::Scalar::CompileTime>
L<Perl6::Variable::Scalar::MatchIndex>
L<Perl6::Variable::Scalar::Positional>
L<Perl6::Variable::Scalar::Named>
L<Perl6::Variable::Scalar::Pod>
L<Perl6::Variable::Scalar::SubLanguage>
L<Perl6::Variable::Scalar::Contextualizer>
L<Perl6::Variable::Hash>
(and the same subtypes)
L<Perl6::Variable::Array>
(and the same subtypes)
L<Perl6::Variable::Callable>
(and the same subtypes)
=cut
=item L<Perl6::Variable::Scalar::Contextualizer>
Children: L<Perl6::Variable::Scalar::Contextualizer> and so forth.
(a side note - These really should be L<Perl6::Variable::Scalar::Contextualizer>, but that would mean that these were both C<.textual> (from the parent L<Perl6::Variable::Scalar> and Branching because they have children). Resolving this would mean removing the L<Perl6::Textual> role from the L<Perl6::Variable::Scalar> class, which means that I either have to create a longer class name for L<Perl6::Variable::JustAPlainScalarVariable> or manually add the L<Perl6::Textual>'s contents to the L<Perl6::Variable::Scalar>, and forget to update it when I change my mind in a few weeks' time about what L<Perl6::Textual> does. Adding a separate class for this seems the lesser of two evils, especially given how often they'll appear in "real world" code.)
=cut
=item L<Perl6::Sir-Not-Appearing-In-This-Statement>
By way of caveat: If you stick to the APIs mentioned in the documentation, you should never deal with objects in this class. Ever. If you see this, you're probably debugging internals of this module, and if you're not, please send the author a snippet of code B<and> the associated Perl 6 code that replicates it.
While you should stick to the published APIs, there are of course times when you need to get your proverbial hands dirty. Read on if you want the scoop.
Your humble author has gone to a great deal of trouble to assure that every character of user code is parsed and represented in some fashion, and the internals keep track of the text down to the by..chara...glyph level. More to the point, each Perl6::Parser element has its own start and end point.
The internal method C<check-tree> does a B<rigorous> check of the entire parse tree, at least while self-tests are runnin. Relevant to the discussion at hand are two things: Checking that each element has exactly the number of glyphs that its start and end claim that it has, and checking that each element exactly overlaps its neighbos, with no gaps.
Look at a sample contrived here-doc:
# It starts
# V-- here. V--- But does it end here?
say Q:to[_END_]; say 2 +
First line
_END_
# ^--- Wait, it ends here, in the *middle* of the next statement.
5;
While a contrived example, it'll do to make my points. The first rule the internals assert is that tokens start at glyph X and end at glyph Y. Here-docs break that rule right off the bat. They start at the C<Q:to[_END_]>, skip a bunch of glyphs ahead, then stop at the terninal C<_END_> several lines later.
So, B<internally>, here-docs start at the C<Q> of C<Q:to[_END_]> and end at the closing C<]> of C<Q:to[_END_]>. Any other text that it might have is stored somewhere that the self-test algorithm won't find it. So if you're probing the L<Here-Doc> class for its text directly (which the author disrecommends), you won't find it all there.
There also can't be any gaps between two tokens, and again, here-docs break that rule. If we take it at face value, C<Q:to[_END_]> and C<First line.._END_> are two separate tokens, simply because there's an intervening 'say 2', which (to make matters worse) is in a different L<Perl6::Statement>.
Now L<Perl6::Sir-Not-Appearing-In-This-Statement> comes into play. Since tokens can only be a single block of text, we can't have both the C<Q:to[_END_]> and C<First line> be in the same token; and if we did break them up (which we do), they can't be the same class, because we'd end up with multiple heredocs later on when parsing.
So our single here-doc internally becomes two elements. First comes the official L<Perl6::String> element, which you can query to get the delimiter (C<_END_>), full text (C<Q:to[_END_]>\nFirst line\n_END_> and the body text, C<First line>.
Later on, we run across the actual body of the here-doc, and rather than fiddling with the validation algorithms, we drop in L<Sir-Not-Appearing-In-This-Statement>, a "token" that doesn't exist. It has the bounds of the C<First line\n_END_> text, so that it appears for our validation algorithm. But it's special-cased to not appear in the C<dump-tree> method, or anything that deals with L<Perl6::Statement>s because while B<syntactically> it's in a statement's boundary, it's not B<semantically> part of the statement it "appears" in.
Whew. If you're doing manipulation of statements, B<now> hopefully you'll see why the author recommends sticking to the methods in the API. Eventually this little kink may get ironed out and here-docs may be relegated to a special case somewhere, but not today.
=cut
=end CLASSES
=begin ROLES
=item L<Twig>
Lets the tree walking utilities know this node is a decision point.
=cut
=item C<.textual>
Represents things such as numbers that are a token unto themselves.
Classes such as C<Perl6::Number> and C<Perl6::Quote> set this attribute in order to declare that they represent stand-alone tokens. Any class that uses this can expect a C<$.content> member to contain the full text of the token, whether it be a variable such as C<$a> or a 50-line heredoc string.
Classes can have custom attributes such as a number's radix value, or a string's delimiters, but they'll always have a C<$.content> value.
=cut
=item Perl6::Branch
Represents things such as lists and circumfix operators that have children.
Anything that's not C<.textual> wil have this role mixed in, and provide a C<@.child> accessor to get at, say, elements in a list or the expressions in a standalone subroutine.
Child elements aren't restricted to leaves, because a document is a tree the C<@.child> elements can be anything, even including the class itself. Although not the object itself, to avoid recursive loops.
=cut
=end ROLES
=begin DEVELOPER_NOTES
Just to keep the hierarchy reasonably clean, classes do only the preprocessing necessary to generate the C<:content()> attribute. Everything else is done by methods. See L<Perl6::PackageName> and the L<Perl6::Variable> hierarchy for examples.
=end DEVELOPER_NOTES
=begin DEBUGGING_NOTES
Should you brave the internals in search of a missing term, the first thing you should probably do is get an idea of what your parse tree looks like with the C<.dump> method. After that, you should be aware that C<.dump> only displays keys that actually have content, so there may be other keys that are merely defined. Those go in the third argument of C<.assert-hash-keys>.
=end DEBUGGING_NOTES
=end pod
my role Movement {
method next( Int $count = 1 ) {
my $node = self;
$node = $node.next-node for ^$count;
$node;
}
method next-leaf( Int $count = 1 ) {
my $node = self;
my $_count = $count;
while !$node.is-end and $_count > 0 {
$node = $node.next;
$_count-- if $node.is-leaf;
}
return Any unless $node.is-leaf;
$node;
}
method previous( Int $count = 1 ) {
my $node = self;
$node = $node.previous-node for ^$count;
$node;
}
method previous-leaf( Int $count = 1 ) {
my $node = self;
my $_count = $count;
while !$node.is-start and $_count > 0 {
$node = $node.previous;
$_count-- if $node.is-leaf;
}
return Any unless $node.is-leaf;
$node;
}
method parent( Int $count = 1 ) {
my $node = self;
$node = $node.parent-node for ^$count;
$node;
}
method first-child {
my $node = self;
return Any unless $node.is-twig;
$node = $node.child.[0];
$node;
}
method last-child {
my $node = self;
return Any unless $node.is-twig;
$node = $node.child.[*-1];
$node;
}
}
class Perl6::Element {
also does Movement;
# .from and .to are the start glyph and (one after) the end glyph
# of the token or sequence in the file.
#
has Int $.from is required is rw = 0;
has Int $.to is required is rw = 0;
# Debugging aide to let anyone interested in the internals jump
# directly to the code that generates the token.
#
has $.factory-line-number;
# Links to the previous and next node, along with up and down the
# tree.
#
# These *may* go away if I choose to rely more on the iterator, and
# you'll need to use the iterator method to get a sequence of
# tokens.
#
has Perl6::Element $.next-node is rw;
has Perl6::Element $.previous-node is rw;
has Perl6::Element $.parent-node is rw;
# Should only be run only flattened element lists.
# This is because it does nothing WRT .child.
# And by design, it has no access to that anyway.
#
method _add-offset( Perl6::Element $node, Int $delta ) {
my $head = $node;
while !$head.is-end {
$head.to += $delta;
$head.from += $delta;
$head = $head.next;
}
}
# Remove just this node.
#
method remove-node {
self._add-offset( self.next, -( $.to - $.from ) ) if
$*UPDATE-RANGES;
if self.is-end {
self.previous.next-node = self.previous;
}
else {
self.next.previous-node = self.previous;
self.previous.next-node = self.next;
}
$.next-node = self;
$.previous-node = self;
return self;
}
method replace-node-with( Perl6::Element $node ) {
my $cur-length = $.to - $.from;
my $new-length = $node.to - $node.from;
self._add-offset( self.next, $new-length - $cur-length ) if
$*UPDATE-RANGES;
if self.is-start {
$node.previous-node = $node;
$node.parent-node = self.parent;
$node.next-node = self.next;
self.next.previous-node = $node;
}
elsif self.is-end {
$node.previous-node = self.previous;
$node.parent-node = self.parent;
$node.next-node = $node;
self.previous.next-node = $node;
}
else {
$node.previous-node = self.previous;
$node.parent-node = self.parent;
$node.next-node = self.next;
self.previous.next-node = $node;
self.next.previous-node = $node;
}
return $node;
}
method insert-node-before( Perl6::Element $node ) {
self._add-offset( self, $.to - $.from ) if
$*UPDATE-RANGES;
$node.next-node = self;
if self.is-start {
$node.previous-node = $node;
$node.parent-node = $node;
}
else {
$node.previous-node = self.previous;
$node.parent-node = self.parent;
self.previous.next-node = $node;
}
$.previous-node = $node;
}
method insert-node-after( Perl6::Element $node ) {
self._add-offset( self.next, $.to - $.from ) if
$*UPDATE-RANGES;
$node.parent-node = self.parent;
$node.previous-node = self;
if self.is-end {
$node.next-node = $node;
}
else {
$node.next-node = self.next;
self.next.previous-node = $node;
}
$.next-node = $node;
}
}
my role Ordered-Tree {
method is-root returns Bool {
self.parent === self;
}
method is-end returns Bool {
self.next === self;
}
method is-start returns Bool {
self.previous === self;
}
}
my role Leaf {
also does Ordered-Tree;
method is-leaf returns Bool { True }
method is-twig returns Bool { False }
method is-start-leaf returns Bool {
return True if self.is-start;
return True unless self.previous-leaf;
return False;
}
method is-end-leaf returns Bool {
return True if self.is-end;
return True unless self.next-leaf;
return False;
}
}
my role Twig {
also does Ordered-Tree;
# By default, no Twig can be a leaf, so these are all False.
#
method is-leaf returns Bool { False }
method is-start-leaf returns Bool { False }
method is-end-leaf returns Bool { False }
method is-twig returns Bool { True }
method is-empty returns Bool { @.child.elems == 0 }
method first returns Perl6::Element { @.child[0] }
method last returns Perl6::Element { @.child[*-1] }
}
my role Textual {
also does Leaf;
method textual returns Bool { True }
method structural returns Bool { False }
}
my role Structural {
method textual returns Bool { False }
method structural returns Bool { True }
}
my role Token {
has Str $.content is required is rw;
method to-string returns Str {
~$.content
}
}
my role Constructor-from-match {
method from-match( Mu $p ) returns Perl6::Element {
self.bless(
:factory-line-number( callframe(1).line ),
:from( $p.from ),
:to( $p.to ),
:content( $p.Str )
)
}
}
my role Constructor-from-int {
method from-int( Int $from, Str $str ) returns Perl6::Element {
self.bless(
:factory-line-number( callframe(1).line ),
:from( $from ),
:to( $from + $str.chars ),
:content( $str )
)
}
}
my role Constructor-from-sample {
method from-sample( Mu $p, Str $token ) returns Perl6::Element {
$p.Str ~~ m{ ($token) };
my Int $left-margin = $0.from;
self.bless(
:factory-line-number( callframe(1).line ),
:from( $left-margin + $p.from ),
:to( $left-margin + $p.from + $token.chars ),
:content( $token )
)
}
}
class Perl6::Visible is Perl6::Element { }
class Perl6::Operator is Perl6::Visible { }
class Perl6::String is Perl6::Visible { }
my role BasicTextual {
also does Textual;
also does Token;
}
# Don't refactor documentation just yet, as POD should be more complex than
# just a raw text block.
#
class Perl6::Documentation is Perl6::Visible { }
class Perl6::Invisible is Perl6::Element {
also does BasicTextual;
also does Constructor-from-int;
}
class Perl6::Element-List {
has Perl6::Element @.child;
method fall-through( Mu $p ) {
if $*FALL-THROUGH {
my %classified = classify {
$p.hash.{$_}.Str ?? 'with' !! 'without'
}, $p.hash.keys;
my Str @keys-with-content = @( %classified<with> );
my Str @keys-without-content =
@( %classified<without> );
$*ERR.say( "With content: {@keys-with-content.gist}" );
$*ERR.say(
"Without content: {@keys-without-content.gist}"
);
die;
}
else {
Perl6::Catch-All.from-match( $p );
}
}
multi method append( Perl6::Element $element ) {
@.child.append( $element );
}
multi method append( Perl6::Element-List $element-list ) {
@.child.append( $element-list.child )
}
}
class Perl6::Balanced is Perl6::Visible {
also does BasicTextual;
also does Constructor-from-int;
}
class Perl6::Balanced::Enter is Perl6::Balanced { }
class Perl6::Balanced::Exit is Perl6::Balanced { }
class Perl6::Block::Enter is Perl6::Balanced::Enter { }
class Perl6::Block::Exit is Perl6::Balanced::Exit { }
my role Constructor-Enter-from-int {
method Enter-from-int( Int $from, Str $content ) {
return ( $content eq Q'{' ) ??
Perl6::Block::Enter.from-int( $from, $content ) !!
Perl6::Balanced::Enter.from-int( $from, $content );
}
}
my role Constructor-Exit-from-int {
method Exit-from-int( Int $from, Str $content ) {
return ( $content eq Q'}' ) ??
Perl6::Block::Exit.from-int( $from, $content ) !!
Perl6::Balanced::Exit.from-int( $from, $content );
}
}
my role Constructor-from-match-child {
method from-match( Mu $p, Perl6::Element-List $child )
returns Perl6::Element {
my $_child = Perl6::Element-List.new;
$p.Str ~~ m{ ^ (.) .* (.) $ };
$_child.append(
self.Enter-from-int( $p.from, $0.Str )
);
$_child.append( $child );
$_child.append(
self.Exit-from-int(
$p.to - $1.Str.chars,
$1.Str
)
);
self.bless(
:factory-line-number( callframe(1).line ),
:from( $p.from ),
:to( $p.to ),
:child( $_child.child )
)
}
}
my role Constructor-from-delims {
method from-delims(
Mu $p, Str $front, Str $back, Perl6::Element-List $child )
returns Perl6::Element {
my $_child = Perl6::Element-List.new;
$_child.append(
self.Enter-from-int( $p.from, $front )
);
$_child.append( $child );
$_child.append(
self.Exit-from-int(
$p.to - $back.chars,
$back
)
);
self.bless(
:factory-line-number( callframe(1).line ),
:from( $p.from ),
:to( $p.to ),
:child( $_child.child )
)
}
}
my role Constructor-from-outer-match {
method from-outer-match( Mu $p, Perl6::Element-List $child )
returns Perl6::Element {
my $_child = Perl6::Element-List.new;
my $x = $p.orig.substr( 0, $p.from );
$x ~~ m{ (.) ( \s* ) $ };
my $left-edge = $0.Str;
my $left-margin = $1.Str.chars;
$x = $p.orig.substr( $p.to );
$x ~~ m{ ^ ( \s* ) (.) };
my $right-edge = $1.Str;
my $right-margin = $0.Str.chars;
$_child.append(
self.Enter-from-int(
$p.from - $left-margin - $left-edge.chars,
$left-edge
)
);
$_child.append( $child );
$_child.append(
self.Exit-from-int(
$p.to + $right-margin,
$right-edge
)
);
self.bless(
:factory-line-number( callframe(1).line ),
:from( $p.from - $left-margin - $left-edge.chars ),
:to( $p.to + $right-margin + $right-edge.chars ),
:child( $_child.child )
)
}
}
my role Constructor-from-outer-int {
method from-outer-int( Mu $p, Int $from, Str $str,
Perl6::Element-List $child )
returns Perl6::Element {
my $to = $from + $str.chars;
my $_child = Perl6::Element-List.new;
my $x = $p.orig.substr( 0, $from );
$x ~~ m{ (.) ( \s* ) $ };
my $left-edge = $0.Str;
my $left-margin = $1.Str.chars;
$x = $p.orig.substr( $to );
$x ~~ m{ ^ ( \s* ) (.) };
my $right-edge = $1.Str;
my $right-margin = $0.Str.chars;
$_child.append(
self.Enter-from-int(
$from - $left-margin - $left-edge.chars,
$left-edge
)
);
$_child.append( $child );
$_child.append(
self.Exit-from-int(
$to + $right-margin,
$right-edge
)
);
self.bless(
:factory-line-number( callframe(1).line ),
:from( $from - $left-margin - $left-edge.chars ),
:to( $to + $right-margin + $right-edge.chars ),
:child( $_child.child )
)
}
}
my role Constructor-from-int-balanced {
method from-int( Int $from, Str $str, Perl6::Element-List $child )
returns Perl6::Element {
my $_child = Perl6::Element-List.new;
$str ~~ m{ ^ (.) .* (.) $ };
$_child.append(
self.Enter-from-int( $from, $0.Str )
);
$_child.append( $child );
$_child.append(
self.Exit-from-int(
$from + $str.chars - 1,
$1.Str
)
);
self.bless(
:factory-line-number( callframe(1).line ),
:from( $from ),
:to( $from + $str.chars ),
:child( $_child.child )
)
}
}
my role Child {
has Perl6::Element @.child;
}
my role Branching does Child {
method to-string returns Str {
join( '', map { .to-string( ) }, @.child )
}
}
class Perl6::Catch-All is Perl6::Visible {
also does BasicTextual;
}
class Perl6::Whatever is Perl6::Visible {
also does BasicTextual;
also does Constructor-from-int;
}
class Perl6::Loop-Separator is Perl6::Visible {
also does BasicTextual;
also does Constructor-from-int;
}
class Perl6::Dimension-Separator is Perl6::Visible {
also does BasicTextual;
also does Constructor-from-int;
}
class Perl6::Semicolon is Perl6::Visible {
also does BasicTextual;
also does Constructor-from-int;
}
class Perl6::Backslash is Perl6::Visible {
also does BasicTextual;
also does Constructor-from-match;
also does Constructor-from-int;
}
class Perl6::String::Enter is Perl6::Balanced::Enter { }
class Perl6::String::Exit is Perl6::Balanced::Exit { }
class Perl6::Operator::Hyper is Perl6::Operator {
also does Structural;
also does Branching;
also does Twig;
also does Constructor-Enter-from-int;
also does Constructor-Exit-from-int;
also does Constructor-from-outer-match;
also does Constructor-from-outer-int;
also does Constructor-from-delims;
}
class Perl6::Operator::Prefix is Perl6::Operator {
also does BasicTextual;
also does Constructor-from-match;
also does Constructor-from-int;
also does Constructor-from-sample;
}
class Perl6::Operator::Infix is Perl6::Operator {
also does BasicTextual;
also does Constructor-from-match;
also does Constructor-from-int;
also does Constructor-from-sample;
}
class Perl6::Operator::Postfix is Perl6::Operator {
also does BasicTextual;
also does Constructor-from-match;
}
class Perl6::Operator::Circumfix is Perl6::Operator {
also does Structural;
also does Branching;
also does Twig;
also does Constructor-Enter-from-int;
also does Constructor-Exit-from-int;
also does Constructor-from-match-child;
also does Constructor-from-outer-match;
also does Constructor-from-int-balanced;
also does Constructor-from-delims;
}
class Perl6::Operator::PostCircumfix is Perl6::Operator {
also does Structural;
also does Branching;
also does Twig;
also does Constructor-Enter-from-int;
also does Constructor-Exit-from-int;
also does Constructor-from-match-child;
also does Constructor-from-delims;
}
class Perl6::WS is Perl6::Invisible { }
class Perl6::Newline is Perl6::Invisible { }
class Perl6::Pod is Perl6::Documentation {
also does BasicTextual;
also does Constructor-from-int;
}
class Perl6::Comment is Perl6::Documentation {
also does BasicTextual;
also does Constructor-from-int;
}
class Perl6::Structural is Perl6::Element {
}
class Perl6::Document is Perl6::Structural {
also does Structural;
also does Branching;
also does Twig;
method from-list( Perl6::Element-List $child ) returns Perl6::Element {
if $child.child {
self.bless(
:from( $child.child.[0].from ),
:to( $child.child.[*-1].to ),
:child( $child.child )
);
}
else {
self.bless(
:from( 0 ),
:to( 0 ),
:child( $child.child )
);
}
}
}
# If you have any curiosity about this, please search for /Sir-Not in the
# docs. This workaround may be gone by the time you read about this class,
# and if so, I'm glad.
#
class Perl6::Sir-Not-Appearing-In-This-Statement is Perl6::Visible {
also does Textual;
has $.content; # because it's not quite a token.
method to-string returns Str {
~$.content
}
}
class Perl6::Statement is Perl6::Structural {
also does Structural;
also does Branching;
also does Twig;
method from-list( Perl6::Element-List $child ) returns Perl6::Element {
self.bless(
:factory-line-number( callframe(1).line ),
:from( $child.child[0].from ),
:to( $child.child[*-1].to ),
:child( $child.child )
)
}
}
# And now for the most basic tokens...
#
class Perl6::Number is Perl6::Visible {
also does BasicTextual;
method base { !!! }
also does Constructor-from-match;
}
class Perl6::Number::Binary is Perl6::Number {
method base { 2 }
}
class Perl6::Number::Octal is Perl6::Number {
method base { 8 }
}
class Perl6::Number::Decimal is Perl6::Number {
method base { 10 }
}
class Perl6::Number::Decimal::Explicit is Perl6::Number::Decimal {
}
class Perl6::Number::Hexadecimal is Perl6::Number {
method base { 16 }
}
class Perl6::Number::Radix is Perl6::Number {
}
class Perl6::Number::FloatingPoint is Perl6::Number {
method base { 10 }
}
class Perl6::NotANumber is Perl6::Visible {
also does BasicTextual;
also does Constructor-from-match;
}
class Perl6::Infinity is Perl6::Visible {
also does BasicTextual;
also does Constructor-from-match;
}
# XXX Come up with a better name.
class Perl6::String::Body is Perl6::String {
also does BasicTextual;
also does Constructor-from-match;
}
class Perl6::String::WordQuoting is Perl6::String {
also does BasicTextual;
also does Constructor-from-match;
has Str $.quote;
has Str $.delimiter-start;
has Str $.delimiter-end;
has Str @.adverb;
has Str $.here-doc;
}
class Perl6::String::WordQuoting::QuoteProtection {
also is Perl6::String::WordQuoting;
also does Token;
}
class Perl6::String::Interpolation is Perl6::String {
also does BasicTextual;
has Str $.quote;
has Str $.delimiter-start;
has Str $.delimiter-end;
has Str @.adverb;
has Str $.here-doc;
}
class Perl6::String::Interpolation::Shell {
also is Perl6::String::Interpolation;
}
class Perl6::String::Interpolation::WordQuoting {
also is Perl6::String::Interpolation;
}
class Perl6::String::Interpolation::WordQuoting::QuoteProtection {
also is Perl6::String::Interpolation::WordQuoting;
}