58namespace std _GLIBCXX_VISIBILITY(default)
60_GLIBCXX_BEGIN_NAMESPACE_VERSION
64 template<
typename _TraitsT>
66 _Compiler(
const _CharT* __b,
const _CharT* __e,
67 const typename _TraitsT::locale_type& __loc, _FlagT __flags)
68 : _M_flags(_S_validate(__flags)),
69 _M_scanner(__b, __e, _M_flags, __loc),
70 _M_nfa(make_shared<_RegexT>(__loc, _M_flags)),
71 _M_traits(_M_nfa->_M_traits),
74 _StateSeqT __r(*_M_nfa, _M_nfa->_M_start());
75 __r._M_append(_M_nfa->_M_insert_subexpr_begin());
76 this->_M_disjunction();
77 if (!_M_match_token(_ScannerT::_S_token_eof))
78 __throw_regex_error(regex_constants::error_paren);
79 __r._M_append(_M_pop());
80 __glibcxx_assert(_M_stack.empty());
81 __r._M_append(_M_nfa->_M_insert_subexpr_end());
82 __r._M_append(_M_nfa->_M_insert_accept());
83 _M_nfa->_M_eliminate_dummy();
86 template<
typename _TraitsT>
91 this->_M_alternative();
92 while (_M_match_token(_ScannerT::_S_token_or))
94 _StateSeqT __alt1 = _M_pop();
95 this->_M_alternative();
96 _StateSeqT __alt2 = _M_pop();
97 auto __end = _M_nfa->_M_insert_dummy();
98 __alt1._M_append(__end);
99 __alt2._M_append(__end);
103 _M_stack.push(_StateSeqT(*_M_nfa,
104 _M_nfa->_M_insert_alt(
105 __alt2._M_start, __alt1._M_start,
false),
110 template<
typename _TraitsT>
112 _Compiler<_TraitsT>::
117 _StateSeqT __re = _M_pop();
118 this->_M_alternative();
119 __re._M_append(_M_pop());
123 _M_stack.push(_StateSeqT(*_M_nfa, _M_nfa->_M_insert_dummy()));
126 template<
typename _TraitsT>
128 _Compiler<_TraitsT>::
131 if (this->_M_assertion())
135 while (this->_M_quantifier())
142 template<
typename _TraitsT>
144 _Compiler<_TraitsT>::
147 if (_M_match_token(_ScannerT::_S_token_line_begin))
148 _M_stack.push(_StateSeqT(*_M_nfa, _M_nfa->_M_insert_line_begin()));
149 else if (_M_match_token(_ScannerT::_S_token_line_end))
150 _M_stack.push(_StateSeqT(*_M_nfa, _M_nfa->_M_insert_line_end()));
151 else if (_M_match_token(_ScannerT::_S_token_word_bound))
153 _M_stack.push(_StateSeqT(*_M_nfa, _M_nfa->
154 _M_insert_word_bound(_M_value[0] ==
'n')));
155 else if (_M_match_token(_ScannerT::_S_token_subexpr_lookahead_begin))
157 auto __neg = _M_value[0] ==
'n';
158 this->_M_disjunction();
159 if (!_M_match_token(_ScannerT::_S_token_subexpr_end))
160 __throw_regex_error(regex_constants::error_paren,
161 "Parenthesis is not closed.");
162 auto __tmp = _M_pop();
163 __tmp._M_append(_M_nfa->_M_insert_accept());
167 _M_nfa->_M_insert_lookahead(__tmp._M_start, __neg)));
174 template<
typename _TraitsT>
176 _Compiler<_TraitsT>::
179 bool __neg = (_M_flags & regex_constants::ECMAScript);
180 auto __init = [
this, &__neg]()
182 if (_M_stack.empty())
183 __throw_regex_error(regex_constants::error_badrepeat,
184 "Nothing to repeat before a quantifier.");
185 __neg = __neg && _M_match_token(_ScannerT::_S_token_opt);
187 if (_M_match_token(_ScannerT::_S_token_closure0))
191 _StateSeqT __r(*_M_nfa,
192 _M_nfa->_M_insert_repeat(_S_invalid_state_id,
193 __e._M_start, __neg));
197 else if (_M_match_token(_ScannerT::_S_token_closure1))
201 __e._M_append(_M_nfa->_M_insert_repeat(_S_invalid_state_id,
202 __e._M_start, __neg));
205 else if (_M_match_token(_ScannerT::_S_token_opt))
209 auto __end = _M_nfa->_M_insert_dummy();
210 _StateSeqT __r(*_M_nfa,
211 _M_nfa->_M_insert_repeat(_S_invalid_state_id,
212 __e._M_start, __neg));
213 __e._M_append(__end);
214 __r._M_append(__end);
217 else if (_M_match_token(_ScannerT::_S_token_interval_begin))
219 if (_M_stack.empty())
220 __throw_regex_error(regex_constants::error_badrepeat,
221 "Nothing to repeat before a quantifier.");
222 if (!_M_match_token(_ScannerT::_S_token_dup_count))
223 __throw_regex_error(regex_constants::error_badbrace,
224 "Unexpected token in brace expression.");
225 _StateSeqT __r(_M_pop());
226 _StateSeqT __e(*_M_nfa, _M_nfa->_M_insert_dummy());
227 long __min_rep = _M_cur_int_value(10);
232 if (_M_match_token(_ScannerT::_S_token_comma))
234 if (_M_match_token(_ScannerT::_S_token_dup_count))
235 __n = _M_cur_int_value(10) - __min_rep;
239 if (!_M_match_token(_ScannerT::_S_token_interval_end))
240 __throw_regex_error(regex_constants::error_brace,
241 "Unexpected end of brace expression.");
243 __neg = __neg && _M_match_token(_ScannerT::_S_token_opt);
245 for (
long __i = 0; __i < __min_rep; ++__i)
246 __e._M_append(__r._M_clone());
250 auto __tmp = __r._M_clone();
251 _StateSeqT __s(*_M_nfa,
252 _M_nfa->_M_insert_repeat(_S_invalid_state_id,
253 __tmp._M_start, __neg));
254 __tmp._M_append(__s);
260 __throw_regex_error(regex_constants::error_badbrace,
261 "Invalid range in brace expression.");
262 auto __end = _M_nfa->_M_insert_dummy();
267 for (
long __i = 0; __i < __n; ++__i)
269 auto __tmp = __r._M_clone();
270 auto __alt = _M_nfa->_M_insert_repeat(__tmp._M_start,
273 __e._M_append(_StateSeqT(*_M_nfa, __alt, __tmp._M_end));
275 __e._M_append(__end);
276 while (!__stack.
empty())
278 auto& __tmp = (*_M_nfa)[__stack.
top()];
290#define __INSERT_REGEX_MATCHER(__func, ...)\
292 if (!(_M_flags & regex_constants::icase))\
293 if (!(_M_flags & regex_constants::collate))\
294 __func<false, false>(__VA_ARGS__);\
296 __func<false, true>(__VA_ARGS__);\
298 if (!(_M_flags & regex_constants::collate))\
299 __func<true, false>(__VA_ARGS__);\
301 __func<true, true>(__VA_ARGS__);\
304 template<
typename _TraitsT>
306 _Compiler<_TraitsT>::
309 if (_M_match_token(_ScannerT::_S_token_anychar))
311 if (!(_M_flags & regex_constants::ECMAScript))
312 __INSERT_REGEX_MATCHER(_M_insert_any_matcher_posix);
314 __INSERT_REGEX_MATCHER(_M_insert_any_matcher_ecma);
316 else if (_M_try_char())
317 __INSERT_REGEX_MATCHER(_M_insert_char_matcher);
318 else if (_M_match_token(_ScannerT::_S_token_backref))
319 _M_stack.push(_StateSeqT(*_M_nfa, _M_nfa->
320 _M_insert_backref(_M_cur_int_value(10))));
321 else if (_M_match_token(_ScannerT::_S_token_quoted_class))
322 __INSERT_REGEX_MATCHER(_M_insert_character_class_matcher);
323 else if (_M_match_token(_ScannerT::_S_token_subexpr_no_group_begin))
325 _StateSeqT __r(*_M_nfa, _M_nfa->_M_insert_dummy());
326 this->_M_disjunction();
327 if (!_M_match_token(_ScannerT::_S_token_subexpr_end))
328 __throw_regex_error(regex_constants::error_paren,
329 "Parenthesis is not closed.");
330 __r._M_append(_M_pop());
333 else if (_M_match_token(_ScannerT::_S_token_subexpr_begin))
335 _StateSeqT __r(*_M_nfa, _M_nfa->_M_insert_subexpr_begin());
336 this->_M_disjunction();
337 if (!_M_match_token(_ScannerT::_S_token_subexpr_end))
338 __throw_regex_error(regex_constants::error_paren,
339 "Parenthesis is not closed.");
340 __r._M_append(_M_pop());
341 __r._M_append(_M_nfa->_M_insert_subexpr_end());
344 else if (!_M_bracket_expression())
349 template<
typename _TraitsT>
351 _Compiler<_TraitsT>::
352 _M_bracket_expression()
355 _M_match_token(_ScannerT::_S_token_bracket_neg_begin);
356 if (!(__neg || _M_match_token(_ScannerT::_S_token_bracket_begin)))
358 __INSERT_REGEX_MATCHER(_M_insert_bracket_matcher, __neg);
361#undef __INSERT_REGEX_MATCHER
363 template<
typename _TraitsT>
364 template<
bool __icase,
bool __collate>
366 _Compiler<_TraitsT>::
367 _M_insert_any_matcher_ecma()
369 _M_stack.push(_StateSeqT(*_M_nfa,
370 _M_nfa->_M_insert_matcher
371 (_AnyMatcher<_TraitsT, true, __icase, __collate>
375 template<
typename _TraitsT>
376 template<
bool __icase,
bool __collate>
378 _Compiler<_TraitsT>::
379 _M_insert_any_matcher_posix()
381 _M_stack.push(_StateSeqT(*_M_nfa,
382 _M_nfa->_M_insert_matcher
383 (_AnyMatcher<_TraitsT, false, __icase, __collate>
387 template<
typename _TraitsT>
388 template<
bool __icase,
bool __collate>
390 _Compiler<_TraitsT>::
391 _M_insert_char_matcher()
393 _M_stack.push(_StateSeqT(*_M_nfa,
394 _M_nfa->_M_insert_matcher
395 (_CharMatcher<_TraitsT, __icase, __collate>
396 (_M_value[0], _M_traits))));
399 template<
typename _TraitsT>
400 template<
bool __icase,
bool __collate>
402 _Compiler<_TraitsT>::
403 _M_insert_character_class_matcher()
405 __glibcxx_assert(_M_value.size() == 1);
406 _BracketMatcher<__icase, __collate> __matcher
407 (_M_ctype.is(_CtypeT::upper, _M_value[0]), _M_traits);
408 __matcher._M_add_character_class(_M_value,
false);
409 __matcher._M_ready();
410 _M_stack.push(_StateSeqT(*_M_nfa,
411 _M_nfa->_M_insert_matcher(
std::move(__matcher))));
414 template<
typename _TraitsT>
415 template<
bool __icase,
bool __collate>
417 _Compiler<_TraitsT>::
418 _M_insert_bracket_matcher(
bool __neg)
420 _BracketMatcher<__icase, __collate> __matcher(__neg, _M_traits);
421 _BracketState __last_char;
423 __last_char.set(_M_value[0]);
424 else if (_M_match_token(_ScannerT::_S_token_bracket_dash))
426 __last_char.set(
'-');
427 while (_M_expression_term(__last_char, __matcher))
429 if (__last_char._M_is_char())
430 __matcher._M_add_char(__last_char.get());
431 __matcher._M_ready();
432 _M_stack.push(_StateSeqT(
434 _M_nfa->_M_insert_matcher(
std::move(__matcher))));
437 template<
typename _TraitsT>
438 template<
bool __icase,
bool __collate>
440 _Compiler<_TraitsT>::
441 _M_expression_term(_BracketState& __last_char,
442 _BracketMatcher<__icase, __collate>& __matcher)
444 if (_M_match_token(_ScannerT::_S_token_bracket_end))
448 const auto __push_char = [&](_CharT __ch)
450 if (__last_char._M_is_char())
451 __matcher._M_add_char(__last_char.get());
452 __last_char.set(__ch);
455 const auto __push_class = [&]
457 if (__last_char._M_is_char())
458 __matcher._M_add_char(__last_char.get());
461 __last_char.reset(_BracketState::_Type::_Class);
464 if (_M_match_token(_ScannerT::_S_token_collsymbol))
466 auto __symbol = __matcher._M_add_collate_element(_M_value);
467 if (__symbol.size() == 1)
468 __push_char(__symbol[0]);
472 else if (_M_match_token(_ScannerT::_S_token_equiv_class_name))
475 __matcher._M_add_equivalence_class(_M_value);
477 else if (_M_match_token(_ScannerT::_S_token_char_class_name))
480 __matcher._M_add_character_class(_M_value,
false);
482 else if (_M_try_char())
483 __push_char(_M_value[0]);
494 else if (_M_match_token(_ScannerT::_S_token_bracket_dash))
496 if (_M_match_token(_ScannerT::_S_token_bracket_end))
502 else if (__last_char._M_is_class())
505 __throw_regex_error(regex_constants::error_range,
506 "Invalid start of range in bracket expression.");
508 else if (__last_char._M_is_char())
513 __matcher._M_make_range(__last_char.get(), _M_value[0]);
516 else if (_M_match_token(_ScannerT::_S_token_bracket_dash))
519 __matcher._M_make_range(__last_char.get(),
'-');
523 __throw_regex_error(regex_constants::error_range,
524 "Invalid end of range in bracket expression.");
526 else if (_M_flags & regex_constants::ECMAScript)
534 __throw_regex_error(regex_constants::error_range,
535 "Invalid dash in bracket expression.");
537 else if (_M_match_token(_ScannerT::_S_token_quoted_class))
540 __matcher._M_add_character_class(_M_value,
541 _M_ctype.is(_CtypeT::upper,
545 __throw_regex_error(regex_constants::error_brack,
546 "Unexpected character in bracket expression.");
551 template<
typename _TraitsT>
553 _Compiler<_TraitsT>::
556 bool __is_char =
false;
557 if (_M_match_token(_ScannerT::_S_token_oct_num))
560 _M_value.assign(1, _M_cur_int_value(8));
562 else if (_M_match_token(_ScannerT::_S_token_hex_num))
565 _M_value.assign(1, _M_cur_int_value(16));
567 else if (_M_match_token(_ScannerT::_S_token_ord_char))
572 template<
typename _TraitsT>
574 _Compiler<_TraitsT>::
575 _M_match_token(_TokenT __token)
577 if (__token == _M_scanner._M_get_token())
579 _M_value = _M_scanner._M_get_value();
580 _M_scanner._M_advance();
586 template<
typename _TraitsT>
588 _Compiler<_TraitsT>::
589 _M_cur_int_value(
int __radix)
592 for (_CharT __c : _M_value)
593 if (__builtin_mul_overflow(__v, __radix, &__v)
594 || __builtin_add_overflow(__v, _M_traits.value(__c, __radix), &__v))
596 "invalid back reference");
600 template<
typename _TraitsT,
bool __icase,
bool __collate>
602 _BracketMatcher<_TraitsT, __icase, __collate>::
603 _M_apply(_CharT __ch, false_type)
const
607 if (std::binary_search(_M_char_set.begin(), _M_char_set.end(),
608 _M_translator._M_translate(__ch)))
610 auto __s = _M_translator._M_transform(__ch);
611 for (
auto& __it : _M_range_set)
612 if (_M_translator._M_match_range(__it.first, __it.second, __s))
614 if (_M_traits.isctype(__ch, _M_class_set))
616 if (std::find(_M_equiv_set.begin(), _M_equiv_set.end(),
617 _M_traits.transform_primary(&__ch, &__ch+1))
618 != _M_equiv_set.end())
620 for (
auto& __it : _M_neg_class_set)
621 if (!_M_traits.isctype(__ch, __it))
624 }() ^ _M_is_non_matching;
628_GLIBCXX_END_NAMESPACE_VERSION
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
void swap(any &__x, any &__y) noexcept
Exchange the states of two any objects.
const _Facet & use_facet(const locale &__loc)
Return a facet.
ISO C++ entities toplevel namespace is std.
constexpr error_type error_backref(_S_error_backref)
A standard container giving FILO behavior.
void pop()
Removes first element.
void push(const value_type &__x)
Add data to the top of the stack.