This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
make regen_perly following 5ca68648d92617d8.
[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
24d0fc42 6print "1..104\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
f15b33d3
RGS
72eval "a.b.c.d.e.f;sub";
73like( $@, qr/^Illegal declaration of anonymous subroutine/,
74 'found by Markov chain stress testing' );
75
76# Bug 20010831.001
77eval '($a, b) = (1, 2);';
78like( $@, qr/^Can't modify constant item in list assignment/,
79 'bareword in list assignment' );
80
81eval 'tie FOO, "Foo";';
82like( $@, qr/^Can't modify constant item in tie /,
83 'tying a bareword causes a segfault in 5.6.1' );
84
85eval 'undef foo';
86like( $@, qr/^Can't modify constant item in undef operator /,
87 'undefing constant causes a segfault in 5.6.1 [ID 20010906.019]' );
88
89eval 'read($bla, FILE, 1);';
90like( $@, qr/^Can't modify constant item in read /,
91 'read($var, FILE, 1) segfaults on 5.6.1 [ID 20011025.054]' );
923be969
RGS
92
93# This used to dump core (bug #17920)
94eval q{ sub { sub { f1(f2();); my($a,$b,$c) } } };
f15b33d3 95like( $@, qr/error/, 'lexical block discarded by yacc' );
961ce445
RGS
96
97# bug #18573, used to corrupt memory
98eval q{ "\c" };
99like( $@, qr/^Missing control char name in \\c/, q("\c" string) );
8edd5f42 100
a40a317b
RGS
101eval q{ qq(foo$) };
102like( $@, qr/Final \$ should be \\\$ or \$name/, q($ at end of "" string) );
103
8edd5f42
RGS
104# two tests for memory corruption problems in the said variables
105# (used to dump core or produce strange results)
106
107is( "\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Qa", "a", "PL_lex_casestack" );
108
109eval {
110{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
111{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
112{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
113}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
114}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
115}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
116};
117is( $@, '', 'PL_lex_brackstack' );
437fd210
AE
118
119{
7df0d042
AE
120 # tests for bug #20716
121 undef $a;
122 undef @b;
123 my $a="A";
124 is("${a}{", "A{", "interpolation, qq//");
125 is("${a}[", "A[", "interpolation, qq//");
126 my @b=("B");
127 is("@{b}{", "B{", "interpolation, qq//");
128 is(qr/${a}{/, '(?-xism:A{)', "interpolation, qr//");
129 my $c = "A{";
130 $c =~ /${a}{/;
131 is($&, 'A{', "interpolation, m//");
132 $c =~ s/${a}{/foo/;
133 is($c, 'foo', "interpolation, s/...//");
134 $c =~ s/foo/${a}{/;
135 is($c, 'A{', "interpolation, s//.../");
136 is(<<"${a}{", "A{ A[ B{\n", "interpolation, here doc");
137${a}{ ${a}[ @{b}{
138${a}{
437fd210 139}
4a202259 140
0f5d0394
AE
141eval q{ sub a(;; &) { } a { } };
142is($@, '', "';&' sub prototype confuses the lexer");
143
4a202259
AE
144# Bug #21575
145# ensure that the second print statement works, by playing a bit
146# with the test output.
147my %data = ( foo => "\n" );
148print "#";
149print(
150$data{foo});
c9786f60
NC
151$test = $test + 1;
152print "ok $test\n";
abc667d1
DM
153
154# Bug #21875
155# { q.* => ... } should be interpreted as hash, not block
156
157foreach my $line (split /\n/, <<'EOF')
1581 { foo => 'bar' }
1591 { qoo => 'bar' }
1601 { q => 'bar' }
1611 { qq => 'bar' }
1620 { q,'bar', }
1630 { q=bar= }
1640 { qq=bar= }
1651 { q=bar= => 'bar' }
166EOF
167{
168 my ($expect, $eval) = split / /, $line, 2;
169 my $result = eval $eval;
c9786f60 170 is($@, '', "eval $eval");
abc667d1
DM
171 is(ref $result, $expect ? 'HASH' : '', $eval);
172}
3cf7b4c4
RGS
173
174# Bug #24212
175{
176 local $SIG{__WARN__} = sub { }; # silence mandatory warning
177 eval q{ my $x = -F 1; };
250d67eb 178 like( $@, qr/(?i:syntax|parse) error .* near "F 1"/, "unknown filetest operators" );
3cf7b4c4
RGS
179 is(
180 eval q{ sub F { 42 } -F 1 },
181 '-42',
182 '-F calls the F function'
183 );
184}
1c03aa9b
RGS
185
186# Bug #24762
187{
188 eval q{ *foo{CODE} ? 1 : 0 };
189 is( $@, '', "glob subscript in conditional" );
190}
500bedb6
DM
191
192# Bug #25824
193{
194 eval q{ sub f { @a=@b=@c; {use} } };
195 like( $@, qr/syntax error/, "use without body" );
196}
e1548254 197
8e742a20
MHM
198# [perl #2738] perl segfautls on input
199{
200 eval q{ sub _ <> {} };
201 like($@, qr/Illegal declaration of subroutine main::_/, "readline operator as prototype");
202
203 eval q{ $s = sub <> {} };
204 like($@, qr/Illegal declaration of anonymous subroutine/, "readline operator as prototype");
205
206 eval q{ sub _ __FILE__ {} };
207 like($@, qr/Illegal declaration of subroutine main::_/, "__FILE__ as prototype");
208}
dbfe47cf 209
917949e3
RGS
210# tests for "Bad name"
211eval q{ foo::$bar };
212like( $@, qr/Bad name after foo::/, 'Bad name after foo::' );
213eval q{ foo''bar };
214like( $@, qr/Bad name after foo'/, 'Bad name after foo\'' );
a6a8bb49
RGS
215
216# test for ?: context error
217eval q{($a ? $x : ($y)) = 5};
218like( $@, qr/Assignment to both a list and a scalar/, 'Assignment to both a list and a scalar' );
2d997502
BC
219
220eval q{ s/x/#/e };
221is( $@, '', 'comments in s///e' );
2af555bf 222
718a7425 223# these five used to coredump because the op cleanup on parse error could
2af555bf
DM
224# be to the wrong pad
225
226eval q[
227 sub { our $a= 1;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;
228 sub { my $z
229];
230
231like($@, qr/Missing right curly/, 'nested sub syntax error' );
232
233eval q[
234 sub { my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$r);
235 sub { my $z
236];
237like($@, qr/Missing right curly/, 'nested sub syntax error 2' );
238
718a7425
DM
239eval q[
240 sub { our $a= 1;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;
241 use DieDieDie;
242];
243
244like($@, qr/Can't locate DieDieDie.pm/, 'croak cleanup' );
245
246eval q[
247 sub { my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$r);
248 use DieDieDie;
249];
250
251like($@, qr/Can't locate DieDieDie.pm/, 'croak cleanup 2' );
252
253
254eval q[
255 my @a;
256 my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$r);
257 @a =~ s/a/b/; # compile-time error
258 use DieDieDie;
259];
260
261like($@, qr/Can't modify/, 'croak cleanup 3' );
262
7e5d8ed2
DM
263# these might leak, or have duplicate frees, depending on the bugginess of
264# the parser stack 'fail in reduce' cleanup code. They're here mainly as
265# something to be run under valgrind, with PERL_DESTRUCT_LEVEL=1.
266
267eval q[ BEGIN { } ] for 1..10;
268is($@, "", 'BEGIN 1' );
269
270eval q[ BEGIN { my $x; $x = 1 } ] for 1..10;
271is($@, "", 'BEGIN 2' );
272
273eval q[ BEGIN { \&foo1 } ] for 1..10;
274is($@, "", 'BEGIN 3' );
275
276eval q[ sub foo2 { } ] for 1..10;
277is($@, "", 'BEGIN 4' );
278
279eval q[ sub foo3 { my $x; $x=1 } ] for 1..10;
280is($@, "", 'BEGIN 5' );
281
282eval q[ BEGIN { die } ] for 1..10;
283like($@, qr/BEGIN failed--compilation aborted/, 'BEGIN 6' );
284
285eval q[ BEGIN {\&foo4; die } ] for 1..10;
286like($@, qr/BEGIN failed--compilation aborted/, 'BEGIN 7' );
44867030
NC
287
288# Add new tests HERE:
289
290# More awkward tests for #line. Keep these at the end, as they will screw
291# with sane line reporting for any other test failures
292
293sub check ($$$) {
294 my ($file, $line, $name) = @_;
295 my (undef, $got_file, $got_line) = caller;
296 like ($got_file, $file, "file of $name");
44867030
NC
297 is ($got_line, $line, "line of $name");
298}
299
c9de86d5 300my $this_file = qr/parser\.t(?:\.[bl]eb?)?$/;
44867030 301#line 3
c9de86d5 302check($this_file, 3, "bare line");
44867030
NC
303
304# line 5
c9de86d5 305check($this_file, 5, "bare line with leading space");
44867030
NC
306
307#line 7
c9de86d5 308check($this_file, 7, "trailing space still valid");
44867030
NC
309
310# line 11
c9de86d5 311check($this_file, 11, "leading and trailing");
44867030
NC
312
313# line 13
c9de86d5 314check($this_file, 13, "leading tab");
44867030
NC
315
316#line 17
c9de86d5 317check($this_file, 17, "middle tab");
44867030
NC
318
319#line 19
c9de86d5 320check($this_file, 19, "loadsaspaces");
44867030
NC
321
322#line 23 KASHPRITZA
323check(qr/^KASHPRITZA$/, 23, "bare filename");
324
325#line 29 "KAHEEEE"
326check(qr/^KAHEEEE$/, 29, "filename in quotes");
327
328#line 31 "CLINK CLOINK BZZT"
329check(qr/^CLINK CLOINK BZZT$/, 31, "filename with spaces in quotes");
330
331#line 37 "THOOM THOOM"
332check(qr/^THOOM THOOM$/, 37, "filename with tabs in quotes");
333
334#line 41 "GLINK PLINK GLUNK DINK"
335check(qr/^GLINK PLINK GLUNK DINK$/, 41, "a space after the quotes");
336
337#line 43 "BBFRPRAFPGHPP
338check(qr/^"BBFRPRAFPGHPP$/, 43, "actually missing a quote is still valid");
339
340#line 47 bang eth
341check(qr/^"BBFRPRAFPGHPP$/, 46, "but spaces aren't allowed without quotes");
342
26b6dc3f
RGS
343#line 77sevenseven
344check(qr/^"BBFRPRAFPGHPP$/, 49, "need a space after the line number");
345
44867030
NC
346eval <<'EOSTANZA'; die $@ if $@;
347#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."
348check(qr/^With.*down\.$/, 51, "Overflow the second small buffer check");
349EOSTANZA
350
351# And now, turn on the debugger flag for long names
352$^P = 0x100;
353
354#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."
355check(qr/^For we.*must die\.$/, 53, "Our long line is set up");
356
357eval <<'EOT'; die $@ if $@;
358#line 59 " "
359check(qr/^ $/, 59, "Overflow the first small buffer check only");
360EOT
361
362eval <<'EOSTANZA'; die $@ if $@;
363#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."
364check(qr/^Great hail!.*no more\.$/, 61, "Overflow both small buffer checks");
365EOSTANZA
366
02255c60
FC
367{
368 my @x = 'string';
369 is(eval q{ "$x[0]->strung" }, 'string->strung',
370 'literal -> after an array subscript within ""');
371 @x = ['string'];
372 # this used to give "string"
373 like("$x[0]-> [0]", qr/^ARRAY\([^)]*\)-> \[0]\z/,
374 'literal -> [0] after an array subscript within ""');
375}
376
44867030
NC
377__END__
378# Don't add new tests HERE. See note above