This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
pad.h: Rename PadnameSTATE; make it a proper boolean
[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
5097bf9b 6print "1..144\n";
c9786f60
NC
7
8sub failed {
9 my ($got, $expected, $name) = @_;
10
11 print "not ok $test - $name\n";
12 my @caller = caller(1);
13 print "# Failed test at $caller[1] line $caller[2]\n";
14 if (defined $got) {
15 print "# Got '$got'\n";
16 } else {
17 print "# Got undef\n";
18 }
19 print "# Expected $expected\n";
20 return;
923be969
RGS
21}
22
c9786f60
NC
23sub like {
24 my ($got, $pattern, $name) = @_;
25 $test = $test + 1;
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).
30 return 1;
31 }
32 failed($got, $pattern, $name);
33}
34
35sub is {
36 my ($got, $expect, $name) = @_;
37 $test = $test + 1;
38 if (defined $expect) {
39 if (defined $got && $got eq $expect) {
40 print "ok $test - $name\n";
41 return 1;
42 }
43 failed($got, "'$expect'", $name);
44 } else {
45 if (!defined $got) {
46 print "ok $test - $name\n";
47 return 1;
48 }
49 failed($got, 'undef', $name);
50 }
51}
f15b33d3
RGS
52
53eval '%@x=0;';
54like( $@, qr/^Can't modify hash dereference in repeat \(x\)/, '%@x=0' );
55
56# Bug 20010422.005
57eval q{{s//${}/; //}};
58like( $@, qr/syntax error/, 'syntax error, used to dump core' );
59
60# Bug 20010528.007
61eval q/"\x{"/;
62like( $@, qr/^Missing right brace on \\x/,
63 'syntax error in string, used to dump core' );
64
fd7808cb
RGS
65eval q/"\N{"/;
66like( $@, qr/^Missing right brace on \\N/,
67 'syntax error in string with incomplete \N' );
68eval q/"\Nfoo"/;
69like( $@, qr/^Missing braces on \\N/,
70 'syntax error in string with incomplete \N' );
71
ae9ce25e
KW
72eval q/"\o{"/;
73like( $@, qr/^Missing right brace on \\o/,
74 'syntax error in string with incomplete \o' );
75eval q/"\ofoo"/;
76like( $@, qr/^Missing braces on \\o/,
77 'syntax error in string with incomplete \o' );
78
f15b33d3
RGS
79eval "a.b.c.d.e.f;sub";
80like( $@, qr/^Illegal declaration of anonymous subroutine/,
81 'found by Markov chain stress testing' );
82
83# Bug 20010831.001
84eval '($a, b) = (1, 2);';
85like( $@, qr/^Can't modify constant item in list assignment/,
86 'bareword in list assignment' );
87
88eval 'tie FOO, "Foo";';
89like( $@, qr/^Can't modify constant item in tie /,
90 'tying a bareword causes a segfault in 5.6.1' );
91
92eval 'undef foo';
93like( $@, qr/^Can't modify constant item in undef operator /,
94 'undefing constant causes a segfault in 5.6.1 [ID 20010906.019]' );
95
96eval 'read($bla, FILE, 1);';
97like( $@, qr/^Can't modify constant item in read /,
98 'read($var, FILE, 1) segfaults on 5.6.1 [ID 20011025.054]' );
923be969
RGS
99
100# This used to dump core (bug #17920)
101eval q{ sub { sub { f1(f2();); my($a,$b,$c) } } };
f15b33d3 102like( $@, qr/error/, 'lexical block discarded by yacc' );
961ce445
RGS
103
104# bug #18573, used to corrupt memory
105eval q{ "\c" };
106like( $@, qr/^Missing control char name in \\c/, q("\c" string) );
8edd5f42 107
a40a317b
RGS
108eval q{ qq(foo$) };
109like( $@, qr/Final \$ should be \\\$ or \$name/, q($ at end of "" string) );
110
8edd5f42
RGS
111# two tests for memory corruption problems in the said variables
112# (used to dump core or produce strange results)
113
114is( "\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Qa", "a", "PL_lex_casestack" );
115
116eval {
117{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
118{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
119{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
120}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
121}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
122}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
123};
124is( $@, '', 'PL_lex_brackstack' );
437fd210
AE
125
126{
7df0d042
AE
127 # tests for bug #20716
128 undef $a;
129 undef @b;
130 my $a="A";
131 is("${a}{", "A{", "interpolation, qq//");
132 is("${a}[", "A[", "interpolation, qq//");
133 my @b=("B");
134 is("@{b}{", "B{", "interpolation, qq//");
2a53d331 135 is(qr/${a}\{/, '(?^:A\{)', "interpolation, qr//");
7df0d042 136 my $c = "A{";
2a53d331 137 $c =~ /${a}\{/;
7df0d042 138 is($&, 'A{', "interpolation, m//");
2a53d331 139 $c =~ s/${a}\{/foo/;
7df0d042
AE
140 is($c, 'foo', "interpolation, s/...//");
141 $c =~ s/foo/${a}{/;
142 is($c, 'A{', "interpolation, s//.../");
143 is(<<"${a}{", "A{ A[ B{\n", "interpolation, here doc");
144${a}{ ${a}[ @{b}{
145${a}{
437fd210 146}
4a202259 147
0f5d0394
AE
148eval q{ sub a(;; &) { } a { } };
149is($@, '', "';&' sub prototype confuses the lexer");
150
4a202259
AE
151# Bug #21575
152# ensure that the second print statement works, by playing a bit
153# with the test output.
154my %data = ( foo => "\n" );
155print "#";
156print(
157$data{foo});
c9786f60
NC
158$test = $test + 1;
159print "ok $test\n";
abc667d1
DM
160
161# Bug #21875
162# { q.* => ... } should be interpreted as hash, not block
163
164foreach my $line (split /\n/, <<'EOF')
1651 { foo => 'bar' }
1661 { qoo => 'bar' }
1671 { q => 'bar' }
1681 { qq => 'bar' }
1690 { q,'bar', }
1700 { q=bar= }
1710 { qq=bar= }
1721 { q=bar= => 'bar' }
173EOF
174{
175 my ($expect, $eval) = split / /, $line, 2;
176 my $result = eval $eval;
c9786f60 177 is($@, '', "eval $eval");
abc667d1
DM
178 is(ref $result, $expect ? 'HASH' : '', $eval);
179}
3cf7b4c4
RGS
180
181# Bug #24212
182{
183 local $SIG{__WARN__} = sub { }; # silence mandatory warning
184 eval q{ my $x = -F 1; };
250d67eb 185 like( $@, qr/(?i:syntax|parse) error .* near "F 1"/, "unknown filetest operators" );
3cf7b4c4
RGS
186 is(
187 eval q{ sub F { 42 } -F 1 },
188 '-42',
189 '-F calls the F function'
190 );
191}
1c03aa9b
RGS
192
193# Bug #24762
194{
195 eval q{ *foo{CODE} ? 1 : 0 };
196 is( $@, '', "glob subscript in conditional" );
197}
500bedb6
DM
198
199# Bug #25824
200{
201 eval q{ sub f { @a=@b=@c; {use} } };
202 like( $@, qr/syntax error/, "use without body" );
203}
e1548254 204
8e742a20
MHM
205# [perl #2738] perl segfautls on input
206{
207 eval q{ sub _ <> {} };
208 like($@, qr/Illegal declaration of subroutine main::_/, "readline operator as prototype");
209
210 eval q{ $s = sub <> {} };
211 like($@, qr/Illegal declaration of anonymous subroutine/, "readline operator as prototype");
212
213 eval q{ sub _ __FILE__ {} };
214 like($@, qr/Illegal declaration of subroutine main::_/, "__FILE__ as prototype");
215}
dbfe47cf 216
917949e3
RGS
217# tests for "Bad name"
218eval q{ foo::$bar };
219like( $@, qr/Bad name after foo::/, 'Bad name after foo::' );
220eval q{ foo''bar };
221like( $@, qr/Bad name after foo'/, 'Bad name after foo\'' );
a6a8bb49
RGS
222
223# test for ?: context error
224eval q{($a ? $x : ($y)) = 5};
225like( $@, qr/Assignment to both a list and a scalar/, 'Assignment to both a list and a scalar' );
2d997502
BC
226
227eval q{ s/x/#/e };
228is( $@, '', 'comments in s///e' );
2af555bf 229
718a7425 230# these five used to coredump because the op cleanup on parse error could
2af555bf
DM
231# be to the wrong pad
232
233eval q[
234 sub { our $a= 1;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;
235 sub { my $z
236];
237
238like($@, qr/Missing right curly/, 'nested sub syntax error' );
239
240eval q[
241 sub { my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$r);
242 sub { my $z
243];
244like($@, qr/Missing right curly/, 'nested sub syntax error 2' );
245
718a7425
DM
246eval q[
247 sub { our $a= 1;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;
248 use DieDieDie;
249];
250
251like($@, qr/Can't locate DieDieDie.pm/, 'croak cleanup' );
252
253eval q[
254 sub { my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$r);
255 use DieDieDie;
256];
257
258like($@, qr/Can't locate DieDieDie.pm/, 'croak cleanup 2' );
259
260
261eval q[
262 my @a;
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
265 use DieDieDie;
266];
267
268like($@, qr/Can't modify/, 'croak cleanup 3' );
269
7e5d8ed2
DM
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.
273
274eval q[ BEGIN { } ] for 1..10;
275is($@, "", 'BEGIN 1' );
276
277eval q[ BEGIN { my $x; $x = 1 } ] for 1..10;
278is($@, "", 'BEGIN 2' );
279
280eval q[ BEGIN { \&foo1 } ] for 1..10;
281is($@, "", 'BEGIN 3' );
282
283eval q[ sub foo2 { } ] for 1..10;
284is($@, "", 'BEGIN 4' );
285
286eval q[ sub foo3 { my $x; $x=1 } ] for 1..10;
287is($@, "", 'BEGIN 5' );
288
289eval q[ BEGIN { die } ] for 1..10;
290like($@, qr/BEGIN failed--compilation aborted/, 'BEGIN 6' );
291
292eval q[ BEGIN {\&foo4; die } ] for 1..10;
293like($@, qr/BEGIN failed--compilation aborted/, 'BEGIN 7' );
44867030 294
0b3da58d
TC
295{
296 # RT #70934
297 # check both the specific case in the ticket, and a few other paths into
298 # S_scan_ident()
299 # simplify long ids
300 my $x100 = "x" x 256;
301 my $xFE = "x" x 254;
302 my $xFD = "x" x 253;
303 my $xFC = "x" x 252;
304 my $xFB = "x" x 251;
305
306 eval qq[ \$#$xFB ];
307 is($@, "", "251 character \$# sigil ident ok");
308 eval qq[ \$#$xFC ];
309 like($@, qr/Identifier too long/, "too long id in \$# sigil ctx");
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[ \\&$xFC ]; # take a ref since I don't want to call it
322 is($@, "", "252 character & sigil ident ok");
323 eval qq[ \\&$xFD ];
324 like($@, qr/Identifier too long/, "too long id in & sigil ctx");
325
326 eval qq[ *$xFC ];
327 is($@, "", "252 character glob ident ok");
328 eval qq[ *$xFD ];
329 like($@, qr/Identifier too long/, "too long id in glob ctx");
330
331 eval qq[ for $xFD ];
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");
336
337 # the specific case from the ticket
338 my $x = "x" x 257;
339 eval qq[ for $x ];
340 like($@, qr/Identifier too long/, "too long id ticket case");
341}
342
fd909433 343{
c9e0c59b 344 is(exists &zlonk, '', 'sub not present');
fd909433
VP
345 eval qq[ {sub zlonk} ];
346 is($@, '', 'sub declaration followed by a closing curly');
c9e0c59b
NC
347 is(exists &zlonk, 1, 'sub now stubbed');
348 is(defined &zlonk, '', 'but no body defined');
fd909433
VP
349}
350
ee36fb64
FC
351# [perl #113016] CORE::print::foo
352sub CORE'print'foo { 43 } # apostrophes intentional; do not tempt fate
353sub CORE'foo'bar { 43 }
354is CORE::print::foo, 43, 'CORE::print::foo is not CORE::print ::foo';
355is scalar eval "CORE::foo'bar", 43, "CORE::foo'bar is not an error";
356
f0998909
Z
357# bug #71748
358eval q{
359 $_ = "";
360 s/(.)/
361 {
362 #
363 }->{$1};
364 /e;
365 1;
366};
367is($@, "", "multiline whitespace inside substitute expression");
368
a73ef99b
FC
369eval '@A =~ s/a/b/; # compilation error
370 sub tahi {}
371 sub rua;
372 sub toru ($);
373 sub wha :lvalue;
374 sub rima ($%&*$&*\$%\*&$%*&) :method;
375 sub ono :lvalue { die }
376 sub whitu (_) { die }
377 sub waru ($;) :method { die }
378 sub iwa { die }
379 BEGIN { }';
380is $::{tahi}, undef, 'empty sub decl ignored after compilation error';
381is $::{rua}, undef, 'stub decl ignored after compilation error';
382is $::{toru}, undef, 'stub+proto decl ignored after compilation error';
383is $::{wha}, undef, 'stub+attr decl ignored after compilation error';
384is $::{rima}, undef, 'stub+proto+attr ignored after compilation error';
385is $::{ono}, undef, 'sub decl with attr ignored after compilation error';
386is $::{whitu}, undef, 'sub decl w proto ignored after compilation error';
387is $::{waru}, undef, 'sub w attr+proto ignored after compilation error';
388is $::{iwa}, undef, 'non-empty sub decl ignored after compilation error';
389is *BEGIN{CODE}, undef, 'BEGIN leaves no stub after compilation error';
390
583c9d5c
FC
391$test = $test + 1;
392"ok $test - format inside re-eval" =~ /(?{
393 format =
394@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
395$_
396.
397write
398}).*/;
399
4a7239ff
FC
400eval '
401"${;
402
403=pod
404
405=cut
406
407}";
408';
409is $@, "", 'pod inside string in string eval';
410"${;
411
412=pod
413
414=cut
415
416}";
417print "ok ", ++$test, " - pod inside string outside of string eval\n";
418
5097bf9b
FC
419like "blah blah blah\n", qr/${\ <<END
420blah blah blah
421END
422 }/, 'here docs in multiline quoted construct';
423like "blah blah blah\n", eval q|qr/${\ <<END
424blah blah blah
425END
426 }/|, 'here docs in multiline quoted construct in string eval';
427
428# Unterminated here-docs in subst in eval; used to crash
429eval 's/${<<END}//';
430eval 's//${<<END}/';
431print "ok ", ++$test, " - unterminated here-docs in s/// in string eval\n";
432
0484b403
FC
433sub 'Hello'_he_said (_);
434is prototype "Hello::_he_said", '_', 'initial tick in sub declaration';
435
2e4e39b1
FC
436# Add new tests HERE (above this line)
437
d7425188
FC
438# bug #74022: Loop on characters in \p{OtherIDContinue}
439# This test hangs if it fails.
440eval chr 0x387;
441is(1,1, '[perl #74022] Parser looping on OtherIDContinue chars');
442
44867030
NC
443# More awkward tests for #line. Keep these at the end, as they will screw
444# with sane line reporting for any other test failures
445
446sub check ($$$) {
447 my ($file, $line, $name) = @_;
448 my (undef, $got_file, $got_line) = caller;
449 like ($got_file, $file, "file of $name");
44867030
NC
450 is ($got_line, $line, "line of $name");
451}
452
c9de86d5 453my $this_file = qr/parser\.t(?:\.[bl]eb?)?$/;
44867030 454#line 3
c9de86d5 455check($this_file, 3, "bare line");
44867030
NC
456
457# line 5
c9de86d5 458check($this_file, 5, "bare line with leading space");
44867030
NC
459
460#line 7
c9de86d5 461check($this_file, 7, "trailing space still valid");
44867030
NC
462
463# line 11
c9de86d5 464check($this_file, 11, "leading and trailing");
44867030
NC
465
466# line 13
c9de86d5 467check($this_file, 13, "leading tab");
44867030
NC
468
469#line 17
c9de86d5 470check($this_file, 17, "middle tab");
44867030
NC
471
472#line 19
c9de86d5 473check($this_file, 19, "loadsaspaces");
44867030
NC
474
475#line 23 KASHPRITZA
476check(qr/^KASHPRITZA$/, 23, "bare filename");
477
478#line 29 "KAHEEEE"
479check(qr/^KAHEEEE$/, 29, "filename in quotes");
480
481#line 31 "CLINK CLOINK BZZT"
482check(qr/^CLINK CLOINK BZZT$/, 31, "filename with spaces in quotes");
483
484#line 37 "THOOM THOOM"
485check(qr/^THOOM THOOM$/, 37, "filename with tabs in quotes");
486
487#line 41 "GLINK PLINK GLUNK DINK"
488check(qr/^GLINK PLINK GLUNK DINK$/, 41, "a space after the quotes");
489
490#line 43 "BBFRPRAFPGHPP
491check(qr/^"BBFRPRAFPGHPP$/, 43, "actually missing a quote is still valid");
492
493#line 47 bang eth
494check(qr/^"BBFRPRAFPGHPP$/, 46, "but spaces aren't allowed without quotes");
495
26b6dc3f
RGS
496#line 77sevenseven
497check(qr/^"BBFRPRAFPGHPP$/, 49, "need a space after the line number");
498
44867030
NC
499eval <<'EOSTANZA'; die $@ if $@;
500#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."
501check(qr/^With.*down\.$/, 51, "Overflow the second small buffer check");
502EOSTANZA
503
504# And now, turn on the debugger flag for long names
505$^P = 0x100;
506
507#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."
508check(qr/^For we.*must die\.$/, 53, "Our long line is set up");
509
510eval <<'EOT'; die $@ if $@;
511#line 59 " "
512check(qr/^ $/, 59, "Overflow the first small buffer check only");
513EOT
514
515eval <<'EOSTANZA'; die $@ if $@;
516#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."
517check(qr/^Great hail!.*no more\.$/, 61, "Overflow both small buffer checks");
518EOSTANZA
519
02255c60
FC
520{
521 my @x = 'string';
522 is(eval q{ "$x[0]->strung" }, 'string->strung',
523 'literal -> after an array subscript within ""');
524 @x = ['string'];
525 # this used to give "string"
526 like("$x[0]-> [0]", qr/^ARRAY\([^)]*\)-> \[0]\z/,
527 'literal -> [0] after an array subscript within ""');
528}
529
44867030
NC
530__END__
531# Don't add new tests HERE. See note above