/
S05.html
11116 lines (7671 loc) · 331 KB
/
S05.html
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html><head><title>S05</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
<link rel="stylesheet" type="text/css" title="pod_stylesheet" href="/syn/perl.css">
<script type="text/javascript">// EVENT HANDLING
// http://therealcrisp.xs4all.nl/upload/addEvent_dean.html
// written by Dean Edwards, 2005
// with input from Tino Zijdel - crisp@xs4all.nl
// http://dean.edwards.name/weblog/2005/10/add-event/
// modified by Aankhen
var addEvent;
if (window.addEventListener) {
addEvent = function (element, type, handler) { element.addEventListener(type, handler, (typeof arguments[3] != 'undefined') ? arguments[3] : false); };
} else {
addEvent = function (element, type, handler) {
// assign each event handler a unique ID
if (!handler.$$guid) handler.$$guid = addEvent.guid++;
// create a hash table of event types for the element
if (!element.events) element.events = {};
// create a hash table of event handlers for each element/event pair
var handlers = element.events[type];
if (!handlers) {
handlers = element.events[type] = {};
// store the existing event handler (if there is one)
if (element['on' + type]) {
handlers[0] = element['on' + type];
}
// assign a global event handler to do all the work
element['on' + type] = handleEvent;
}
// store the event handler in the hash table
handlers[handler.$$guid] = handler;
}
}
// a counter used to create unique IDs
addEvent.guid = 1;
function removeEvent(element, type, handler) {
if (element.removeEventListener)
element.removeEventListener(type, handler, false);
// delete the event handler from the hash table
else if (element.events && element.events[type] && handler.$$guid)
delete element.events[type][handler.$$guid];
}
function handleEvent(event) {
// grab the event object (IE uses a global event object)
event = event || fixEvent(window.event);
var returnValue = true;
// get a reference to the hash table of event handlers
var handlers = this.events[event.type];
// execute each event handler
for (var i in handlers) {
// don't copy object properties
if (!Object.prototype[i]) {
this.$$handler = handlers[i];
if (this.$$handler(event) === false) {
returnValue = false;
// in accordance with DOM2-Events, all remaining event handlers on the object will be triggered, hence the absence of a `break`
}
}
}
// clean up
if (this.$$handler) this.$$handler = null;
return returnValue;
}
function fixEvent(event) {
// add W3C standard event methods
event.preventDefault = fixEvent.preventDefault;
event.stopPropagation = fixEvent.stopPropagation;
return event;
}
fixEvent.preventDefault = function() {
this.returnValue = false;
};
fixEvent.stopPropagation = function() {
this.cancelBubble = true;
};
// VISIBILITY TOGGLE
function toggle_snippet (e) {
var matches = this.id.match(/smartlink_toggle(\d+)/);
var num = matches[1];
var id = 'smartlink_' + num;
var div = document.getElementById(id);
div.style.display = (div.style.display == 'none') ? '' : 'none';
var text = this.firstChild;
text.nodeValue = text.nodeValue.replace(/^- (Show|Hide)/, function (full, p1) { return "- " + ((p1 == 'Show') ? 'Hide' : 'Show') }); // this may be unnecessarily complicated, or it may not. you get to decide. :-)
e.stopPropagation();
e.preventDefault();
return false;
}
function toggle_hilite(id,url) {
var el = document.getElementById(id);
if(el) {
if(el.style.display == "none") {
el.src = url;
el.style.display = "block";
} else {
el.style.display = "none";
}
}
return false;
}
// LINK GENERATION
// this would be simpler if we used a library like YUI to simplify retrieval and creation of elements, but oh well
function collectionToArray(col) {
a = new Array();
for (i = 0; i < col.length; i++)
a[a.length] = col[i];
return a;
}
addEvent(window, 'load', function () {
var divs = collectionToArray(document.getElementsByTagName('div'));
for (var i = 0, j = divs.length; i < j; i++) {
var curr = divs[i];
if (curr.id && curr.id.match(/smartlink_(\d+)/)) {
var num = RegExp.$1;
var toBeRemoved = [ "smartlink_skip_", "smartlink_skipto_" ]; // let it be reusable since this list could conceivably grow :-)
for (var k = 0, l = toBeRemoved.length; k < l; k++) {
var id = toBeRemoved[k] + num;
var elm = document.getElementById(id);
elm.parentNode.removeChild(elm);
}
var p = curr.previousSibling;
while (p.nodeType != 1) { p = p.previousSibling; } // ignore any whitespace-only nodes
var text = p.firstChild;
text.nodeValue = text.nodeValue.replace(/^From/, '- Show');
var link = document.createElement('a');
var child;
while (child = p.firstChild) {
link.appendChild(child);
}
var end = link.lastChild;
if ((end.nodeType == 3) && (end.nodeValue.search(/:$/) > -1)) {
end.nodeValue = end.nodeValue.replace(/:$/, ' -');
}
link.href = '#';
link.id = 'smartlink_toggle' + num;
addEvent(link, 'click', toggle_snippet);
p.appendChild(link);
curr.parentNode.insertBefore(p, curr);
curr.style.display = 'none';
}
// explicitly jump to the page anchor (if any) since the code above messes it up
if (location.hash && location.hash.match(/#.+/)) location.hash = RegExp.lastMatch;
}
});
</script>
<style type="text/css">
.ok {
color: green;
font-weight: bold;
}
.nok {
color: red;
font-weight: bold;
}
.snip { margin-left: 6px; }
.snipres {
margin-left: 6px;
border-width: 0;
}
.smartlink_snippet {
border: 1px solid;
padding: 0.2em;
}
</style>
</head>
<body class='pod'>
<!--
generated by Pod::Simple::HTML v3.28,
using Pod::Simple::PullParser v3.28,
under Perl v5.018002 at Mon Apr 14 23:01:08 2014 GMT.
If you want to change this HTML document, you probably shouldn't do that
by changing it directly. Instead, see about changing the calling options
to Pod::Simple::HTML, and/or subclassing Pod::Simple::HTML,
then reconverting this document from the Pod source.
When in doubt, email the author of Pod::Simple::HTML for advice.
See 'perldoc Pod::Simple::HTML' for more info.
-->
<!-- start doc -->
<em>This page was generated by Text::SmartLinks v0.01 at 2014-04-14 23:01:08 GMT.<br>
(<a href="https://github.com/perl6/specs/">syn</a> <strong>3ab8296</strong>)</em>
[ <a href="http://perlcabal.org/syn/">Index of Synopses</a> ]<br>
<a id='__top'></a>
<a name='___top' class='dummyTopAnchor' ></a>
<div class='indexgroup'>
<ul class='indexList indexList1'>
<li class='indexItem indexItem1'><a href='#TITLE'>TITLE</a>
<li class='indexItem indexItem1'><a href='#AUTHORS'>AUTHORS</a>
<li class='indexItem indexItem1'><a href='#VERSION'>VERSION</a>
<li class='indexItem indexItem1'><a href='#Overview'>Overview</a>
<li class='indexItem indexItem1'><a href='#New_match_result_and_capture_variables'>New match result and capture variables</a>
<li class='indexItem indexItem1'><a href='#Unchanged_syntactic_features'>Unchanged syntactic features</a>
<li class='indexItem indexItem1'><a href='#Simplified_lexical_parsing_of_patterns'>Simplified lexical parsing of patterns</a>
<li class='indexItem indexItem1'><a href='#Modifiers'>Modifiers</a>
<ul class='indexList indexList2'>
<li class='indexItem indexItem2'><a href='#Allowed_modifiers'>Allowed modifiers</a>
</ul>
<li class='indexItem indexItem1'><a href='#Changed_metacharacters'>Changed metacharacters</a>
<li class='indexItem indexItem1'><a href='#New_metacharacters'>New metacharacters</a>
<li class='indexItem indexItem1'><a href='#Bracket_rationalization'>Bracket rationalization</a>
<li class='indexItem indexItem1'><a href='#Variable_(non-)interpolation'>Variable (non-)interpolation</a>
<li class='indexItem indexItem1'><a href='#Extensible_metasyntax_(%3C...%3E)'>Extensible metasyntax (<...>)</a>
<ul class='indexList indexList2'>
<li class='indexItem indexItem2'><a href='#Predefined_Subrules'>Predefined Subrules</a>
</ul>
<li class='indexItem indexItem1'><a href='#Backslash_reform'>Backslash reform</a>
<ul class='indexList indexList2'>
<li class='indexItem indexItem2'><a href='#Character_class_shortcuts'>Character class shortcuts</a>
</ul>
<li class='indexItem indexItem1'><a href='#Regexes_constitute_a_first-class_language%2C_rather_than_just_being_strings'>Regexes constitute a first-class language, rather than just being strings</a>
<li class='indexItem indexItem1'><a href='#Backtracking_control'>Backtracking control</a>
<li class='indexItem indexItem1'><a href='#Regex_Routines%2C_Named_and_Anonymous'>Regex Routines, Named and Anonymous</a>
<li class='indexItem indexItem1'><a href='#Nothing_is_illegal'>Nothing is illegal</a>
<li class='indexItem indexItem1'><a href='#Longest-token_matching'>Longest-token matching</a>
<li class='indexItem indexItem1'><a href='#Return_values_from_matches'>Return values from matches</a>
<ul class='indexList indexList2'>
<li class='indexItem indexItem2'><a href='#Match_objects'>Match objects</a>
<li class='indexItem indexItem2'><a href='#Subpattern_captures'>Subpattern captures</a>
<li class='indexItem indexItem2'><a href='#Accessing_captured_subpatterns'>Accessing captured subpatterns</a>
<li class='indexItem indexItem2'><a href='#Nested_subpattern_captures'>Nested subpattern captures</a>
<li class='indexItem indexItem2'><a href='#Quantified_subpattern_captures'>Quantified subpattern captures</a>
<li class='indexItem indexItem2'><a href='#Indirectly_quantified_subpattern_captures'>Indirectly quantified subpattern captures</a>
<li class='indexItem indexItem2'><a href='#Subpattern_numbering'>Subpattern numbering</a>
<li class='indexItem indexItem2'><a href='#Subrule_captures'>Subrule captures</a>
<li class='indexItem indexItem2'><a href='#Accessing_captured_subrules'>Accessing captured subrules</a>
<li class='indexItem indexItem2'><a href='#Repeated_captures_of_the_same_subrule'>Repeated captures of the same subrule</a>
<li class='indexItem indexItem2'><a href='#Aliasing'>Aliasing</a>
<ul class='indexList indexList3'>
<li class='indexItem indexItem3'><a href='#Named_scalar_aliasing_to_subpatterns'>Named scalar aliasing to subpatterns</a>
<li class='indexItem indexItem3'><a href='#Named_scalar_aliases_applied_to_non-capturing_brackets'>Named scalar aliases applied to non-capturing brackets</a>
<li class='indexItem indexItem3'><a href='#Named_scalar_aliasing_to_subrules'>Named scalar aliasing to subrules</a>
<li class='indexItem indexItem3'><a href='#Numbered_scalar_aliasing'>Numbered scalar aliasing</a>
<li class='indexItem indexItem3'><a href='#Scalar_aliases_applied_to_quantified_constructs'>Scalar aliases applied to quantified constructs</a>
<li class='indexItem indexItem3'><a href='#Array_aliasing'>Array aliasing</a>
<li class='indexItem indexItem3'><a href='#Hash_aliasing'>Hash aliasing</a>
<li class='indexItem indexItem3'><a href='#External_aliasing'>External aliasing</a>
</ul>
<li class='indexItem indexItem2'><a href='#Capturing_from_repeated_matches'>Capturing from repeated matches</a>
</ul>
<li class='indexItem indexItem1'><a href='#Grammars'>Grammars</a>
<li class='indexItem indexItem1'><a href='#Syntactic_categories'>Syntactic categories</a>
<li class='indexItem indexItem1'><a href='#Pragmas'>Pragmas</a>
<li class='indexItem indexItem1'><a href='#Transliteration'>Transliteration</a>
<li class='indexItem indexItem1'><a href='#Substitution'>Substitution</a>
<li class='indexItem indexItem1'><a href='#Positional_matching%2C_fixed_width_types'>Positional matching, fixed width types</a>
<li class='indexItem indexItem1'><a href='#Matching_against_non-strings'>Matching against non-strings</a>
<li class='indexItem indexItem1'><a href='#When_%24%2F_is_valid'>When $/ is valid</a>
<li class='indexItem indexItem1'><a href='#POD_ERRORS'>POD ERRORS</a>
</ul>
</div>
<a id="line_1"></a>
<a id="line_2"></a>
<a id="line_3"></a>
<a id="line_4"></a>
<a id="line_5"></a>
<h1><a class='u' href='#___top' title='click to go to top of document'
name="TITLE"
>TITLE</a></h1>
<p>Synopsis 5: Regexes and Rules</p>
<a id="line_6"></a>
<a id="line_7"></a>
<a id="line_8"></a>
<a id="line_9"></a>
<a id="line_10"></a>
<a id="line_11"></a>
<a id="line_12"></a>
<a id="line_13"></a>
<a id="line_14"></a>
<h1><a class='u' href='#___top' title='click to go to top of document'
name="AUTHORS"
>AUTHORS</a></h1>
<pre> Damian Conway <damian@conway.org>
Allison Randal <al@shadowed.net>
Patrick Michaud <pmichaud@pobox.com>
Larry Wall <larry@wall.org>
Moritz Lenz <moritz@faui2k3.org>
Tobias Leich <email@froggs.de></pre>
<a id="line_15"></a>
<a id="line_16"></a>
<a id="line_17"></a>
<a id="line_18"></a>
<h1><a class='u' href='#___top' title='click to go to top of document'
name="VERSION"
>VERSION</a></h1>
<pre> Created: 24 Jun 2002</pre>
<a id="line_19"></a>
<a id="line_20"></a>
<a id="line_21"></a>
<pre> Last Modified: 27 Feb 2014
Version: 175</pre>
<a id="line_22"></a>
<a id="line_23"></a>
<a id="line_24"></a>
<a id="line_25"></a>
<a id="line_26"></a>
<a id="line_27"></a>
<a id="line_28"></a>
<a id="line_29"></a>
<a id="line_30"></a>
<a id="line_31"></a>
<a id="line_32"></a>
<a id="line_33"></a>
<p>This document summarizes Apocalypse 5, which is about the new regex syntax. We now try to call them <i>regex</i> rather than "regular expressions" because they haven't been regular expressions for a long time, and we think the popular term "regex" is in the process of becoming a technical term with a precise meaning of: "something you do pattern matching with, kinda like a regular expression". On the other hand, one of the purposes of the redesign is to make portions of our patterns more amenable to analysis under traditional regular expression and parser semantics, and that involves making careful distinctions between which parts of our patterns and grammars are to be treated as declarative, and which parts as procedural.</p>
<a id="line_34"></a>
<a id="line_35"></a>
<a id="line_36"></a>
<p>In any case, when referring to recursive patterns within a grammar, the terms <i>rule</i> and <i>token</i> are generally preferred over <i>regex</i>.</p>
<a id="line_37"></a>
<a id="line_38"></a>
<a id="line_39"></a>
<a id="line_40"></a>
<a id="line_41"></a>
<a id="line_42"></a>
<a id="line_43"></a>
<h1><a class='u' href='#___top' title='click to go to top of document'
name="Overview"
>Overview</a></h1>
<p>In essence, Perl 6 natively implements Parsing Expression Grammars (PEGs) as an extension of regular expression notation. PEGs require that you provide a "pecking order" for ambiguous parses. Perl 6's pecking order is determined by a multi-level tie-breaking test:</p>
<a id="line_44"></a>
<a id="line_45"></a>
<a id="line_46"></a>
<a id="line_47"></a>
<a id="line_48"></a>
<a id="line_49"></a>
<pre> 1) Longest token matching: food\s+ beats foo by 2 or more positions
2) Longest literal prefix: food\w* beats foo\w* by 1 position
3) Declaration from most-derived grammar beats less-derived
4) Within a given compilation unit, earlier declaration wins
5) Declaration with least number of 'uses' wins</pre>
<a id="line_50"></a>
<a id="line_51"></a>
<a id="line_52"></a>
<a id="line_53"></a>
<p>Note that tiebreaker #5 can occur only when a grammar is monkey-patched from another compilation unit. Like #3, it privileges local declarations over distant ones.</p>
<a id="line_54"></a>
<a id="line_55"></a>
<a id="line_56"></a>
<a id="line_57"></a>
<a id="line_58"></a>
<a id="line_59"></a>
<a id="line_60"></a>
<p>In addition to this pecking order, if any rule chosen under the pecking backtracks, the next best rule is chosen. That is, the pecking order determines a candidate list; just because one candidate is chosen does not mean the rest are thrown away. They may, however, be explicitly thrown away by an appropriate backtracking control (sometimes called a "cut" operator, but Perl 6 has several of them, depending on how much you want to cut).</p>
<a id="line_61"></a>
<a id="line_62"></a>
<a id="line_63"></a>
<a id="line_64"></a>
<a id="line_65"></a>
<a id="line_66"></a>
<a id="line_67"></a>
<a id="line_68"></a>
<h1><a class='u' href='#___top' title='click to go to top of document'
name="New_match_result_and_capture_variables"
>New match result and capture variables</a></h1>
<p>The underlying match object is now available via the <code>$/</code> variable, which is implicitly lexically scoped. All user access to the most recent match is through this variable, even when it doesn't look like it. The individual capture variables (such as <code>$0</code>, <code>$1</code>, etc.) are just elements of <code>$/</code>.</p>
<a id="line_69"></a>
<a id="line_70"></a>
<a id="line_71"></a>
<p>By the way, unlike in Perl 5, the numbered capture variables now start at <code>$0</code> instead of <code>$1</code>. See below.</p>
<a id="line_72"></a>
<a id="line_73"></a>
<a id="line_74"></a>
<p>In order to detect accidental use of Perl 5's unrelated <code>$/</code> variable, Perl 6's <code>$/</code> variable may not be assigned to directly.</p>
<a id="line_75"></a>
<a id="line_76"></a>
<a id="line_77"></a>
<a id="line_78"></a>
<a id="line_79"></a>
<pre> $/ = $x; # "Unsupported use of $/ variable as input record separator"
$/ := $x; # OK, binding
$/ RR= $x; # OK, metaoperator
($/) = $x; # OK, list assignment</pre>
<a id="line_80"></a>
<a id="line_81"></a>
<a id="line_82"></a>
<a id="line_83"></a>
<h1><a class='u' href='#___top' title='click to go to top of document'
name="Unchanged_syntactic_features"
>Unchanged syntactic features</a></h1>
<p>The following regex features use the same syntax as in Perl 5:</p>
<a id="line_84"></a>
<a id="line_85"></a>
<a id="line_86"></a>
<a id="line_87"></a>
<a id="line_88"></a>
<a id="line_89"></a>
<ul>
<li>Capturing: (...)
<div class="smartlink">
<p class="smartlink-file"><a href="https://github.com/perl6/roast/blob/master/S05-mass/rx.t#L111-L306"><code>S05-mass/rx.t</code> lines <code>111–306</code></a>
</p>
</div>
<a id="line_90"></a>
<a id="line_91"></a>
<a id="line_92"></a>
<a id="line_93"></a>
</li>
<li>Repetition quantifiers: *, +, and ?
<a id="line_94"></a>
<a id="line_95"></a>
<a id="line_96"></a>
<a id="line_97"></a>
</li>
<li>Alternatives: |
<a id="line_98"></a>
<a id="line_99"></a>
<a id="line_100"></a>
<a id="line_101"></a>
</li>
<li>Backslash escape: \
<a id="line_102"></a>
<a id="line_103"></a>
<a id="line_104"></a>
<a id="line_105"></a>
</li>
<li>Minimal matching suffix: ??, *?, +?
<a id="line_106"></a>
<a id="line_107"></a>
</li>
</ul>
<a id="line_108"></a>
<a id="line_109"></a>
<a id="line_110"></a>
<a id="line_111"></a>
<a id="line_112"></a>
<a id="line_113"></a>
<a id="line_114"></a>
<p>While the syntax of <code>|</code> does not change, the default semantics do change slightly. We are attempting to concoct a pleasing mixture of declarative and procedural matching so that we can have the best of both. In short, you need not write your own tokener for a grammar because Perl will write one for you. See the section below on "Longest-token matching".</p>
<div class="smartlink">
<p class="smartlink-file"><a href="https://github.com/perl6/roast/blob/master/S05-metasyntax/longest-alternative.t#L6-L52"><code>S05-metasyntax/longest-alternative.t</code> lines <code>6–52</code></a>
</p>
</div>
<a id="line_115"></a>
<a id="line_116"></a>
<a id="line_117"></a>
<a id="line_118"></a>
<a id="line_119"></a>
<a id="line_120"></a>
<a id="line_121"></a>
<a id="line_122"></a>
<a id="line_123"></a>
<a id="line_124"></a>
<a id="line_125"></a>
<a id="line_126"></a>
<h1><a class='u' href='#___top' title='click to go to top of document'
name="Simplified_lexical_parsing_of_patterns"
>Simplified lexical parsing of patterns</a></h1>
<p>Unlike traditional regular expressions, Perl 6 does not require you to memorize an arbitrary list of metacharacters. Instead it classifies characters by a simple rule. All glyphs (graphemes) whose base characters are either the underscore (<code>_</code>) or have a Unicode classification beginning with 'L' (i.e. letters) or 'N' (i.e. numbers) are always literal (i.e. self-matching) in regexes. They must be escaped with a <code>\</code> to make them metasyntactic (in which case that single alphanumeric character is itself metasyntactic, but any immediately following alphanumeric character is not).</p>
<a id="line_127"></a>
<a id="line_128"></a>
<a id="line_129"></a>
<a id="line_130"></a>
<a id="line_131"></a>
<a id="line_132"></a>
<p>All other glyphs--including whitespace--are exactly the opposite: they are always considered metasyntactic (i.e. non-self-matching) and must be escaped or quoted to make them literal. As is traditional, they may be individually escaped with <code>\</code>, but in Perl 6 they may be also quoted as follows.</p>
<a id="line_133"></a>
<a id="line_134"></a>
<a id="line_135"></a>
<a id="line_136"></a>
<a id="line_137"></a>
<a id="line_138"></a>
<p>Sequences of one or more glyphs of either type (i.e. any glyphs at all) may be made literal by placing them inside single quotes. (Double quotes are also allowed, with the same interpolative semantics as the current language in which the regex is lexically embedded.) Quotes create a quantifiable atom, so while</p>
<div class="smartlink">
<p class="smartlink-file"><a href="https://github.com/perl6/roast/blob/master/S05-metasyntax/single-quotes.t#L16-L28"><code>S05-metasyntax/single-quotes.t</code> lines <code>16–28</code></a>
</p>
</div>
<a id="line_139"></a>
<a id="line_140"></a>
<pre> moose*</pre>
<a id="line_141"></a>
<a id="line_142"></a>
<p>quantifies only the 'e' and matches "mooseee", saying</p>
<a id="line_143"></a>
<a id="line_144"></a>
<pre> 'moose'*</pre>
<a id="line_145"></a>
<a id="line_146"></a>
<p>quantifies the whole string and would match "moosemoose".</p>
<a id="line_147"></a>
<a id="line_148"></a>
<p>Here is a table that summarizes the distinctions:</p>
<a id="line_149"></a>
<a id="line_150"></a>
<pre> Alphanumerics Non-alphanumerics Mixed</pre>
<a id="line_151"></a>
<a id="line_152"></a>
<a id="line_153"></a>
<a id="line_154"></a>
<pre> Literal glyphs a 1 _ \* \$ \. \\ \' K\-9\!
Metasyntax \a \1 \_ * $ . \ ' \K-\9!
Quoted glyphs 'a' '1' '_' '*' '$' '.' '\\' '\'' 'K-9!'</pre>
<a id="line_155"></a>
<a id="line_156"></a>
<a id="line_157"></a>
<a id="line_158"></a>
<p>In other words, identifier glyphs are literal (or metasyntactic when escaped), non-identifier glyphs are metasyntactic (or literal when escaped), and single quotes make everything inside them literal.</p>
<a id="line_159"></a>
<a id="line_160"></a>
<a id="line_161"></a>
<a id="line_162"></a>
<a id="line_163"></a>
<a id="line_164"></a>
<a id="line_165"></a>
<a id="line_166"></a>
<p>Note, however, that not all non-identifier glyphs are currently meaningful as metasyntax in Perl 6 regexes (e.g. <code>\1</code> <code>\_</code> <code>-</code> <code>!</code>). It is more accurate to say that all unescaped non-identifier glyphs are <i>potential</i> metasyntax, and reserved for future use. If you use such a sequence, a helpful compile-time error is issued indicating that you either need to quote the sequence or define a new operator to recognize it.</p>
<div class="smartlink">
<p class="smartlink-file"><a href="https://github.com/perl6/roast/blob/master/S05-metasyntax/unknown.t#L6-L43"><code>S05-metasyntax/unknown.t</code> lines <code>6–43</code></a>
</p>
</div>
<a id="line_167"></a>
<a id="line_168"></a>
<a id="line_169"></a>
<a id="line_170"></a>
<p>The semicolon character is specifically reserved as a non-meaningful metacharacter; if an unquoted semicolon is seen, the compiler will complain that the regex is missing its terminator.</p>
<div class="smartlink">
<p class="smartlink-file"><a href="https://github.com/perl6/roast/blob/master/S05-metasyntax/regex.t#L73-L91"><code>S05-metasyntax/regex.t</code> lines <code>73–91</code></a>
</p>
</div>
<a id="line_171"></a>
<a id="line_172"></a>
<a id="line_173"></a>
<a id="line_174"></a>
<a id="line_175"></a>
<a id="line_176"></a>
<a id="line_177"></a>
<a id="line_178"></a>
<a id="line_179"></a>
<a id="line_180"></a>
<h1><a class='u' href='#___top' title='click to go to top of document'
name="Modifiers"
>Modifiers</a></h1>
<ul>
<li>The extended syntax (<code>/x</code>) is no longer required...it's the default. (In fact, it's pretty much mandatory--the only way to get back to the old syntax is with the <code>:Perl5</code>/<code>:P5</code> modifier.)
<div class="smartlink">
<p class="smartlink-file"><a href="https://github.com/perl6/roast/blob/master/S05-modifier/perl5_4.t#L7-L119"><code>S05-modifier/perl5_4.t</code> lines <code>7–119</code></a>
</p>
</div>
<div class="smartlink">
<p class="smartlink-file"><a href="https://github.com/perl6/roast/blob/master/S05-modifier/perl5_3.t#L7-L119"><code>S05-modifier/perl5_3.t</code> lines <code>7–119</code></a>
</p>
</div>
<div class="smartlink">
<p class="smartlink-file"><a href="https://github.com/perl6/roast/blob/master/S05-modifier/perl5_8.t#L7-L134"><code>S05-modifier/perl5_8.t</code> lines <code>7–134</code></a>
</p>
</div>
<div class="smartlink">
<p class="smartlink-file"><a href="https://github.com/perl6/roast/blob/master/S05-modifier/perl5_2.t#L7-L119"><code>S05-modifier/perl5_2.t</code> lines <code>7–119</code></a>
</p>
</div>
<div class="smartlink">
<p class="smartlink-file"><a href="https://github.com/perl6/roast/blob/master/S05-modifier/perl5_6.t#L7-L125"><code>S05-modifier/perl5_6.t</code> lines <code>7–125</code></a>
</p>
</div>
<div class="smartlink">
<p class="smartlink-file"><a href="https://github.com/perl6/roast/blob/master/S05-modifier/perl5_1.t#L7-L119"><code>S05-modifier/perl5_1.t</code> lines <code>7–119</code></a>
</p>
</div>
<div class="smartlink">
<p class="smartlink-file"><a href="https://github.com/perl6/roast/blob/master/S05-modifier/perl5_7.t#L7-L119"><code>S05-modifier/perl5_7.t</code> lines <code>7–119</code></a>
</p>
</div>
<div class="smartlink">
<p class="smartlink-file"><a href="https://github.com/perl6/roast/blob/master/S05-modifier/perl5_9.t#L7-L108"><code>S05-modifier/perl5_9.t</code> lines <code>7–108</code></a>
</p>
</div>
<div class="smartlink">
<p class="smartlink-file"><a href="https://github.com/perl6/roast/blob/master/S05-modifier/perl5_5.t#L7-L128"><code>S05-modifier/perl5_5.t</code> lines <code>7–128</code></a>
</p>
</div>
<div class="smartlink">
<p class="smartlink-file"><a href="https://github.com/perl6/roast/blob/master/S05-modifier/perl5_0.t#L9-L137"><code>S05-modifier/perl5_0.t</code> lines <code>9–137</code></a>
</p>
</div>
<a id="line_181"></a>
<a id="line_182"></a>
<a id="line_183"></a>
<a id="line_184"></a>
<a id="line_185"></a>
</li>
<li>There are no <code>/s</code> or <code>/m</code> modifiers (changes to the meta-characters replace them - see below).
<a id="line_186"></a>
<a id="line_187"></a>
<a id="line_188"></a>
<a id="line_189"></a>
</li>
<li>There is no <code>/e</code> evaluation modifier on substitutions; instead use:
<a id="line_190"></a>
<a id="line_191"></a>
<pre> s/pattern/{ doit() }/</pre>
<a id="line_192"></a>
<a id="line_193"></a>
<p>or:</p>
<a id="line_194"></a>
<a id="line_195"></a>
<pre> s[pattern] = doit()</pre>
<a id="line_196"></a>
<a id="line_197"></a>
<p>Instead of <code>/ee</code> say:</p>
<a id="line_198"></a>
<a id="line_199"></a>
<pre> s/pattern/{ EVAL doit() }/</pre>
<a id="line_200"></a>
<a id="line_201"></a>
<p>or:</p>
<a id="line_202"></a>
<a id="line_203"></a>
<pre> s[pattern] = doit().EVAL</pre>
<a id="line_204"></a>
<a id="line_205"></a>
<a id="line_206"></a>
<a id="line_207"></a>
</li>
<li>Modifiers are now placed as adverbs at the <i>start</i> of a match/substitution:
<a id="line_208"></a>
<a id="line_209"></a>
<pre> m:g:i/\s* (\w*) \s* ,?/;</pre>
<a id="line_210"></a>
<a id="line_211"></a>
<a id="line_212"></a>
<a id="line_213"></a>
<a id="line_214"></a>
<p>Every modifier must start with its own colon. The delimiter must be separated from the final modifier by whitespace if it would otherwise be taken as an argument to the preceding modifier (which is true if and only if the next character is a left parenthesis.)</p>
<a id="line_215"></a>
<a id="line_216"></a>
<a id="line_217"></a>
<a id="line_218"></a>
</li>
<li>The single-character modifiers also have longer versions:
<a id="line_219"></a>
<a id="line_220"></a>
<a id="line_221"></a>
<a id="line_222"></a>
<a id="line_223"></a>
<pre> :i :ignorecase
:m :ignoremark
:g :global
:r :ratchet</pre>
<a id="line_224"></a>
<a id="line_225"></a>
<a id="line_226"></a>
<a id="line_227"></a>
<a id="line_228"></a>
<a id="line_229"></a>
<a id="line_230"></a>
<a id="line_231"></a>
<a id="line_232"></a>
</li>
<li>The <code>:i</code> (or <code>:ignorecase</code>) modifier causes case distinctions to be ignored in its lexical scope, but not in its dynamic scope. That is, subrules always use their own case settings. The amount of case folding depends on the current context. In byte and codepoint mode, level 1 case folding is required (as defined in TR18 section 2.4). In grapheme mode level 2 is required.
<div class="smartlink">
<p class="smartlink-file"><a href="https://github.com/perl6/roast/blob/master/S05-modifier/ignorecase.t#L24-L85"><code>S05-modifier/ignorecase.t</code> lines <code>24–85</code></a>
</p>
</div>
<a id="line_233"></a>
<a id="line_234"></a>
<a id="line_235"></a>
<a id="line_236"></a>
<a id="line_237"></a>
<p>The <code>:ii</code> (or <code>:samecase</code>) variant may be used on a substitution to change the substituted string to the same case pattern as the matched string. It implies the same pattern semantics as <code>:i</code> above, so it is not necessary to put both <code>:i</code> and <code>:ii</code>.</p>
<div class="smartlink">
<p class="smartlink-file"><a href="https://github.com/perl6/roast/blob/master/S05-modifier/ii.t#L9-L31"><code>S05-modifier/ii.t</code> lines <code>9–31</code></a>
</p>
</div>
<a id="line_238"></a>
<a id="line_239"></a>
<a id="line_240"></a>
<a id="line_241"></a>
<a id="line_242"></a>
<a id="line_243"></a>
<a id="line_244"></a>
<a id="line_245"></a>
<a id="line_246"></a>
<a id="line_247"></a>
<a id="line_248"></a>
<a id="line_249"></a>
<p>If the pattern is matched without the <code>:sigspace</code> modifier, case info is carried across on a character by character basis. If the right string is longer than the left one, the case of the final character is replicated. Titlecase is carried across if possible regardless of whether the resulting letter is at the beginning of a word or not; if there is no titlecase character available, the corresponding uppercase character is used. (This policy can be modified within a lexical scope by a language-dependent Unicode declaration to substitute titlecase according to the orthographic rules of the specified language.) Characters that carry no case information leave their corresponding replacement character unchanged.</p>
<a id="line_250"></a>
<a id="line_251"></a>
<a id="line_252"></a>
<a id="line_253"></a>
<a id="line_254"></a>
<a id="line_255"></a>
<a id="line_256"></a>
<a id="line_257"></a>
<a id="line_258"></a>
<a id="line_259"></a>
<p>If the pattern is matched with <code>:sigspace</code>, then a slightly smarter algorithm is used which attempts to determine if there is a uniform capitalization policy over each matched word, and applies the same policy to each replacement word. If there doesn't seem to be a uniform policy on the left, the policy for each word is carried over word by word, with the last pattern word replicated if necessary. If a word does not appear to have a recognizable policy, the replacement word is translated character for character as in the non-sigspace case. Recognized policies include:</p>
<div class="smartlink">
<p class="smartlink-file"><a href="https://github.com/perl6/roast/blob/master/S05-modifier/ii.t#L32-L68"><code>S05-modifier/ii.t</code> lines <code>32–68</code></a>
</p>
</div>
<a id="line_260"></a>
<a id="line_261"></a>
<a id="line_262"></a>
<a id="line_263"></a>
<a id="line_264"></a>
<a id="line_265"></a>
<pre> lc()
uc()
tc()
tclc()
tcuc()</pre>
<a id="line_266"></a>
<a id="line_267"></a>
<a id="line_268"></a>
<a id="line_269"></a>
<p>In any case, only the officially matched string part of the pattern match counts, so any sort of lookahead or contextual matching is not included in the analysis.</p>
<a id="line_270"></a>
<a id="line_271"></a>
<a id="line_272"></a>
<a id="line_273"></a>
<a id="line_274"></a>
<a id="line_275"></a>
<a id="line_276"></a>
<a id="line_277"></a>
<a id="line_278"></a>
<a id="line_279"></a>
<a id="line_280"></a>
<a id="line_281"></a>
</li>
<li>The <code>:m</code> (or <code>:ignoremark</code>) modifier scopes exactly like <code>:ignorecase</code> except that it ignores marks (accents and such) instead of case. It is equivalent to taking each grapheme (in both target and pattern), converting both to NFD (maximally decomposed) and then comparing the two base characters (Unicode non-mark characters) while ignoring any trailing mark characters. The mark characters are ignored only for the purpose of determining the truth of the assertion; the actual text matched includes all ignored characters, including any that follow the final base character.
<div class="smartlink">
<p class="smartlink-file"><a href="https://github.com/perl6/roast/blob/master/S05-modifier/ignoremark.t#L15-L30"><code>S05-modifier/ignoremark.t</code> lines <code>15–30</code></a>
</p>
</div>
<a id="line_282"></a>
<a id="line_283"></a>
<a id="line_284"></a>
<a id="line_285"></a>
<a id="line_286"></a>