Skip to content

Commit ee50e95

Browse files
committed
Convert panic() calls to more specific, overrideable methods.
1 parent 435e62c commit ee50e95

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

src/QRegex/P6Regex/Grammar.nqp

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,30 @@ grammar QRegex::P6Regex::Grammar is HLL::Grammar {
5151
self.panic('Unrecognized regex metacharacter ' ~ $char ~ ' (must be quoted to match literally)');
5252
}
5353

54+
method throw_malformed_range() {
55+
self.panic('Malformed range.');
56+
}
57+
58+
method throw_confused() {
59+
self.panic('Confused.');
60+
}
61+
62+
method throw_unspace($char) {
63+
self.panic: "No unspace allowed in regex; " ~
64+
" if you meant to match the literal character," ~
65+
" please enclose in single quotes ('"
66+
~ $char ~ "') or use a backslashed form like \\x"
67+
~ nqp::sprintf('%02x', [nqp::ord($char)]);
68+
}
69+
70+
method throw_regex_not_terminated() {
71+
self.panic('Regex not terminated.');
72+
}
73+
74+
method throw_spaces_in_bare_range() {
75+
self.panic('Spaces not allowed in bare range.');
76+
}
77+
5478
method throw_null_pattern() {
5579
self.panic('Null regex not allowed');
5680
}
@@ -81,7 +105,7 @@ grammar QRegex::P6Regex::Grammar is HLL::Grammar {
81105
:my $handle := '__QREGEX_P6REGEX__' ~ $cur_handle++;
82106
:my $*W := QRegex::P6Regex::World.new(:$handle);
83107
<nibbler>
84-
[ $ || <.panic: 'Confused'> ]
108+
[ $ || <.throw_confused> ]
85109
}
86110

87111
token nibbler {
@@ -104,9 +128,9 @@ grammar QRegex::P6Regex::Grammar is HLL::Grammar {
104128
<termseq>
105129
[
106130
|| <?infixstopper>
107-
|| $$ <.panic: "Regex not terminated">
131+
|| $$ <.throw_regex_not_terminated>
108132
|| (\W) { self.throw_unrecognized_metachar: ~$/[0] }
109-
|| <.panic: "Regex not terminated">
133+
|| <.throw_regex_not_terminated>
110134
]
111135
}
112136

@@ -154,9 +178,9 @@ grammar QRegex::P6Regex::Grammar is HLL::Grammar {
154178
|| <noun=.quantified_atom>+
155179
|| <?before <rxstopper> | <[&|~]> > <.throw_null_pattern>
156180
|| <?before <infixstopper> > <.throw_null_pattern> # XXX Check if unmatched bracket
157-
|| $$ <.panic: "Regex not terminated">
181+
|| $$ <.throw_regex_not_terminated>
158182
|| (\W) { self.throw_unrecognized_metachar: ~$/[0] }
159-
|| <.panic: "Regex not terminated">
183+
|| <.throw_regex_not_terminated>
160184
]
161185
}
162186

@@ -213,15 +237,15 @@ grammar QRegex::P6Regex::Grammar is HLL::Grammar {
213237
token quantifier:sym<**> {
214238
<sym> <.normspace>? <backmod> <.normspace>?
215239
[
216-
| <.integer> \s+ '..' <.panic: "Spaces not allowed in bare range">
240+
| <.integer> \s+ '..' <.throw_spaces_in_bare_range>
217241
| <min=.integer>
218242
[ '..'
219243
[
220244
| <max=.integer> {
221245
$/.CURSOR.panic("Negative numbers are not allowed as quantifiers") if $<max>.Str < 0;
222246
}
223247
| $<max>=['*']
224-
| <.panic: "Malformed range">
248+
| <.throw_malformed_range>
225249
]
226250
]?
227251
{ $/.CURSOR.panic("Negative numbers are not allowed as quantifiers") if $<min>.Str < 0 }
@@ -308,8 +332,7 @@ grammar QRegex::P6Regex::Grammar is HLL::Grammar {
308332
token backslash:sym<unrec> { {} (\w) { self.throw_unrecog_backslash_seq: $/[0].Str } }
309333
token backslash:sym<unsp> {
310334
[\s|'#'] {}
311-
<.panic: "No unspace allowed in regex; if you meant to match the literal character, please enclose in single quotes ('"
312-
~ $/ ~ "') or use a backslashed form like \\x" ~ nqp::sprintf('%02x', [nqp::ord(~$/)])>
335+
<.throw_unspace(~$/)>
313336
}
314337
token backslash:sym<misc> { \W }
315338

0 commit comments

Comments
 (0)