3 # Checks if the parser behaves correctly in edge cases
4 # (including weird syntax errors)
9 my ($got, $expected, $name) = @_;
11 print "not ok $test - $name\n";
12 my @caller = caller(1);
13 print "# Failed test at $caller[1] line $caller[2]\n";
15 print "# Got '$got'\n";
17 print "# Got undef\n";
19 print "# Expected $expected\n";
24 my ($got, $pattern, $name) = @_;
26 if (defined $got && $got =~ $pattern) {
27 print "ok $test - $name\n";
28 # Principle of least surprise - maintain the expected interface, even
29 # though we aren't using it here (yet).
32 failed($got, $pattern, $name);
36 my ($got, $expect, $name) = @_;
38 if (defined $expect) {
39 if (defined $got && $got eq $expect) {
40 print "ok $test - $name\n";
43 failed($got, "'$expect'", $name);
46 print "ok $test - $name\n";
49 failed($got, 'undef', $name);
54 like( $@, qr/^Can't modify hash dereference in repeat \(x\)/, '%@x=0' );
57 eval q{{s//${}/; //}};
58 like( $@, qr/syntax error/, 'syntax error, used to dump core' );
62 like( $@, qr/^Missing right brace on \\x/,
63 'syntax error in string, used to dump core' );
66 like( $@, qr/^Missing right brace on \\N/,
67 'syntax error in string with incomplete \N' );
69 like( $@, qr/^Missing braces on \\N/,
70 'syntax error in string with incomplete \N' );
73 like( $@, qr/^Missing right brace on \\o/,
74 'syntax error in string with incomplete \o' );
76 like( $@, qr/^Missing braces on \\o/,
77 'syntax error in string with incomplete \o' );
79 eval "a.b.c.d.e.f;sub";
80 like( $@, qr/^Illegal declaration of anonymous subroutine/,
81 'found by Markov chain stress testing' );
84 eval '($a, b) = (1, 2);';
85 like( $@, qr/^Can't modify constant item in list assignment/,
86 'bareword in list assignment' );
88 eval 'tie FOO, "Foo";';
89 like( $@, qr/^Can't modify constant item in tie /,
90 'tying a bareword causes a segfault in 5.6.1' );
93 like( $@, qr/^Can't modify constant item in undef operator /,
94 'undefing constant causes a segfault in 5.6.1 [ID 20010906.019]' );
96 eval 'read($bla, FILE, 1);';
97 like( $@, qr/^Can't modify constant item in read /,
98 'read($var, FILE, 1) segfaults on 5.6.1 [ID 20011025.054]' );
100 # This used to dump core (bug #17920)
101 eval q{ sub { sub { f1(f2();); my($a,$b,$c) } } };
102 like( $@, qr/error/, 'lexical block discarded by yacc' );
104 # bug #18573, used to corrupt memory
106 like( $@, qr/^Missing control char name in \\c/, q("\c" string) );
109 like( $@, qr/Final \$ should be \\\$ or \$name/, q($ at end of "" string) );
111 # two tests for memory corruption problems in the said variables
112 # (used to dump core or produce strange results)
114 is( "\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Qa", "a", "PL_lex_casestack" );
117 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
118 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
119 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
120 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
121 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
122 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
124 is( $@, '', 'PL_lex_brackstack' );
127 # tests for bug #20716
131 is("${a}{", "A{", "interpolation, qq//");
132 is("${a}[", "A[", "interpolation, qq//");
134 is("@{b}{", "B{", "interpolation, qq//");
135 is(qr/${a}\{/, '(?^:A\{)', "interpolation, qr//");
138 is($&, 'A{', "interpolation, m//");
140 is($c, 'foo', "interpolation, s/...//");
142 is($c, 'A{', "interpolation, s//.../");
143 is(<<"${a}{", "A{ A[ B{\n", "interpolation, here doc");
148 eval q{ sub a(;; &) { } a { } };
149 is($@, '', "';&' sub prototype confuses the lexer");
152 # ensure that the second print statement works, by playing a bit
153 # with the test output.
154 my %data = ( foo => "\n" );
162 # { q.* => ... } should be interpreted as hash, not block
164 foreach my $line (split /\n/, <<'EOF')
172 1 { q=bar= => 'bar' }
175 my ($expect, $eval) = split / /, $line, 2;
176 my $result = eval $eval;
177 is($@, '', "eval $eval");
178 is(ref $result, $expect ? 'HASH' : '', $eval);
183 local $SIG{__WARN__} = sub { }; # silence mandatory warning
184 eval q{ my $x = -F 1; };
185 like( $@, qr/(?i:syntax|parse) error .* near "F 1"/, "unknown filetest operators" );
187 eval q{ sub F { 42 } -F 1 },
189 '-F calls the F function'
195 eval q{ *foo{CODE} ? 1 : 0 };
196 is( $@, '', "glob subscript in conditional" );
201 eval q{ sub f { @a=@b=@c; {use} } };
202 like( $@, qr/syntax error/, "use without body" );
205 # [perl #2738] perl segfautls on input
207 eval q{ sub _ <> {} };
208 like($@, qr/Illegal declaration of subroutine main::_/, "readline operator as prototype");
210 eval q{ $s = sub <> {} };
211 like($@, qr/Illegal declaration of anonymous subroutine/, "readline operator as prototype");
213 eval q{ sub _ __FILE__ {} };
214 like($@, qr/Illegal declaration of subroutine main::_/, "__FILE__ as prototype");
217 # tests for "Bad name"
219 like( $@, qr/Bad name after foo::/, 'Bad name after foo::' );
221 like( $@, qr/Bad name after foo'/, 'Bad name after foo\'' );
223 # test for ?: context error
224 eval q{($a ? $x : ($y)) = 5};
225 like( $@, qr/Assignment to both a list and a scalar/, 'Assignment to both a list and a scalar' );
228 is( $@, '', 'comments in s///e' );
230 # these five used to coredump because the op cleanup on parse error could
231 # be to the wrong pad
234 sub { our $a= 1;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;
238 like($@, qr/Missing right curly/, 'nested sub syntax error' );
241 sub { my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$r);
244 like($@, qr/Missing right curly/, 'nested sub syntax error 2' );
247 sub { our $a= 1;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;
251 like($@, qr/Can't locate DieDieDie.pm/, 'croak cleanup' );
254 sub { my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$r);
258 like($@, qr/Can't locate DieDieDie.pm/, 'croak cleanup 2' );
263 my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$r);
264 @a =~ s/a/b/; # compile-time error
268 like($@, qr/Can't modify/, 'croak cleanup 3' );
270 # these might leak, or have duplicate frees, depending on the bugginess of
271 # the parser stack 'fail in reduce' cleanup code. They're here mainly as
272 # something to be run under valgrind, with PERL_DESTRUCT_LEVEL=1.
274 eval q[ BEGIN { } ] for 1..10;
275 is($@, "", 'BEGIN 1' );
277 eval q[ BEGIN { my $x; $x = 1 } ] for 1..10;
278 is($@, "", 'BEGIN 2' );
280 eval q[ BEGIN { \&foo1 } ] for 1..10;
281 is($@, "", 'BEGIN 3' );
283 eval q[ sub foo2 { } ] for 1..10;
284 is($@, "", 'BEGIN 4' );
286 eval q[ sub foo3 { my $x; $x=1 } ] for 1..10;
287 is($@, "", 'BEGIN 5' );
289 eval q[ BEGIN { die } ] for 1..10;
290 like($@, qr/BEGIN failed--compilation aborted/, 'BEGIN 6' );
292 eval q[ BEGIN {\&foo4; die } ] for 1..10;
293 like($@, qr/BEGIN failed--compilation aborted/, 'BEGIN 7' );
297 # check both the specific case in the ticket, and a few other paths into
300 my $x100 = "x" x 256;
307 is($@, "", "251 character \$# sigil ident ok");
309 like($@, qr/Identifier too long/, "too long id in \$# sigil ctx");
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");
321 eval qq[ \\&$xFC ]; # take a ref since I don't want to call it
322 is($@, "", "252 character & sigil ident ok");
324 like($@, qr/Identifier too long/, "too long id in & sigil ctx");
327 is($@, "", "252 character glob ident ok");
329 like($@, qr/Identifier too long/, "too long id in glob ctx");
332 like($@, qr/Missing \$ on loop variable/,
333 "253 char id ok, but a different error");
334 eval qq[ for $xFE; ];
335 like($@, qr/Identifier too long/, "too long id in for ctx");
337 # the specific case from the ticket
340 like($@, qr/Identifier too long/, "too long id ticket case");
344 is(exists &zlonk, '', 'sub not present');
345 eval qq[ {sub zlonk} ];
346 is($@, '', 'sub declaration followed by a closing curly');
347 is(exists &zlonk, 1, 'sub now stubbed');
348 is(defined &zlonk, '', 'but no body defined');
361 is($@, "", "multiline whitespace inside substitute expression");
363 # Add new tests HERE:
365 eval '@A =~ s/a/b/; # compilation error
370 sub rima ($%&*$&*\$%\*&$%*&) :method;
371 sub ono :lvalue { die }
372 sub whitu (_) { die }
373 sub waru ($;) :method { die }
376 is $::{tahi}, undef, 'empty sub decl ignored after compilation error';
377 is $::{rua}, undef, 'stub decl ignored after compilation error';
378 is $::{toru}, undef, 'stub+proto decl ignored after compilation error';
379 is $::{wha}, undef, 'stub+attr decl ignored after compilation error';
380 is $::{rima}, undef, 'stub+proto+attr ignored after compilation error';
381 is $::{ono}, undef, 'sub decl with attr ignored after compilation error';
382 is $::{whitu}, undef, 'sub decl w proto ignored after compilation error';
383 is $::{waru}, undef, 'sub w attr+proto ignored after compilation error';
384 is $::{iwa}, undef, 'non-empty sub decl ignored after compilation error';
385 is *BEGIN{CODE}, undef, 'BEGIN leaves no stub after compilation error';
387 # bug #74022: Loop on characters in \p{OtherIDContinue}
388 # This test hangs if it fails.
390 is(1,1, '[perl #74022] Parser looping on OtherIDContinue chars');
392 # More awkward tests for #line. Keep these at the end, as they will screw
393 # with sane line reporting for any other test failures
396 my ($file, $line, $name) = @_;
397 my (undef, $got_file, $got_line) = caller;
398 like ($got_file, $file, "file of $name");
399 is ($got_line, $line, "line of $name");
402 my $this_file = qr/parser\.t(?:\.[bl]eb?)?$/;
404 check($this_file, 3, "bare line");
407 check($this_file, 5, "bare line with leading space");
410 check($this_file, 7, "trailing space still valid");
413 check($this_file, 11, "leading and trailing");
416 check($this_file, 13, "leading tab");
419 check($this_file, 17, "middle tab");
422 check($this_file, 19, "loadsaspaces");
425 check(qr/^KASHPRITZA$/, 23, "bare filename");
428 check(qr/^KAHEEEE$/, 29, "filename in quotes");
430 #line 31 "CLINK CLOINK BZZT"
431 check(qr/^CLINK CLOINK BZZT$/, 31, "filename with spaces in quotes");
433 #line 37 "THOOM THOOM"
434 check(qr/^THOOM THOOM$/, 37, "filename with tabs in quotes");
436 #line 41 "GLINK PLINK GLUNK DINK"
437 check(qr/^GLINK PLINK GLUNK DINK$/, 41, "a space after the quotes");
439 #line 43 "BBFRPRAFPGHPP
440 check(qr/^"BBFRPRAFPGHPP$/, 43, "actually missing a quote is still valid");
443 check(qr/^"BBFRPRAFPGHPP$/, 46, "but spaces aren't allowed without quotes");
446 check(qr/^"BBFRPRAFPGHPP$/, 49, "need a space after the line number");
448 eval <<'EOSTANZA'; die $@ if $@;
449 #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."
450 check(qr/^With.*down\.$/, 51, "Overflow the second small buffer check");
453 # And now, turn on the debugger flag for long names
456 #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."
457 check(qr/^For we.*must die\.$/, 53, "Our long line is set up");
459 eval <<'EOT'; die $@ if $@;
461 check(qr/^ $/, 59, "Overflow the first small buffer check only");
464 eval <<'EOSTANZA'; die $@ if $@;
465 #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."
466 check(qr/^Great hail!.*no more\.$/, 61, "Overflow both small buffer checks");
471 is(eval q{ "$x[0]->strung" }, 'string->strung',
472 'literal -> after an array subscript within ""');
474 # this used to give "string"
475 like("$x[0]-> [0]", qr/^ARRAY\([^)]*\)-> \[0]\z/,
476 'literal -> [0] after an array subscript within ""');
480 # Don't add new tests HERE. See note above