1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
13 _LIBCPP_BEGIN_NAMESPACE_STD
15 static const char* make_error_type_string(regex_constants::error_type ecode
) {
17 case regex_constants::error_collate
:
18 return "The expression contained an invalid collating element name.";
19 case regex_constants::error_ctype
:
20 return "The expression contained an invalid character class name.";
21 case regex_constants::error_escape
:
22 return "The expression contained an invalid escaped character, or a "
24 case regex_constants::error_backref
:
25 return "The expression contained an invalid back reference.";
26 case regex_constants::error_brack
:
27 return "The expression contained mismatched [ and ].";
28 case regex_constants::error_paren
:
29 return "The expression contained mismatched ( and ).";
30 case regex_constants::error_brace
:
31 return "The expression contained mismatched { and }.";
32 case regex_constants::error_badbrace
:
33 return "The expression contained an invalid range in a {} expression.";
34 case regex_constants::error_range
:
35 return "The expression contained an invalid character range, "
36 "such as [b-a] in most encodings.";
37 case regex_constants::error_space
:
38 return "There was insufficient memory to convert the expression into "
39 "a finite state machine.";
40 case regex_constants::error_badrepeat
:
41 return "One of *?+{ was not preceded by a valid regular expression.";
42 case regex_constants::error_complexity
:
43 return "The complexity of an attempted match against a regular "
44 "expression exceeded a pre-set level.";
45 case regex_constants::error_stack
:
46 return "There was insufficient memory to determine whether the regular "
47 "expression could match the specified character sequence.";
48 case regex_constants::__re_err_grammar
:
49 return "An invalid regex grammar has been requested.";
50 case regex_constants::__re_err_empty
:
51 return "An empty regex is not allowed in the POSIX grammar.";
52 case regex_constants::__re_err_parse
:
53 return "The parser did not consume the entire regular expression.";
57 return "Unknown error type";
60 regex_error::regex_error(regex_constants::error_type ecode
)
61 : runtime_error(make_error_type_string(ecode
)), __code_(ecode
) {}
63 regex_error::~regex_error() throw() {}
67 struct collationnames
{
72 #if defined(__MVS__) && !defined(__NATIVE_ASCII_F)
74 // Sorted via the EBCDIC collating sequence
75 const collationnames collatenames
[] = {
85 {"carriage-return", 0xd},
87 {"circumflex-accent", 0x5f},
90 {"commercial-at", 0x7c},
92 {"dollar-sign", 0x5b},
95 {"equals-sign", 0x7e},
96 {"exclamation-mark", 0x5a},
103 {"grave-accent", 0x79},
104 {"greater-than-sign", 0x6e},
107 {"hyphen-minus", 0x60},
112 {"left-brace", 0xc0},
113 {"left-curly-bracket", 0xc0},
114 {"left-parenthesis", 0x4d},
115 {"left-square-bracket", 0xad},
116 {"less-than-sign", 0x4c},
122 {"number-sign", 0x7b},
126 {"percent-sign", 0x6c},
130 {"question-mark", 0x6f},
131 {"quotation-mark", 0x7f},
133 {"reverse-solidus", 0xe0},
134 {"right-brace", 0xd0},
135 {"right-curly-bracket", 0xd0},
136 {"right-parenthesis", 0x5d},
137 {"right-square-bracket", 0xbd},
151 {"underscore", 0x6d},
153 {"vertical-line", 0x4f},
154 {"vertical-tab", 0xb},
189 const collationnames collatenames
[] = {
220 {"apostrophe", 0x27},
226 {"carriage-return", 0x0d},
227 {"circumflex", 0x5e},
228 {"circumflex-accent", 0x5e},
231 {"commercial-at", 0x40},
233 {"dollar-sign", 0x24},
236 {"equals-sign", 0x3d},
237 {"exclamation-mark", 0x21},
244 {"grave-accent", 0x60},
245 {"greater-than-sign", 0x3e},
248 {"hyphen-minus", 0x2d},
253 {"left-brace", 0x7b},
254 {"left-curly-bracket", 0x7b},
255 {"left-parenthesis", 0x28},
256 {"left-square-bracket", 0x5b},
257 {"less-than-sign", 0x3c},
263 {"number-sign", 0x23},
267 {"percent-sign", 0x25},
271 {"question-mark", 0x3f},
272 {"quotation-mark", 0x22},
274 {"reverse-solidus", 0x5c},
275 {"right-brace", 0x7d},
276 {"right-curly-bracket", 0x7d},
277 {"right-parenthesis", 0x29},
278 {"right-square-bracket", 0x5d},
292 {"underscore", 0x5f},
294 {"vertical-line", 0x7c},
295 {"vertical-tab", 0x0b},
305 regex_traits
<char>::char_class_type mask_
;
308 const classnames ClassNames
[] = {
309 {"alnum", ctype_base::alnum
},
310 {"alpha", ctype_base::alpha
},
311 {"blank", ctype_base::blank
},
312 {"cntrl", ctype_base::cntrl
},
313 {"d", ctype_base::digit
},
314 {"digit", ctype_base::digit
},
315 {"graph", ctype_base::graph
},
316 {"lower", ctype_base::lower
},
317 {"print", ctype_base::print
},
318 {"punct", ctype_base::punct
},
319 {"s", ctype_base::space
},
320 {"space", ctype_base::space
},
321 {"upper", ctype_base::upper
},
322 {"w", regex_traits
<char>::__regex_word
},
323 {"xdigit", ctype_base::xdigit
}};
326 bool operator()(const collationnames
& x
, const char* y
) const { return strcmp(x
.elem_
, y
) < 0; }
327 bool operator()(const classnames
& x
, const char* y
) const { return strcmp(x
.elem_
, y
) < 0; }
332 string
__get_collation_name(const char* s
) {
333 const collationnames
* i
= std::lower_bound(begin(collatenames
), end(collatenames
), s
, use_strcmp());
335 if (i
!= end(collatenames
) && strcmp(s
, i
->elem_
) == 0)
340 regex_traits
<char>::char_class_type
__get_classname(const char* s
, bool __icase
) {
341 const classnames
* i
= std::lower_bound(begin(ClassNames
), end(ClassNames
), s
, use_strcmp());
342 regex_traits
<char>::char_class_type r
= 0;
343 if (i
!= end(ClassNames
) && strcmp(s
, i
->elem_
) == 0) {
345 if (r
== regex_traits
<char>::__regex_word
)
346 r
|= ctype_base::alnum
| ctype_base::upper
| ctype_base::lower
;
348 if (r
& (ctype_base::lower
| ctype_base::upper
))
349 r
|= ctype_base::alpha
;
356 void __match_any_but_newline
<char>::__exec(__state
& __s
) const {
357 if (__s
.__current_
!= __s
.__last_
) {
358 switch (*__s
.__current_
) {
361 __s
.__do_
= __state::__reject
;
362 __s
.__node_
= nullptr;
365 __s
.__do_
= __state::__accept_and_consume
;
367 __s
.__node_
= this->first();
371 __s
.__do_
= __state::__reject
;
372 __s
.__node_
= nullptr;
377 void __match_any_but_newline
<wchar_t>::__exec(__state
& __s
) const {
378 if (__s
.__current_
!= __s
.__last_
) {
379 switch (*__s
.__current_
) {
384 __s
.__do_
= __state::__reject
;
385 __s
.__node_
= nullptr;
388 __s
.__do_
= __state::__accept_and_consume
;
390 __s
.__node_
= this->first();
394 __s
.__do_
= __state::__reject
;
395 __s
.__node_
= nullptr;
399 _LIBCPP_END_NAMESPACE_STD