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 branchs should
41 // be constructed. However, these two shall merge to an "end_tag" at the end of
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 _M_stack.push(_StateSeqT(_M_nfa,
107 _M_nfa._M_insert_alt(__alt1._M_start,
108 __alt2._M_start, false),
113 template<typename _TraitsT>
115 _Compiler<_TraitsT>::
120 _StateSeqT __re = _M_pop();
121 this->_M_alternative();
122 __re._M_append(_M_pop());
126 _M_stack.push(_StateSeqT(_M_nfa, _M_nfa._M_insert_dummy()));
129 template<typename _TraitsT>
131 _Compiler<_TraitsT>::
134 if (this->_M_assertion())
138 this->_M_quantifier();
144 template<typename _TraitsT>
146 _Compiler<_TraitsT>::
149 if (_M_match_token(_ScannerT::_S_token_line_begin))
150 _M_stack.push(_StateSeqT(_M_nfa, _M_nfa._M_insert_line_begin()));
151 else if (_M_match_token(_ScannerT::_S_token_line_end))
152 _M_stack.push(_StateSeqT(_M_nfa, _M_nfa._M_insert_line_end()));
153 else if (_M_match_token(_ScannerT::_S_token_word_bound))
154 // _M_value[0] == 'n' means it's negtive, say "not word boundary".
155 _M_stack.push(_StateSeqT(_M_nfa, _M_nfa.
156 _M_insert_word_bound(_M_value[0] == 'n')));
157 else if (_M_match_token(_ScannerT::_S_token_subexpr_lookahead_begin))
159 auto __neg = _M_value[0] == 'n';
160 this->_M_disjunction();
161 if (!_M_match_token(_ScannerT::_S_token_subexpr_end))
162 __throw_regex_error(regex_constants::error_paren);
163 auto __tmp = _M_pop();
164 __tmp._M_append(_M_nfa._M_insert_accept());
168 _M_nfa._M_insert_lookahead(__tmp._M_start, __neg)));
175 template<typename _TraitsT>
177 _Compiler<_TraitsT>::
180 bool __neg = (_M_flags & regex_constants::ECMAScript);
181 auto __init = [this, &__neg]()
183 if (_M_stack.empty())
184 __throw_regex_error(regex_constants::error_badrepeat);
185 __neg = __neg && _M_match_token(_ScannerT::_S_token_opt);
187 if (_M_match_token(_ScannerT::_S_token_closure0))
191 _StateSeqT __r(_M_nfa, _M_nfa._M_insert_alt(_S_invalid_state_id,
192 __e._M_start, __neg));
196 else if (_M_match_token(_ScannerT::_S_token_closure1))
200 __e._M_append(_M_nfa._M_insert_alt(_S_invalid_state_id, __e._M_start,
204 else if (_M_match_token(_ScannerT::_S_token_opt))
208 auto __end = _M_nfa._M_insert_dummy();
209 _StateSeqT __r(_M_nfa, _M_nfa._M_insert_alt(_S_invalid_state_id,
210 __e._M_start, __neg));
211 __e._M_append(__end);
212 __r._M_append(__end);
215 else if (_M_match_token(_ScannerT::_S_token_interval_begin))
217 if (_M_stack.empty())
218 __throw_regex_error(regex_constants::error_badrepeat);
219 if (!_M_match_token(_ScannerT::_S_token_dup_count))
220 __throw_regex_error(regex_constants::error_badbrace);
221 _StateSeqT __r(_M_pop());
222 _StateSeqT __e(_M_nfa, _M_nfa._M_insert_dummy());
223 long __min_rep = _M_cur_int_value(10);
228 if (_M_match_token(_ScannerT::_S_token_comma))
229 if (_M_match_token(_ScannerT::_S_token_dup_count)) // {3,7}
230 __n = _M_cur_int_value(10) - __min_rep;
235 if (!_M_match_token(_ScannerT::_S_token_interval_end))
236 __throw_regex_error(regex_constants::error_brace);
238 __neg = __neg && _M_match_token(_ScannerT::_S_token_opt);
240 for (long __i = 0; __i < __min_rep; ++__i)
241 __e._M_append(__r._M_clone());
245 auto __tmp = __r._M_clone();
246 _StateSeqT __s(_M_nfa,
247 _M_nfa._M_insert_alt(_S_invalid_state_id,
248 __tmp._M_start, __neg));
249 __tmp._M_append(__s);
255 __throw_regex_error(regex_constants::error_badbrace);
256 auto __end = _M_nfa._M_insert_dummy();
257 // _M_alt is the "match more" branch, and _M_next is the
258 // "match less" one. Switch _M_alt and _M_next of all created
259 // nodes. This is a hacking but IMO works well.
260 std::stack<_StateIdT> __stack;
261 for (long __i = 0; __i < __n; ++__i)
263 auto __tmp = __r._M_clone();
264 auto __alt = _M_nfa._M_insert_alt(__tmp._M_start,
267 __e._M_append(_StateSeqT(_M_nfa, __alt, __tmp._M_end));
269 __e._M_append(__end);
270 while (!__stack.empty())
272 auto& __tmp = _M_nfa[__stack.top()];
274 swap(__tmp._M_next, __tmp._M_alt);
281 #define __INSERT_REGEX_MATCHER(__func, args...)\
283 if (!(_M_flags & regex_constants::icase))\
284 if (!(_M_flags & regex_constants::collate))\
285 __func<false, false>(args);\
287 __func<false, true>(args);\
289 if (!(_M_flags & regex_constants::collate))\
290 __func<true, false>(args);\
292 __func<true, true>(args);\
295 template<typename _TraitsT>
297 _Compiler<_TraitsT>::
300 if (_M_match_token(_ScannerT::_S_token_anychar))
302 if (!(_M_flags & regex_constants::ECMAScript))
303 __INSERT_REGEX_MATCHER(_M_insert_any_matcher_posix);
305 __INSERT_REGEX_MATCHER(_M_insert_any_matcher_ecma);
307 else if (_M_try_char())
308 __INSERT_REGEX_MATCHER(_M_insert_char_matcher);
309 else if (_M_match_token(_ScannerT::_S_token_backref))
310 _M_stack.push(_StateSeqT(_M_nfa, _M_nfa.
311 _M_insert_backref(_M_cur_int_value(10))));
312 else if (_M_match_token(_ScannerT::_S_token_quoted_class))
313 __INSERT_REGEX_MATCHER(_M_insert_character_class_matcher);
314 else if (_M_match_token(_ScannerT::_S_token_subexpr_no_group_begin))
316 _StateSeqT __r(_M_nfa, _M_nfa._M_insert_dummy());
317 this->_M_disjunction();
318 if (!_M_match_token(_ScannerT::_S_token_subexpr_end))
319 __throw_regex_error(regex_constants::error_paren);
320 __r._M_append(_M_pop());
323 else if (_M_match_token(_ScannerT::_S_token_subexpr_begin))
325 _StateSeqT __r(_M_nfa, _M_nfa._M_insert_subexpr_begin());
326 this->_M_disjunction();
327 if (!_M_match_token(_ScannerT::_S_token_subexpr_end))
328 __throw_regex_error(regex_constants::error_paren);
329 __r._M_append(_M_pop());
330 __r._M_append(_M_nfa._M_insert_subexpr_end());
333 else if (!_M_bracket_expression())
338 template<typename _TraitsT>
340 _Compiler<_TraitsT>::
341 _M_bracket_expression()
344 _M_match_token(_ScannerT::_S_token_bracket_neg_begin);
345 if (!(__neg || _M_match_token(_ScannerT::_S_token_bracket_begin)))
347 __INSERT_REGEX_MATCHER(_M_insert_bracket_matcher, __neg);
350 #undef __INSERT_REGEX_MATCHER
352 template<typename _TraitsT>
353 template<bool __icase, bool __collate>
355 _Compiler<_TraitsT>::
356 _M_insert_any_matcher_ecma()
358 _M_stack.push(_StateSeqT(_M_nfa,
359 _M_nfa._M_insert_matcher
360 (_AnyMatcher<_TraitsT, true, __icase, __collate>
364 template<typename _TraitsT>
365 template<bool __icase, bool __collate>
367 _Compiler<_TraitsT>::
368 _M_insert_any_matcher_posix()
370 _M_stack.push(_StateSeqT(_M_nfa,
371 _M_nfa._M_insert_matcher
372 (_AnyMatcher<_TraitsT, false, __icase, __collate>
376 template<typename _TraitsT>
377 template<bool __icase, bool __collate>
379 _Compiler<_TraitsT>::
380 _M_insert_char_matcher()
382 _M_stack.push(_StateSeqT(_M_nfa,
383 _M_nfa._M_insert_matcher
384 (_CharMatcher<_TraitsT, __icase, __collate>
385 (_M_value[0], _M_traits))));
388 template<typename _TraitsT>
389 template<bool __icase, bool __collate>
391 _Compiler<_TraitsT>::
392 _M_insert_character_class_matcher()
394 _GLIBCXX_DEBUG_ASSERT(_M_value.size() == 1);
395 _BracketMatcher<_TraitsT, __icase, __collate> __matcher
396 (_M_ctype.is(_CtypeT::upper, _M_value[0]), _M_traits);
397 __matcher._M_add_character_class(_M_value);
398 __matcher._M_ready();
399 _M_stack.push(_StateSeqT(_M_nfa,
400 _M_nfa._M_insert_matcher(std::move(__matcher))));
403 template<typename _TraitsT>
404 template<bool __icase, bool __collate>
406 _Compiler<_TraitsT>::
407 _M_insert_bracket_matcher(bool __neg)
409 _BracketMatcher<_TraitsT, __icase, __collate> __matcher(__neg, _M_traits);
410 while (!_M_match_token(_ScannerT::_S_token_bracket_end))
411 _M_expression_term(__matcher);
412 __matcher._M_ready();
413 _M_stack.push(_StateSeqT(_M_nfa,
414 _M_nfa._M_insert_matcher(std::move(__matcher))));
417 template<typename _TraitsT>
418 template<bool __icase, bool __collate>
420 _Compiler<_TraitsT>::
421 _M_expression_term(_BracketMatcher<_TraitsT, __icase, __collate>& __matcher)
423 if (_M_match_token(_ScannerT::_S_token_collsymbol))
424 __matcher._M_add_collating_element(_M_value);
425 else if (_M_match_token(_ScannerT::_S_token_equiv_class_name))
426 __matcher._M_add_equivalence_class(_M_value);
427 else if (_M_match_token(_ScannerT::_S_token_char_class_name))
428 __matcher._M_add_character_class(_M_value);
429 else if (_M_try_char()) // [a
431 auto __ch = _M_value[0];
434 if (_M_value[0] == '-') // [a-
436 if (_M_try_char()) // [a-z]
438 __matcher._M_make_range(__ch, _M_value[0]);
441 // If the dash is the last character in the bracket
442 // expression, it is not special.
443 if (_M_scanner._M_get_token()
444 != _ScannerT::_S_token_bracket_end)
445 __throw_regex_error(regex_constants::error_range);
447 __matcher._M_add_char(_M_value[0]);
449 __matcher._M_add_char(__ch);
452 __throw_regex_error(regex_constants::error_brack);
455 template<typename _TraitsT>
457 _Compiler<_TraitsT>::
460 bool __is_char = false;
461 if (_M_match_token(_ScannerT::_S_token_oct_num))
464 _M_value.assign(1, _M_cur_int_value(8));
466 else if (_M_match_token(_ScannerT::_S_token_hex_num))
469 _M_value.assign(1, _M_cur_int_value(16));
471 else if (_M_match_token(_ScannerT::_S_token_ord_char))
476 template<typename _TraitsT>
478 _Compiler<_TraitsT>::
479 _M_match_token(_TokenT token)
481 if (token == _M_scanner._M_get_token())
483 _M_value = _M_scanner._M_get_value();
484 _M_scanner._M_advance();
490 template<typename _TraitsT>
492 _Compiler<_TraitsT>::
493 _M_cur_int_value(int __radix)
496 for (typename _StringT::size_type __i = 0;
497 __i < _M_value.length(); ++__i)
498 __v =__v * __radix + _M_traits.value(_M_value[__i], __radix);
502 template<typename _TraitsT, bool __icase, bool __collate>
504 _BracketMatcher<_TraitsT, __icase, __collate>::
505 _M_apply(_CharT __ch, false_type) const
508 if (std::find(_M_char_set.begin(), _M_char_set.end(),
509 _M_translator._M_translate(__ch))
510 != _M_char_set.end())
514 auto __s = _M_translator._M_transform(__ch);
515 for (auto& __it : _M_range_set)
516 if (__it.first <= __s && __s <= __it.second)
521 if (_M_traits.isctype(__ch, _M_class_set))
523 else if (std::find(_M_equiv_set.begin(), _M_equiv_set.end(),
524 _M_traits.transform_primary(&__ch, &__ch+1))
525 != _M_equiv_set.end())
528 if (_M_is_non_matching)
534 _GLIBCXX_END_NAMESPACE_VERSION
535 } // namespace __detail