Skip to content

Commit 83f2ff5

Browse files
authored
Add section on parsing and whitespace issues
And rename regex section to whitespace and just stuff the single regex whitespace trap up in there.
1 parent b7d08b5 commit 83f2ff5

File tree

1 file changed

+65
-1
lines changed

1 file changed

+65
-1
lines changed

doc/Language/traps.pod6

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ default, there is only one such method per class hierarchy, whereas C<BUILD>
147147
is explicitly called per class. Which also explains why you need the
148148
C<callsame> inside C<BUILDALL>, but not inside C<BUILD>).
149149
150-
=head1 Regexes
150+
=head1 Whitespace
151151
152152
=head2 Whitespace in Regexes does not match literally
153153
@@ -168,6 +168,70 @@ Ways to match whitespace:
168168
=item with C<m:s/a b/> or C<m:sigspace/a b/>, the blank in the regexes
169169
matches arbitrary whitespace
170170
171+
=head2 Ambiguities in Parsing
172+
173+
While some languages will let get away with removing as much whitespace
174+
as possible between tokens, Perl 6 is less forgiving. The overarching
175+
mantra is we discourage code golf, so don't scrimp on whitespace (the
176+
more serious underlying reason behind these restrictions is
177+
single-pass parsing and ability to parse Perl 6 programs with virtually
178+
no L<backtracking|https://en.wikipedia.org/wiki/Backtracking>)).
179+
180+
The common areas you should watch out for are:
181+
182+
=head3 Block vs. Hash slice ambiguity
183+
184+
=begin code :skip-test
185+
# WRONG; trying to hash-slice a list:
186+
while ($++ > 5){ .say }
187+
188+
# RIGHT:
189+
while ($++ > 5) { .say }
190+
191+
# EVEN BETTER; Perl 6 does not require parentheses there
192+
while $++ > 5 { .say }
193+
=end code
194+
195+
=head3 Reduction vs. Array constructor ambiguity
196+
197+
=begin code :skip-test
198+
# WRONG; ambiguity with `[<]` meta op
199+
my @a = [[<foo>],];
200+
201+
# RIGHT; reductions cannot have spaces in them, so put one in
202+
my @a = [[ <foo>],];
203+
204+
# No ambiguity here, natural spaces between items suffice to resolve it
205+
my @a = [[<foo bar ber>],];
206+
=end code
207+
208+
=head3 The «» quoters vs. Hyper ambiguity
209+
210+
=begin code :skip-test
211+
my $foo = 'bar ber';
212+
213+
# WRONG; ambiguity whether `$foo»` means end of quoter or to hyper a postfix
214+
my @a = «foo $foo»;
215+
216+
# RIGHT; put a space in to resolve ambiguity
217+
my @a = «foo $foo »;
218+
219+
# Also good; use different delimiters
220+
my @a = qqww|foo $foo|;
221+
222+
223+
# WRONG; is it a hyper in the middle or end of quoters?
224+
my @a = «foo $foo».uc() Perl»;
225+
226+
# RIGHT; simply use Texas version of hyper
227+
my @a = «foo $foo>>.uc() Perl»;
228+
229+
# Also good; use different delimiters
230+
my @a = qqww|foo $foo».uc() Perl|;
231+
=end code
232+
233+
=head3
234+
171235
=head1 Captures
172236
173237
=head2 Containers versus values in a Capture

0 commit comments

Comments
 (0)