58 namespace std _GLIBCXX_VISIBILITY(default)
60 _GLIBCXX_BEGIN_NAMESPACE_VERSION
64 template<
typename _TraitsT>
66 _Compiler(_IterT __b, _IterT __e,
67 const typename _TraitsT::locale_type& __loc, _FlagT __flags)
70 | regex_constants::
basic 72 | regex_constants::
grep 73 | regex_constants::
egrep 74 | regex_constants::
awk))
77 _M_scanner(__b, __e, _M_flags, __loc),
79 _M_traits(_M_nfa->_M_traits),
82 _StateSeqT __r(*_M_nfa, _M_nfa->_M_start());
83 __r._M_append(_M_nfa->_M_insert_subexpr_begin());
84 this->_M_disjunction();
85 if (!_M_match_token(_ScannerT::_S_token_eof))
87 __r._M_append(_M_pop());
88 __glibcxx_assert(_M_stack.
empty());
89 __r._M_append(_M_nfa->_M_insert_subexpr_end());
90 __r._M_append(_M_nfa->_M_insert_accept());
91 _M_nfa->_M_eliminate_dummy();
94 template<
typename _TraitsT>
99 this->_M_alternative();
100 while (_M_match_token(_ScannerT::_S_token_or))
102 _StateSeqT __alt1 = _M_pop();
103 this->_M_alternative();
104 _StateSeqT __alt2 = _M_pop();
105 auto __end = _M_nfa->_M_insert_dummy();
106 __alt1._M_append(__end);
107 __alt2._M_append(__end);
111 _M_stack.push(_StateSeqT(*_M_nfa,
112 _M_nfa->_M_insert_alt(
113 __alt2._M_start, __alt1._M_start,
false),
118 template<
typename _TraitsT>
120 _Compiler<_TraitsT>::
125 _StateSeqT __re = _M_pop();
126 this->_M_alternative();
127 __re._M_append(_M_pop());
131 _M_stack.push(_StateSeqT(*_M_nfa, _M_nfa->_M_insert_dummy()));
134 template<
typename _TraitsT>
136 _Compiler<_TraitsT>::
139 if (this->_M_assertion())
143 while (this->_M_quantifier());
149 template<
typename _TraitsT>
151 _Compiler<_TraitsT>::
154 if (_M_match_token(_ScannerT::_S_token_line_begin))
155 _M_stack.push(_StateSeqT(*_M_nfa, _M_nfa->_M_insert_line_begin()));
156 else if (_M_match_token(_ScannerT::_S_token_line_end))
157 _M_stack.push(_StateSeqT(*_M_nfa, _M_nfa->_M_insert_line_end()));
158 else if (_M_match_token(_ScannerT::_S_token_word_bound))
160 _M_stack.push(_StateSeqT(*_M_nfa, _M_nfa->
161 _M_insert_word_bound(_M_value[0] ==
'n')));
162 else if (_M_match_token(_ScannerT::_S_token_subexpr_lookahead_begin))
164 auto __neg = _M_value[0] ==
'n';
165 this->_M_disjunction();
166 if (!_M_match_token(_ScannerT::_S_token_subexpr_end))
168 "Parenthesis is not closed.");
169 auto __tmp = _M_pop();
170 __tmp._M_append(_M_nfa->_M_insert_accept());
174 _M_nfa->_M_insert_lookahead(__tmp._M_start, __neg)));
181 template<
typename _TraitsT>
183 _Compiler<_TraitsT>::
187 auto __init = [
this, &__neg]()
189 if (_M_stack.empty())
191 "Nothing to repeat before a quantifier.");
192 __neg = __neg && _M_match_token(_ScannerT::_S_token_opt);
194 if (_M_match_token(_ScannerT::_S_token_closure0))
198 _StateSeqT __r(*_M_nfa,
199 _M_nfa->_M_insert_repeat(_S_invalid_state_id,
200 __e._M_start, __neg));
204 else if (_M_match_token(_ScannerT::_S_token_closure1))
208 __e._M_append(_M_nfa->_M_insert_repeat(_S_invalid_state_id,
209 __e._M_start, __neg));
212 else if (_M_match_token(_ScannerT::_S_token_opt))
216 auto __end = _M_nfa->_M_insert_dummy();
217 _StateSeqT __r(*_M_nfa,
218 _M_nfa->_M_insert_repeat(_S_invalid_state_id,
219 __e._M_start, __neg));
220 __e._M_append(__end);
221 __r._M_append(__end);
224 else if (_M_match_token(_ScannerT::_S_token_interval_begin))
226 if (_M_stack.empty())
228 "Nothing to repeat before a quantifier.");
229 if (!_M_match_token(_ScannerT::_S_token_dup_count))
231 "Unexpected token in brace expression.");
232 _StateSeqT __r(_M_pop());
233 _StateSeqT __e(*_M_nfa, _M_nfa->_M_insert_dummy());
234 long __min_rep = _M_cur_int_value(10);
239 if (_M_match_token(_ScannerT::_S_token_comma))
240 if (_M_match_token(_ScannerT::_S_token_dup_count))
241 __n = _M_cur_int_value(10) - __min_rep;
246 if (!_M_match_token(_ScannerT::_S_token_interval_end))
248 "Unexpected end of brace expression.");
250 __neg = __neg && _M_match_token(_ScannerT::_S_token_opt);
252 for (
long __i = 0; __i < __min_rep; ++__i)
253 __e._M_append(__r._M_clone());
257 auto __tmp = __r._M_clone();
258 _StateSeqT __s(*_M_nfa,
259 _M_nfa->_M_insert_repeat(_S_invalid_state_id,
260 __tmp._M_start, __neg));
261 __tmp._M_append(__s);
268 "Invalid range in brace expression.");
269 auto __end = _M_nfa->_M_insert_dummy();
274 for (
long __i = 0; __i < __n; ++__i)
276 auto __tmp = __r._M_clone();
277 auto __alt = _M_nfa->_M_insert_repeat(__tmp._M_start,
280 __e._M_append(_StateSeqT(*_M_nfa, __alt, __tmp._M_end));
282 __e._M_append(__end);
283 while (!__stack.
empty())
285 auto& __tmp = (*_M_nfa)[__stack.
top()];
287 std::swap(__tmp._M_next, __tmp._M_alt);
297 #define __INSERT_REGEX_MATCHER(__func, ...)\ 299 if (!(_M_flags & regex_constants::icase))\ 300 if (!(_M_flags & regex_constants::collate))\ 301 __func<false, false>(__VA_ARGS__);\ 303 __func<false, true>(__VA_ARGS__);\ 305 if (!(_M_flags & regex_constants::collate))\ 306 __func<true, false>(__VA_ARGS__);\ 308 __func<true, true>(__VA_ARGS__);\ 311 template<
typename _TraitsT>
313 _Compiler<_TraitsT>::
316 if (_M_match_token(_ScannerT::_S_token_anychar))
319 __INSERT_REGEX_MATCHER(_M_insert_any_matcher_posix);
321 __INSERT_REGEX_MATCHER(_M_insert_any_matcher_ecma);
323 else if (_M_try_char())
324 __INSERT_REGEX_MATCHER(_M_insert_char_matcher);
325 else if (_M_match_token(_ScannerT::_S_token_backref))
326 _M_stack.push(_StateSeqT(*_M_nfa, _M_nfa->
327 _M_insert_backref(_M_cur_int_value(10))));
328 else if (_M_match_token(_ScannerT::_S_token_quoted_class))
329 __INSERT_REGEX_MATCHER(_M_insert_character_class_matcher);
330 else if (_M_match_token(_ScannerT::_S_token_subexpr_no_group_begin))
332 _StateSeqT __r(*_M_nfa, _M_nfa->_M_insert_dummy());
333 this->_M_disjunction();
334 if (!_M_match_token(_ScannerT::_S_token_subexpr_end))
336 "Parenthesis is not closed.");
337 __r._M_append(_M_pop());
340 else if (_M_match_token(_ScannerT::_S_token_subexpr_begin))
342 _StateSeqT __r(*_M_nfa, _M_nfa->_M_insert_subexpr_begin());
343 this->_M_disjunction();
344 if (!_M_match_token(_ScannerT::_S_token_subexpr_end))
346 "Parenthesis is not closed.");
347 __r._M_append(_M_pop());
348 __r._M_append(_M_nfa->_M_insert_subexpr_end());
351 else if (!_M_bracket_expression())
356 template<
typename _TraitsT>
358 _Compiler<_TraitsT>::
359 _M_bracket_expression()
362 _M_match_token(_ScannerT::_S_token_bracket_neg_begin);
363 if (!(__neg || _M_match_token(_ScannerT::_S_token_bracket_begin)))
365 __INSERT_REGEX_MATCHER(_M_insert_bracket_matcher, __neg);
368 #undef __INSERT_REGEX_MATCHER 370 template<
typename _TraitsT>
371 template<
bool __icase,
bool __collate>
373 _Compiler<_TraitsT>::
374 _M_insert_any_matcher_ecma()
376 _M_stack.push(_StateSeqT(*_M_nfa,
377 _M_nfa->_M_insert_matcher
378 (_AnyMatcher<_TraitsT, true, __icase, __collate>
382 template<
typename _TraitsT>
383 template<
bool __icase,
bool __collate>
385 _Compiler<_TraitsT>::
386 _M_insert_any_matcher_posix()
388 _M_stack.push(_StateSeqT(*_M_nfa,
389 _M_nfa->_M_insert_matcher
390 (_AnyMatcher<_TraitsT, false, __icase, __collate>
394 template<
typename _TraitsT>
395 template<
bool __icase,
bool __collate>
397 _Compiler<_TraitsT>::
398 _M_insert_char_matcher()
400 _M_stack.push(_StateSeqT(*_M_nfa,
401 _M_nfa->_M_insert_matcher
402 (_CharMatcher<_TraitsT, __icase, __collate>
403 (_M_value[0], _M_traits))));
406 template<
typename _TraitsT>
407 template<
bool __icase,
bool __collate>
409 _Compiler<_TraitsT>::
410 _M_insert_character_class_matcher()
412 __glibcxx_assert(_M_value.size() == 1);
413 _BracketMatcher<_TraitsT, __icase, __collate> __matcher
414 (_M_ctype.is(_CtypeT::upper, _M_value[0]), _M_traits);
415 __matcher._M_add_character_class(_M_value,
false);
416 __matcher._M_ready();
417 _M_stack.push(_StateSeqT(*_M_nfa,
418 _M_nfa->_M_insert_matcher(std::move(__matcher))));
421 template<
typename _TraitsT>
422 template<
bool __icase,
bool __collate>
424 _Compiler<_TraitsT>::
425 _M_insert_bracket_matcher(
bool __neg)
427 _BracketMatcher<_TraitsT, __icase, __collate> __matcher(__neg, _M_traits);
428 pair<bool, _CharT> __last_char;
429 __last_char.first =
false;
434 __last_char.first =
true;
435 __last_char.second = _M_value[0];
437 else if (_M_match_token(_ScannerT::_S_token_bracket_dash))
439 __last_char.first =
true;
440 __last_char.second =
'-';
443 while (_M_expression_term(__last_char, __matcher));
444 if (__last_char.first)
445 __matcher._M_add_char(__last_char.second);
446 __matcher._M_ready();
447 _M_stack.push(_StateSeqT(
449 _M_nfa->_M_insert_matcher(std::move(__matcher))));
452 template<
typename _TraitsT>
453 template<
bool __icase,
bool __collate>
455 _Compiler<_TraitsT>::
456 _M_expression_term(pair<bool, _CharT>& __last_char,
457 _BracketMatcher<_TraitsT, __icase, __collate>& __matcher)
459 if (_M_match_token(_ScannerT::_S_token_bracket_end))
462 const auto __push_char = [&](_CharT __ch)
464 if (__last_char.first)
465 __matcher._M_add_char(__last_char.second);
467 __last_char.first =
true;
468 __last_char.second = __ch;
470 const auto __flush = [&]
472 if (__last_char.first)
474 __matcher._M_add_char(__last_char.second);
475 __last_char.first =
false;
479 if (_M_match_token(_ScannerT::_S_token_collsymbol))
481 auto __symbol = __matcher._M_add_collate_element(_M_value);
482 if (__symbol.size() == 1)
483 __push_char(__symbol[0]);
487 else if (_M_match_token(_ScannerT::_S_token_equiv_class_name))
490 __matcher._M_add_equivalence_class(_M_value);
492 else if (_M_match_token(_ScannerT::_S_token_char_class_name))
495 __matcher._M_add_character_class(_M_value,
false);
497 else if (_M_try_char())
498 __push_char(_M_value[0]);
509 else if (_M_match_token(_ScannerT::_S_token_bracket_dash))
511 if (!__last_char.first)
515 if (_M_match_token(_ScannerT::_S_token_bracket_end))
522 "Unexpected dash in bracket expression. For POSIX syntax, " 523 "a dash is not treated literally only when it is at " 524 "beginning or end.");
532 __matcher._M_make_range(__last_char.second, _M_value[0]);
533 __last_char.first =
false;
535 else if (_M_match_token(_ScannerT::_S_token_bracket_dash))
537 __matcher._M_make_range(__last_char.second,
'-');
538 __last_char.first =
false;
542 if (_M_scanner._M_get_token()
543 != _ScannerT::_S_token_bracket_end)
546 "Character is expected after a dash.");
551 else if (_M_match_token(_ScannerT::_S_token_quoted_class))
554 __matcher._M_add_character_class(_M_value,
555 _M_ctype.is(_CtypeT::upper,
560 "Unexpected character in bracket expression.");
565 template<
typename _TraitsT>
567 _Compiler<_TraitsT>::
570 bool __is_char =
false;
571 if (_M_match_token(_ScannerT::_S_token_oct_num))
574 _M_value.assign(1, _M_cur_int_value(8));
576 else if (_M_match_token(_ScannerT::_S_token_hex_num))
579 _M_value.assign(1, _M_cur_int_value(16));
581 else if (_M_match_token(_ScannerT::_S_token_ord_char))
586 template<
typename _TraitsT>
588 _Compiler<_TraitsT>::
589 _M_match_token(_TokenT token)
591 if (token == _M_scanner._M_get_token())
593 _M_value = _M_scanner._M_get_value();
594 _M_scanner._M_advance();
600 template<
typename _TraitsT>
602 _Compiler<_TraitsT>::
603 _M_cur_int_value(
int __radix)
606 for (
typename _StringT::size_type __i = 0;
607 __i < _M_value.length(); ++__i)
608 __v =__v * __radix + _M_traits.value(_M_value[__i], __radix);
612 template<
typename _TraitsT,
bool __icase,
bool __collate>
614 _BracketMatcher<_TraitsT, __icase, __collate>::
619 if (std::binary_search(_M_char_set.begin(), _M_char_set.end(),
620 _M_translator._M_translate(__ch)))
622 auto __s = _M_translator._M_transform(__ch);
623 for (
auto& __it : _M_range_set)
624 if (_M_translator._M_match_range(__it.first, __it.second, __s))
626 if (_M_traits.isctype(__ch, _M_class_set))
628 if (std::find(_M_equiv_set.begin(), _M_equiv_set.end(),
629 _M_traits.transform_primary(&__ch, &__ch+1))
630 != _M_equiv_set.end())
632 for (
auto& __it : _M_neg_class_set)
633 if (!_M_traits.isctype(__ch, __it))
636 }() ^ _M_is_non_matching;
640 _GLIBCXX_END_NAMESPACE_VERSION
_GLIBCXX_NODISCARD bool empty() const
integral_constant< bool, false > false_type
The type used as a compile-time boolean with false value.
constexpr error_type error_brack(_S_error_brack)
constexpr error_type error_range(_S_error_range)
_GLIBCXX17_INLINE constexpr syntax_option_type awk
ISO C++ entities toplevel namespace is std.
_GLIBCXX17_INLINE constexpr syntax_option_type grep
_GLIBCXX17_INLINE constexpr syntax_option_type extended
constexpr error_type error_badrepeat(_S_error_badrepeat)
constexpr error_type error_badbrace(_S_error_badbrace)
void pop()
Removes first element.
void push(const value_type &__x)
Add data to the top of the stack.
const _Facet & use_facet(const locale &__loc)
Return a facet.use_facet looks for and returns a reference to a facet of type Facet where Facet is th...
constexpr error_type error_brace(_S_error_brace)
_GLIBCXX17_INLINE constexpr syntax_option_type ECMAScript
A standard container giving FILO behavior.
shared_ptr< _Tp > make_shared(_Args &&... __args)
Create an object that is owned by a shared_ptr.
_GLIBCXX17_INLINE constexpr syntax_option_type egrep
_GLIBCXX17_INLINE constexpr syntax_option_type basic
constexpr error_type error_paren(_S_error_paren)