This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regcomp.c: Use mnemonic instead of number
[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  'my $m = "\\\"; $m =~ $m', => 'Trailing \ in regex m/\/',
108
109  '/\x{1/' => 'Missing right brace on \x{} {#} m/\x{1{#}/',
110  '/\x{X/' => 'Missing right brace on \x{} {#} m/\x{{#}X/',
111
112  '/[\x{X]/' => 'Missing right brace on \x{} {#} m/[\x{{#}X]/',
113  '/[\x{A]/' => 'Missing right brace on \x{} {#} m/[\x{A{#}]/',
114
115  '/\o{1/' => 'Missing right brace on \o{ {#} m/\o{1{#}/',
116  '/\o{X/' => 'Missing right brace on \o{ {#} m/\o{{#}X/',
117
118  '/[\o{X]/' => 'Missing right brace on \o{ {#} m/[\o{{#}X]/',
119  '/[\o{7]/' => 'Missing right brace on \o{ {#} m/[\o{7{#}]/',
120
121  '/[[:barf:]]/' => 'POSIX class [:barf:] unknown {#} m/[[:barf:]{#}]/',
122
123  '/[[=barf=]]/' => 'POSIX syntax [= =] is reserved for future extensions {#} m/[[=barf=]{#}]/',
124
125  '/[[.barf.]]/' => 'POSIX syntax [. .] is reserved for future extensions {#} m/[[.barf.]{#}]/',
126
127  '/[z-a]/' => 'Invalid [] range "z-a" {#} m/[z-a{#}]/',
128
129  '/\p/' => 'Empty \p{} {#} m/\p{#}/',
130
131  '/\P{}/' => 'Empty \P{} {#} m/\P{{#}}/',
132  '/(?[[[:word]]])/' => "Unmatched ':' in POSIX class {#} m/(?[[[:word{#}]]])/",
133  '/(?[[:word]])/' => "Unmatched ':' in POSIX class {#} m/(?[[:word{#}]])/",
134  '/(?[[[:digit: ])/' => "Unmatched '[' in POSIX class {#} m/(?[[[:digit:{#} ])/",
135  '/(?[[:digit: ])/' => "Unmatched '[' in POSIX class {#} m/(?[[:digit:{#} ])/",
136  '/(?[[[::]]])/' => "POSIX class [::] unknown {#} m/(?[[[::]{#}]])/",
137  '/(?[[[:w:]]])/' => "POSIX class [:w:] unknown {#} m/(?[[[:w:]{#}]])/",
138  '/(?[[:w:]])/' => "POSIX class [:w:] unknown {#} m/(?[[:w:]{#}])/",
139  '/(?[a])/' =>  'Unexpected character {#} m/(?[a{#}])/',
140  '/(?[\t])/l' => '(?[...]) not valid in locale {#} m/(?[{#}\t])/',
141  '/(?[ + \t ])/' => 'Unexpected binary operator \'+\' with no preceding operand {#} m/(?[ +{#} \t ])/',
142  '/(?[ \cK - ( + \t ) ])/' => 'Unexpected binary operator \'+\' with no preceding operand {#} m/(?[ \cK - ( +{#} \t ) ])/',
143  '/(?[ \cK ( \t ) ])/' => 'Unexpected \'(\' with no preceding operator {#} m/(?[ \cK ({#} \t ) ])/',
144  '/(?[ \cK \t ])/' => 'Operand with no preceding operator {#} m/(?[ \cK \t{#} ])/',
145  '/(?[ \0004 ])/' => 'Need exactly 3 octal digits {#} m/(?[ \0004 {#}])/',
146  '/(?[ \05 ])/' => 'Need exactly 3 octal digits {#} m/(?[ \05 {#}])/',
147  '/(?[ \o{1038} ])/' => 'Non-octal character {#} m/(?[ \o{1038{#}} ])/',
148  '/(?[ \o{} ])/' => 'Number with no digits {#} m/(?[ \o{}{#} ])/',
149  '/(?[ \x{defg} ])/' => 'Non-hex character {#} m/(?[ \x{defg{#}} ])/',
150  '/(?[ \xabcdef ])/' => 'Use \\x{...} for more than two hex characters {#} m/(?[ \xabc{#}def ])/',
151  '/(?[ \x{} ])/' => 'Number with no digits {#} m/(?[ \x{}{#} ])/',
152  '/(?[ \cK + ) ])/' => 'Unexpected \')\' {#} m/(?[ \cK + ){#} ])/',
153  '/(?[ \cK + ])/' => 'Incomplete expression within \'(?[ ])\' {#} m/(?[ \cK + {#}])/',
154  '/(?[ \p{foo} ])/' => 'Property \'foo\' is unknown {#} m/(?[ \p{foo}{#} ])/',
155  '/(?[ \p{ foo = bar } ])/' => 'Property \'foo = bar\' is unknown {#} m/(?[ \p{ foo = bar }{#} ])/',
156  '/(?[ \8 ])/' => 'Unrecognized escape \8 in character class {#} m/(?[ \8{#} ])/',
157  '/(?[ \t ]/' => 'Syntax error in (?[...]) in regex m/(?[ \t ]/',
158  '/(?[ [ \t ]/' => 'Syntax error in (?[...]) in regex m/(?[ [ \t ]/',
159  '/(?[ \t ] ]/' => 'Syntax error in (?[...]) in regex m/(?[ \t ] ]/',
160  '/(?[ [ ] ]/' => 'Syntax error in (?[...]) in regex m/(?[ [ ] ]/',
161  '/(?[ \t + \e # This was supposed to be a comment ])/' => 'Syntax error in (?[...]) in regex m/(?[ \t + \e # This was supposed to be a comment ])/',
162  '/(?[ ])/' => 'Incomplete expression within \'(?[ ])\' {#} m/(?[ {#}])/',
163  'm/(?[[a-\d]])/' => 'False [] range "a-\d" {#} m/(?[[a-\d{#}]])/',
164  'm/(?[[\w-x]])/' => 'False [] range "\w-" {#} m/(?[[\w-{#}x]])/',
165  'm/(?[[a-\pM]])/' => 'False [] range "a-\pM" {#} m/(?[[a-\pM{#}]])/',
166  'm/(?[[\pM-x]])/' => 'False [] range "\pM-" {#} m/(?[[\pM-{#}x]])/',
167  'm/(?[[\N{LATIN CAPITAL LETTER A WITH MACRON AND GRAVE}]])/' => '\N{} in character class restricted to one character {#} m/(?[[\N{U+100.300{#}}]])/',
168  'm/(?[ \p{Digit} & (?(?[ \p{Thai} | \p{Lao} ]))])/' => 'Sequence (?(...) not recognized {#} m/(?[ \p{Digit} & (?({#}?[ \p{Thai} | \p{Lao} ]))])/',
169  'm/(?[ \p{Digit} & (?:(?[ \p{Thai} | \p{Lao} ]))])/' => 'Expecting \'(?flags:(?[...\' {#} m/(?[ \p{Digit} & (?{#}:(?[ \p{Thai} | \p{Lao} ]))])/',
170  'm/\o{/' => 'Missing right brace on \o{ {#} m/\o{{#}/',
171  'm/\o/' => 'Missing braces on \o{} {#} m/\o{#}/',
172  'm/\o{}/' => 'Number with no digits {#} m/\o{}{#}/',
173  'm/[\o{]/' => 'Missing right brace on \o{ {#} m/[\o{{#}]/',
174  'm/[\o]/' => 'Missing braces on \o{} {#} m/[\o{#}]/',
175  'm/[\o{}]/' => 'Number with no digits {#} m/[\o{}{#}]/',
176  'm/(?^-i:foo)/' => 'Sequence (?^-...) not recognized {#} m/(?^-{#}i:foo)/',
177 );
178 # Tests involving a user-defined charnames translator are in pat_advanced.t
179
180 # In the following arrays of warnings, the value can be an array of things to
181 # expect.  If the empty string, it means no warning should be raised.
182
183 ##
184 ## Key-value pairs of code/error of code that should have non-fatal regexp warnings.
185 ##
186 my @warning = (
187     'm/\b*/' => '\b* matches null string many times {#} m/\b*{#}/',
188
189     'm/[:blank:]/' => 'POSIX syntax [: :] belongs inside character classes {#} m/[:blank:]{#}/',
190
191     "m'[\\y]'"     => 'Unrecognized escape \y in character class passed through {#} m/[\y{#}]/',
192
193     'm/[a-\d]/' => 'False [] range "a-\d" {#} m/[a-\d{#}]/',
194     'm/[\w-x]/' => 'False [] range "\w-" {#} m/[\w-{#}x]/',
195     'm/[a-\pM]/' => 'False [] range "a-\pM" {#} m/[a-\pM{#}]/',
196     'm/[\pM-x]/' => 'False [] range "\pM-" {#} m/[\pM-{#}x]/',
197     "m'\\y'"     => 'Unrecognized escape \y passed through {#} m/\y{#}/',
198     '/x{3,1}/'   => 'Quantifier {n,m} with n > m can\'t match {#} m/x{3,1}{#}/',
199     '/\08/' => '\'\08\' resolved to \'\o{0}8\' {#} m/\08{#}/',
200     '/\018/' => '\'\018\' resolved to \'\o{1}8\' {#} m/\018{#}/',
201     '/[\08]/' => '\'\08\' resolved to \'\o{0}8\' {#} m/[\08{#}]/',
202     '/[\018]/' => '\'\018\' resolved to \'\o{1}8\' {#} m/[\018{#}]/',
203     '/\87/' => 'Unrecognized escape \8 passed through {#} m/\8{#}7/',
204     '/a\87/' => 'Unrecognized escape \8 passed through {#} m/a\8{#}7/',
205     '/a\97/' => 'Unrecognized escape \9 passed through {#} m/a\9{#}7/',
206     '/(?=a)*/' => '(?=a)* matches null string many times {#} m/(?=a)*{#}/',
207     'my $x = \'\m\'; qr/a$x/' => 'Unrecognized escape \m passed through {#} m/a\m{#}/',
208     '/\q/' => 'Unrecognized escape \q passed through {#} m/\q{#}/',
209     '/\q{/' => 'Unrecognized escape \q{ passed through {#} m/\q{{#}/',
210     '/(?=a){1,3}/' => 'Quantifier unexpected on zero-length expression {#} m/(?=a){1,3}{#}/',
211     '/\_/' => "",
212     '/[\_\0]/' => "",
213     '/[\07]/' => "",
214     '/[\006]/' => "",
215     '/[\0005]/' => "",
216     '/[\8\9]/' => ['Unrecognized escape \8 in character class passed through {#} m/[\8{#}\9]/',
217                    'Unrecognized escape \9 in character class passed through {#} m/[\8\9{#}]/',
218                   ],
219     '/[:alpha:]/' => 'POSIX syntax [: :] belongs inside character classes {#} m/[:alpha:]{#}/',
220     '/[:zog:]/' => 'POSIX syntax [: :] belongs inside character classes {#} m/[:zog:]{#}/',
221     '/[.zog.]/' => 'POSIX syntax [. .] belongs inside character classes {#} m/[.zog.]{#}/',
222     '/[a-b]/' => "",
223     '/[a-\d]/' => 'False [] range "a-\d" {#} m/[a-\d{#}]/',
224     '/[\d-b]/' => 'False [] range "\d-" {#} m/[\d-{#}b]/',
225     '/[\s-\d]/' => 'False [] range "\s-" {#} m/[\s-{#}\d]/',
226     '/[\d-\s]/' => 'False [] range "\d-" {#} m/[\d-{#}\s]/',
227     '/[a-[:digit:]]/' => 'False [] range "a-[:digit:]" {#} m/[a-[:digit:]{#}]/',
228     '/[[:digit:]-b]/' => 'False [] range "[:digit:]-" {#} m/[[:digit:]-{#}b]/',
229     '/[[:alpha:]-[:digit:]]/' => 'False [] range "[:alpha:]-" {#} m/[[:alpha:]-{#}[:digit:]]/',
230     '/[[:digit:]-[:alpha:]]/' => 'False [] range "[:digit:]-" {#} m/[[:digit:]-{#}[:alpha:]]/',
231     '/[a\zb]/' => 'Unrecognized escape \z in character class passed through {#} m/[a\z{#}b]/',
232     '/(?c)/' => 'Useless (?c) - use /gc modifier {#} m/(?c{#})/',
233     '/(?-c)/' => 'Useless (?-c) - don\'t use /gc modifier {#} m/(?-c{#})/',
234     '/(?g)/' => 'Useless (?g) - use /g modifier {#} m/(?g{#})/',
235     '/(?-g)/' => 'Useless (?-g) - don\'t use /g modifier {#} m/(?-g{#})/',
236     '/(?o)/' => 'Useless (?o) - use /o modifier {#} m/(?o{#})/',
237     '/(?-o)/' => 'Useless (?-o) - don\'t use /o modifier {#} m/(?-o{#})/',
238     '/(?g-o)/' => [ 'Useless (?g) - use /g modifier {#} m/(?g{#}-o)/',
239                     'Useless (?-o) - don\'t use /o modifier {#} m/(?g-o{#})/',
240                   ],
241     '/(?g-c)/' => [ 'Useless (?g) - use /g modifier {#} m/(?g{#}-c)/',
242                     'Useless (?-c) - don\'t use /gc modifier {#} m/(?g-c{#})/',
243                   ],
244       # (?c) means (?g) error won't be thrown
245      '/(?o-cg)/' => [ 'Useless (?o) - use /o modifier {#} m/(?o{#}-cg)/',
246                       'Useless (?-c) - don\'t use /gc modifier {#} m/(?o-c{#}g)/',
247                     ],
248     '/(?ogc)/' => [ 'Useless (?o) - use /o modifier {#} m/(?o{#}gc)/',
249                     'Useless (?g) - use /g modifier {#} m/(?og{#}c)/',
250                     'Useless (?c) - use /gc modifier {#} m/(?ogc{#})/',
251                   ],
252 );
253
254 my @experimental_regex_sets = (
255     '/(?[ \t ])/' => 'The regex_sets feature is experimental {#} m/(?[{#} \t ])/',
256 );
257
258 my @deprecated = (
259     '/a\b{cde/' => '"\b{" is deprecated; use "\b\{" or "\b[{]" instead {#} m/a\{#}b{cde/',
260     '/a\B{cde/' => '"\B{" is deprecated; use "\B\{" or "\B[{]" instead {#} m/a\{#}B{cde/',
261     'use utf8; /(?x)\\85\85\\85/' => 'Escape literal pattern white space under /x {#} m/(?x)\\85\85{#}\\85/',
262     '/((?# 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)/',
263     '/((?# 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)/',
264 );
265
266 while (my ($regex, $expect) = splice @death, 0, 2) {
267     my $expect = fixup_expect($expect);
268     no warnings 'experimental::regex_sets';
269     # skip the utf8 test on EBCDIC since they do not die
270     next if $::IS_EBCDIC && $regex =~ /utf8/;
271
272     warning_is(sub {
273                    $_ = "x";
274                    eval $regex;
275                    like($@, qr/\Q$expect/, $regex);
276                }, undef, "... and died without any other warnings");
277 }
278
279 foreach my $ref (\@warning, \@experimental_regex_sets, \@deprecated) {
280     my $warning_type = ($ref == \@warning)
281                        ? 'regexp'
282                        : ($ref == \@deprecated)
283                          ? 'regexp, deprecated'
284                          : 'experimental::regex_sets';
285     while (my ($regex, $expect) = splice @$ref, 0, 2) {
286         my @expect = fixup_expect($expect);
287         {
288             $_ = "x";
289             no warnings;
290             eval $regex;
291         }
292         if (is($@, "", "$regex did not die")) {
293             my @got = capture_warnings(sub {
294                                     $_ = "x";
295                                     eval $regex });
296             my $count = @expect;
297             if (! is(scalar @got, scalar @expect, "... and gave expected number ($count) of warnings")) {
298                 if (@got < @expect) {
299                     $count = @got;
300                     note "Expected warnings not gotten:\n\t" . join "\n\t", @expect[$count .. $#expect];
301                 }
302                 else {
303                     note "Unexpected warnings gotten:\n\t" . join("\n\t", @got[$count .. $#got]);
304                 }
305             }
306             foreach my $i (0 .. $count - 1) {
307                 if (like($got[$i], qr/\Q$expect[$i]/, "... and gave expected warning[$i]")) {
308                     ok (0 == capture_warnings(sub {
309                                     $_ = "x";
310                                     eval "no warnings '$warning_type'; $regex;" }
311                                 ),
312                     "... and turning off '$warning_type' warnings suppressed it");
313                 }
314             }
315         }
316     }
317 }
318
319 done_testing();