31 namespace std _GLIBCXX_VISIBILITY(default)
35 _GLIBCXX_BEGIN_NAMESPACE_VERSION
45 typedef unsigned int _StateT;
47 static constexpr _StateT _S_state_at_start = 1 << 0;
48 static constexpr _StateT _S_state_in_brace = 1 << 2;
49 static constexpr _StateT _S_state_in_bracket = 1 << 3;
64 template<
typename _InputIterator>
68 typedef _InputIterator _IteratorT;
69 typedef typename std::iterator_traits<_IteratorT>::value_type _CharT;
79 _S_token_bracket_begin,
81 _S_token_inverse_class,
82 _S_token_char_class_name,
85 _S_token_collelem_multi,
86 _S_token_collelem_single,
92 _S_token_equiv_class_name,
93 _S_token_interval_begin,
94 _S_token_interval_end,
100 _S_token_quoted_char,
101 _S_token_subexpr_begin,
102 _S_token_subexpr_end,
108 _Scanner(_IteratorT __begin, _IteratorT __end, _FlagT __flags,
110 : _M_current(__begin) , _M_end(__end) , _M_flags(__flags),
111 _M_ctype(
std::use_facet<_CtypeT>(__loc)), _M_state(_S_state_at_start)
119 {
return _M_curToken; }
123 {
return _M_curValue; }
125 #ifdef _GLIBCXX_DEBUG
127 _M_print(std::ostream&);
138 _M_scan_in_bracket();
149 _IteratorT _M_current;
154 _StringT _M_curValue;
158 template<
typename _InputIterator>
160 _Scanner<_InputIterator>::
163 if (_M_current == _M_end)
165 _M_curToken = _S_token_eof;
169 _CharT __c = *_M_current;
170 if (_M_state & _S_state_in_bracket)
172 _M_scan_in_bracket();
175 if (_M_state & _S_state_in_brace)
183 else if (_M_state & _S_state_at_start && __c == _M_ctype.widen(
'^'))
185 _M_curToken = _S_token_line_begin;
189 else if (__c == _M_ctype.widen(
'$'))
191 _M_curToken = _S_token_line_end;
196 else if (__c == _M_ctype.widen(
'.'))
198 _M_curToken = _S_token_anychar;
202 else if (__c == _M_ctype.widen(
'*'))
204 _M_curToken = _S_token_closure0;
208 else if (__c == _M_ctype.widen(
'+'))
210 _M_curToken = _S_token_closure1;
214 else if (__c == _M_ctype.widen(
'|'))
216 _M_curToken = _S_token_or;
220 else if (__c == _M_ctype.widen(
'['))
222 _M_curToken = _S_token_bracket_begin;
223 _M_state |= (_S_state_in_bracket | _S_state_at_start);
227 else if (__c == _M_ctype.widen(
'\\'))
234 if (__c == _M_ctype.widen(
'('))
236 _M_curToken = _S_token_subexpr_begin;
240 else if (__c == _M_ctype.widen(
')'))
242 _M_curToken = _S_token_subexpr_end;
246 else if (__c == _M_ctype.widen(
'{'))
248 _M_curToken = _S_token_interval_begin;
249 _M_state |= _S_state_in_brace;
255 _M_curToken = _S_token_ord_char;
256 _M_curValue.assign(1, __c);
261 template<
typename _InputIterator>
263 _Scanner<_InputIterator>::
266 if (_M_ctype.is(_CtypeT::digit, *_M_current))
268 _M_curToken = _S_token_dup_count;
269 _M_curValue.assign(1, *_M_current);
271 while (_M_current != _M_end
272 && _M_ctype.is(_CtypeT::digit, *_M_current))
274 _M_curValue += *_M_current;
279 else if (*_M_current == _M_ctype.widen(
','))
281 _M_curToken = _S_token_comma;
287 if (*_M_current == _M_ctype.widen(
'\\'))
292 if (*_M_current == _M_ctype.widen(
'}'))
294 _M_curToken = _S_token_interval_end;
295 _M_state &= ~_S_state_in_brace;
302 template<
typename _InputIterator>
304 _Scanner<_InputIterator>::
307 if (_M_state & _S_state_at_start && *_M_current == _M_ctype.widen(
'^'))
309 _M_curToken = _S_token_inverse_class;
310 _M_state &= ~_S_state_at_start;
314 else if (*_M_current == _M_ctype.widen(
'['))
317 if (_M_current == _M_end)
319 _M_curToken = _S_token_eof;
323 if (*_M_current == _M_ctype.widen(
'.'))
325 _M_curToken = _S_token_collsymbol;
329 else if (*_M_current == _M_ctype.widen(
':'))
331 _M_curToken = _S_token_char_class_name;
335 else if (*_M_current == _M_ctype.widen(
'='))
337 _M_curToken = _S_token_equiv_class_name;
342 else if (*_M_current == _M_ctype.widen(
'-'))
344 _M_curToken = _S_token_dash;
348 else if (*_M_current == _M_ctype.widen(
']'))
351 || !(_M_state & _S_state_at_start))
355 _M_curToken = _S_token_bracket_end;
360 _M_curToken = _S_token_collelem_single;
361 _M_curValue.assign(1, *_M_current);
365 template<
typename _InputIterator>
367 _Scanner<_InputIterator>::
371 if (_M_current == _M_end)
373 _M_curToken = _S_token_eof;
376 _CharT __c = *_M_current;
379 if (__c == _M_ctype.widen(
'('))
383 _M_curToken = _S_token_ord_char;
384 _M_curValue.assign(1, __c);
387 _M_curToken = _S_token_subexpr_begin;
389 else if (__c == _M_ctype.widen(
')'))
393 _M_curToken = _S_token_ord_char;
394 _M_curValue.assign(1, __c);
397 _M_curToken = _S_token_subexpr_end;
399 else if (__c == _M_ctype.widen(
'{'))
403 _M_curToken = _S_token_ord_char;
404 _M_curValue.assign(1, __c);
408 _M_curToken = _S_token_interval_begin;
409 _M_state |= _S_state_in_brace;
412 else if (__c == _M_ctype.widen(
'}'))
416 _M_curToken = _S_token_ord_char;
417 _M_curValue.assign(1, __c);
421 if (!(_M_state && _S_state_in_brace))
423 _M_state &= ~_S_state_in_brace;
424 _M_curToken = _S_token_interval_end;
427 else if (__c == _M_ctype.widen(
'x'))
430 if (_M_current == _M_end)
432 _M_curToken = _S_token_eof;
435 if (_M_ctype.is(_CtypeT::digit, *_M_current))
437 _M_curValue.assign(1, *_M_current);
439 if (_M_current == _M_end)
441 _M_curToken = _S_token_eof;
444 if (_M_ctype.is(_CtypeT::digit, *_M_current))
446 _M_curValue += *_M_current;
452 else if (__c == _M_ctype.widen(
'^')
453 || __c == _M_ctype.widen(
'.')
454 || __c == _M_ctype.widen(
'*')
455 || __c == _M_ctype.widen(
'$')
456 || __c == _M_ctype.widen(
'\\'))
458 _M_curToken = _S_token_ord_char;
459 _M_curValue.assign(1, __c);
461 else if (_M_ctype.is(_CtypeT::digit, __c))
463 _M_curToken = _S_token_backref;
464 _M_curValue.assign(1, __c);
473 template<
typename _InputIterator>
475 _Scanner<_InputIterator>::
479 if (_M_current == _M_end)
481 for (_M_curValue.clear();
482 _M_current != _M_end && *_M_current != _M_ctype.widen(
':');
484 _M_curValue += *_M_current;
485 if (_M_current == _M_end)
488 if (*_M_current != _M_ctype.widen(
']'))
494 template<
typename _InputIterator>
496 _Scanner<_InputIterator>::
500 if (_M_current == _M_end)
502 for (_M_curValue.clear();
503 _M_current != _M_end && *_M_current != _M_ctype.widen(
'=');
505 _M_curValue += *_M_current;
506 if (_M_current == _M_end)
509 if (*_M_current != _M_ctype.widen(
']'))
515 template<
typename _InputIterator>
517 _Scanner<_InputIterator>::
521 if (_M_current == _M_end)
523 for (_M_curValue.clear();
524 _M_current != _M_end && *_M_current != _M_ctype.widen(
'.');
526 _M_curValue += *_M_current;
527 if (_M_current == _M_end)
530 if (*_M_current != _M_ctype.widen(
']'))
535 #ifdef _GLIBCXX_DEBUG
536 template<
typename _InputIterator>
538 _Scanner<_InputIterator>::
539 _M_print(std::ostream& ostr)
543 case _S_token_anychar:
544 ostr <<
"any-character\n";
546 case _S_token_backref:
549 case _S_token_bracket_begin:
550 ostr <<
"bracket-begin\n";
552 case _S_token_bracket_end:
553 ostr <<
"bracket-end\n";
555 case _S_token_char_class_name:
556 ostr <<
"char-class-name \"" << _M_curValue <<
"\"\n";
558 case _S_token_closure0:
559 ostr <<
"closure0\n";
561 case _S_token_closure1:
562 ostr <<
"closure1\n";
564 case _S_token_collelem_multi:
565 ostr <<
"coll-elem-multi \"" << _M_curValue <<
"\"\n";
567 case _S_token_collelem_single:
568 ostr <<
"coll-elem-single \"" << _M_curValue <<
"\"\n";
570 case _S_token_collsymbol:
571 ostr <<
"collsymbol \"" << _M_curValue <<
"\"\n";
579 case _S_token_dup_count:
580 ostr <<
"dup count: " << _M_curValue <<
"\n";
585 case _S_token_equiv_class_name:
586 ostr <<
"equiv-class-name \"" << _M_curValue <<
"\"\n";
588 case _S_token_interval_begin:
589 ostr <<
"interval begin\n";
591 case _S_token_interval_end:
592 ostr <<
"interval end\n";
594 case _S_token_line_begin:
595 ostr <<
"line begin\n";
597 case _S_token_line_end:
598 ostr <<
"line end\n";
606 case _S_token_ord_char:
607 ostr <<
"ordinary character: \"" << _M_value() <<
"\"\n";
609 case _S_token_quoted_char:
610 ostr <<
"quoted char\n";
612 case _S_token_subexpr_begin:
613 ostr <<
"subexpr begin\n";
615 case _S_token_subexpr_end:
616 ostr <<
"subexpr end\n";
618 case _S_token_word_begin:
619 ostr <<
"word begin\n";
621 case _S_token_word_end:
622 ostr <<
"word end\n";
624 case _S_token_unknown:
625 ostr <<
"-- unknown token --\n";
633 template<
typename _InIter,
typename _TraitsT>
637 typedef _InIter _IterT;
638 typedef typename std::iterator_traits<_InIter>::value_type _CharT;
642 _Compiler(
const _InIter& __b,
const _InIter& __e,
643 _TraitsT& __traits, _FlagT __flags);
647 {
return _M_state_store; }
657 _M_match_token(_TokenT __token);
678 _M_bracket_expression();
681 _M_bracket_list(_RMatcherT& __matcher);
684 _M_follow_list(_RMatcherT& __matcher);
687 _M_follow_list2(_RMatcherT& __matcher);
690 _M_expression_term(_RMatcherT& __matcher);
693 _M_range_expression(_RMatcherT& __matcher);
696 _M_start_range(_RMatcherT& __matcher);
699 _M_collating_symbol(_RMatcherT& __matcher);
702 _M_equivalence_class(_RMatcherT& __matcher);
705 _M_character_class(_RMatcherT& __matcher);
708 _M_cur_int_value(
int __radix);
711 _ScannerT _M_scanner;
712 _StringT _M_cur_value;
717 template<
typename _InIter,
typename _TraitsT>
719 _Compiler(
const _InIter& __b,
const _InIter& __e, _TraitsT& __traits,
720 _Compiler<_InIter, _TraitsT>::_FlagT __flags)
721 : _M_traits(__traits), _M_scanner(__b, __e, __flags, _M_traits.getloc()),
722 _M_state_store(__flags)
728 _M_state_store._M_insert_subexpr_begin(_Start(0)));
730 if (!_M_stack.
empty())
732 __r._M_append(_M_stack.
top());
735 __r._M_append(_M_state_store._M_insert_subexpr_end(0, _End(0)));
736 __r._M_append(_M_state_store._M_insert_accept());
739 template<
typename _InIter,
typename _TraitsT>
741 _Compiler<_InIter, _TraitsT>::
742 _M_match_token(_Compiler<_InIter, _TraitsT>::_TokenT token)
744 if (token == _M_scanner._M_token())
746 _M_cur_value = _M_scanner._M_value();
747 _M_scanner._M_advance();
753 template<
typename _InIter,
typename _TraitsT>
755 _Compiler<_InIter, _TraitsT>::
758 this->_M_alternative();
759 if (_M_match_token(_ScannerT::_S_token_or))
761 _StateSeq __alt1 = _M_stack.top(); _M_stack.pop();
762 this->_M_disjunction();
763 _StateSeq __alt2 = _M_stack.top(); _M_stack.pop();
764 _M_stack.push(_StateSeq(__alt1, __alt2));
768 template<
typename _InIter,
typename _TraitsT>
770 _Compiler<_InIter, _TraitsT>::
775 _StateSeq __re = _M_stack.top(); _M_stack.pop();
776 this->_M_alternative();
777 if (!_M_stack.empty())
779 __re._M_append(_M_stack.top());
788 template<
typename _InIter,
typename _TraitsT>
790 _Compiler<_InIter, _TraitsT>::
793 if (this->_M_assertion())
797 this->_M_quantifier();
803 template<
typename _InIter,
typename _TraitsT>
805 _Compiler<_InIter, _TraitsT>::
808 if (_M_match_token(_ScannerT::_S_token_line_begin))
813 if (_M_match_token(_ScannerT::_S_token_line_end))
818 if (_M_match_token(_ScannerT::_S_token_word_begin))
823 if (_M_match_token(_ScannerT::_S_token_word_end))
831 template<
typename _InIter,
typename _TraitsT>
833 _Compiler<_InIter, _TraitsT>::
836 if (_M_match_token(_ScannerT::_S_token_closure0))
838 if (_M_stack.empty())
840 _StateSeq __r(_M_stack.top(), -1);
841 __r._M_append(__r._M_front());
846 if (_M_match_token(_ScannerT::_S_token_closure1))
848 if (_M_stack.empty())
850 _StateSeq __r(_M_state_store,
853 _M_stack.top()._M_front()));
854 _M_stack.top()._M_append(__r);
857 if (_M_match_token(_ScannerT::_S_token_opt))
859 if (_M_stack.empty())
861 _StateSeq __r(_M_stack.top(), -1);
866 if (_M_match_token(_ScannerT::_S_token_interval_begin))
868 if (_M_stack.empty())
870 if (!_M_match_token(_ScannerT::_S_token_dup_count))
872 _StateSeq __r(_M_stack.top());
873 int __min_rep = _M_cur_int_value(10);
874 for (
int __i = 1; __i < __min_rep; ++__i)
875 _M_stack.top()._M_append(__r._M_clone());
876 if (_M_match_token(_ScannerT::_S_token_comma))
877 if (_M_match_token(_ScannerT::_S_token_dup_count))
879 int __n = _M_cur_int_value(10) - __min_rep;
882 for (
int __i = 0; __i < __n; ++__i)
884 _StateSeq __r(_M_state_store,
887 _M_stack.top()._M_front()));
888 _M_stack.top()._M_append(__r);
893 _StateSeq __r(_M_stack.top(), -1);
894 __r._M_push_back(__r._M_front());
898 if (!_M_match_token(_ScannerT::_S_token_interval_end))
905 template<
typename _InIter,
typename _TraitsT>
907 _Compiler<_InIter, _TraitsT>::
910 typedef _CharMatcher<_InIter, _TraitsT> _CMatcher;
911 typedef _StartTagger<_InIter, _TraitsT> _Start;
912 typedef _EndTagger<_InIter, _TraitsT> _End;
914 if (_M_match_token(_ScannerT::_S_token_anychar))
916 _M_stack.push(_StateSeq(_M_state_store,
917 _M_state_store._M_insert_matcher
921 if (_M_match_token(_ScannerT::_S_token_ord_char))
923 _M_stack.push(_StateSeq(_M_state_store,
924 _M_state_store._M_insert_matcher
925 (_CMatcher(_M_cur_value[0], _M_traits))));
928 if (_M_match_token(_ScannerT::_S_token_quoted_char))
931 _M_stack.push(_StateSeq(_M_state_store,
932 _M_state_store._M_insert_matcher
933 (_CMatcher(_M_cur_value[0], _M_traits))));
936 if (_M_match_token(_ScannerT::_S_token_backref))
941 if (_M_match_token(_ScannerT::_S_token_subexpr_begin))
943 int __mark = _M_state_store._M_sub_count();
944 _StateSeq __r(_M_state_store,
946 _M_insert_subexpr_begin(_Start(__mark)));
947 this->_M_disjunction();
948 if (!_M_match_token(_ScannerT::_S_token_subexpr_end))
950 if (!_M_stack.empty())
952 __r._M_append(_M_stack.top());
955 __r._M_append(_M_state_store._M_insert_subexpr_end
956 (__mark, _End(__mark)));
960 return _M_bracket_expression();
963 template<
typename _InIter,
typename _TraitsT>
965 _Compiler<_InIter, _TraitsT>::
966 _M_bracket_expression()
968 if (_M_match_token(_ScannerT::_S_token_bracket_begin))
970 _RMatcherT __matcher(_M_match_token(_ScannerT::_S_token_line_begin),
972 if (!_M_bracket_list(__matcher)
973 || !_M_match_token(_ScannerT::_S_token_bracket_end))
975 _M_stack.push(_StateSeq(_M_state_store,
976 _M_state_store._M_insert_matcher(__matcher)));
984 template<
typename _InIter,
typename _TraitsT>
986 _Compiler<_InIter, _TraitsT>::
987 _M_bracket_list(_RMatcherT& __matcher)
989 if (_M_follow_list(__matcher))
991 if (_M_match_token(_ScannerT::_S_token_dash))
992 __matcher._M_add_char(_M_cur_value[0]);
998 template<
typename _InIter,
typename _TraitsT>
1000 _Compiler<_InIter, _TraitsT>::
1001 _M_follow_list(_RMatcherT& __matcher)
1002 {
return _M_expression_term(__matcher) && _M_follow_list2(__matcher); }
1004 template<
typename _InIter,
typename _TraitsT>
1006 _Compiler<_InIter, _TraitsT>::
1007 _M_follow_list2(_RMatcherT& __matcher)
1009 if (_M_expression_term(__matcher))
1010 return _M_follow_list2(__matcher);
1014 template<
typename _InIter,
typename _TraitsT>
1016 _Compiler<_InIter, _TraitsT>::
1017 _M_expression_term(_RMatcherT& __matcher)
1019 return (_M_collating_symbol(__matcher)
1020 || _M_character_class(__matcher)
1021 || _M_equivalence_class(__matcher)
1022 || (_M_start_range(__matcher)
1023 && _M_range_expression(__matcher)));
1026 template<
typename _InIter,
typename _TraitsT>
1028 _Compiler<_InIter, _TraitsT>::
1029 _M_range_expression(_RMatcherT& __matcher)
1031 if (!_M_collating_symbol(__matcher))
1032 if (!_M_match_token(_ScannerT::_S_token_dash))
1034 __matcher._M_make_range();
1038 template<
typename _InIter,
typename _TraitsT>
1040 _Compiler<_InIter, _TraitsT>::
1041 _M_start_range(_RMatcherT& __matcher)
1042 {
return _M_match_token(_ScannerT::_S_token_dash); }
1044 template<
typename _InIter,
typename _TraitsT>
1046 _Compiler<_InIter, _TraitsT>::
1047 _M_collating_symbol(_RMatcherT& __matcher)
1049 if (_M_match_token(_ScannerT::_S_token_collelem_single))
1051 __matcher._M_add_char(_M_cur_value[0]);
1054 if (_M_match_token(_ScannerT::_S_token_collsymbol))
1056 __matcher._M_add_collating_element(_M_cur_value);
1062 template<
typename _InIter,
typename _TraitsT>
1064 _Compiler<_InIter, _TraitsT>::
1065 _M_equivalence_class(_RMatcherT& __matcher)
1067 if (_M_match_token(_ScannerT::_S_token_equiv_class_name))
1069 __matcher._M_add_equivalence_class(_M_cur_value);
1075 template<
typename _InIter,
typename _TraitsT>
1077 _Compiler<_InIter, _TraitsT>::
1078 _M_character_class(_RMatcherT& __matcher)
1080 if (_M_match_token(_ScannerT::_S_token_char_class_name))
1082 __matcher._M_add_character_class(_M_cur_value);
1088 template<
typename _InIter,
typename _TraitsT>
1090 _Compiler<_InIter, _TraitsT>::
1091 _M_cur_int_value(
int __radix)
1094 for (
typename _StringT::size_type __i = 0;
1095 __i < _M_cur_value.length(); ++__i)
1096 __v =__v * __radix + _M_traits.value(_M_cur_value[__i], __radix);
1100 template<
typename _InIter,
typename _TraitsT>
1102 __compile(
const _InIter& __b,
const _InIter& __e, _TraitsT& __t,
1104 {
return _AutomatonPtr(
new _Nfa(_Compiler<_InIter, _TraitsT>(__b, __e, __t,
1108 _GLIBCXX_END_NAMESPACE_VERSION
Container class for localization functionality.The locale class is first a class wrapper for C librar...
constexpr error_type error_badbrace(_S_error_badbrace)
constexpr error_type error_badrepeat(_S_error_badrepeat)
void pop()
Removes first element.
constexpr syntax_option_type grep
Builds an NFA from an input iterator interval.
constexpr error_type error_paren(_S_error_paren)
constexpr syntax_option_type ECMAScript
constexpr error_type error_ctype(_S_error_ctype)
static const _StateIdT _S_invalid_state_id
The special case in which a state identifier is not an index.
ISO C++ entities toplevel namespace is std.
Describes a sequence of one or more _State, its current start and end(s). This structure contains fra...
struct _Scanner. Scans an input range for regex tokens.
constexpr error_type error_range(_S_error_range)
std::shared_ptr< _Automaton > _AutomatonPtr
Generic shared pointer to an automaton.
Matches a character range (bracket expression)
_TokenT
Token types returned from the scanner.
bool _AnyMatcher(const _PatternCursor &)
Matches any character.
constexpr error_type error_brace(_S_error_brace)
unsigned int syntax_option_type
This is a bitmask type indicating how to interpret the regex.
Primary class template ctype facet.This template class defines classification and conversion function...
A standard container giving FILO behavior.
constexpr syntax_option_type basic
constexpr error_type error_collate(_S_error_collate)
constexpr error_type error_brack(_S_error_brack)
constexpr error_type error_escape(_S_error_escape)