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' );
72 eval "a.b.c.d.e.f;sub";
73 like( $@, qr/^Illegal declaration of anonymous subroutine/,
74 'found by Markov chain stress testing' );
77 eval '($a, b) = (1, 2);';
78 like( $@, qr/^Can't modify constant item in list assignment/,
79 'bareword in list assignment' );
81 eval 'tie FOO, "Foo";';
82 like( $@, qr/^Can't modify constant item in tie /,
83 'tying a bareword causes a segfault in 5.6.1' );
86 like( $@, qr/^Can't modify constant item in undef operator /,
87 'undefing constant causes a segfault in 5.6.1 [ID 20010906.019]' );
89 eval 'read($bla, FILE, 1);';
90 like( $@, qr/^Can't modify constant item in read /,
91 'read($var, FILE, 1) segfaults on 5.6.1 [ID 20011025.054]' );
93 # This used to dump core (bug #17920)
94 eval q{ sub { sub { f1(f2();); my($a,$b,$c) } } };
95 like( $@, qr/error/, 'lexical block discarded by yacc' );
97 # bug #18573, used to corrupt memory
99 like( $@, qr/^Missing control char name in \\c/, q("\c" string) );
102 like( $@, qr/Final \$ should be \\\$ or \$name/, q($ at end of "" string) );
104 # two tests for memory corruption problems in the said variables
105 # (used to dump core or produce strange results)
107 is( "\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Qa", "a", "PL_lex_casestack" );
110 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
111 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
112 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
113 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
114 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
115 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
117 is( $@, '', 'PL_lex_brackstack' );
120 # tests for bug #20716
124 is("${a}{", "A{", "interpolation, qq//");
125 is("${a}[", "A[", "interpolation, qq//");
127 is("@{b}{", "B{", "interpolation, qq//");
128 is(qr/${a}{/, '(?-xism:A{)', "interpolation, qr//");
131 is($&, 'A{', "interpolation, m//");
133 is($c, 'foo', "interpolation, s/...//");
135 is($c, 'A{', "interpolation, s//.../");
136 is(<<"${a}{", "A{ A[ B{\n", "interpolation, here doc");
141 eval q{ sub a(;; &) { } a { } };
142 is($@, '', "';&' sub prototype confuses the lexer");
145 # ensure that the second print statement works, by playing a bit
146 # with the test output.
147 my %data = ( foo => "\n" );
155 # { q.* => ... } should be interpreted as hash, not block
157 foreach my $line (split /\n/, <<'EOF')
165 1 { q=bar= => 'bar' }
168 my ($expect, $eval) = split / /, $line, 2;
169 my $result = eval $eval;
170 is($@, '', "eval $eval");
171 is(ref $result, $expect ? 'HASH' : '', $eval);
176 local $SIG{__WARN__} = sub { }; # silence mandatory warning
177 eval q{ my $x = -F 1; };
178 like( $@, qr/(?i:syntax|parse) error .* near "F 1"/, "unknown filetest operators" );
180 eval q{ sub F { 42 } -F 1 },
182 '-F calls the F function'
188 eval q{ *foo{CODE} ? 1 : 0 };
189 is( $@, '', "glob subscript in conditional" );
194 eval q{ sub f { @a=@b=@c; {use} } };
195 like( $@, qr/syntax error/, "use without body" );
200 # this used to segfault (because $[=1 is optimized away to a null block)
205 $[ = 0; # restore the original value for less side-effects
208 # [perl #2738] perl segfautls on input
210 eval q{ sub _ <> {} };
211 like($@, qr/Illegal declaration of subroutine main::_/, "readline operator as prototype");
213 eval q{ $s = sub <> {} };
214 like($@, qr/Illegal declaration of anonymous subroutine/, "readline operator as prototype");
216 eval q{ sub _ __FILE__ {} };
217 like($@, qr/Illegal declaration of subroutine main::_/, "__FILE__ as prototype");
220 # [perl #36313] perl -e "1for$[=0" crash
225 print "ok $test - optimized assignment to \$[ used to segfault in list context\n";
226 if ($[ = 0) { $x = 1 }
228 print "ok $test - optimized assignment to \$[ used to segfault in scalar context\n";
230 is($x, 2, 'scalar assignment to $[ behaves like other variables');
232 is($x, 1, 'list assignment to $[ behaves like other variables');
233 $x = eval q{ ($[, $x) = (0) };
234 like($@, qr/That use of \$\[ is unsupported/,
235 'cannot assign to $[ in a list');
236 eval q{ ($[) = (0, 1) };
237 like($@, qr/That use of \$\[ is unsupported/,
238 'cannot assign list of >1 elements to $[');
240 like($@, qr/That use of \$\[ is unsupported/,
241 'cannot assign list of <1 elements to $[');
244 # tests for "Bad name"
246 like( $@, qr/Bad name after foo::/, 'Bad name after foo::' );
248 like( $@, qr/Bad name after foo'/, 'Bad name after foo\'' );
250 # test for ?: context error
251 eval q{($a ? $x : ($y)) = 5};
252 like( $@, qr/Assignment to both a list and a scalar/, 'Assignment to both a list and a scalar' );
255 is( $@, '', 'comments in s///e' );
257 # these five used to coredump because the op cleanup on parse error could
258 # be to the wrong pad
261 sub { our $a= 1;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;
265 like($@, qr/Missing right curly/, 'nested sub syntax error' );
268 sub { my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$r);
271 like($@, qr/Missing right curly/, 'nested sub syntax error 2' );
274 sub { our $a= 1;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;
278 like($@, qr/Can't locate DieDieDie.pm/, 'croak cleanup' );
281 sub { my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$r);
285 like($@, qr/Can't locate DieDieDie.pm/, 'croak cleanup 2' );
290 my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$r);
291 @a =~ s/a/b/; # compile-time error
295 like($@, qr/Can't modify/, 'croak cleanup 3' );
297 # these might leak, or have duplicate frees, depending on the bugginess of
298 # the parser stack 'fail in reduce' cleanup code. They're here mainly as
299 # something to be run under valgrind, with PERL_DESTRUCT_LEVEL=1.
301 eval q[ BEGIN { } ] for 1..10;
302 is($@, "", 'BEGIN 1' );
304 eval q[ BEGIN { my $x; $x = 1 } ] for 1..10;
305 is($@, "", 'BEGIN 2' );
307 eval q[ BEGIN { \&foo1 } ] for 1..10;
308 is($@, "", 'BEGIN 3' );
310 eval q[ sub foo2 { } ] for 1..10;
311 is($@, "", 'BEGIN 4' );
313 eval q[ sub foo3 { my $x; $x=1 } ] for 1..10;
314 is($@, "", 'BEGIN 5' );
316 eval q[ BEGIN { die } ] for 1..10;
317 like($@, qr/BEGIN failed--compilation aborted/, 'BEGIN 6' );
319 eval q[ BEGIN {\&foo4; die } ] for 1..10;
320 like($@, qr/BEGIN failed--compilation aborted/, 'BEGIN 7' );
322 # Add new tests HERE:
324 # More awkward tests for #line. Keep these at the end, as they will screw
325 # with sane line reporting for any other test failures
328 my ($file, $line, $name) = @_;
329 my (undef, $got_file, $got_line) = caller;
330 like ($got_file, $file, "file of $name");
331 is ($got_line, $line, "line of $name");
335 check(qr/parser\.t$/, 3, "bare line");
338 check(qr/parser\.t$/, 5, "bare line with leading space");
341 check(qr/parser\.t$/, 7, "trailing space still valid");
344 check(qr/parser\.t$/, 11, "leading and trailing");
347 check(qr/parser\.t$/, 13, "leading tab");
350 check(qr/parser\.t$/, 17, "middle tab");
353 check(qr/parser\.t$/, 19, "loadsaspaces");
356 check(qr/^KASHPRITZA$/, 23, "bare filename");
359 check(qr/^KAHEEEE$/, 29, "filename in quotes");
361 #line 31 "CLINK CLOINK BZZT"
362 check(qr/^CLINK CLOINK BZZT$/, 31, "filename with spaces in quotes");
364 #line 37 "THOOM THOOM"
365 check(qr/^THOOM THOOM$/, 37, "filename with tabs in quotes");
367 #line 41 "GLINK PLINK GLUNK DINK"
368 check(qr/^GLINK PLINK GLUNK DINK$/, 41, "a space after the quotes");
370 #line 43 "BBFRPRAFPGHPP
371 check(qr/^"BBFRPRAFPGHPP$/, 43, "actually missing a quote is still valid");
374 check(qr/^"BBFRPRAFPGHPP$/, 46, "but spaces aren't allowed without quotes");
377 check(qr/^"BBFRPRAFPGHPP$/, 49, "need a space after the line number");
379 eval <<'EOSTANZA'; die $@ if $@;
380 #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."
381 check(qr/^With.*down\.$/, 51, "Overflow the second small buffer check");
384 # And now, turn on the debugger flag for long names
387 #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."
388 check(qr/^For we.*must die\.$/, 53, "Our long line is set up");
390 eval <<'EOT'; die $@ if $@;
392 check(qr/^ $/, 59, "Overflow the first small buffer check only");
395 eval <<'EOSTANZA'; die $@ if $@;
396 #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."
397 check(qr/^Great hail!.*no more\.$/, 61, "Overflow both small buffer checks");
402 is(eval q{ "$x[0]->strung" }, 'string->strung',
403 'literal -> after an array subscript within ""');
405 # this used to give "string"
406 like("$x[0]-> [0]", qr/^ARRAY\([^)]*\)-> \[0]\z/,
407 'literal -> [0] after an array subscript within ""');
411 # Don't add new tests HERE. See note above