1 // class template regex -*- C++ -*-
3 // Copyright (C) 2013 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 _FwdIter, typename _CharT, typename _TraitsT>
63 _Compiler<_FwdIter, _CharT, _TraitsT>::
64 _Compiler(_FwdIter __b, _FwdIter __e,
65 const _TraitsT& __traits, _FlagT __flags)
66 : _M_traits(__traits), _M_scanner(__b, __e, __flags, _M_traits.getloc()),
67 _M_ctype(std::use_facet<std::ctype<_CharT>>(_M_traits.getloc())),
68 _M_nfa(__flags), _M_flags(__flags)
70 _StateSeqT __r(_M_nfa, _M_nfa._M_start());
71 __r._M_append(_M_nfa._M_insert_subexpr_begin());
72 this->_M_disjunction();
73 if (!_M_match_token(_ScannerT::_S_token_eof))
74 __throw_regex_error(regex_constants::error_paren);
75 __r._M_append(_M_pop());
76 _GLIBCXX_DEBUG_ASSERT(_M_stack.empty());
77 __r._M_append(_M_nfa._M_insert_subexpr_end());
78 __r._M_append(_M_nfa._M_insert_accept());
79 _M_nfa._M_eliminate_dummy();
82 template<typename _FwdIter, typename _CharT, typename _TraitsT>
84 _Compiler<_FwdIter, _CharT, _TraitsT>::
87 this->_M_alternative();
88 // TODO empty alternative like, um, "(|asdf)"
89 while (_M_match_token(_ScannerT::_S_token_or))
91 _StateSeqT __alt1 = _M_pop();
92 this->_M_alternative();
93 _StateSeqT __alt2 = _M_pop();
94 auto __end = _M_nfa._M_insert_dummy();
95 __alt1._M_append(__end);
96 __alt2._M_append(__end);
97 _M_stack.push(_StateSeqT(_M_nfa,
98 _M_nfa._M_insert_alt(__alt1._M_start,
99 __alt2._M_start, false),
104 template<typename _FwdIter, typename _CharT, typename _TraitsT>
106 _Compiler<_FwdIter, _CharT, _TraitsT>::
111 _StateSeqT __re = _M_pop();
112 this->_M_alternative();
113 __re._M_append(_M_pop());
117 _M_stack.push(_StateSeqT(_M_nfa, _M_nfa._M_insert_dummy()));
120 template<typename _FwdIter, typename _CharT, typename _TraitsT>
122 _Compiler<_FwdIter, _CharT, _TraitsT>::
125 if (this->_M_assertion())
129 this->_M_quantifier();
135 template<typename _FwdIter, typename _CharT, typename _TraitsT>
137 _Compiler<_FwdIter, _CharT, _TraitsT>::
140 if (_M_match_token(_ScannerT::_S_token_line_begin))
141 _M_stack.push(_StateSeqT(_M_nfa, _M_nfa.
142 _M_insert_line_begin()));
143 else if (_M_match_token(_ScannerT::_S_token_line_end))
144 _M_stack.push(_StateSeqT(_M_nfa, _M_nfa.
145 _M_insert_line_end()));
146 else if (_M_match_token(_ScannerT::_S_token_word_bound))
147 // _M_value[0] == 'n' means it's negtive, say "not word boundary".
148 _M_stack.push(_StateSeqT(_M_nfa, _M_nfa.
149 _M_insert_word_bound(_M_value[0] == 'n')));
150 else if (_M_match_token(_ScannerT::_S_token_subexpr_lookahead_begin))
152 auto __neg = _M_value[0] == 'n';
153 this->_M_disjunction();
154 if (!_M_match_token(_ScannerT::_S_token_subexpr_end))
155 __throw_regex_error(regex_constants::error_paren);
156 auto __tmp = _M_pop();
157 __tmp._M_append(_M_nfa._M_insert_accept());
161 _M_nfa._M_insert_lookahead(__tmp._M_start, __neg)));
168 template<typename _FwdIter, typename _CharT, typename _TraitsT>
170 _Compiler<_FwdIter, _CharT, _TraitsT>::
173 bool __neg = regex_constants::ECMAScript;
174 auto __init = [this, &__neg]()
176 if (_M_stack.empty())
177 __throw_regex_error(regex_constants::error_badrepeat);
178 __neg = __neg && _M_match_token(_ScannerT::_S_token_opt);
180 if (_M_match_token(_ScannerT::_S_token_closure0))
184 _StateSeqT __r(_M_nfa, _M_nfa._M_insert_alt(_S_invalid_state_id,
185 __e._M_start, __neg));
189 else if (_M_match_token(_ScannerT::_S_token_closure1))
193 __e._M_append(_M_nfa._M_insert_alt(_S_invalid_state_id, __e._M_start,
197 else if (_M_match_token(_ScannerT::_S_token_opt))
201 auto __end = _M_nfa._M_insert_dummy();
202 _StateSeqT __r(_M_nfa, _M_nfa._M_insert_alt(_S_invalid_state_id,
203 __e._M_start, __neg));
204 __e._M_append(__end);
205 __r._M_append(__end);
208 else if (_M_match_token(_ScannerT::_S_token_interval_begin))
211 if (!_M_match_token(_ScannerT::_S_token_dup_count))
212 __throw_regex_error(regex_constants::error_badbrace);
213 _StateSeqT __r(_M_pop());
214 _StateSeqT __e(_M_nfa, _M_nfa._M_insert_dummy());
215 int __min_rep = _M_cur_int_value(10);
217 for (int __i = 0; __i < __min_rep; ++__i)
218 __e._M_append(__r._M_clone());
219 if (_M_match_token(_ScannerT::_S_token_comma))
220 if (_M_match_token(_ScannerT::_S_token_dup_count)) // {3,7}
222 int __n = _M_cur_int_value(10) - __min_rep;
224 __throw_regex_error(regex_constants::error_badbrace);
225 auto __end = _M_nfa._M_insert_dummy();
226 // _M_alt is the "match more" branch, and _M_next is the
227 // "match less" one. Switch _M_alt and _M_next of all created
228 // nodes. This is a hacking but IMO works well.
229 std::stack<_StateIdT> __stack;
230 for (int __i = 0; __i < __n; ++__i)
232 auto __tmp = __r._M_clone();
233 auto __alt = _M_nfa._M_insert_alt(__tmp._M_start,
236 __e._M_append(_StateSeqT(_M_nfa, __alt, __tmp._M_end));
238 __e._M_append(__end);
239 while (!__stack.empty())
241 auto& __tmp = _M_nfa[__stack.top()];
243 swap(__tmp._M_next, __tmp._M_alt);
248 auto __tmp = __r._M_clone();
249 _StateSeqT __s(_M_nfa,
250 _M_nfa._M_insert_alt(_S_invalid_state_id,
251 __tmp._M_start, __neg));
252 __tmp._M_append(__s);
255 if (!_M_match_token(_ScannerT::_S_token_interval_end))
256 __throw_regex_error(regex_constants::error_brace);
261 template<typename _FwdIter, typename _CharT, typename _TraitsT>
263 _Compiler<_FwdIter, _CharT, _TraitsT>::
266 if (_M_match_token(_ScannerT::_S_token_anychar))
267 _M_stack.push(_StateSeqT(_M_nfa,
268 _M_nfa._M_insert_matcher
269 (_AnyMatcher<_CharT, _TraitsT>(_M_traits))));
270 else if (_M_try_char())
271 _M_stack.push(_StateSeqT(_M_nfa,
272 _M_nfa._M_insert_matcher
273 (_CharMatcher<_CharT, _TraitsT>(_M_value[0],
276 else if (_M_match_token(_ScannerT::_S_token_backref))
277 _M_stack.push(_StateSeqT(_M_nfa, _M_nfa.
278 _M_insert_backref(_M_cur_int_value(10))));
279 else if (_M_match_token(_ScannerT::_S_token_quoted_class))
281 _GLIBCXX_DEBUG_ASSERT(_M_value.size() == 1);
282 _BMatcherT __matcher(_M_ctype.is(_CtypeT::upper, _M_value[0]),
283 _M_traits, _M_flags);
284 __matcher._M_add_character_class(_M_value);
285 _M_stack.push(_StateSeqT(_M_nfa,
286 _M_nfa._M_insert_matcher(__matcher)));
288 else if (_M_match_token(_ScannerT::_S_token_subexpr_no_group_begin))
290 _StateSeqT __r(_M_nfa, _M_nfa._M_insert_dummy());
291 this->_M_disjunction();
292 if (!_M_match_token(_ScannerT::_S_token_subexpr_end))
293 __throw_regex_error(regex_constants::error_paren);
294 __r._M_append(_M_pop());
297 else if (_M_match_token(_ScannerT::_S_token_subexpr_begin))
299 int __mark = _M_nfa._M_sub_count();
300 _StateSeqT __r(_M_nfa, _M_nfa._M_insert_subexpr_begin());
301 this->_M_disjunction();
302 if (!_M_match_token(_ScannerT::_S_token_subexpr_end))
303 __throw_regex_error(regex_constants::error_paren);
304 __r._M_append(_M_pop());
305 __r._M_append(_M_nfa._M_insert_subexpr_end());
308 else if (!_M_bracket_expression())
313 template<typename _FwdIter, typename _CharT, typename _TraitsT>
315 _Compiler<_FwdIter, _CharT, _TraitsT>::
316 _M_bracket_expression()
319 _M_match_token(_ScannerT::_S_token_bracket_neg_begin);
320 if (!(__neg || _M_match_token(_ScannerT::_S_token_bracket_begin)))
322 _BMatcherT __matcher(__neg, _M_traits, _M_flags);
323 while (!_M_match_token(_ScannerT::_S_token_bracket_end))
324 _M_expression_term(__matcher);
325 _M_stack.push(_StateSeqT(_M_nfa, _M_nfa._M_insert_matcher(__matcher)));
329 template<typename _FwdIter, typename _CharT, typename _TraitsT>
331 _Compiler<_FwdIter, _CharT, _TraitsT>::
332 _M_expression_term(_BMatcherT& __matcher)
334 if (_M_match_token(_ScannerT::_S_token_collsymbol))
335 __matcher._M_add_collating_element(_M_value);
336 else if (_M_match_token(_ScannerT::_S_token_equiv_class_name))
337 __matcher._M_add_equivalence_class(_M_value);
338 else if (_M_match_token(_ScannerT::_S_token_char_class_name))
339 __matcher._M_add_character_class(_M_value);
340 else if (_M_try_char()) // [a
342 auto __ch = _M_value[0];
345 if (_M_value[0] == '-') // [a-
347 if (_M_try_char()) // [a-z]
349 __matcher._M_make_range(__ch, _M_value[0]);
352 // If the dash is the last character in the bracket
353 // expression, it is not special.
354 if (_M_scanner._M_get_token()
355 != _ScannerT::_S_token_bracket_end)
356 __throw_regex_error(regex_constants::error_range);
358 __matcher._M_add_char(_M_value[0]);
360 __matcher._M_add_char(__ch);
363 __throw_regex_error(regex_constants::error_brack);
366 template<typename _FwdIter, typename _CharT, typename _TraitsT>
368 _Compiler<_FwdIter, _CharT, _TraitsT>::
371 bool __is_char = false;
372 if (_M_match_token(_ScannerT::_S_token_oct_num))
375 _M_value.assign(1, _M_cur_int_value(8));
377 else if (_M_match_token(_ScannerT::_S_token_hex_num))
380 _M_value.assign(1, _M_cur_int_value(16));
382 else if (_M_match_token(_ScannerT::_S_token_ord_char))
387 template<typename _FwdIter, typename _CharT, typename _TraitsT>
389 _Compiler<_FwdIter, _CharT, _TraitsT>::
390 _M_match_token(_TokenT token)
392 if (token == _M_scanner._M_get_token())
394 _M_value = _M_scanner._M_get_value();
395 _M_scanner._M_advance();
401 template<typename _FwdIter, typename _CharT, typename _TraitsT>
403 _Compiler<_FwdIter, _CharT, _TraitsT>::
404 _M_cur_int_value(int __radix)
407 for (typename _StringT::size_type __i = 0;
408 __i < _M_value.length(); ++__i)
409 __v =__v * __radix + _M_traits.value(_M_value[__i], __radix);
413 template<typename _CharT, typename _TraitsT>
414 bool _BracketMatcher<_CharT, _TraitsT>::
415 operator()(_CharT __ch) const
418 if (_M_traits.isctype(__ch, _M_class_set))
420 else if (_M_char_set.count(_M_translate(__ch)))
424 _StringT __s = _M_get_str(_M_flags & regex_constants::collate
425 ? _M_translate(__ch) : __ch);
426 for (auto& __it : _M_range_set)
427 if (__it.first <= __s && __s <= __it.second)
433 if (_M_is_non_matching)
439 _GLIBCXX_END_NAMESPACE_VERSION
440 } // namespace __detail