7 eval 'require Config'; # assume defaults if this fails
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
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
24 my $expect_ref = shift;
25 return if $expect_ref eq "";
28 if (ref $expect_ref) {
29 @expect = @$expect_ref;
32 @expect = $expect_ref;
35 foreach my $element (@expect) {
36 $element =~ s/{\#}/in regex; marked by <-- HERE in/;
37 $element =~ s/{\#}/ <-- HERE /;
40 return wantarray ? @expect : join "", @expect;
43 my $inf_m1 = ($Config::Config{reg_infty} || 32767) - 1;
44 my $inf_p1 = $inf_m1 + 2;
47 ## Key-value pairs of code/error of code that should have fatal errors.
51 '/[[=foo=]]/' => 'POSIX syntax [= =] is reserved for future extensions {#} m/[[=foo=]{#}]/',
53 '/(?<= .*)/' => 'Variable length lookbehind not implemented in regex m/(?<= .*)/',
55 '/(?<= x{1000})/' => 'Lookbehind longer than 255 not implemented in regex m/(?<= x{1000})/',
57 '/(?@)/' => 'Sequence (?@...) not implemented {#} m/(?@{#})/',
59 '/(?{ 1/' => 'Missing right curly or square bracket',
61 '/(?(1x))/' => 'Switch condition not recognized {#} m/(?(1x{#}))/',
63 '/(?(1)x|y|z)/' => 'Switch (?(condition)... contains too many branches {#} m/(?(1)x|y|{#}z)/',
65 '/(?(x)y|x)/' => 'Unknown switch condition (?(x) {#} m/(?({#}x)y|x)/',
67 '/(?/' => 'Sequence (? incomplete {#} m/(?{#}/',
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)/',
90 '/((x)/' => 'Unmatched ( {#} m/({#}(x)/',
92 "/x{$inf_p1}/" => "Quantifier in {,} bigger than $inf_m1 {#} m/x{{#}$inf_p1}/",
95 '/x**/' => 'Nested quantifiers {#} m/x**{#}/',
97 '/x[/' => 'Unmatched [ {#} m/x[{#}/',
99 '/*/', => 'Quantifier follows nothing {#} m/*{#}/',
101 '/\p{x/' => 'Missing right brace on \p{} {#} m/\p{{#}x/',
103 '/[\p{x]/' => 'Missing right brace on \p{} {#} m/[\p{{#}x]/',
105 '/(x)\2/' => 'Reference to nonexistent group {#} m/(x)\2{#}/',
107 '/\g{1/' => 'Unterminated \g{...} pattern {#} m/\g{1{#}/',
109 'my $m = "\\\"; $m =~ $m', => 'Trailing \ in regex m/\/',
111 '/\x{1/' => 'Missing right brace on \x{} {#} m/\x{1{#}/',
112 '/\x{X/' => 'Missing right brace on \x{} {#} m/\x{{#}X/',
114 '/[\x{X]/' => 'Missing right brace on \x{} {#} m/[\x{{#}X]/',
115 '/[\x{A]/' => 'Missing right brace on \x{} {#} m/[\x{A{#}]/',
117 '/\o{1/' => 'Missing right brace on \o{ {#} m/\o{1{#}/',
118 '/\o{X/' => 'Missing right brace on \o{ {#} m/\o{{#}X/',
120 '/[\o{X]/' => 'Missing right brace on \o{ {#} m/[\o{{#}X]/',
121 '/[\o{7]/' => 'Missing right brace on \o{ {#} m/[\o{7{#}]/',
123 '/[[:barf:]]/' => 'POSIX class [:barf:] unknown {#} m/[[:barf:]{#}]/',
125 '/[[=barf=]]/' => 'POSIX syntax [= =] is reserved for future extensions {#} m/[[=barf=]{#}]/',
127 '/[[.barf.]]/' => 'POSIX syntax [. .] is reserved for future extensions {#} m/[[.barf.]{#}]/',
129 '/[z-a]/' => 'Invalid [] range "z-a" {#} m/[z-a{#}]/',
131 '/\p/' => 'Empty \p{} {#} m/\p{#}/',
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)/',
180 # Tests involving a user-defined charnames translator are in pat_advanced.t
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.
186 ## Key-value pairs of code/error of code that should have non-fatal regexp warnings.
189 'm/\b*/' => '\b* matches null string many times {#} m/\b*{#}/',
191 'm/[:blank:]/' => 'POSIX syntax [: :] belongs inside character classes {#} m/[:blank:]{#}/',
193 "m'[\\y]'" => 'Unrecognized escape \y in character class passed through {#} m/[\y{#}]/',
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}{#}/',
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{#}]/',
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.]{#}/',
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{#})/',
243 '/(?g-c)/' => [ 'Useless (?g) - use /g modifier {#} m/(?g{#}-c)/',
244 'Useless (?-c) - don\'t use /gc modifier {#} m/(?g-c{#})/',
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)/',
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{#})/',
256 my @experimental_regex_sets = (
257 '/(?[ \t ])/' => 'The regex_sets feature is experimental {#} m/(?[{#} \t ])/',
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)/',
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/;
277 like($@, qr/\Q$expect/, $regex);
278 }, undef, "... and died without any other warnings");
281 foreach my $ref (\@warning, \@experimental_regex_sets, \@deprecated) {
282 my $warning_type = ($ref == \@warning)
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);
294 if (is($@, "", "$regex did not die")) {
295 my @got = capture_warnings(sub {
299 if (! is(scalar @got, scalar @expect, "... and gave expected number ($count) of warnings")) {
300 if (@got < @expect) {
302 note "Expected warnings not gotten:\n\t" . join "\n\t", @expect[$count .. $#expect];
305 note "Unexpected warnings gotten:\n\t" . join("\n\t", @got[$count .. $#got]);
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 {
312 eval "no warnings '$warning_type'; $regex;" }
314 "... and turning off '$warning_type' warnings suppressed it");