This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
new perldelta
[perl5.git] / t / comp / parser.t
CommitLineData
923be969
RGS
1#!./perl
2
3# Checks if the parser behaves correctly in edge cases
4# (including weird syntax errors)
5
561e8675
CB
6BEGIN {
7 @INC = qw(. ../lib);
a817e89d 8 chdir 't' if -d 't';
561e8675
CB
9}
10
bd09be5b 11print "1..191\n";
c9786f60
NC
12
13sub failed {
14 my ($got, $expected, $name) = @_;
15
16 print "not ok $test - $name\n";
17 my @caller = caller(1);
18 print "# Failed test at $caller[1] line $caller[2]\n";
19 if (defined $got) {
20 print "# Got '$got'\n";
21 } else {
22 print "# Got undef\n";
23 }
24 print "# Expected $expected\n";
25 return;
923be969
RGS
26}
27
c9786f60
NC
28sub like {
29 my ($got, $pattern, $name) = @_;
30 $test = $test + 1;
31 if (defined $got && $got =~ $pattern) {
32 print "ok $test - $name\n";
33 # Principle of least surprise - maintain the expected interface, even
34 # though we aren't using it here (yet).
35 return 1;
36 }
37 failed($got, $pattern, $name);
38}
39
40sub is {
41 my ($got, $expect, $name) = @_;
42 $test = $test + 1;
43 if (defined $expect) {
44 if (defined $got && $got eq $expect) {
45 print "ok $test - $name\n";
46 return 1;
47 }
48 failed($got, "'$expect'", $name);
49 } else {
50 if (!defined $got) {
51 print "ok $test - $name\n";
52 return 1;
53 }
54 failed($got, 'undef', $name);
55 }
56}
f15b33d3
RGS
57
58eval '%@x=0;';
59like( $@, qr/^Can't modify hash dereference in repeat \(x\)/, '%@x=0' );
60
ee95e30c 61# Bug 20010422.005 (#6874)
f15b33d3
RGS
62eval q{{s//${}/; //}};
63like( $@, qr/syntax error/, 'syntax error, used to dump core' );
64
ee95e30c 65# Bug 20010528.007 (#7052)
f15b33d3
RGS
66eval q/"\x{"/;
67like( $@, qr/^Missing right brace on \\x/,
68 'syntax error in string, used to dump core' );
69
fd7808cb
RGS
70eval q/"\N{"/;
71like( $@, qr/^Missing right brace on \\N/,
72 'syntax error in string with incomplete \N' );
73eval q/"\Nfoo"/;
74like( $@, qr/^Missing braces on \\N/,
75 'syntax error in string with incomplete \N' );
76
ae9ce25e
KW
77eval q/"\o{"/;
78like( $@, qr/^Missing right brace on \\o/,
79 'syntax error in string with incomplete \o' );
80eval q/"\ofoo"/;
81like( $@, qr/^Missing braces on \\o/,
82 'syntax error in string with incomplete \o' );
83
f15b33d3
RGS
84eval "a.b.c.d.e.f;sub";
85like( $@, qr/^Illegal declaration of anonymous subroutine/,
86 'found by Markov chain stress testing' );
87
ee95e30c 88# Bug 20010831.001 (#7605)
f15b33d3
RGS
89eval '($a, b) = (1, 2);';
90like( $@, qr/^Can't modify constant item in list assignment/,
91 'bareword in list assignment' );
92
93eval 'tie FOO, "Foo";';
94like( $@, qr/^Can't modify constant item in tie /,
95 'tying a bareword causes a segfault in 5.6.1' );
96
97eval 'undef foo';
98like( $@, qr/^Can't modify constant item in undef operator /,
ee95e30c 99 'undefing constant causes a segfault in 5.6.1 [ID 20010906.019 (#7642)]' );
f15b33d3
RGS
100
101eval 'read($bla, FILE, 1);';
102like( $@, qr/^Can't modify constant item in read /,
ee95e30c 103 'read($var, FILE, 1) segfaults on 5.6.1 [ID 20011025.054 (#7847)]' );
923be969
RGS
104
105# This used to dump core (bug #17920)
106eval q{ sub { sub { f1(f2();); my($a,$b,$c) } } };
f15b33d3 107like( $@, qr/error/, 'lexical block discarded by yacc' );
961ce445
RGS
108
109# bug #18573, used to corrupt memory
110eval q{ "\c" };
111like( $@, qr/^Missing control char name in \\c/, q("\c" string) );
8edd5f42 112
a40a317b
RGS
113eval q{ qq(foo$) };
114like( $@, qr/Final \$ should be \\\$ or \$name/, q($ at end of "" string) );
115
8edd5f42
RGS
116# two tests for memory corruption problems in the said variables
117# (used to dump core or produce strange results)
118
119is( "\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Qa", "a", "PL_lex_casestack" );
120
121eval {
122{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
123{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
124{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
125}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
126}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
127}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
128};
129is( $@, '', 'PL_lex_brackstack' );
437fd210
AE
130
131{
7df0d042
AE
132 # tests for bug #20716
133 undef $a;
134 undef @b;
135 my $a="A";
136 is("${a}{", "A{", "interpolation, qq//");
137 is("${a}[", "A[", "interpolation, qq//");
138 my @b=("B");
139 is("@{b}{", "B{", "interpolation, qq//");
2a53d331 140 is(qr/${a}\{/, '(?^:A\{)', "interpolation, qr//");
7df0d042 141 my $c = "A{";
2a53d331 142 $c =~ /${a}\{/;
7df0d042 143 is($&, 'A{', "interpolation, m//");
2a53d331 144 $c =~ s/${a}\{/foo/;
7df0d042
AE
145 is($c, 'foo', "interpolation, s/...//");
146 $c =~ s/foo/${a}{/;
147 is($c, 'A{', "interpolation, s//.../");
148 is(<<"${a}{", "A{ A[ B{\n", "interpolation, here doc");
149${a}{ ${a}[ @{b}{
150${a}{
437fd210 151}
4a202259 152
0f5d0394
AE
153eval q{ sub a(;; &) { } a { } };
154is($@, '', "';&' sub prototype confuses the lexer");
155
4a202259
AE
156# Bug #21575
157# ensure that the second print statement works, by playing a bit
158# with the test output.
159my %data = ( foo => "\n" );
160print "#";
161print(
162$data{foo});
c9786f60
NC
163$test = $test + 1;
164print "ok $test\n";
abc667d1
DM
165
166# Bug #21875
167# { q.* => ... } should be interpreted as hash, not block
168
169foreach my $line (split /\n/, <<'EOF')
1701 { foo => 'bar' }
1711 { qoo => 'bar' }
1721 { q => 'bar' }
1731 { qq => 'bar' }
1740 { q,'bar', }
1750 { q=bar= }
1760 { qq=bar= }
1771 { q=bar= => 'bar' }
178EOF
179{
180 my ($expect, $eval) = split / /, $line, 2;
181 my $result = eval $eval;
c9786f60 182 is($@, '', "eval $eval");
abc667d1
DM
183 is(ref $result, $expect ? 'HASH' : '', $eval);
184}
3cf7b4c4
RGS
185
186# Bug #24212
187{
188 local $SIG{__WARN__} = sub { }; # silence mandatory warning
189 eval q{ my $x = -F 1; };
250d67eb 190 like( $@, qr/(?i:syntax|parse) error .* near "F 1"/, "unknown filetest operators" );
3cf7b4c4
RGS
191 is(
192 eval q{ sub F { 42 } -F 1 },
193 '-42',
194 '-F calls the F function'
195 );
196}
1c03aa9b
RGS
197
198# Bug #24762
199{
200 eval q{ *foo{CODE} ? 1 : 0 };
201 is( $@, '', "glob subscript in conditional" );
202}
500bedb6
DM
203
204# Bug #25824
205{
206 eval q{ sub f { @a=@b=@c; {use} } };
207 like( $@, qr/syntax error/, "use without body" );
208}
e1548254 209
8e742a20
MHM
210# [perl #2738] perl segfautls on input
211{
212 eval q{ sub _ <> {} };
213 like($@, qr/Illegal declaration of subroutine main::_/, "readline operator as prototype");
214
215 eval q{ $s = sub <> {} };
216 like($@, qr/Illegal declaration of anonymous subroutine/, "readline operator as prototype");
217
218 eval q{ sub _ __FILE__ {} };
219 like($@, qr/Illegal declaration of subroutine main::_/, "__FILE__ as prototype");
220}
dbfe47cf 221
917949e3
RGS
222# tests for "Bad name"
223eval q{ foo::$bar };
224like( $@, qr/Bad name after foo::/, 'Bad name after foo::' );
225eval q{ foo''bar };
226like( $@, qr/Bad name after foo'/, 'Bad name after foo\'' );
a6a8bb49
RGS
227
228# test for ?: context error
229eval q{($a ? $x : ($y)) = 5};
230like( $@, qr/Assignment to both a list and a scalar/, 'Assignment to both a list and a scalar' );
2d997502
BC
231
232eval q{ s/x/#/e };
233is( $@, '', 'comments in s///e' );
2af555bf 234
718a7425 235# these five used to coredump because the op cleanup on parse error could
2af555bf
DM
236# be to the wrong pad
237
238eval q[
239 sub { our $a= 1;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;
240 sub { my $z
241];
242
243like($@, qr/Missing right curly/, 'nested sub syntax error' );
244
245eval q[
a5fba4f1 246 sub { my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s);
2af555bf
DM
247 sub { my $z
248];
249like($@, qr/Missing right curly/, 'nested sub syntax error 2' );
250
718a7425
DM
251eval q[
252 sub { our $a= 1;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;
253 use DieDieDie;
254];
255
256like($@, qr/Can't locate DieDieDie.pm/, 'croak cleanup' );
257
258eval q[
a5fba4f1 259 sub { my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s);
718a7425
DM
260 use DieDieDie;
261];
262
263like($@, qr/Can't locate DieDieDie.pm/, 'croak cleanup 2' );
264
265
266eval q[
267 my @a;
a5fba4f1 268 my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s);
718a7425
DM
269 @a =~ s/a/b/; # compile-time error
270 use DieDieDie;
271];
272
273like($@, qr/Can't modify/, 'croak cleanup 3' );
274
7e5d8ed2
DM
275# these might leak, or have duplicate frees, depending on the bugginess of
276# the parser stack 'fail in reduce' cleanup code. They're here mainly as
277# something to be run under valgrind, with PERL_DESTRUCT_LEVEL=1.
278
279eval q[ BEGIN { } ] for 1..10;
280is($@, "", 'BEGIN 1' );
281
282eval q[ BEGIN { my $x; $x = 1 } ] for 1..10;
283is($@, "", 'BEGIN 2' );
284
285eval q[ BEGIN { \&foo1 } ] for 1..10;
286is($@, "", 'BEGIN 3' );
287
288eval q[ sub foo2 { } ] for 1..10;
289is($@, "", 'BEGIN 4' );
290
291eval q[ sub foo3 { my $x; $x=1 } ] for 1..10;
292is($@, "", 'BEGIN 5' );
293
294eval q[ BEGIN { die } ] for 1..10;
295like($@, qr/BEGIN failed--compilation aborted/, 'BEGIN 6' );
296
297eval q[ BEGIN {\&foo4; die } ] for 1..10;
298like($@, qr/BEGIN failed--compilation aborted/, 'BEGIN 7' );
44867030 299
0b3da58d
TC
300{
301 # RT #70934
302 # check both the specific case in the ticket, and a few other paths into
303 # S_scan_ident()
304 # simplify long ids
305 my $x100 = "x" x 256;
306 my $xFE = "x" x 254;
307 my $xFD = "x" x 253;
308 my $xFC = "x" x 252;
309 my $xFB = "x" x 251;
310
311 eval qq[ \$#$xFB ];
312 is($@, "", "251 character \$# sigil ident ok");
313 eval qq[ \$#$xFC ];
314 like($@, qr/Identifier too long/, "too long id in \$# sigil ctx");
315
316 eval qq[ \$$xFB ];
317 is($@, "", "251 character \$ sigil ident ok");
318 eval qq[ \$$xFC ];
319 like($@, qr/Identifier too long/, "too long id in \$ sigil ctx");
320
321 eval qq[ %$xFB ];
322 is($@, "", "251 character % sigil ident ok");
323 eval qq[ %$xFC ];
324 like($@, qr/Identifier too long/, "too long id in % sigil ctx");
325
c07656ed
FC
326 eval qq[ \\&$xFB ]; # take a ref since I don't want to call it
327 is($@, "", "251 character & sigil ident ok");
328 eval qq[ \\&$xFC ];
0b3da58d
TC
329 like($@, qr/Identifier too long/, "too long id in & sigil ctx");
330
331 eval qq[ *$xFC ];
332 is($@, "", "252 character glob ident ok");
333 eval qq[ *$xFD ];
334 like($@, qr/Identifier too long/, "too long id in glob ctx");
335
61bc2258 336 eval qq[ for $xFC ];
bd09be5b 337 like($@, qr/^Missing \$ on loop variable /,
61bc2258
YH
338 "252 char id ok, but a different error");
339 eval qq[ for $xFD; ];
bd09be5b 340 like($@, qr/^Missing \$ on loop variable /, "too long id in for ctx");
0b3da58d
TC
341
342 # the specific case from the ticket
bd09be5b 343 # however the parsing code in yyl_foreach has now changed
0b3da58d
TC
344 my $x = "x" x 257;
345 eval qq[ for $x ];
bd09be5b
NC
346 like($@, qr/^Missing \$ on loop variable /, "too long id ticket case");
347
348 # as PL_tokenbuf is now PL_parser->tokenbuf, the "buffer overflow" that was
349 # reported in GH #9993 now corrupts some other part of the parser structure.
350 # Currently, that seems to be the line number. Hence this test will fail if
351 # the fix from commit 0b3da58dfdc35079 is reversed. (However, as the later
352 # commit 61bc22580524a6d9 changed the code (now) in yyl_foreach() from
353 # scan_ident() to scan_word(), to recreate the problem one needs to apply
354 # the buggy change to the calculation of the variable `e` in scan_word()
355 # instead.
356
357 my $x = "x" x 260;
358 eval qq[ for my $x \$foo ];
359 like($@, qr/at \(eval \d+\) line 1[,.]/, "line number is reported correctly");
0b3da58d
TC
360}
361
fd909433 362{
c9e0c59b 363 is(exists &zlonk, '', 'sub not present');
fd909433
VP
364 eval qq[ {sub zlonk} ];
365 is($@, '', 'sub declaration followed by a closing curly');
c9e0c59b
NC
366 is(exists &zlonk, 1, 'sub now stubbed');
367 is(defined &zlonk, '', 'but no body defined');
fd909433
VP
368}
369
7e2d91e6
TC
370{
371 no warnings;
372 # [perl #113016] CORE::print::foo
373 sub CORE'print'foo { 43 } # apostrophes intentional; do not tempt fate
374 sub CORE'foo'bar { 43 }
375 is CORE::print::foo, 43, 'CORE::print::foo is not CORE::print ::foo';
376 is scalar eval "CORE::foo'bar", 43, "CORE::foo'bar is not an error";
377}
ee36fb64 378
f0998909
Z
379# bug #71748
380eval q{
381 $_ = "";
382 s/(.)/
383 {
384 #
385 }->{$1};
386 /e;
387 1;
388};
389is($@, "", "multiline whitespace inside substitute expression");
390
a73ef99b
FC
391eval '@A =~ s/a/b/; # compilation error
392 sub tahi {}
393 sub rua;
394 sub toru ($);
395 sub wha :lvalue;
396 sub rima ($%&*$&*\$%\*&$%*&) :method;
397 sub ono :lvalue { die }
398 sub whitu (_) { die }
399 sub waru ($;) :method { die }
400 sub iwa { die }
401 BEGIN { }';
402is $::{tahi}, undef, 'empty sub decl ignored after compilation error';
403is $::{rua}, undef, 'stub decl ignored after compilation error';
404is $::{toru}, undef, 'stub+proto decl ignored after compilation error';
405is $::{wha}, undef, 'stub+attr decl ignored after compilation error';
406is $::{rima}, undef, 'stub+proto+attr ignored after compilation error';
407is $::{ono}, undef, 'sub decl with attr ignored after compilation error';
408is $::{whitu}, undef, 'sub decl w proto ignored after compilation error';
409is $::{waru}, undef, 'sub w attr+proto ignored after compilation error';
410is $::{iwa}, undef, 'non-empty sub decl ignored after compilation error';
411is *BEGIN{CODE}, undef, 'BEGIN leaves no stub after compilation error';
412
583c9d5c
FC
413$test = $test + 1;
414"ok $test - format inside re-eval" =~ /(?{
415 format =
416@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
417$_
418.
419write
420}).*/;
421
4a7239ff
FC
422eval '
423"${;
424
425=pod
426
427=cut
428
429}";
430';
431is $@, "", 'pod inside string in string eval';
432"${;
433
434=pod
435
436=cut
437
438}";
439print "ok ", ++$test, " - pod inside string outside of string eval\n";
440
5097bf9b
FC
441like "blah blah blah\n", qr/${\ <<END
442blah blah blah
443END
444 }/, 'here docs in multiline quoted construct';
445like "blah blah blah\n", eval q|qr/${\ <<END
446blah blah blah
447END
448 }/|, 'here docs in multiline quoted construct in string eval';
449
450# Unterminated here-docs in subst in eval; used to crash
451eval 's/${<<END}//';
452eval 's//${<<END}/';
453print "ok ", ++$test, " - unterminated here-docs in s/// in string eval\n";
7e2d91e6
TC
454{
455 no warnings qw(syntax deprecated);
456 sub 'Hello'_he_said (_);
457}
0484b403
FC
458is prototype "Hello::_he_said", '_', 'initial tick in sub declaration';
459
321f1aea
FC
460{
461 my @x = 'string';
462 is(eval q{ "$x[0]->strung" }, 'string->strung',
463 'literal -> after an array subscript within ""');
464 @x = ['string'];
465 # this used to give "string"
92e8e650 466 like("$x[0]-> [0]", qr/^ARRAY\([^)]*\)-> \[0\]\z/,
321f1aea
FC
467 'literal -> [0] after an array subscript within ""');
468}
469
d2691ae2
FC
470eval 'no if $] >= 5.17.4 warnings => "deprecated"';
471is 1,1, ' no crash for "no ... syntax error"';
472
3f33d153
FC
473for my $pkg(()){}
474$pkg = 3;
475is $pkg, 3, '[perl #114942] for my $foo()){} $foo';
d2691ae2 476
a20e6aae
BF
477# Check that format 'Foo still works after removing the hack from
478# force_word
7e2d91e6
TC
479{
480 no warnings qw(syntax deprecated);
481 $test++;
482 format 'one =
a20e6aae
BF
483ok @<< - format 'foo still works
484$test
485.
7e2d91e6 486}
a20e6aae
BF
487{
488 local $~ = "one";
489 write();
490}
491
1a01716a
NC
492$test++;
493format ::two =
494ok @<< - format ::foo still works
495$test
496.
497{
498 local $~ = "two";
499 write();
500}
501
9dbc968d
FC
502for(__PACKAGE__) {
503 eval '$_=42';
504 is $_, 'main', '__PACKAGE__ is read-only';
505}
506
1311cfc0
FC
507$file = __FILE__;
508BEGIN{ ${"_<".__FILE__} = \1 }
509is __FILE__, $file,
510 'no __FILE__ corruption when setting CopFILESV to a ref';
511
eaaaaa32
FC
512eval 'Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo'
513 .'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo'
514 .'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo'
515 .'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo'
516 .'oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo'
517 .'ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo';
518like $@, "^Identifier too long at ", 'ident buffer overflow';
519
61bc2258
YH
520eval 'for my a1b $i (1) {}';
521# ng: 'Missing $ on loop variable'
522like $@, "^No such class a1b at ", 'TYPE of my of for statement';
523
b5b6210a
FC
524eval 'method {} {$_,undef}';
525like $@, qq/^Can't call method "method" on unblessed reference at /,
526 'method BLOCK {...} does not try to disambiguate';
527
51a82aa9
FC
528eval '#line 1 maggapom
529 if ($a>3) { $a ++; }
530 else {printf(1/0);}';
531is $@, "Illegal division by zero at maggapom line 2.\n",
532 'else {foo} line number (no space after {) [perl #122695]';
533
1553b1d3
FC
534# parentheses needed for this to fail an assertion in S_maybe_multideref
535is +(${[{a=>214}]}[0])->{a}, 214, '($array[...])->{...}';
536
4245fea4 537# This used to fail an assertion because of the OPf_SPECIAL flag on an
4281700a
JK
538# OP_GV that started out as an OP_CONST.
539
540 sub FILE1 () { 1 }
541 sub dummy { tell FILE1 }
4245fea4 542
b39c1059
FC
543# More potential multideref assertion failures
544# OPf_PARENS on OP_RV2SV in subscript
4281700a
JK
545 $x[($_)];
546 is(1,1, "PASS: Previous line successfully parsed. OPf_PARENS on OP_RV2SV");
db7bc125 547# OPf_SPECIAL on OP_GV in subscript
4281700a
JK
548 $x[FILE1->[0]];
549 is(1,1, "PASS: Previous line successfully parsed. OPf_SPECIAL on OP_GV");
b39c1059 550
cc5af377 551# Used to crash [perl #123542]
08b999a9
FC
552eval 's /${<>{}) //';
553
2f465e08
FC
554# Also used to crash [perl #123652]
555eval{$1=eval{a:}};
556
fb0c7c3c
FC
557# Used to fail assertions [perl #123753]
558eval "map+map";
559eval "grep+grep";
560
57db463f
FC
561# ALso failed an assertion [perl #123848]
562{
563 local $SIG{__WARN__} = sub{};
564 eval 'my $_; m// ~~ 0';
565}
566
2b6a5bfb
FC
567# Used to crash [perl #125679]
568eval 'BEGIN {$^H=-1} \eval=time';
569
bdc377e5 570# Used to fail an assertion [perl #129073]
03a1fa1e
FC
571{
572 local $SIG{__WARN__} = sub{};
573 eval '${p{};sub p}()';
574}
bdc377e5 575
82269f56
DM
576# RT #124207 syntax error during stringify can leave stringify op
577# with multiple children and assertion failures
578
579eval 'qq{@{0]}${}},{})';
580is(1, 1, "RT #124207");
581
397c43d8 582# RT #127993 version control conflict markers
54446d4f 583my @conflict_markers = map { $_ x 7 } qw( < = > );
397c43d8 584" this should keep working
54446d4f 585$conflict_markers[0]
397c43d8 586" =~ /
54446d4f 587$conflict_markers[2]
397c43d8 588/;
54446d4f 589for my $marker (@conflict_markers) {
397c43d8 590 eval "$marker";
9c88bb56 591 like $@, qr/^Version control conflict marker at \(eval \d+\) line 1, near "$marker"/, "VCS marker '$marker' at beginning";
397c43d8 592 eval "\$_\n$marker";
9c88bb56 593 like $@, qr/^Version control conflict marker at \(eval \d+\) line 2, near "$marker"/, "VCS marker '$marker' after value";
397c43d8 594 eval "\n\$_ =\n$marker";
9c88bb56 595 like $@, qr/^Version control conflict marker at \(eval \d+\) line 3, near "$marker"/, "VCS marker '$marker' after operator";
397c43d8
LM
596}
597
e4fc7082
FC
598# keys assignments in weird contexts (mentioned in perl #128260)
599eval 'keys(%h) .= "00"';
600is $@, "", 'keys .=';
601eval 'sub { read $fh, keys %h, 0 }';
602is $@, "", 'read into keys';
603eval 'substr keys(%h),0,=3';
604is $@, "", 'substr keys assignment';
82269f56 605
10463ce6
KW
606{ # very large utf8 char in error message was overflowing buffer
607 if (length sprintf("%x", ~0) <= 8) {
608 is 1, 1, "skip because overflows on 32-bit machine";
609 }
610 else {
611 no warnings;
612 eval "q" . chr(100000000064);
613 like $@, qr/Can't find string terminator "." anywhere before EOF/,
614 'RT 128952';
615 }
e487ff5e
DM
616}
617
df13534a
DM
618# RT #130311: many parser shifts before a reduce
619
620{
621 eval '[' . ('{' x 300);
622 like $@, qr/Missing right curly or square bracket/, 'RT #130311';
623}
624
e5c165a0
AC
625# RT #130815: crash in ck_return for malformed code
626{
627 eval 'm(@{if(0){sub d{]]])}return';
628 like $@, qr/^syntax error at \(eval \d+\) line 1, near "\{\]"/,
629 'RT #130815: null pointer deref';
630}
df13534a 631
2e4e39b1
FC
632# Add new tests HERE (above this line)
633
d7425188
FC
634# bug #74022: Loop on characters in \p{OtherIDContinue}
635# This test hangs if it fails.
561e8675 636eval chr 0x387; # forces loading of utf8.pm
d7425188
FC
637is(1,1, '[perl #74022] Parser looping on OtherIDContinue chars');
638
44867030
NC
639# More awkward tests for #line. Keep these at the end, as they will screw
640# with sane line reporting for any other test failures
641
642sub check ($$$) {
643 my ($file, $line, $name) = @_;
644 my (undef, $got_file, $got_line) = caller;
645 like ($got_file, $file, "file of $name");
44867030
NC
646 is ($got_line, $line, "line of $name");
647}
648
c9de86d5 649my $this_file = qr/parser\.t(?:\.[bl]eb?)?$/;
44867030 650#line 3
bf1b738b
FC
6511 unless
6521;
653check($this_file, 5, "[perl #118931]");
654
655#line 3
c9de86d5 656check($this_file, 3, "bare line");
44867030
NC
657
658# line 5
c9de86d5 659check($this_file, 5, "bare line with leading space");
44867030 660
a5fba4f1 661#line 7
c9de86d5 662check($this_file, 7, "trailing space still valid");
44867030 663
a5fba4f1 664# line 11
c9de86d5 665check($this_file, 11, "leading and trailing");
44867030
NC
666
667# line 13
c9de86d5 668check($this_file, 13, "leading tab");
44867030
NC
669
670#line 17
c9de86d5 671check($this_file, 17, "middle tab");
44867030
NC
672
673#line 19
c9de86d5 674check($this_file, 19, "loadsaspaces");
44867030
NC
675
676#line 23 KASHPRITZA
677check(qr/^KASHPRITZA$/, 23, "bare filename");
678
679#line 29 "KAHEEEE"
680check(qr/^KAHEEEE$/, 29, "filename in quotes");
681
682#line 31 "CLINK CLOINK BZZT"
683check(qr/^CLINK CLOINK BZZT$/, 31, "filename with spaces in quotes");
684
685#line 37 "THOOM THOOM"
686check(qr/^THOOM THOOM$/, 37, "filename with tabs in quotes");
687
a5fba4f1 688#line 41 "GLINK PLINK GLUNK DINK"
44867030
NC
689check(qr/^GLINK PLINK GLUNK DINK$/, 41, "a space after the quotes");
690
691#line 43 "BBFRPRAFPGHPP
692check(qr/^"BBFRPRAFPGHPP$/, 43, "actually missing a quote is still valid");
693
694#line 47 bang eth
695check(qr/^"BBFRPRAFPGHPP$/, 46, "but spaces aren't allowed without quotes");
696
26b6dc3f
RGS
697#line 77sevenseven
698check(qr/^"BBFRPRAFPGHPP$/, 49, "need a space after the line number");
699
44867030
NC
700eval <<'EOSTANZA'; die $@ if $@;
701#line 51 "With wonderful deathless ditties|We build up the world's great cities,|And out of a fabulous story|We fashion an empire's glory:|One man with a dream, at pleasure,|Shall go forth and conquer a crown;|And three with a new song's measure|Can trample a kingdom down."
702check(qr/^With.*down\.$/, 51, "Overflow the second small buffer check");
703EOSTANZA
704
705# And now, turn on the debugger flag for long names
706$^P = 0x100;
707
708#line 53 "For we are afar with the dawning|And the suns that are not yet high,|And out of the infinite morning|Intrepid you hear us cry-|How, spite of your human scorning,|Once more God's future draws nigh,|And already goes forth the warning|That ye of the past must die."
709check(qr/^For we.*must die\.$/, 53, "Our long line is set up");
710
711eval <<'EOT'; die $@ if $@;
712#line 59 " "
713check(qr/^ $/, 59, "Overflow the first small buffer check only");
714EOT
715
716eval <<'EOSTANZA'; die $@ if $@;
717#line 61 "Great hail! we cry to the comers|From the dazzling unknown shore;|Bring us hither your sun and your summers;|And renew our world as of yore;|You shall teach us your song's new numbers,|And things that we dreamed not before:|Yea, in spite of a dreamer who slumbers,|And a singer who sings no more."
718check(qr/^Great hail!.*no more\.$/, 61, "Overflow both small buffer checks");
719EOSTANZA
720
cbf45e84
FC
721sub check_line ($$) {
722 my ($line, $name) = @_;
723 my (undef, undef, $got_line) = caller;
724 is ($got_line, $line, $name);
725}
726
83944c01 727#line 531 parser.t
cbf45e84 728<<EOU; check_line(531, 'on same line as heredoc');
83944c01
FC
729EOU
730s//<<EOV/e if 0;
731EOV
cbf45e84 732check_line(535, 'after here-doc in quotes');
6ddcf93b 733<<EOW; <<EOX;
cbf45e84
FC
734${check_line(537, 'first line of interp in here-doc');;
735 check_line(538, 'second line of interp in here-doc');}
78a635de 736EOW
6ddcf93b
FC
737${check_line(540, 'first line of interp in second here-doc on same line');;
738 check_line(541, 'second line of interp in second heredoc on same line');}
739EOX
740eval <<'EVAL';
741#line 545
742"${<<EOY; <<EOZ}";
743${check_line(546, 'first line of interp in here-doc in quotes in eval');;
744 check_line(547, 'second line of interp in here-doc in quotes in eval');}
745EOY
746${check_line(549, '1st line of interp in 2nd hd, same line in q in eval');;
747 check_line(550, '2nd line of interp in 2nd hd, same line in q in eval');}
748EOZ
749EVAL
83944c01 750
9cf27252
FC
751time
752#line 42
cbf45e84 753;check_line(42, 'line number after "nullary\n#line"');
9cf27252 754
ffdb8b16
FC
755"${
756#line 53
757_}";
cbf45e84 758check_line(54, 'line number after qq"${#line}"');
ffdb8b16
FC
759
760#line 24
761"
cbf45e84 762${check_line(25, 'line number inside qq/<newline>${...}/')}";
9cf27252 763
b42366d4
FC
764<<"END";
765${;
766#line 625
767}
768END
769check_line(627, 'line number after heredoc containing #line');
770
8c29fccb
FC
771#line 638
772<<ENE . ${
773
774ENE
775"bar"};
776check_line(642, 'line number after ${expr} surrounding heredoc body');
777
778
44867030 779__END__
82269f56 780# Don't add new tests HERE. See "Add new tests HERE" above.