1 // class template regex -*- C++ -*-
3 // Copyright (C) 2013-2014 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
26 * @file bits/regex_compiler.tcc
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{regex}
31 // FIXME make comments doxygen format.
33 // This compiler refers to "Regular Expression Matching Can Be Simple And Fast"
34 // (http://swtch.com/~rsc/regexp/regexp1.html"),
35 // but doesn't strictly follow it.
37 // When compiling, states are *chained* instead of tree- or graph-constructed.
38 // It's more like structured programs: there's if statement and loop statement.
40 // For alternative structure (say "a|b"), aka "if statement", two branches
41 // should be constructed. However, these two shall merge to an "end_tag" at
42 // the end of this operator:
46 // => begin_tag end_tag =>
50 // This is the difference between this implementation and that in Russ's
53 // That's why we introduced dummy node here ------ "end_tag" is a dummy node.
54 // All dummy node will be eliminated at the end of compiling process.
56 namespace std _GLIBCXX_VISIBILITY(default)
60 _GLIBCXX_BEGIN_NAMESPACE_VERSION
62 template<typename _TraitsT>
64 _Compiler(_IterT __b, _IterT __e,
65 const _TraitsT& __traits, _FlagT __flags)
67 & (regex_constants::ECMAScript
68 | regex_constants::basic
69 | regex_constants::extended
70 | regex_constants::grep
71 | regex_constants::egrep
72 | regex_constants::awk))
74 : __flags | regex_constants::ECMAScript),
76 _M_ctype(std::use_facet<_CtypeT>(_M_traits.getloc())),
77 _M_scanner(__b, __e, _M_flags, _M_traits.getloc()),
80 _StateSeqT __r(_M_nfa, _M_nfa._M_start());
81 __r._M_append(_M_nfa._M_insert_subexpr_begin());
82 this->_M_disjunction();
83 if (!_M_match_token(_ScannerT::_S_token_eof))
84 __throw_regex_error(regex_constants::error_paren);
85 __r._M_append(_M_pop());
86 _GLIBCXX_DEBUG_ASSERT(_M_stack.empty());
87 __r._M_append(_M_nfa._M_insert_subexpr_end());
88 __r._M_append(_M_nfa._M_insert_accept());
89 _M_nfa._M_eliminate_dummy();
92 template<typename _TraitsT>
97 this->_M_alternative();
98 while (_M_match_token(_ScannerT::_S_token_or))
100 _StateSeqT __alt1 = _M_pop();
101 this->_M_alternative();
102 _StateSeqT __alt2 = _M_pop();
103 auto __end = _M_nfa._M_insert_dummy();
104 __alt1._M_append(__end);
105 __alt2._M_append(__end);
106 // __alt2 is state._M_next, __alt1 is state._M_alt. The executor
107 // executes _M_alt before _M_next, as well as executing left
108 // alternative before right one.
109 _M_stack.push(_StateSeqT(_M_nfa,
110 _M_nfa._M_insert_alt(__alt2._M_start,
111 __alt1._M_start, false),
116 template<typename _TraitsT>
118 _Compiler<_TraitsT>::
123 _StateSeqT __re = _M_pop();
124 this->_M_alternative();
125 __re._M_append(_M_pop());
129 _M_stack.push(_StateSeqT(_M_nfa, _M_nfa._M_insert_dummy()));
132 template<typename _TraitsT>
134 _Compiler<_TraitsT>::
137 if (this->_M_assertion())
141 while (this->_M_quantifier());
147 template<typename _TraitsT>
149 _Compiler<_TraitsT>::
152 if (_M_match_token(_ScannerT::_S_token_line_begin))
153 _M_stack.push(_StateSeqT(_M_nfa, _M_nfa._M_insert_line_begin()));
154 else if (_M_match_token(_ScannerT::_S_token_line_end))
155 _M_stack.push(_StateSeqT(_M_nfa, _M_nfa._M_insert_line_end()));
156 else if (_M_match_token(_ScannerT::_S_token_word_bound))
157 // _M_value[0] == 'n' means it's negative, say "not word boundary".
158 _M_stack.push(_StateSeqT(_M_nfa, _M_nfa.
159 _M_insert_word_bound(_M_value[0] == 'n')));
160 else if (_M_match_token(_ScannerT::_S_token_subexpr_lookahead_begin))
162 auto __neg = _M_value[0] == 'n';
163 this->_M_disjunction();
164 if (!_M_match_token(_ScannerT::_S_token_subexpr_end))
165 __throw_regex_error(regex_constants::error_paren);
166 auto __tmp = _M_pop();
167 __tmp._M_append(_M_nfa._M_insert_accept());
171 _M_nfa._M_insert_lookahead(__tmp._M_start, __neg)));
178 template<typename _TraitsT>
180 _Compiler<_TraitsT>::
183 bool __neg = (_M_flags & regex_constants::ECMAScript);
184 auto __init = [this, &__neg]()
186 if (_M_stack.empty())
187 __throw_regex_error(regex_constants::error_badrepeat);
188 __neg = __neg && _M_match_token(_ScannerT::_S_token_opt);
190 if (_M_match_token(_ScannerT::_S_token_closure0))
194 _StateSeqT __r(_M_nfa, _M_nfa._M_insert_repeat(_S_invalid_state_id,
195 __e._M_start, __neg));
199 else if (_M_match_token(_ScannerT::_S_token_closure1))
203 __e._M_append(_M_nfa._M_insert_repeat(_S_invalid_state_id,
204 __e._M_start, __neg));
207 else if (_M_match_token(_ScannerT::_S_token_opt))
211 auto __end = _M_nfa._M_insert_dummy();
212 _StateSeqT __r(_M_nfa, _M_nfa._M_insert_repeat(_S_invalid_state_id,
213 __e._M_start, __neg));
214 __e._M_append(__end);
215 __r._M_append(__end);
218 else if (_M_match_token(_ScannerT::_S_token_interval_begin))
220 if (_M_stack.empty())
221 __throw_regex_error(regex_constants::error_badrepeat);
222 if (!_M_match_token(_ScannerT::_S_token_dup_count))
223 __throw_regex_error(regex_constants::error_badbrace);
224 _StateSeqT __r(_M_pop());
225 _StateSeqT __e(_M_nfa, _M_nfa._M_insert_dummy());
226 long __min_rep = _M_cur_int_value(10);
231 if (_M_match_token(_ScannerT::_S_token_comma))
232 if (_M_match_token(_ScannerT::_S_token_dup_count)) // {3,7}
233 __n = _M_cur_int_value(10) - __min_rep;
238 if (!_M_match_token(_ScannerT::_S_token_interval_end))
239 __throw_regex_error(regex_constants::error_brace);
241 __neg = __neg && _M_match_token(_ScannerT::_S_token_opt);
243 for (long __i = 0; __i < __min_rep; ++__i)
244 __e._M_append(__r._M_clone());
248 auto __tmp = __r._M_clone();
249 _StateSeqT __s(_M_nfa,
250 _M_nfa._M_insert_repeat(_S_invalid_state_id,
251 __tmp._M_start, __neg));
252 __tmp._M_append(__s);
258 __throw_regex_error(regex_constants::error_badbrace);
259 auto __end = _M_nfa._M_insert_dummy();
260 // _M_alt is the "match more" branch, and _M_next is the
261 // "match less" one. Switch _M_alt and _M_next of all created
262 // nodes. This is a hack but IMO works well.
263 std::stack<_StateIdT> __stack;
264 for (long __i = 0; __i < __n; ++__i)
266 auto __tmp = __r._M_clone();
267 auto __alt = _M_nfa._M_insert_repeat(__tmp._M_start,
270 __e._M_append(_StateSeqT(_M_nfa, __alt, __tmp._M_end));
272 __e._M_append(__end);
273 while (!__stack.empty())
275 auto& __tmp = _M_nfa[__stack.top()];
277 swap(__tmp._M_next, __tmp._M_alt);
287 #define __INSERT_REGEX_MATCHER(__func, args...)\
289 if (!(_M_flags & regex_constants::icase))\
290 if (!(_M_flags & regex_constants::collate))\
291 __func<false, false>(args);\
293 __func<false, true>(args);\
295 if (!(_M_flags & regex_constants::collate))\
296 __func<true, false>(args);\
298 __func<true, true>(args);\
301 template<typename _TraitsT>
303 _Compiler<_TraitsT>::
306 if (_M_match_token(_ScannerT::_S_token_anychar))
308 if (!(_M_flags & regex_constants::ECMAScript))
309 __INSERT_REGEX_MATCHER(_M_insert_any_matcher_posix);
311 __INSERT_REGEX_MATCHER(_M_insert_any_matcher_ecma);
313 else if (_M_try_char())
314 __INSERT_REGEX_MATCHER(_M_insert_char_matcher);
315 else if (_M_match_token(_ScannerT::_S_token_backref))
316 _M_stack.push(_StateSeqT(_M_nfa, _M_nfa.
317 _M_insert_backref(_M_cur_int_value(10))));
318 else if (_M_match_token(_ScannerT::_S_token_quoted_class))
319 __INSERT_REGEX_MATCHER(_M_insert_character_class_matcher);
320 else if (_M_match_token(_ScannerT::_S_token_subexpr_no_group_begin))
322 _StateSeqT __r(_M_nfa, _M_nfa._M_insert_dummy());
323 this->_M_disjunction();
324 if (!_M_match_token(_ScannerT::_S_token_subexpr_end))
325 __throw_regex_error(regex_constants::error_paren);
326 __r._M_append(_M_pop());
329 else if (_M_match_token(_ScannerT::_S_token_subexpr_begin))
331 _StateSeqT __r(_M_nfa, _M_nfa._M_insert_subexpr_begin());
332 this->_M_disjunction();
333 if (!_M_match_token(_ScannerT::_S_token_subexpr_end))
334 __throw_regex_error(regex_constants::error_paren);
335 __r._M_append(_M_pop());
336 __r._M_append(_M_nfa._M_insert_subexpr_end());
339 else if (!_M_bracket_expression())
344 template<typename _TraitsT>
346 _Compiler<_TraitsT>::
347 _M_bracket_expression()
350 _M_match_token(_ScannerT::_S_token_bracket_neg_begin);
351 if (!(__neg || _M_match_token(_ScannerT::_S_token_bracket_begin)))
353 __INSERT_REGEX_MATCHER(_M_insert_bracket_matcher, __neg);
356 #undef __INSERT_REGEX_MATCHER
358 template<typename _TraitsT>
359 template<bool __icase, bool __collate>
361 _Compiler<_TraitsT>::
362 _M_insert_any_matcher_ecma()
364 _M_stack.push(_StateSeqT(_M_nfa,
365 _M_nfa._M_insert_matcher
366 (_AnyMatcher<_TraitsT, true, __icase, __collate>
370 template<typename _TraitsT>
371 template<bool __icase, bool __collate>
373 _Compiler<_TraitsT>::
374 _M_insert_any_matcher_posix()
376 _M_stack.push(_StateSeqT(_M_nfa,
377 _M_nfa._M_insert_matcher
378 (_AnyMatcher<_TraitsT, false, __icase, __collate>
382 template<typename _TraitsT>
383 template<bool __icase, bool __collate>
385 _Compiler<_TraitsT>::
386 _M_insert_char_matcher()
388 _M_stack.push(_StateSeqT(_M_nfa,
389 _M_nfa._M_insert_matcher
390 (_CharMatcher<_TraitsT, __icase, __collate>
391 (_M_value[0], _M_traits))));
394 template<typename _TraitsT>
395 template<bool __icase, bool __collate>
397 _Compiler<_TraitsT>::
398 _M_insert_character_class_matcher()
400 _GLIBCXX_DEBUG_ASSERT(_M_value.size() == 1);
401 _BracketMatcher<_TraitsT, __icase, __collate> __matcher
402 (_M_ctype.is(_CtypeT::upper, _M_value[0]), _M_traits);
403 __matcher._M_add_character_class(_M_value, false);
404 __matcher._M_ready();
405 _M_stack.push(_StateSeqT(_M_nfa,
406 _M_nfa._M_insert_matcher(std::move(__matcher))));
409 template<typename _TraitsT>
410 template<bool __icase, bool __collate>
412 _Compiler<_TraitsT>::
413 _M_insert_bracket_matcher(bool __neg)
415 _BracketMatcher<_TraitsT, __icase, __collate> __matcher(__neg, _M_traits);
416 while (!_M_match_token(_ScannerT::_S_token_bracket_end))
417 _M_expression_term(__matcher);
418 __matcher._M_ready();
419 _M_stack.push(_StateSeqT(_M_nfa,
420 _M_nfa._M_insert_matcher(std::move(__matcher))));
423 template<typename _TraitsT>
424 template<bool __icase, bool __collate>
426 _Compiler<_TraitsT>::
427 _M_expression_term(_BracketMatcher<_TraitsT, __icase, __collate>& __matcher)
429 if (_M_match_token(_ScannerT::_S_token_collsymbol))
430 __matcher._M_add_collating_element(_M_value);
431 else if (_M_match_token(_ScannerT::_S_token_equiv_class_name))
432 __matcher._M_add_equivalence_class(_M_value);
433 else if (_M_match_token(_ScannerT::_S_token_char_class_name))
434 __matcher._M_add_character_class(_M_value, false);
435 else if (_M_try_char()) // [a
437 auto __ch = _M_value[0];
440 if (_M_value[0] == '-') // [a-
442 if (_M_try_char()) // [a-z]
444 __matcher._M_make_range(__ch, _M_value[0]);
447 // If the dash is the last character in the bracket
448 // expression, it is not special.
449 if (_M_scanner._M_get_token()
450 != _ScannerT::_S_token_bracket_end)
451 __throw_regex_error(regex_constants::error_range);
453 __matcher._M_add_char(_M_value[0]);
455 __matcher._M_add_char(__ch);
457 else if (_M_match_token(_ScannerT::_S_token_quoted_class))
458 __matcher._M_add_character_class(_M_value,
459 _M_ctype.is(_CtypeT::upper,
462 __throw_regex_error(regex_constants::error_brack);
465 template<typename _TraitsT>
467 _Compiler<_TraitsT>::
470 bool __is_char = false;
471 if (_M_match_token(_ScannerT::_S_token_oct_num))
474 _M_value.assign(1, _M_cur_int_value(8));
476 else if (_M_match_token(_ScannerT::_S_token_hex_num))
479 _M_value.assign(1, _M_cur_int_value(16));
481 else if (_M_match_token(_ScannerT::_S_token_ord_char))
486 template<typename _TraitsT>
488 _Compiler<_TraitsT>::
489 _M_match_token(_TokenT token)
491 if (token == _M_scanner._M_get_token())
493 _M_value = _M_scanner._M_get_value();
494 _M_scanner._M_advance();
500 template<typename _TraitsT>
502 _Compiler<_TraitsT>::
503 _M_cur_int_value(int __radix)
506 for (typename _StringT::size_type __i = 0;
507 __i < _M_value.length(); ++__i)
508 __v =__v * __radix + _M_traits.value(_M_value[__i], __radix);
512 template<typename _TraitsT, bool __icase, bool __collate>
514 _BracketMatcher<_TraitsT, __icase, __collate>::
515 _M_apply(_CharT __ch, false_type) const
517 bool __ret = std::binary_search(_M_char_set.begin(), _M_char_set.end(),
518 _M_translator._M_translate(__ch));
521 auto __s = _M_translator._M_transform(__ch);
522 for (auto& __it : _M_range_set)
523 if (__it.first <= __s && __s <= __it.second)
528 if (_M_traits.isctype(__ch, _M_class_set))
530 else if (std::find(_M_equiv_set.begin(), _M_equiv_set.end(),
531 _M_traits.transform_primary(&__ch, &__ch+1))
532 != _M_equiv_set.end())
536 for (auto& __it : _M_neg_class_set)
537 if (!_M_traits.isctype(__ch, __it))
544 if (_M_is_non_matching)
550 _GLIBCXX_END_NAMESPACE_VERSION
551 } // namespace __detail