3 # Checks if the parser behaves correctly in edge cases
4 # (including weird syntax errors)
11 BEGIN { require "./test.pl"; }
15 like( $@, qr/^Can't modify hash dereference in repeat \(x\)/, '%@x=0' );
18 eval q{{s//${}/; //}};
19 like( $@, qr/syntax error/, 'syntax error, used to dump core' );
23 like( $@, qr/^Missing right brace on \\x/,
24 'syntax error in string, used to dump core' );
27 like( $@, qr/^Missing right brace on \\N/,
28 'syntax error in string with incomplete \N' );
30 like( $@, qr/^Missing braces on \\N/,
31 'syntax error in string with incomplete \N' );
33 eval "a.b.c.d.e.f;sub";
34 like( $@, qr/^Illegal declaration of anonymous subroutine/,
35 'found by Markov chain stress testing' );
38 eval '($a, b) = (1, 2);';
39 like( $@, qr/^Can't modify constant item in list assignment/,
40 'bareword in list assignment' );
42 eval 'tie FOO, "Foo";';
43 like( $@, qr/^Can't modify constant item in tie /,
44 'tying a bareword causes a segfault in 5.6.1' );
47 like( $@, qr/^Can't modify constant item in undef operator /,
48 'undefing constant causes a segfault in 5.6.1 [ID 20010906.019]' );
50 eval 'read($bla, FILE, 1);';
51 like( $@, qr/^Can't modify constant item in read /,
52 'read($var, FILE, 1) segfaults on 5.6.1 [ID 20011025.054]' );
54 # This used to dump core (bug #17920)
55 eval q{ sub { sub { f1(f2();); my($a,$b,$c) } } };
56 like( $@, qr/error/, 'lexical block discarded by yacc' );
58 # bug #18573, used to corrupt memory
60 like( $@, qr/^Missing control char name in \\c/, q("\c" string) );
63 like( $@, qr/Final \$ should be \\\$ or \$name/, q($ at end of "" string) );
65 # two tests for memory corruption problems in the said variables
66 # (used to dump core or produce strange results)
68 is( "\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Qa", "a", "PL_lex_casestack" );
71 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
72 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
73 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
74 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
75 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
76 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
78 is( $@, '', 'PL_lex_brackstack' );
81 # tests for bug #20716
85 is("${a}{", "A{", "interpolation, qq//");
86 is("${a}[", "A[", "interpolation, qq//");
88 is("@{b}{", "B{", "interpolation, qq//");
89 is(qr/${a}{/, '(?-xism:A{)', "interpolation, qr//");
92 is($&, 'A{', "interpolation, m//");
94 is($c, 'foo', "interpolation, s/...//");
96 is($c, 'A{', "interpolation, s//.../");
97 is(<<"${a}{", "A{ A[ B{\n", "interpolation, here doc");
102 eval q{ sub a(;; &) { } a { } };
103 is($@, '', "';&' sub prototype confuses the lexer");
106 # ensure that the second print statement works, by playing a bit
107 # with the test output.
108 my %data = ( foo => "\n" );
115 # { q.* => ... } should be interpreted as hash, not block
117 foreach my $line (split /\n/, <<'EOF')
125 1 { q=bar= => 'bar' }
128 my ($expect, $eval) = split / /, $line, 2;
129 my $result = eval $eval;
130 ok($@ eq '', "eval $eval");
131 is(ref $result, $expect ? 'HASH' : '', $eval);
136 local $SIG{__WARN__} = sub { }; # silence mandatory warning
137 eval q{ my $x = -F 1; };
138 like( $@, qr/(?i:syntax|parse) error .* near "F 1"/, "unknown filetest operators" );
140 eval q{ sub F { 42 } -F 1 },
142 '-F calls the F function'
148 eval q{ *foo{CODE} ? 1 : 0 };
149 is( $@, '', "glob subscript in conditional" );
154 eval q{ sub f { @a=@b=@c; {use} } };
155 like( $@, qr/syntax error/, "use without body" );
160 # this used to segfault (because $[=1 is optimized away to a null block)
164 $[ = 0; # restore the original value for less side-effects
167 # [perl #2738] perl segfautls on input
169 eval q{ sub _ <> {} };
170 like($@, qr/Illegal declaration of subroutine main::_/, "readline operator as prototype");
172 eval q{ $s = sub <> {} };
173 like($@, qr/Illegal declaration of anonymous subroutine/, "readline operator as prototype");
175 eval q{ sub _ __FILE__ {} };
176 like($@, qr/Illegal declaration of subroutine main::_/, "__FILE__ as prototype");
179 # [perl #36313] perl -e "1for$[=0" crash
183 pass('optimized assignment to $[ used to segfault in list context');
184 if ($[ = 0) { $x = 1 }
185 pass('optimized assignment to $[ used to segfault in scalar context');
187 is($x, 2, 'scalar assignment to $[ behaves like other variables');
189 is($x, 1, 'list assignment to $[ behaves like other variables');
190 $x = eval q{ ($[, $x) = (0) };
191 like($@, qr/That use of \$\[ is unsupported/,
192 'cannot assign to $[ in a list');
193 eval q{ ($[) = (0, 1) };
194 like($@, qr/That use of \$\[ is unsupported/,
195 'cannot assign list of >1 elements to $[');
197 like($@, qr/That use of \$\[ is unsupported/,
198 'cannot assign list of <1 elements to $[');
201 # tests for "Bad name"
203 like( $@, qr/Bad name after foo::/, 'Bad name after foo::' );
205 like( $@, qr/Bad name after foo'/, 'Bad name after foo\'' );
207 # test for ?: context error
208 eval q{($a ? $x : ($y)) = 5};
209 like( $@, qr/Assignment to both a list and a scalar/, 'Assignment to both a list and a scalar' );
212 is( $@, '', 'comments in s///e' );
214 # these five used to coredump because the op cleanup on parse error could
215 # be to the wrong pad
218 sub { our $a= 1;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;
222 like($@, qr/Missing right curly/, 'nested sub syntax error' );
225 sub { my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$r);
228 like($@, qr/Missing right curly/, 'nested sub syntax error 2' );
231 sub { our $a= 1;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;
235 like($@, qr/Can't locate DieDieDie.pm/, 'croak cleanup' );
238 sub { my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$r);
242 like($@, qr/Can't locate DieDieDie.pm/, 'croak cleanup 2' );
247 my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$r);
248 @a =~ s/a/b/; # compile-time error
252 like($@, qr/Can't modify/, 'croak cleanup 3' );
254 # these might leak, or have duplicate frees, depending on the bugginess of
255 # the parser stack 'fail in reduce' cleanup code. They're here mainly as
256 # something to be run under valgrind, with PERL_DESTRUCT_LEVEL=1.
258 eval q[ BEGIN { } ] for 1..10;
259 is($@, "", 'BEGIN 1' );
261 eval q[ BEGIN { my $x; $x = 1 } ] for 1..10;
262 is($@, "", 'BEGIN 2' );
264 eval q[ BEGIN { \&foo1 } ] for 1..10;
265 is($@, "", 'BEGIN 3' );
267 eval q[ sub foo2 { } ] for 1..10;
268 is($@, "", 'BEGIN 4' );
270 eval q[ sub foo3 { my $x; $x=1 } ] for 1..10;
271 is($@, "", 'BEGIN 5' );
273 eval q[ BEGIN { die } ] for 1..10;
274 like($@, qr/BEGIN failed--compilation aborted/, 'BEGIN 6' );
276 eval q[ BEGIN {\&foo4; die } ] for 1..10;
277 like($@, qr/BEGIN failed--compilation aborted/, 'BEGIN 7' );
279 # Add new tests HERE:
281 # More awkward tests for #line. Keep these at the end, as they will screw
282 # with sane line reporting for any other test failures
285 my ($file, $line, $name) = @_;
286 my (undef, $got_file, $got_line) = caller;
287 like ($got_file, $file, "file of $name");
288 is ($got_line, $line, "line of $name");
292 check(qr/parser\.t$/, 3, "bare line");
295 check(qr/parser\.t$/, 5, "bare line with leading space");
298 check(qr/parser\.t$/, 7, "trailing space still valid");
301 check(qr/parser\.t$/, 11, "leading and trailing");
304 check(qr/parser\.t$/, 13, "leading tab");
307 check(qr/parser\.t$/, 17, "middle tab");
310 check(qr/parser\.t$/, 19, "loadsaspaces");
313 check(qr/^KASHPRITZA$/, 23, "bare filename");
316 check(qr/^KAHEEEE$/, 29, "filename in quotes");
318 #line 31 "CLINK CLOINK BZZT"
319 check(qr/^CLINK CLOINK BZZT$/, 31, "filename with spaces in quotes");
321 #line 37 "THOOM THOOM"
322 check(qr/^THOOM THOOM$/, 37, "filename with tabs in quotes");
324 #line 41 "GLINK PLINK GLUNK DINK"
325 check(qr/^GLINK PLINK GLUNK DINK$/, 41, "a space after the quotes");
327 #line 43 "BBFRPRAFPGHPP
328 check(qr/^"BBFRPRAFPGHPP$/, 43, "actually missing a quote is still valid");
331 check(qr/^"BBFRPRAFPGHPP$/, 46, "but spaces aren't allowed without quotes");
334 check(qr/^"BBFRPRAFPGHPP$/, 49, "need a space after the line number");
336 eval <<'EOSTANZA'; die $@ if $@;
337 #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."
338 check(qr/^With.*down\.$/, 51, "Overflow the second small buffer check");
341 # And now, turn on the debugger flag for long names
344 #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."
345 check(qr/^For we.*must die\.$/, 53, "Our long line is set up");
347 eval <<'EOT'; die $@ if $@;
349 check(qr/^ $/, 59, "Overflow the first small buffer check only");
352 eval <<'EOSTANZA'; die $@ if $@;
353 #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."
354 check(qr/^Great hail!.*no more\.$/, 61, "Overflow both small buffer checks");
359 is(eval q{ "$x[0]->strung" }, 'string->strung',
360 'literal -> after an array subscript within ""');
362 # this used to give "string"
363 like("$x[0]-> [0]", qr/^ARRAY\([^)]*\)-> \[0]\z/,
364 'literal -> [0] after an array subscript within ""');
368 # Don't add new tests HERE. See note above