3 # Checks if the parser behaves correctly in edge cases
4 # (including weird syntax errors)
14 my ($got, $expected, $name) = @_;
16 print "not ok $test - $name\n";
17 my @caller = caller(1);
18 print "# Failed test at $caller[1] line $caller[2]\n";
20 print "# Got '$got'\n";
22 print "# Got undef\n";
24 print "# Expected $expected\n";
29 my ($got, $pattern, $name) = @_;
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).
37 failed($got, $pattern, $name);
41 my ($got, $expect, $name) = @_;
43 if (defined $expect) {
44 if (defined $got && $got eq $expect) {
45 print "ok $test - $name\n";
48 failed($got, "'$expect'", $name);
51 print "ok $test - $name\n";
54 failed($got, 'undef', $name);
59 like( $@, qr/^Can't modify hash dereference in repeat \(x\)/, '%@x=0' );
62 eval q{{s//${}/; //}};
63 like( $@, qr/syntax error/, 'syntax error, used to dump core' );
67 like( $@, qr/^Missing right brace on \\x/,
68 'syntax error in string, used to dump core' );
71 like( $@, qr/^Missing right brace on \\N/,
72 'syntax error in string with incomplete \N' );
74 like( $@, qr/^Missing braces on \\N/,
75 'syntax error in string with incomplete \N' );
78 like( $@, qr/^Missing right brace on \\o/,
79 'syntax error in string with incomplete \o' );
81 like( $@, qr/^Missing braces on \\o/,
82 'syntax error in string with incomplete \o' );
84 eval "a.b.c.d.e.f;sub";
85 like( $@, qr/^Illegal declaration of anonymous subroutine/,
86 'found by Markov chain stress testing' );
89 eval '($a, b) = (1, 2);';
90 like( $@, qr/^Can't modify constant item in list assignment/,
91 'bareword in list assignment' );
93 eval 'tie FOO, "Foo";';
94 like( $@, qr/^Can't modify constant item in tie /,
95 'tying a bareword causes a segfault in 5.6.1' );
98 like( $@, qr/^Can't modify constant item in undef operator /,
99 'undefing constant causes a segfault in 5.6.1 [ID 20010906.019]' );
101 eval 'read($bla, FILE, 1);';
102 like( $@, qr/^Can't modify constant item in read /,
103 'read($var, FILE, 1) segfaults on 5.6.1 [ID 20011025.054]' );
105 # This used to dump core (bug #17920)
106 eval q{ sub { sub { f1(f2();); my($a,$b,$c) } } };
107 like( $@, qr/error/, 'lexical block discarded by yacc' );
109 # bug #18573, used to corrupt memory
111 like( $@, qr/^Missing control char name in \\c/, q("\c" string) );
114 like( $@, qr/Final \$ should be \\\$ or \$name/, q($ at end of "" string) );
116 # two tests for memory corruption problems in the said variables
117 # (used to dump core or produce strange results)
119 is( "\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Qa", "a", "PL_lex_casestack" );
122 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
123 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
124 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
125 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
126 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
127 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
129 is( $@, '', 'PL_lex_brackstack' );
132 # tests for bug #20716
136 is("${a}{", "A{", "interpolation, qq//");
137 is("${a}[", "A[", "interpolation, qq//");
139 is("@{b}{", "B{", "interpolation, qq//");
140 is(qr/${a}\{/, '(?^:A\{)', "interpolation, qr//");
143 is($&, 'A{', "interpolation, m//");
145 is($c, 'foo', "interpolation, s/...//");
147 is($c, 'A{', "interpolation, s//.../");
148 is(<<"${a}{", "A{ A[ B{\n", "interpolation, here doc");
153 eval q{ sub a(;; &) { } a { } };
154 is($@, '', "';&' sub prototype confuses the lexer");
157 # ensure that the second print statement works, by playing a bit
158 # with the test output.
159 my %data = ( foo => "\n" );
167 # { q.* => ... } should be interpreted as hash, not block
169 foreach my $line (split /\n/, <<'EOF')
177 1 { q=bar= => 'bar' }
180 my ($expect, $eval) = split / /, $line, 2;
181 my $result = eval $eval;
182 is($@, '', "eval $eval");
183 is(ref $result, $expect ? 'HASH' : '', $eval);
188 local $SIG{__WARN__} = sub { }; # silence mandatory warning
189 eval q{ my $x = -F 1; };
190 like( $@, qr/(?i:syntax|parse) error .* near "F 1"/, "unknown filetest operators" );
192 eval q{ sub F { 42 } -F 1 },
194 '-F calls the F function'
200 eval q{ *foo{CODE} ? 1 : 0 };
201 is( $@, '', "glob subscript in conditional" );
206 eval q{ sub f { @a=@b=@c; {use} } };
207 like( $@, qr/syntax error/, "use without body" );
210 # [perl #2738] perl segfautls on input
212 eval q{ sub _ <> {} };
213 like($@, qr/Illegal declaration of subroutine main::_/, "readline operator as prototype");
215 eval q{ $s = sub <> {} };
216 like($@, qr/Illegal declaration of anonymous subroutine/, "readline operator as prototype");
218 eval q{ sub _ __FILE__ {} };
219 like($@, qr/Illegal declaration of subroutine main::_/, "__FILE__ as prototype");
222 # tests for "Bad name"
224 like( $@, qr/Bad name after foo::/, 'Bad name after foo::' );
226 like( $@, qr/Bad name after foo'/, 'Bad name after foo\'' );
228 # test for ?: context error
229 eval q{($a ? $x : ($y)) = 5};
230 like( $@, qr/Assignment to both a list and a scalar/, 'Assignment to both a list and a scalar' );
233 is( $@, '', 'comments in s///e' );
235 # these five used to coredump because the op cleanup on parse error could
236 # be to the wrong pad
239 sub { our $a= 1;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;
243 like($@, qr/Missing right curly/, 'nested sub syntax error' );
246 sub { my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$r);
249 like($@, qr/Missing right curly/, 'nested sub syntax error 2' );
252 sub { our $a= 1;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;
256 like($@, qr/Can't locate DieDieDie.pm/, 'croak cleanup' );
259 sub { my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$r);
263 like($@, qr/Can't locate DieDieDie.pm/, 'croak cleanup 2' );
268 my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$r);
269 @a =~ s/a/b/; # compile-time error
273 like($@, qr/Can't modify/, 'croak cleanup 3' );
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.
279 eval q[ BEGIN { } ] for 1..10;
280 is($@, "", 'BEGIN 1' );
282 eval q[ BEGIN { my $x; $x = 1 } ] for 1..10;
283 is($@, "", 'BEGIN 2' );
285 eval q[ BEGIN { \&foo1 } ] for 1..10;
286 is($@, "", 'BEGIN 3' );
288 eval q[ sub foo2 { } ] for 1..10;
289 is($@, "", 'BEGIN 4' );
291 eval q[ sub foo3 { my $x; $x=1 } ] for 1..10;
292 is($@, "", 'BEGIN 5' );
294 eval q[ BEGIN { die } ] for 1..10;
295 like($@, qr/BEGIN failed--compilation aborted/, 'BEGIN 6' );
297 eval q[ BEGIN {\&foo4; die } ] for 1..10;
298 like($@, qr/BEGIN failed--compilation aborted/, 'BEGIN 7' );
302 # check both the specific case in the ticket, and a few other paths into
305 my $x100 = "x" x 256;
312 is($@, "", "251 character \$# sigil ident ok");
314 like($@, qr/Identifier too long/, "too long id in \$# sigil ctx");
317 is($@, "", "251 character \$ sigil ident ok");
319 like($@, qr/Identifier too long/, "too long id in \$ sigil ctx");
322 is($@, "", "251 character % sigil ident ok");
324 like($@, qr/Identifier too long/, "too long id in % sigil ctx");
326 eval qq[ \\&$xFB ]; # take a ref since I don't want to call it
327 is($@, "", "251 character & sigil ident ok");
329 like($@, qr/Identifier too long/, "too long id in & sigil ctx");
332 is($@, "", "252 character glob ident ok");
334 like($@, qr/Identifier too long/, "too long id in glob ctx");
337 like($@, qr/Missing \$ on loop variable/,
338 "253 char id ok, but a different error");
339 eval qq[ for $xFE; ];
340 like($@, qr/Identifier too long/, "too long id in for ctx");
342 # the specific case from the ticket
345 like($@, qr/Identifier too long/, "too long id ticket case");
349 is(exists &zlonk, '', 'sub not present');
350 eval qq[ {sub zlonk} ];
351 is($@, '', 'sub declaration followed by a closing curly');
352 is(exists &zlonk, 1, 'sub now stubbed');
353 is(defined &zlonk, '', 'but no body defined');
356 # [perl #113016] CORE::print::foo
357 sub CORE'print'foo { 43 } # apostrophes intentional; do not tempt fate
358 sub CORE'foo'bar { 43 }
359 is CORE::print::foo, 43, 'CORE::print::foo is not CORE::print ::foo';
360 is scalar eval "CORE::foo'bar", 43, "CORE::foo'bar is not an error";
372 is($@, "", "multiline whitespace inside substitute expression");
374 eval '@A =~ s/a/b/; # compilation error
379 sub rima ($%&*$&*\$%\*&$%*&) :method;
380 sub ono :lvalue { die }
381 sub whitu (_) { die }
382 sub waru ($;) :method { die }
385 is $::{tahi}, undef, 'empty sub decl ignored after compilation error';
386 is $::{rua}, undef, 'stub decl ignored after compilation error';
387 is $::{toru}, undef, 'stub+proto decl ignored after compilation error';
388 is $::{wha}, undef, 'stub+attr decl ignored after compilation error';
389 is $::{rima}, undef, 'stub+proto+attr ignored after compilation error';
390 is $::{ono}, undef, 'sub decl with attr ignored after compilation error';
391 is $::{whitu}, undef, 'sub decl w proto ignored after compilation error';
392 is $::{waru}, undef, 'sub w attr+proto ignored after compilation error';
393 is $::{iwa}, undef, 'non-empty sub decl ignored after compilation error';
394 is *BEGIN{CODE}, undef, 'BEGIN leaves no stub after compilation error';
397 "ok $test - format inside re-eval" =~ /(?{
399 @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
414 is $@, "", 'pod inside string in string eval';
422 print "ok ", ++$test, " - pod inside string outside of string eval\n";
424 like "blah blah blah\n", qr/${\ <<END
427 }/, 'here docs in multiline quoted construct';
428 like "blah blah blah\n", eval q|qr/${\ <<END
431 }/|, 'here docs in multiline quoted construct in string eval';
433 # Unterminated here-docs in subst in eval; used to crash
436 print "ok ", ++$test, " - unterminated here-docs in s/// in string eval\n";
438 sub 'Hello'_he_said (_);
439 is prototype "Hello::_he_said", '_', 'initial tick in sub declaration';
443 is(eval q{ "$x[0]->strung" }, 'string->strung',
444 'literal -> after an array subscript within ""');
446 # this used to give "string"
447 like("$x[0]-> [0]", qr/^ARRAY\([^)]*\)-> \[0]\z/,
448 'literal -> [0] after an array subscript within ""');
451 eval 'no if $] >= 5.17.4 warnings => "deprecated"';
452 is 1,1, ' no crash for "no ... syntax error"';
456 is $pkg, 3, '[perl #114942] for my $foo()){} $foo';
458 # Check that format 'Foo still works after removing the hack from
462 ok @<< - format 'foo still works
472 ok @<< - format ::foo still works
482 is $_, 'main', '__PACKAGE__ is read-only';
486 BEGIN{ ${"_<".__FILE__} = \1 }
488 'no __FILE__ corruption when setting CopFILESV to a ref';
490 # Add new tests HERE (above this line)
492 # bug #74022: Loop on characters in \p{OtherIDContinue}
493 # This test hangs if it fails.
494 eval chr 0x387; # forces loading of utf8.pm
495 is(1,1, '[perl #74022] Parser looping on OtherIDContinue chars');
497 # More awkward tests for #line. Keep these at the end, as they will screw
498 # with sane line reporting for any other test failures
501 my ($file, $line, $name) = @_;
502 my (undef, $got_file, $got_line) = caller;
503 like ($got_file, $file, "file of $name");
504 is ($got_line, $line, "line of $name");
507 my $this_file = qr/parser\.t(?:\.[bl]eb?)?$/;
509 check($this_file, 3, "bare line");
512 check($this_file, 5, "bare line with leading space");
515 check($this_file, 7, "trailing space still valid");
518 check($this_file, 11, "leading and trailing");
521 check($this_file, 13, "leading tab");
524 check($this_file, 17, "middle tab");
527 check($this_file, 19, "loadsaspaces");
530 check(qr/^KASHPRITZA$/, 23, "bare filename");
533 check(qr/^KAHEEEE$/, 29, "filename in quotes");
535 #line 31 "CLINK CLOINK BZZT"
536 check(qr/^CLINK CLOINK BZZT$/, 31, "filename with spaces in quotes");
538 #line 37 "THOOM THOOM"
539 check(qr/^THOOM THOOM$/, 37, "filename with tabs in quotes");
541 #line 41 "GLINK PLINK GLUNK DINK"
542 check(qr/^GLINK PLINK GLUNK DINK$/, 41, "a space after the quotes");
544 #line 43 "BBFRPRAFPGHPP
545 check(qr/^"BBFRPRAFPGHPP$/, 43, "actually missing a quote is still valid");
548 check(qr/^"BBFRPRAFPGHPP$/, 46, "but spaces aren't allowed without quotes");
551 check(qr/^"BBFRPRAFPGHPP$/, 49, "need a space after the line number");
553 eval <<'EOSTANZA'; die $@ if $@;
554 #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."
555 check(qr/^With.*down\.$/, 51, "Overflow the second small buffer check");
558 # And now, turn on the debugger flag for long names
561 #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."
562 check(qr/^For we.*must die\.$/, 53, "Our long line is set up");
564 eval <<'EOT'; die $@ if $@;
566 check(qr/^ $/, 59, "Overflow the first small buffer check only");
569 eval <<'EOSTANZA'; die $@ if $@;
570 #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."
571 check(qr/^Great hail!.*no more\.$/, 61, "Overflow both small buffer checks");
575 <<EOU; check('parser\.t', 531, 'on same line as heredoc');
579 check('parser\.t', 535, 'after here-doc in quotes');
581 ${check('parser\.t', 537, 'first line of interp in here-doc');;
582 check('parser\.t', 538, 'second line of interp in here-doc');}
586 # Don't add new tests HERE. See note above