This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add missing test for regex parse error
[perl5.git] / t / re / reg_mesg.t
1 #!./perl -w
2
3 BEGIN {
4         chdir 't' if -d 't';
5         @INC = '../lib';
6         require './test.pl';
7         eval 'require Config'; # assume defaults if this fails
8 }
9
10 use strict;
11
12 ##
13 ## If the markers used are changed (search for "MARKER1" in regcomp.c),
14 ## update only these two regexs, and leave the {#} in the @death/@warning
15 ## arrays below. The {#} is a meta-marker -- it marks where the marker should
16 ## go.
17 ##
18 ## Returns empty string if that is what is expected.  Otherwise, handles
19 ## either a scalar, turning it into a single element array; or a ref to an
20 ## array, adjusting each element.  If called in array context, returns an
21 ## array, otherwise the join of all elements
22
23 sub fixup_expect {
24     my $expect_ref = shift;
25     return if $expect_ref eq "";
26
27     my @expect;
28     if (ref $expect_ref) {
29         @expect = @$expect_ref;
30     }
31     else {
32         @expect = $expect_ref;
33     }
34
35     foreach my $element (@expect) {
36         $element =~ s/{\#}/in regex; marked by <-- HERE in/;
37         $element =~ s/{\#}/ <-- HERE /;
38         $element .= " at ";
39     }
40     return wantarray ? @expect : join "", @expect;
41 }
42
43 my $inf_m1 = ($Config::Config{reg_infty} || 32767) - 1;
44 my $inf_p1 = $inf_m1 + 2;
45
46 ##
47 ## Key-value pairs of code/error of code that should have fatal errors.
48 ##
49 my @death =
50 (
51  '/[[=foo=]]/' => 'POSIX syntax [= =] is reserved for future extensions {#} m/[[=foo=]{#}]/',
52
53  '/(?<= .*)/' =>  'Variable length lookbehind not implemented in regex m/(?<= .*)/',
54
55  '/(?<= x{1000})/' => 'Lookbehind longer than 255 not implemented in regex m/(?<= x{1000})/',
56
57  '/(?@)/' => 'Sequence (?@...) not implemented {#} m/(?@{#})/',
58
59  '/(?{ 1/' => 'Missing right curly or square bracket',
60
61  '/(?(1x))/' => 'Switch condition not recognized {#} m/(?(1x{#}))/',
62
63  '/(?(1)x|y|z)/' => 'Switch (?(condition)... contains too many branches {#} m/(?(1)x|y|{#}z)/',
64
65  '/(?(x)y|x)/' => 'Unknown switch condition (?(x) {#} m/(?({#}x)y|x)/',
66
67  '/(?/' => 'Sequence (? incomplete {#} m/(?{#}/',
68
69  '/(?;x/' => 'Sequence (?;...) not recognized {#} m/(?;{#}x/',
70  '/(?<;x/' => 'Group name must start with a non-digit word character {#} m/(?<;{#}x/',
71  '/(?\ix/' => 'Sequence (?\...) not recognized {#} m/(?\{#}ix/',
72  '/(?\mx/' => 'Sequence (?\...) not recognized {#} m/(?\{#}mx/',
73  '/(?\:x/' => 'Sequence (?\...) not recognized {#} m/(?\{#}:x/',
74  '/(?\=x/' => 'Sequence (?\...) not recognized {#} m/(?\{#}=x/',
75  '/(?\!x/' => 'Sequence (?\...) not recognized {#} m/(?\{#}!x/',
76  '/(?\<=x/' => 'Sequence (?\...) not recognized {#} m/(?\{#}<=x/',
77  '/(?\<!x/' => 'Sequence (?\...) not recognized {#} m/(?\{#}<!x/',
78  '/(?\>x/' => 'Sequence (?\...) not recognized {#} m/(?\{#}>x/',
79  '/(?^-i:foo)/' => 'Sequence (?^-...) not recognized {#} m/(?^-{#}i:foo)/',
80  '/(?^-i)foo/' => 'Sequence (?^-...) not recognized {#} m/(?^-{#}i)foo/',
81  '/(?^d:foo)/' => 'Sequence (?^d...) not recognized {#} m/(?^d{#}:foo)/',
82  '/(?^d)foo/' => 'Sequence (?^d...) not recognized {#} m/(?^d{#})foo/',
83  '/(?^lu:foo)/' => 'Regexp modifiers "l" and "u" are mutually exclusive {#} m/(?^lu{#}:foo)/',
84  '/(?^lu)foo/' => 'Regexp modifiers "l" and "u" are mutually exclusive {#} m/(?^lu{#})foo/',
85 '/(?da:foo)/' => 'Regexp modifiers "d" and "a" are mutually exclusive {#} m/(?da{#}:foo)/',
86 '/(?lil:foo)/' => 'Regexp modifier "l" may not appear twice {#} m/(?lil{#}:foo)/',
87 '/(?aaia:foo)/' => 'Regexp modifier "a" may appear a maximum of twice {#} m/(?aaia{#}:foo)/',
88 '/(?i-l:foo)/' => 'Regexp modifier "l" may not appear after the "-" {#} m/(?i-l{#}:foo)/',
89
90  '/((x)/' => 'Unmatched ( {#} m/({#}(x)/',
91
92  "/x{$inf_p1}/" => "Quantifier in {,} bigger than $inf_m1 {#} m/x{{#}$inf_p1}/",
93
94
95  '/x**/' => 'Nested quantifiers {#} m/x**{#}/',
96
97  '/x[/' => 'Unmatched [ {#} m/x[{#}/',
98
99  '/*/', => 'Quantifier follows nothing {#} m/*{#}/',
100
101  '/\p{x/' => 'Missing right brace on \p{} {#} m/\p{{#}x/',
102
103  '/[\p{x]/' => 'Missing right brace on \p{} {#} m/[\p{{#}x]/',
104
105  '/(x)\2/' => 'Reference to nonexistent group {#} m/(x)\2{#}/',
106
107  '/\g{1/' => 'Unterminated \g{...} pattern {#} m/\g{1{#}/',
108
109  'my $m = "\\\"; $m =~ $m', => 'Trailing \ in regex m/\/',
110
111  '/\x{1/' => 'Missing right brace on \x{} {#} m/\x{1{#}/',
112  '/\x{X/' => 'Missing right brace on \x{} {#} m/\x{{#}X/',
113
114  '/[\x{X]/' => 'Missing right brace on \x{} {#} m/[\x{{#}X]/',
115  '/[\x{A]/' => 'Missing right brace on \x{} {#} m/[\x{A{#}]/',
116
117  '/\o{1/' => 'Missing right brace on \o{ {#} m/\o{1{#}/',
118  '/\o{X/' => 'Missing right brace on \o{ {#} m/\o{{#}X/',
119
120  '/[\o{X]/' => 'Missing right brace on \o{ {#} m/[\o{{#}X]/',
121  '/[\o{7]/' => 'Missing right brace on \o{ {#} m/[\o{7{#}]/',
122
123  '/[[:barf:]]/' => 'POSIX class [:barf:] unknown {#} m/[[:barf:]{#}]/',
124
125  '/[[=barf=]]/' => 'POSIX syntax [= =] is reserved for future extensions {#} m/[[=barf=]{#}]/',
126
127  '/[[.barf.]]/' => 'POSIX syntax [. .] is reserved for future extensions {#} m/[[.barf.]{#}]/',
128
129  '/[z-a]/' => 'Invalid [] range "z-a" {#} m/[z-a{#}]/',
130
131  '/\p/' => 'Empty \p{} {#} m/\p{#}/',
132
133  '/\P{}/' => 'Empty \P{} {#} m/\P{{#}}/',
134  '/(?[[[:word]]])/' => "Unmatched ':' in POSIX class {#} m/(?[[[:word{#}]]])/",
135  '/(?[[:word]])/' => "Unmatched ':' in POSIX class {#} m/(?[[:word{#}]])/",
136  '/(?[[[:digit: ])/' => "Unmatched '[' in POSIX class {#} m/(?[[[:digit:{#} ])/",
137  '/(?[[:digit: ])/' => "Unmatched '[' in POSIX class {#} m/(?[[:digit:{#} ])/",
138  '/(?[[[::]]])/' => "POSIX class [::] unknown {#} m/(?[[[::]{#}]])/",
139  '/(?[[[:w:]]])/' => "POSIX class [:w:] unknown {#} m/(?[[[:w:]{#}]])/",
140  '/(?[[:w:]])/' => "POSIX class [:w:] unknown {#} m/(?[[:w:]{#}])/",
141  '/(?[a])/' =>  'Unexpected character {#} m/(?[a{#}])/',
142  '/(?[\t])/l' => '(?[...]) not valid in locale {#} m/(?[{#}\t])/',
143  '/(?[ + \t ])/' => 'Unexpected binary operator \'+\' with no preceding operand {#} m/(?[ +{#} \t ])/',
144  '/(?[ \cK - ( + \t ) ])/' => 'Unexpected binary operator \'+\' with no preceding operand {#} m/(?[ \cK - ( +{#} \t ) ])/',
145  '/(?[ \cK ( \t ) ])/' => 'Unexpected \'(\' with no preceding operator {#} m/(?[ \cK ({#} \t ) ])/',
146  '/(?[ \cK \t ])/' => 'Operand with no preceding operator {#} m/(?[ \cK \t{#} ])/',
147  '/(?[ \0004 ])/' => 'Need exactly 3 octal digits {#} m/(?[ \0004 {#}])/',
148  '/(?[ \05 ])/' => 'Need exactly 3 octal digits {#} m/(?[ \05 {#}])/',
149  '/(?[ \o{1038} ])/' => 'Non-octal character {#} m/(?[ \o{1038{#}} ])/',
150  '/(?[ \o{} ])/' => 'Number with no digits {#} m/(?[ \o{}{#} ])/',
151  '/(?[ \x{defg} ])/' => 'Non-hex character {#} m/(?[ \x{defg{#}} ])/',
152  '/(?[ \xabcdef ])/' => 'Use \\x{...} for more than two hex characters {#} m/(?[ \xabc{#}def ])/',
153  '/(?[ \x{} ])/' => 'Number with no digits {#} m/(?[ \x{}{#} ])/',
154  '/(?[ \cK + ) ])/' => 'Unexpected \')\' {#} m/(?[ \cK + ){#} ])/',
155  '/(?[ \cK + ])/' => 'Incomplete expression within \'(?[ ])\' {#} m/(?[ \cK + {#}])/',
156  '/(?[ \p{foo} ])/' => 'Property \'foo\' is unknown {#} m/(?[ \p{foo}{#} ])/',
157  '/(?[ \p{ foo = bar } ])/' => 'Property \'foo = bar\' is unknown {#} m/(?[ \p{ foo = bar }{#} ])/',
158  '/(?[ \8 ])/' => 'Unrecognized escape \8 in character class {#} m/(?[ \8{#} ])/',
159  '/(?[ \t ]/' => 'Syntax error in (?[...]) in regex m/(?[ \t ]/',
160  '/(?[ [ \t ]/' => 'Syntax error in (?[...]) in regex m/(?[ [ \t ]/',
161  '/(?[ \t ] ]/' => 'Syntax error in (?[...]) in regex m/(?[ \t ] ]/',
162  '/(?[ [ ] ]/' => 'Syntax error in (?[...]) in regex m/(?[ [ ] ]/',
163  '/(?[ \t + \e # This was supposed to be a comment ])/' => 'Syntax error in (?[...]) in regex m/(?[ \t + \e # This was supposed to be a comment ])/',
164  '/(?[ ])/' => 'Incomplete expression within \'(?[ ])\' {#} m/(?[ {#}])/',
165  'm/(?[[a-\d]])/' => 'False [] range "a-\d" {#} m/(?[[a-\d{#}]])/',
166  'm/(?[[\w-x]])/' => 'False [] range "\w-" {#} m/(?[[\w-{#}x]])/',
167  'm/(?[[a-\pM]])/' => 'False [] range "a-\pM" {#} m/(?[[a-\pM{#}]])/',
168  'm/(?[[\pM-x]])/' => 'False [] range "\pM-" {#} m/(?[[\pM-{#}x]])/',
169  'm/(?[[\N{LATIN CAPITAL LETTER A WITH MACRON AND GRAVE}]])/' => '\N{} in character class restricted to one character {#} m/(?[[\N{U+100.300{#}}]])/',
170  'm/(?[ \p{Digit} & (?(?[ \p{Thai} | \p{Lao} ]))])/' => 'Sequence (?(...) not recognized {#} m/(?[ \p{Digit} & (?({#}?[ \p{Thai} | \p{Lao} ]))])/',
171  'm/(?[ \p{Digit} & (?:(?[ \p{Thai} | \p{Lao} ]))])/' => 'Expecting \'(?flags:(?[...\' {#} m/(?[ \p{Digit} & (?{#}:(?[ \p{Thai} | \p{Lao} ]))])/',
172  'm/\o{/' => 'Missing right brace on \o{ {#} m/\o{{#}/',
173  'm/\o/' => 'Missing braces on \o{} {#} m/\o{#}/',
174  'm/\o{}/' => 'Number with no digits {#} m/\o{}{#}/',
175  'm/[\o{]/' => 'Missing right brace on \o{ {#} m/[\o{{#}]/',
176  'm/[\o]/' => 'Missing braces on \o{} {#} m/[\o{#}]/',
177  'm/[\o{}]/' => 'Number with no digits {#} m/[\o{}{#}]/',
178  'm/(?^-i:foo)/' => 'Sequence (?^-...) not recognized {#} m/(?^-{#}i:foo)/',
179 );
180 # Tests involving a user-defined charnames translator are in pat_advanced.t
181
182 # In the following arrays of warnings, the value can be an array of things to
183 # expect.  If the empty string, it means no warning should be raised.
184
185 ##
186 ## Key-value pairs of code/error of code that should have non-fatal regexp warnings.
187 ##
188 my @warning = (
189     'm/\b*/' => '\b* matches null string many times {#} m/\b*{#}/',
190
191     'm/[:blank:]/' => 'POSIX syntax [: :] belongs inside character classes {#} m/[:blank:]{#}/',
192
193     "m'[\\y]'"     => 'Unrecognized escape \y in character class passed through {#} m/[\y{#}]/',
194
195     'm/[a-\d]/' => 'False [] range "a-\d" {#} m/[a-\d{#}]/',
196     'm/[\w-x]/' => 'False [] range "\w-" {#} m/[\w-{#}x]/',
197     'm/[a-\pM]/' => 'False [] range "a-\pM" {#} m/[a-\pM{#}]/',
198     'm/[\pM-x]/' => 'False [] range "\pM-" {#} m/[\pM-{#}x]/',
199     "m'\\y'"     => 'Unrecognized escape \y passed through {#} m/\y{#}/',
200     '/x{3,1}/'   => 'Quantifier {n,m} with n > m can\'t match {#} m/x{3,1}{#}/',
201     '/\08/' => '\'\08\' resolved to \'\o{0}8\' {#} m/\08{#}/',
202     '/\018/' => '\'\018\' resolved to \'\o{1}8\' {#} m/\018{#}/',
203     '/[\08]/' => '\'\08\' resolved to \'\o{0}8\' {#} m/[\08{#}]/',
204     '/[\018]/' => '\'\018\' resolved to \'\o{1}8\' {#} m/[\018{#}]/',
205     '/\87/' => 'Unrecognized escape \8 passed through {#} m/\8{#}7/',
206     '/a\87/' => 'Unrecognized escape \8 passed through {#} m/a\8{#}7/',
207     '/a\97/' => 'Unrecognized escape \9 passed through {#} m/a\9{#}7/',
208     '/(?=a)*/' => '(?=a)* matches null string many times {#} m/(?=a)*{#}/',
209     'my $x = \'\m\'; qr/a$x/' => 'Unrecognized escape \m passed through {#} m/a\m{#}/',
210     '/\q/' => 'Unrecognized escape \q passed through {#} m/\q{#}/',
211     '/\q{/' => 'Unrecognized escape \q{ passed through {#} m/\q{{#}/',
212     '/(?=a){1,3}/' => 'Quantifier unexpected on zero-length expression {#} m/(?=a){1,3}{#}/',
213     '/\_/' => "",
214     '/[\_\0]/' => "",
215     '/[\07]/' => "",
216     '/[\006]/' => "",
217     '/[\0005]/' => "",
218     '/[\8\9]/' => ['Unrecognized escape \8 in character class passed through {#} m/[\8{#}\9]/',
219                    'Unrecognized escape \9 in character class passed through {#} m/[\8\9{#}]/',
220                   ],
221     '/[:alpha:]/' => 'POSIX syntax [: :] belongs inside character classes {#} m/[:alpha:]{#}/',
222     '/[:zog:]/' => 'POSIX syntax [: :] belongs inside character classes {#} m/[:zog:]{#}/',
223     '/[.zog.]/' => 'POSIX syntax [. .] belongs inside character classes {#} m/[.zog.]{#}/',
224     '/[a-b]/' => "",
225     '/[a-\d]/' => 'False [] range "a-\d" {#} m/[a-\d{#}]/',
226     '/[\d-b]/' => 'False [] range "\d-" {#} m/[\d-{#}b]/',
227     '/[\s-\d]/' => 'False [] range "\s-" {#} m/[\s-{#}\d]/',
228     '/[\d-\s]/' => 'False [] range "\d-" {#} m/[\d-{#}\s]/',
229     '/[a-[:digit:]]/' => 'False [] range "a-[:digit:]" {#} m/[a-[:digit:]{#}]/',
230     '/[[:digit:]-b]/' => 'False [] range "[:digit:]-" {#} m/[[:digit:]-{#}b]/',
231     '/[[:alpha:]-[:digit:]]/' => 'False [] range "[:alpha:]-" {#} m/[[:alpha:]-{#}[:digit:]]/',
232     '/[[:digit:]-[:alpha:]]/' => 'False [] range "[:digit:]-" {#} m/[[:digit:]-{#}[:alpha:]]/',
233     '/[a\zb]/' => 'Unrecognized escape \z in character class passed through {#} m/[a\z{#}b]/',
234     '/(?c)/' => 'Useless (?c) - use /gc modifier {#} m/(?c{#})/',
235     '/(?-c)/' => 'Useless (?-c) - don\'t use /gc modifier {#} m/(?-c{#})/',
236     '/(?g)/' => 'Useless (?g) - use /g modifier {#} m/(?g{#})/',
237     '/(?-g)/' => 'Useless (?-g) - don\'t use /g modifier {#} m/(?-g{#})/',
238     '/(?o)/' => 'Useless (?o) - use /o modifier {#} m/(?o{#})/',
239     '/(?-o)/' => 'Useless (?-o) - don\'t use /o modifier {#} m/(?-o{#})/',
240     '/(?g-o)/' => [ 'Useless (?g) - use /g modifier {#} m/(?g{#}-o)/',
241                     'Useless (?-o) - don\'t use /o modifier {#} m/(?g-o{#})/',
242                   ],
243     '/(?g-c)/' => [ 'Useless (?g) - use /g modifier {#} m/(?g{#}-c)/',
244                     'Useless (?-c) - don\'t use /gc modifier {#} m/(?g-c{#})/',
245                   ],
246       # (?c) means (?g) error won't be thrown
247      '/(?o-cg)/' => [ 'Useless (?o) - use /o modifier {#} m/(?o{#}-cg)/',
248                       'Useless (?-c) - don\'t use /gc modifier {#} m/(?o-c{#}g)/',
249                     ],
250     '/(?ogc)/' => [ 'Useless (?o) - use /o modifier {#} m/(?o{#}gc)/',
251                     'Useless (?g) - use /g modifier {#} m/(?og{#}c)/',
252                     'Useless (?c) - use /gc modifier {#} m/(?ogc{#})/',
253                   ],
254 );
255
256 my @experimental_regex_sets = (
257     '/(?[ \t ])/' => 'The regex_sets feature is experimental {#} m/(?[{#} \t ])/',
258 );
259
260 my @deprecated = (
261     '/a\b{cde/' => '"\b{" is deprecated; use "\b\{" or "\b[{]" instead {#} m/a\{#}b{cde/',
262     '/a\B{cde/' => '"\B{" is deprecated; use "\B\{" or "\B[{]" instead {#} m/a\{#}B{cde/',
263     'use utf8; /(?x)\\85\85\\85/' => 'Escape literal pattern white space under /x {#} m/(?x)\\85\85{#}\\85/',
264     '/((?# This is a comment in the middle of a token)?:foo)/' => 'In \'(?...)\', splitting the initial \'(?\' is deprecated {#} m/((?# This is a comment in the middle of a token)?{#}:foo)/',
265     '/((?# This is a comment in the middle of a token)*FAIL)/' => 'In \'(*VERB...)\', splitting the initial \'(*\' is deprecated {#} m/((?# This is a comment in the middle of a token)*{#}FAIL)/',
266 );
267
268 while (my ($regex, $expect) = splice @death, 0, 2) {
269     my $expect = fixup_expect($expect);
270     no warnings 'experimental::regex_sets';
271     # skip the utf8 test on EBCDIC since they do not die
272     next if $::IS_EBCDIC && $regex =~ /utf8/;
273
274     warning_is(sub {
275                    $_ = "x";
276                    eval $regex;
277                    like($@, qr/\Q$expect/, $regex);
278                }, undef, "... and died without any other warnings");
279 }
280
281 foreach my $ref (\@warning, \@experimental_regex_sets, \@deprecated) {
282     my $warning_type = ($ref == \@warning)
283                        ? 'regexp'
284                        : ($ref == \@deprecated)
285                          ? 'regexp, deprecated'
286                          : 'experimental::regex_sets';
287     while (my ($regex, $expect) = splice @$ref, 0, 2) {
288         my @expect = fixup_expect($expect);
289         {
290             $_ = "x";
291             no warnings;
292             eval $regex;
293         }
294         if (is($@, "", "$regex did not die")) {
295             my @got = capture_warnings(sub {
296                                     $_ = "x";
297                                     eval $regex });
298             my $count = @expect;
299             if (! is(scalar @got, scalar @expect, "... and gave expected number ($count) of warnings")) {
300                 if (@got < @expect) {
301                     $count = @got;
302                     note "Expected warnings not gotten:\n\t" . join "\n\t", @expect[$count .. $#expect];
303                 }
304                 else {
305                     note "Unexpected warnings gotten:\n\t" . join("\n\t", @got[$count .. $#got]);
306                 }
307             }
308             foreach my $i (0 .. $count - 1) {
309                 if (like($got[$i], qr/\Q$expect[$i]/, "... and gave expected warning[$i]")) {
310                     ok (0 == capture_warnings(sub {
311                                     $_ = "x";
312                                     eval "no warnings '$warning_type'; $regex;" }
313                                 ),
314                     "... and turning off '$warning_type' warnings suppressed it");
315                 }
316             }
317         }
318     }
319 }
320
321 done_testing();