regex.h (regex_token_iterator<>::regex_token_iterator): Fix initialization orders...
[gcc.git] / libstdc++-v3 / include / bits / regex_compiler.tcc
1 // class template regex -*- C++ -*-
2
3 // Copyright (C) 2013 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10
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.
15
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.
19
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/>.
24
25 /**
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}
29 */
30
31 // FIXME make comments doxygen format.
32
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.
36 //
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.
39 //
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
42 // this operator:
43 //
44 // branch1
45 // / \
46 // => begin_tag end_tag =>
47 // \ /
48 // branch2
49 //
50 // This is the difference between this implementation and that in Russ's
51 // article.
52 //
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.
55
56 namespace std _GLIBCXX_VISIBILITY(default)
57 {
58 namespace __detail
59 {
60 _GLIBCXX_BEGIN_NAMESPACE_VERSION
61
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_flags((__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))
73 ? __flags
74 : __flags | regex_constants::ECMAScript),
75 _M_traits(__traits),
76 _M_ctype(std::use_facet<std::ctype<_CharT>>(_M_traits.getloc())),
77 _M_scanner(__b, __e, _M_flags, _M_traits.getloc()),
78 _M_nfa(_M_flags)
79 {
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();
90 }
91
92 template<typename _FwdIter, typename _CharT, typename _TraitsT>
93 void
94 _Compiler<_FwdIter, _CharT, _TraitsT>::
95 _M_disjunction()
96 {
97 this->_M_alternative();
98 while (_M_match_token(_ScannerT::_S_token_or))
99 {
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),
109 __end));
110 }
111 }
112
113 template<typename _FwdIter, typename _CharT, typename _TraitsT>
114 void
115 _Compiler<_FwdIter, _CharT, _TraitsT>::
116 _M_alternative()
117 {
118 if (this->_M_term())
119 {
120 _StateSeqT __re = _M_pop();
121 this->_M_alternative();
122 __re._M_append(_M_pop());
123 _M_stack.push(__re);
124 }
125 else
126 _M_stack.push(_StateSeqT(_M_nfa, _M_nfa._M_insert_dummy()));
127 }
128
129 template<typename _FwdIter, typename _CharT, typename _TraitsT>
130 bool
131 _Compiler<_FwdIter, _CharT, _TraitsT>::
132 _M_term()
133 {
134 if (this->_M_assertion())
135 return true;
136 if (this->_M_atom())
137 {
138 this->_M_quantifier();
139 return true;
140 }
141 return false;
142 }
143
144 template<typename _FwdIter, typename _CharT, typename _TraitsT>
145 bool
146 _Compiler<_FwdIter, _CharT, _TraitsT>::
147 _M_assertion()
148 {
149 if (_M_match_token(_ScannerT::_S_token_line_begin))
150 _M_stack.push(_StateSeqT(_M_nfa, _M_nfa.
151 _M_insert_line_begin()));
152 else if (_M_match_token(_ScannerT::_S_token_line_end))
153 _M_stack.push(_StateSeqT(_M_nfa, _M_nfa.
154 _M_insert_line_end()));
155 else if (_M_match_token(_ScannerT::_S_token_word_bound))
156 // _M_value[0] == 'n' means it's negtive, say "not word boundary".
157 _M_stack.push(_StateSeqT(_M_nfa, _M_nfa.
158 _M_insert_word_bound(_M_value[0] == 'n')));
159 else if (_M_match_token(_ScannerT::_S_token_subexpr_lookahead_begin))
160 {
161 auto __neg = _M_value[0] == 'n';
162 this->_M_disjunction();
163 if (!_M_match_token(_ScannerT::_S_token_subexpr_end))
164 __throw_regex_error(regex_constants::error_paren);
165 auto __tmp = _M_pop();
166 __tmp._M_append(_M_nfa._M_insert_accept());
167 _M_stack.push(
168 _StateSeqT(
169 _M_nfa,
170 _M_nfa._M_insert_lookahead(__tmp._M_start, __neg)));
171 }
172 else
173 return false;
174 return true;
175 }
176
177 template<typename _FwdIter, typename _CharT, typename _TraitsT>
178 void
179 _Compiler<_FwdIter, _CharT, _TraitsT>::
180 _M_quantifier()
181 {
182 bool __neg = (_M_flags & regex_constants::ECMAScript);
183 auto __init = [this, &__neg]()
184 {
185 if (_M_stack.empty())
186 __throw_regex_error(regex_constants::error_badrepeat);
187 __neg = __neg && _M_match_token(_ScannerT::_S_token_opt);
188 };
189 if (_M_match_token(_ScannerT::_S_token_closure0))
190 {
191 __init();
192 auto __e = _M_pop();
193 _StateSeqT __r(_M_nfa, _M_nfa._M_insert_alt(_S_invalid_state_id,
194 __e._M_start, __neg));
195 __e._M_append(__r);
196 _M_stack.push(__r);
197 }
198 else if (_M_match_token(_ScannerT::_S_token_closure1))
199 {
200 __init();
201 auto __e = _M_pop();
202 __e._M_append(_M_nfa._M_insert_alt(_S_invalid_state_id, __e._M_start,
203 __neg));
204 _M_stack.push(__e);
205 }
206 else if (_M_match_token(_ScannerT::_S_token_opt))
207 {
208 __init();
209 auto __e = _M_pop();
210 auto __end = _M_nfa._M_insert_dummy();
211 _StateSeqT __r(_M_nfa, _M_nfa._M_insert_alt(_S_invalid_state_id,
212 __e._M_start, __neg));
213 __e._M_append(__end);
214 __r._M_append(__end);
215 _M_stack.push(__r);
216 }
217 else if (_M_match_token(_ScannerT::_S_token_interval_begin))
218 {
219 if (_M_stack.empty())
220 __throw_regex_error(regex_constants::error_badrepeat);
221 if (!_M_match_token(_ScannerT::_S_token_dup_count))
222 __throw_regex_error(regex_constants::error_badbrace);
223 _StateSeqT __r(_M_pop());
224 _StateSeqT __e(_M_nfa, _M_nfa._M_insert_dummy());
225 long __min_rep = _M_cur_int_value(10);
226 bool __infi = false;
227 long __n;
228
229 // {3
230 if (_M_match_token(_ScannerT::_S_token_comma))
231 if (_M_match_token(_ScannerT::_S_token_dup_count)) // {3,7}
232 __n = _M_cur_int_value(10) - __min_rep;
233 else
234 __infi = true;
235 else
236 __n = 0;
237 if (!_M_match_token(_ScannerT::_S_token_interval_end))
238 __throw_regex_error(regex_constants::error_brace);
239
240 __neg = __neg && _M_match_token(_ScannerT::_S_token_opt);
241
242 for (long __i = 0; __i < __min_rep; ++__i)
243 __e._M_append(__r._M_clone());
244
245 if (__infi)
246 {
247 auto __tmp = __r._M_clone();
248 _StateSeqT __s(_M_nfa,
249 _M_nfa._M_insert_alt(_S_invalid_state_id,
250 __tmp._M_start, __neg));
251 __tmp._M_append(__s);
252 __e._M_append(__s);
253 }
254 else
255 {
256 if (__n < 0)
257 __throw_regex_error(regex_constants::error_badbrace);
258 auto __end = _M_nfa._M_insert_dummy();
259 // _M_alt is the "match more" branch, and _M_next is the
260 // "match less" one. Switch _M_alt and _M_next of all created
261 // nodes. This is a hacking but IMO works well.
262 std::stack<_StateIdT> __stack;
263 for (long __i = 0; __i < __n; ++__i)
264 {
265 auto __tmp = __r._M_clone();
266 auto __alt = _M_nfa._M_insert_alt(__tmp._M_start,
267 __end, __neg);
268 __stack.push(__alt);
269 __e._M_append(_StateSeqT(_M_nfa, __alt, __tmp._M_end));
270 }
271 __e._M_append(__end);
272 while (!__stack.empty())
273 {
274 auto& __tmp = _M_nfa[__stack.top()];
275 __stack.pop();
276 swap(__tmp._M_next, __tmp._M_alt);
277 }
278 }
279 _M_stack.push(__e);
280 }
281 }
282
283 template<typename _FwdIter, typename _CharT, typename _TraitsT>
284 bool
285 _Compiler<_FwdIter, _CharT, _TraitsT>::
286 _M_atom()
287 {
288 if (_M_match_token(_ScannerT::_S_token_anychar))
289 _M_stack.push(_StateSeqT(_M_nfa,
290 _M_nfa._M_insert_matcher
291 (_AnyMatcher<_CharT, _TraitsT>(_M_traits))));
292 else if (_M_try_char())
293 _M_stack.push(_StateSeqT(_M_nfa,
294 _M_nfa._M_insert_matcher
295 (_CharMatcher<_CharT, _TraitsT>(_M_value[0],
296 _M_traits,
297 _M_flags))));
298 else if (_M_match_token(_ScannerT::_S_token_backref))
299 _M_stack.push(_StateSeqT(_M_nfa, _M_nfa.
300 _M_insert_backref(_M_cur_int_value(10))));
301 else if (_M_match_token(_ScannerT::_S_token_quoted_class))
302 {
303 _GLIBCXX_DEBUG_ASSERT(_M_value.size() == 1);
304 _BMatcherT __matcher(_M_ctype.is(_CtypeT::upper, _M_value[0]),
305 _M_traits, _M_flags);
306 __matcher._M_add_character_class(_M_value);
307 _M_stack.push(_StateSeqT(_M_nfa,
308 _M_nfa._M_insert_matcher(__matcher)));
309 }
310 else if (_M_match_token(_ScannerT::_S_token_subexpr_no_group_begin))
311 {
312 _StateSeqT __r(_M_nfa, _M_nfa._M_insert_dummy());
313 this->_M_disjunction();
314 if (!_M_match_token(_ScannerT::_S_token_subexpr_end))
315 __throw_regex_error(regex_constants::error_paren);
316 __r._M_append(_M_pop());
317 _M_stack.push(__r);
318 }
319 else if (_M_match_token(_ScannerT::_S_token_subexpr_begin))
320 {
321 _StateSeqT __r(_M_nfa, _M_nfa._M_insert_subexpr_begin());
322 this->_M_disjunction();
323 if (!_M_match_token(_ScannerT::_S_token_subexpr_end))
324 __throw_regex_error(regex_constants::error_paren);
325 __r._M_append(_M_pop());
326 __r._M_append(_M_nfa._M_insert_subexpr_end());
327 _M_stack.push(__r);
328 }
329 else if (!_M_bracket_expression())
330 return false;
331 return true;
332 }
333
334 template<typename _FwdIter, typename _CharT, typename _TraitsT>
335 bool
336 _Compiler<_FwdIter, _CharT, _TraitsT>::
337 _M_bracket_expression()
338 {
339 bool __neg =
340 _M_match_token(_ScannerT::_S_token_bracket_neg_begin);
341 if (!(__neg || _M_match_token(_ScannerT::_S_token_bracket_begin)))
342 return false;
343 _BMatcherT __matcher(__neg, _M_traits, _M_flags);
344 while (!_M_match_token(_ScannerT::_S_token_bracket_end))
345 _M_expression_term(__matcher);
346 _M_stack.push(_StateSeqT(_M_nfa, _M_nfa._M_insert_matcher(__matcher)));
347 return true;
348 }
349
350 template<typename _FwdIter, typename _CharT, typename _TraitsT>
351 void
352 _Compiler<_FwdIter, _CharT, _TraitsT>::
353 _M_expression_term(_BMatcherT& __matcher)
354 {
355 if (_M_match_token(_ScannerT::_S_token_collsymbol))
356 __matcher._M_add_collating_element(_M_value);
357 else if (_M_match_token(_ScannerT::_S_token_equiv_class_name))
358 __matcher._M_add_equivalence_class(_M_value);
359 else if (_M_match_token(_ScannerT::_S_token_char_class_name))
360 __matcher._M_add_character_class(_M_value);
361 else if (_M_try_char()) // [a
362 {
363 auto __ch = _M_value[0];
364 if (_M_try_char())
365 {
366 if (_M_value[0] == '-') // [a-
367 {
368 if (_M_try_char()) // [a-z]
369 {
370 __matcher._M_make_range(__ch, _M_value[0]);
371 return;
372 }
373 // If the dash is the last character in the bracket
374 // expression, it is not special.
375 if (_M_scanner._M_get_token()
376 != _ScannerT::_S_token_bracket_end)
377 __throw_regex_error(regex_constants::error_range);
378 }
379 __matcher._M_add_char(_M_value[0]);
380 }
381 __matcher._M_add_char(__ch);
382 }
383 else
384 __throw_regex_error(regex_constants::error_brack);
385 }
386
387 template<typename _FwdIter, typename _CharT, typename _TraitsT>
388 bool
389 _Compiler<_FwdIter, _CharT, _TraitsT>::
390 _M_try_char()
391 {
392 bool __is_char = false;
393 if (_M_match_token(_ScannerT::_S_token_oct_num))
394 {
395 __is_char = true;
396 _M_value.assign(1, _M_cur_int_value(8));
397 }
398 else if (_M_match_token(_ScannerT::_S_token_hex_num))
399 {
400 __is_char = true;
401 _M_value.assign(1, _M_cur_int_value(16));
402 }
403 else if (_M_match_token(_ScannerT::_S_token_ord_char))
404 __is_char = true;
405 return __is_char;
406 }
407
408 template<typename _FwdIter, typename _CharT, typename _TraitsT>
409 bool
410 _Compiler<_FwdIter, _CharT, _TraitsT>::
411 _M_match_token(_TokenT token)
412 {
413 if (token == _M_scanner._M_get_token())
414 {
415 _M_value = _M_scanner._M_get_value();
416 _M_scanner._M_advance();
417 return true;
418 }
419 return false;
420 }
421
422 template<typename _FwdIter, typename _CharT, typename _TraitsT>
423 int
424 _Compiler<_FwdIter, _CharT, _TraitsT>::
425 _M_cur_int_value(int __radix)
426 {
427 long __v = 0;
428 for (typename _StringT::size_type __i = 0;
429 __i < _M_value.length(); ++__i)
430 __v =__v * __radix + _M_traits.value(_M_value[__i], __radix);
431 return __v;
432 }
433
434 template<typename _CharT, typename _TraitsT>
435 bool _BracketMatcher<_CharT, _TraitsT>::
436 operator()(_CharT __ch) const
437 {
438 bool __ret = false;
439 if (_M_traits.isctype(__ch, _M_class_set)
440 || _M_char_set.count(_M_translate(__ch))
441 || _M_equiv_set.count(_M_traits.transform_primary(&__ch, &__ch+1)))
442 __ret = true;
443 else
444 {
445 _StringT __s = _M_get_str(_M_flags & regex_constants::collate
446 ? _M_translate(__ch) : __ch);
447 for (auto& __it : _M_range_set)
448 if (__it.first <= __s && __s <= __it.second)
449 {
450 __ret = true;
451 break;
452 }
453 }
454 if (_M_is_non_matching)
455 return !__ret;
456 else
457 return __ret;
458 }
459
460 _GLIBCXX_END_NAMESPACE_VERSION
461 } // namespace __detail
462 } // namespace