]>
Commit | Line | Data |
---|---|---|
6cb784b6 TS |
1 | // class template regex -*- C++ -*- |
2 | ||
cbe34bb5 | 3 | // Copyright (C) 2013-2017 Free Software Foundation, Inc. |
6cb784b6 TS |
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 | ||
b21abcee | 31 | // FIXME make comments doxygen format. |
7c812a2a TS |
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 | // | |
097f0bcf JW |
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: | |
7c812a2a TS |
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 | ||
6cb784b6 TS |
56 | namespace std _GLIBCXX_VISIBILITY(default) |
57 | { | |
6cb784b6 TS |
58 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
59 | ||
4a15d842 FD |
60 | namespace __detail |
61 | { | |
ddf41e9d TS |
62 | template<typename _TraitsT> |
63 | _Compiler<_TraitsT>:: | |
64 | _Compiler(_IterT __b, _IterT __e, | |
2bde8cac | 65 | const typename _TraitsT::locale_type& __loc, _FlagT __flags) |
c2669da9 TS |
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), | |
2bde8cac TS |
75 | _M_scanner(__b, __e, _M_flags, __loc), |
76 | _M_nfa(make_shared<_RegexT>(__loc, _M_flags)), | |
77 | _M_traits(_M_nfa->_M_traits), | |
78 | _M_ctype(std::use_facet<_CtypeT>(__loc)) | |
6cb784b6 | 79 | { |
2bde8cac TS |
80 | _StateSeqT __r(*_M_nfa, _M_nfa->_M_start()); |
81 | __r._M_append(_M_nfa->_M_insert_subexpr_begin()); | |
7c812a2a TS |
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()); | |
2f1e8e7c | 86 | __glibcxx_assert(_M_stack.empty()); |
2bde8cac TS |
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(); | |
6cb784b6 TS |
90 | } |
91 | ||
ddf41e9d | 92 | template<typename _TraitsT> |
6cb784b6 | 93 | void |
ddf41e9d | 94 | _Compiler<_TraitsT>:: |
6cb784b6 TS |
95 | _M_disjunction() |
96 | { | |
97 | this->_M_alternative(); | |
7c812a2a | 98 | while (_M_match_token(_ScannerT::_S_token_or)) |
6cb784b6 | 99 | { |
7c812a2a TS |
100 | _StateSeqT __alt1 = _M_pop(); |
101 | this->_M_alternative(); | |
102 | _StateSeqT __alt2 = _M_pop(); | |
2bde8cac | 103 | auto __end = _M_nfa->_M_insert_dummy(); |
7c812a2a TS |
104 | __alt1._M_append(__end); |
105 | __alt2._M_append(__end); | |
ad9ec7b3 TS |
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. | |
2bde8cac TS |
109 | _M_stack.push(_StateSeqT(*_M_nfa, |
110 | _M_nfa->_M_insert_alt( | |
111 | __alt2._M_start, __alt1._M_start, false), | |
7c812a2a | 112 | __end)); |
6cb784b6 TS |
113 | } |
114 | } | |
115 | ||
ddf41e9d | 116 | template<typename _TraitsT> |
6cb784b6 | 117 | void |
ddf41e9d | 118 | _Compiler<_TraitsT>:: |
6cb784b6 TS |
119 | _M_alternative() |
120 | { | |
121 | if (this->_M_term()) | |
122 | { | |
7c812a2a | 123 | _StateSeqT __re = _M_pop(); |
6cb784b6 | 124 | this->_M_alternative(); |
7c812a2a | 125 | __re._M_append(_M_pop()); |
6cb784b6 TS |
126 | _M_stack.push(__re); |
127 | } | |
7c812a2a | 128 | else |
2bde8cac | 129 | _M_stack.push(_StateSeqT(*_M_nfa, _M_nfa->_M_insert_dummy())); |
6cb784b6 TS |
130 | } |
131 | ||
ddf41e9d | 132 | template<typename _TraitsT> |
6cb784b6 | 133 | bool |
ddf41e9d | 134 | _Compiler<_TraitsT>:: |
6cb784b6 TS |
135 | _M_term() |
136 | { | |
137 | if (this->_M_assertion()) | |
138 | return true; | |
139 | if (this->_M_atom()) | |
140 | { | |
053eb1f3 | 141 | while (this->_M_quantifier()); |
6cb784b6 TS |
142 | return true; |
143 | } | |
144 | return false; | |
145 | } | |
146 | ||
ddf41e9d | 147 | template<typename _TraitsT> |
6cb784b6 | 148 | bool |
ddf41e9d | 149 | _Compiler<_TraitsT>:: |
6cb784b6 TS |
150 | _M_assertion() |
151 | { | |
7c812a2a | 152 | if (_M_match_token(_ScannerT::_S_token_line_begin)) |
2bde8cac | 153 | _M_stack.push(_StateSeqT(*_M_nfa, _M_nfa->_M_insert_line_begin())); |
7c812a2a | 154 | else if (_M_match_token(_ScannerT::_S_token_line_end)) |
2bde8cac | 155 | _M_stack.push(_StateSeqT(*_M_nfa, _M_nfa->_M_insert_line_end())); |
7c812a2a | 156 | else if (_M_match_token(_ScannerT::_S_token_word_bound)) |
097f0bcf | 157 | // _M_value[0] == 'n' means it's negative, say "not word boundary". |
2bde8cac | 158 | _M_stack.push(_StateSeqT(*_M_nfa, _M_nfa-> |
7b86458e | 159 | _M_insert_word_bound(_M_value[0] == 'n'))); |
7c812a2a | 160 | else if (_M_match_token(_ScannerT::_S_token_subexpr_lookahead_begin)) |
7b86458e TS |
161 | { |
162 | auto __neg = _M_value[0] == 'n'; | |
163 | this->_M_disjunction(); | |
164 | if (!_M_match_token(_ScannerT::_S_token_subexpr_end)) | |
236d76c4 TS |
165 | __throw_regex_error(regex_constants::error_paren, |
166 | "Parenthesis is not closed."); | |
7b86458e | 167 | auto __tmp = _M_pop(); |
2bde8cac | 168 | __tmp._M_append(_M_nfa->_M_insert_accept()); |
7b86458e TS |
169 | _M_stack.push( |
170 | _StateSeqT( | |
2bde8cac TS |
171 | *_M_nfa, |
172 | _M_nfa->_M_insert_lookahead(__tmp._M_start, __neg))); | |
7b86458e | 173 | } |
7c812a2a TS |
174 | else |
175 | return false; | |
176 | return true; | |
6cb784b6 TS |
177 | } |
178 | ||
ddf41e9d | 179 | template<typename _TraitsT> |
053eb1f3 | 180 | bool |
ddf41e9d | 181 | _Compiler<_TraitsT>:: |
6cb784b6 TS |
182 | _M_quantifier() |
183 | { | |
c2669da9 | 184 | bool __neg = (_M_flags & regex_constants::ECMAScript); |
7b86458e | 185 | auto __init = [this, &__neg]() |
6cb784b6 TS |
186 | { |
187 | if (_M_stack.empty()) | |
236d76c4 TS |
188 | __throw_regex_error(regex_constants::error_badrepeat, |
189 | "Nothing to repeat before a quantifier."); | |
7b86458e TS |
190 | __neg = __neg && _M_match_token(_ScannerT::_S_token_opt); |
191 | }; | |
192 | if (_M_match_token(_ScannerT::_S_token_closure0)) | |
193 | { | |
194 | __init(); | |
7c812a2a | 195 | auto __e = _M_pop(); |
2bde8cac TS |
196 | _StateSeqT __r(*_M_nfa, |
197 | _M_nfa->_M_insert_repeat(_S_invalid_state_id, | |
198 | __e._M_start, __neg)); | |
7c812a2a | 199 | __e._M_append(__r); |
6cb784b6 | 200 | _M_stack.push(__r); |
6cb784b6 | 201 | } |
7c812a2a | 202 | else if (_M_match_token(_ScannerT::_S_token_closure1)) |
6cb784b6 | 203 | { |
7b86458e | 204 | __init(); |
7c812a2a | 205 | auto __e = _M_pop(); |
2bde8cac TS |
206 | __e._M_append(_M_nfa->_M_insert_repeat(_S_invalid_state_id, |
207 | __e._M_start, __neg)); | |
7c812a2a | 208 | _M_stack.push(__e); |
6cb784b6 | 209 | } |
7c812a2a | 210 | else if (_M_match_token(_ScannerT::_S_token_opt)) |
6cb784b6 | 211 | { |
7b86458e | 212 | __init(); |
7c812a2a | 213 | auto __e = _M_pop(); |
2bde8cac TS |
214 | auto __end = _M_nfa->_M_insert_dummy(); |
215 | _StateSeqT __r(*_M_nfa, | |
216 | _M_nfa->_M_insert_repeat(_S_invalid_state_id, | |
217 | __e._M_start, __neg)); | |
7c812a2a TS |
218 | __e._M_append(__end); |
219 | __r._M_append(__end); | |
6cb784b6 | 220 | _M_stack.push(__r); |
6cb784b6 | 221 | } |
7c812a2a | 222 | else if (_M_match_token(_ScannerT::_S_token_interval_begin)) |
6cb784b6 | 223 | { |
c2669da9 | 224 | if (_M_stack.empty()) |
236d76c4 TS |
225 | __throw_regex_error(regex_constants::error_badrepeat, |
226 | "Nothing to repeat before a quantifier."); | |
6cb784b6 | 227 | if (!_M_match_token(_ScannerT::_S_token_dup_count)) |
236d76c4 TS |
228 | __throw_regex_error(regex_constants::error_badbrace, |
229 | "Unexpected token in brace expression."); | |
7c812a2a | 230 | _StateSeqT __r(_M_pop()); |
2bde8cac | 231 | _StateSeqT __e(*_M_nfa, _M_nfa->_M_insert_dummy()); |
6cb43087 | 232 | long __min_rep = _M_cur_int_value(10); |
c2669da9 | 233 | bool __infi = false; |
6cb43087 | 234 | long __n; |
c2669da9 | 235 | |
7c812a2a | 236 | // {3 |
6cb784b6 | 237 | if (_M_match_token(_ScannerT::_S_token_comma)) |
7c812a2a | 238 | if (_M_match_token(_ScannerT::_S_token_dup_count)) // {3,7} |
c2669da9 TS |
239 | __n = _M_cur_int_value(10) - __min_rep; |
240 | else | |
241 | __infi = true; | |
242 | else | |
243 | __n = 0; | |
6cb784b6 | 244 | if (!_M_match_token(_ScannerT::_S_token_interval_end)) |
236d76c4 TS |
245 | __throw_regex_error(regex_constants::error_brace, |
246 | "Unexpected end of brace expression."); | |
c2669da9 TS |
247 | |
248 | __neg = __neg && _M_match_token(_ScannerT::_S_token_opt); | |
249 | ||
6cb43087 | 250 | for (long __i = 0; __i < __min_rep; ++__i) |
c2669da9 TS |
251 | __e._M_append(__r._M_clone()); |
252 | ||
253 | if (__infi) | |
254 | { | |
255 | auto __tmp = __r._M_clone(); | |
2bde8cac TS |
256 | _StateSeqT __s(*_M_nfa, |
257 | _M_nfa->_M_insert_repeat(_S_invalid_state_id, | |
258 | __tmp._M_start, __neg)); | |
c2669da9 TS |
259 | __tmp._M_append(__s); |
260 | __e._M_append(__s); | |
261 | } | |
262 | else | |
263 | { | |
264 | if (__n < 0) | |
236d76c4 TS |
265 | __throw_regex_error(regex_constants::error_badbrace, |
266 | "Invalid range in brace expression."); | |
2bde8cac | 267 | auto __end = _M_nfa->_M_insert_dummy(); |
c2669da9 TS |
268 | // _M_alt is the "match more" branch, and _M_next is the |
269 | // "match less" one. Switch _M_alt and _M_next of all created | |
097f0bcf | 270 | // nodes. This is a hack but IMO works well. |
c2669da9 | 271 | std::stack<_StateIdT> __stack; |
6cb43087 | 272 | for (long __i = 0; __i < __n; ++__i) |
c2669da9 TS |
273 | { |
274 | auto __tmp = __r._M_clone(); | |
2bde8cac TS |
275 | auto __alt = _M_nfa->_M_insert_repeat(__tmp._M_start, |
276 | __end, __neg); | |
c2669da9 | 277 | __stack.push(__alt); |
2bde8cac | 278 | __e._M_append(_StateSeqT(*_M_nfa, __alt, __tmp._M_end)); |
c2669da9 TS |
279 | } |
280 | __e._M_append(__end); | |
281 | while (!__stack.empty()) | |
282 | { | |
2bde8cac | 283 | auto& __tmp = (*_M_nfa)[__stack.top()]; |
c2669da9 | 284 | __stack.pop(); |
a8e67466 | 285 | std::swap(__tmp._M_next, __tmp._M_alt); |
c2669da9 TS |
286 | } |
287 | } | |
7c812a2a | 288 | _M_stack.push(__e); |
6cb784b6 | 289 | } |
053eb1f3 TS |
290 | else |
291 | return false; | |
292 | return true; | |
6cb784b6 TS |
293 | } |
294 | ||
ddf41e9d TS |
295 | #define __INSERT_REGEX_MATCHER(__func, args...)\ |
296 | do\ | |
297 | if (!(_M_flags & regex_constants::icase))\ | |
298 | if (!(_M_flags & regex_constants::collate))\ | |
299 | __func<false, false>(args);\ | |
300 | else\ | |
301 | __func<false, true>(args);\ | |
302 | else\ | |
303 | if (!(_M_flags & regex_constants::collate))\ | |
304 | __func<true, false>(args);\ | |
305 | else\ | |
306 | __func<true, true>(args);\ | |
307 | while (false) | |
308 | ||
309 | template<typename _TraitsT> | |
6cb784b6 | 310 | bool |
ddf41e9d | 311 | _Compiler<_TraitsT>:: |
6cb784b6 TS |
312 | _M_atom() |
313 | { | |
314 | if (_M_match_token(_ScannerT::_S_token_anychar)) | |
f43cc2a6 | 315 | { |
ddf41e9d TS |
316 | if (!(_M_flags & regex_constants::ECMAScript)) |
317 | __INSERT_REGEX_MATCHER(_M_insert_any_matcher_posix); | |
f43cc2a6 | 318 | else |
ddf41e9d | 319 | __INSERT_REGEX_MATCHER(_M_insert_any_matcher_ecma); |
f43cc2a6 | 320 | } |
7c812a2a | 321 | else if (_M_try_char()) |
ddf41e9d | 322 | __INSERT_REGEX_MATCHER(_M_insert_char_matcher); |
7c812a2a | 323 | else if (_M_match_token(_ScannerT::_S_token_backref)) |
2bde8cac | 324 | _M_stack.push(_StateSeqT(*_M_nfa, _M_nfa-> |
7c812a2a TS |
325 | _M_insert_backref(_M_cur_int_value(10)))); |
326 | else if (_M_match_token(_ScannerT::_S_token_quoted_class)) | |
ddf41e9d | 327 | __INSERT_REGEX_MATCHER(_M_insert_character_class_matcher); |
7c812a2a | 328 | else if (_M_match_token(_ScannerT::_S_token_subexpr_no_group_begin)) |
6cb784b6 | 329 | { |
2bde8cac | 330 | _StateSeqT __r(*_M_nfa, _M_nfa->_M_insert_dummy()); |
7c812a2a TS |
331 | this->_M_disjunction(); |
332 | if (!_M_match_token(_ScannerT::_S_token_subexpr_end)) | |
236d76c4 TS |
333 | __throw_regex_error(regex_constants::error_paren, |
334 | "Parenthesis is not closed."); | |
7c812a2a TS |
335 | __r._M_append(_M_pop()); |
336 | _M_stack.push(__r); | |
6cb784b6 | 337 | } |
7c812a2a | 338 | else if (_M_match_token(_ScannerT::_S_token_subexpr_begin)) |
6cb784b6 | 339 | { |
2bde8cac | 340 | _StateSeqT __r(*_M_nfa, _M_nfa->_M_insert_subexpr_begin()); |
6cb784b6 TS |
341 | this->_M_disjunction(); |
342 | if (!_M_match_token(_ScannerT::_S_token_subexpr_end)) | |
236d76c4 TS |
343 | __throw_regex_error(regex_constants::error_paren, |
344 | "Parenthesis is not closed."); | |
7c812a2a | 345 | __r._M_append(_M_pop()); |
2bde8cac | 346 | __r._M_append(_M_nfa->_M_insert_subexpr_end()); |
6cb784b6 | 347 | _M_stack.push(__r); |
6cb784b6 | 348 | } |
7c812a2a TS |
349 | else if (!_M_bracket_expression()) |
350 | return false; | |
351 | return true; | |
6cb784b6 TS |
352 | } |
353 | ||
ddf41e9d | 354 | template<typename _TraitsT> |
6cb784b6 | 355 | bool |
ddf41e9d | 356 | _Compiler<_TraitsT>:: |
6cb784b6 TS |
357 | _M_bracket_expression() |
358 | { | |
33fbbb76 TS |
359 | bool __neg = |
360 | _M_match_token(_ScannerT::_S_token_bracket_neg_begin); | |
361 | if (!(__neg || _M_match_token(_ScannerT::_S_token_bracket_begin))) | |
e280b6ff | 362 | return false; |
ddf41e9d TS |
363 | __INSERT_REGEX_MATCHER(_M_insert_bracket_matcher, __neg); |
364 | return true; | |
365 | } | |
366 | #undef __INSERT_REGEX_MATCHER | |
367 | ||
368 | template<typename _TraitsT> | |
369 | template<bool __icase, bool __collate> | |
370 | void | |
371 | _Compiler<_TraitsT>:: | |
372 | _M_insert_any_matcher_ecma() | |
373 | { | |
2bde8cac TS |
374 | _M_stack.push(_StateSeqT(*_M_nfa, |
375 | _M_nfa->_M_insert_matcher | |
ddf41e9d TS |
376 | (_AnyMatcher<_TraitsT, true, __icase, __collate> |
377 | (_M_traits)))); | |
378 | } | |
379 | ||
380 | template<typename _TraitsT> | |
381 | template<bool __icase, bool __collate> | |
382 | void | |
383 | _Compiler<_TraitsT>:: | |
384 | _M_insert_any_matcher_posix() | |
385 | { | |
2bde8cac TS |
386 | _M_stack.push(_StateSeqT(*_M_nfa, |
387 | _M_nfa->_M_insert_matcher | |
ddf41e9d TS |
388 | (_AnyMatcher<_TraitsT, false, __icase, __collate> |
389 | (_M_traits)))); | |
390 | } | |
391 | ||
392 | template<typename _TraitsT> | |
393 | template<bool __icase, bool __collate> | |
394 | void | |
395 | _Compiler<_TraitsT>:: | |
396 | _M_insert_char_matcher() | |
397 | { | |
2bde8cac TS |
398 | _M_stack.push(_StateSeqT(*_M_nfa, |
399 | _M_nfa->_M_insert_matcher | |
ddf41e9d TS |
400 | (_CharMatcher<_TraitsT, __icase, __collate> |
401 | (_M_value[0], _M_traits)))); | |
402 | } | |
403 | ||
404 | template<typename _TraitsT> | |
405 | template<bool __icase, bool __collate> | |
406 | void | |
407 | _Compiler<_TraitsT>:: | |
408 | _M_insert_character_class_matcher() | |
409 | { | |
2f1e8e7c | 410 | __glibcxx_assert(_M_value.size() == 1); |
ddf41e9d TS |
411 | _BracketMatcher<_TraitsT, __icase, __collate> __matcher |
412 | (_M_ctype.is(_CtypeT::upper, _M_value[0]), _M_traits); | |
4dae67e0 | 413 | __matcher._M_add_character_class(_M_value, false); |
ddf41e9d | 414 | __matcher._M_ready(); |
2bde8cac TS |
415 | _M_stack.push(_StateSeqT(*_M_nfa, |
416 | _M_nfa->_M_insert_matcher(std::move(__matcher)))); | |
ddf41e9d TS |
417 | } |
418 | ||
419 | template<typename _TraitsT> | |
420 | template<bool __icase, bool __collate> | |
421 | void | |
422 | _Compiler<_TraitsT>:: | |
423 | _M_insert_bracket_matcher(bool __neg) | |
424 | { | |
425 | _BracketMatcher<_TraitsT, __icase, __collate> __matcher(__neg, _M_traits); | |
79b576cc TS |
426 | pair<bool, _CharT> __last_char; // Optional<_CharT> |
427 | __last_char.first = false; | |
428 | if (!(_M_flags & regex_constants::ECMAScript)) | |
4aebb4e4 TS |
429 | { |
430 | if (_M_try_char()) | |
431 | { | |
432 | __last_char.first = true; | |
433 | __last_char.second = _M_value[0]; | |
434 | } | |
435 | else if (_M_match_token(_ScannerT::_S_token_bracket_dash)) | |
436 | { | |
437 | __last_char.first = true; | |
438 | __last_char.second = '-'; | |
439 | } | |
440 | } | |
f9ce3c16 | 441 | while (_M_expression_term(__last_char, __matcher)); |
4aebb4e4 TS |
442 | if (__last_char.first) |
443 | __matcher._M_add_char(__last_char.second); | |
f43cc2a6 | 444 | __matcher._M_ready(); |
2bde8cac TS |
445 | _M_stack.push(_StateSeqT( |
446 | *_M_nfa, | |
447 | _M_nfa->_M_insert_matcher(std::move(__matcher)))); | |
6cb784b6 TS |
448 | } |
449 | ||
ddf41e9d TS |
450 | template<typename _TraitsT> |
451 | template<bool __icase, bool __collate> | |
f9ce3c16 | 452 | bool |
ddf41e9d | 453 | _Compiler<_TraitsT>:: |
79b576cc TS |
454 | _M_expression_term(pair<bool, _CharT>& __last_char, |
455 | _BracketMatcher<_TraitsT, __icase, __collate>& __matcher) | |
6cb784b6 | 456 | { |
f9ce3c16 TS |
457 | if (_M_match_token(_ScannerT::_S_token_bracket_end)) |
458 | return false; | |
459 | ||
4aebb4e4 TS |
460 | const auto __push_char = [&](_CharT __ch) |
461 | { | |
462 | if (__last_char.first) | |
463 | __matcher._M_add_char(__last_char.second); | |
464 | else | |
465 | __last_char.first = true; | |
466 | __last_char.second = __ch; | |
467 | }; | |
468 | const auto __flush = [&] | |
469 | { | |
470 | if (__last_char.first) | |
471 | { | |
472 | __matcher._M_add_char(__last_char.second); | |
473 | __last_char.first = false; | |
474 | } | |
475 | }; | |
476 | ||
6cb784b6 | 477 | if (_M_match_token(_ScannerT::_S_token_collsymbol)) |
f9ce3c16 TS |
478 | { |
479 | auto __symbol = __matcher._M_add_collate_element(_M_value); | |
480 | if (__symbol.size() == 1) | |
4aebb4e4 TS |
481 | __push_char(__symbol[0]); |
482 | else | |
483 | __flush(); | |
f9ce3c16 | 484 | } |
7c812a2a | 485 | else if (_M_match_token(_ScannerT::_S_token_equiv_class_name)) |
4aebb4e4 TS |
486 | { |
487 | __flush(); | |
488 | __matcher._M_add_equivalence_class(_M_value); | |
489 | } | |
7c812a2a | 490 | else if (_M_match_token(_ScannerT::_S_token_char_class_name)) |
4aebb4e4 TS |
491 | { |
492 | __flush(); | |
493 | __matcher._M_add_character_class(_M_value, false); | |
494 | } | |
495 | else if (_M_try_char()) | |
496 | __push_char(_M_value[0]); | |
f9ce3c16 TS |
497 | // POSIX doesn't allow '-' as a start-range char (say [a-z--0]), |
498 | // except when the '-' is the first or last character in the bracket | |
499 | // expression ([--0]). ECMAScript treats all '-' after a range as a | |
500 | // normal character. Also see above, where _M_expression_term gets called. | |
79b576cc TS |
501 | // |
502 | // As a result, POSIX rejects [-----], but ECMAScript doesn't. | |
503 | // Boost (1.57.0) always uses POSIX style even in its ECMAScript syntax. | |
504 | // Clang (3.5) always uses ECMAScript style even in its POSIX syntax. | |
505 | // | |
506 | // It turns out that no one reads BNFs ;) | |
4aebb4e4 | 507 | else if (_M_match_token(_ScannerT::_S_token_bracket_dash)) |
e280b6ff | 508 | { |
79b576cc TS |
509 | if (!__last_char.first) |
510 | { | |
4aebb4e4 | 511 | if (!(_M_flags & regex_constants::ECMAScript)) |
f9ce3c16 TS |
512 | { |
513 | if (_M_match_token(_ScannerT::_S_token_bracket_end)) | |
4aebb4e4 TS |
514 | { |
515 | __push_char('-'); | |
516 | return false; | |
517 | } | |
236d76c4 TS |
518 | __throw_regex_error( |
519 | regex_constants::error_range, | |
520 | "Unexpected dash in bracket expression. For POSIX syntax, " | |
521 | "a dash is not treated literally only when it is at " | |
522 | "beginning or end."); | |
f9ce3c16 | 523 | } |
4aebb4e4 | 524 | __push_char('-'); |
79b576cc TS |
525 | } |
526 | else | |
e280b6ff | 527 | { |
4aebb4e4 | 528 | if (_M_try_char()) |
e280b6ff | 529 | { |
4aebb4e4 TS |
530 | __matcher._M_make_range(__last_char.second, _M_value[0]); |
531 | __last_char.first = false; | |
532 | } | |
533 | else if (_M_match_token(_ScannerT::_S_token_bracket_dash)) | |
534 | { | |
535 | __matcher._M_make_range(__last_char.second, '-'); | |
536 | __last_char.first = false; | |
e280b6ff | 537 | } |
79b576cc TS |
538 | else |
539 | { | |
4aebb4e4 TS |
540 | if (_M_scanner._M_get_token() |
541 | != _ScannerT::_S_token_bracket_end) | |
542 | __throw_regex_error( | |
543 | regex_constants::error_range, | |
544 | "Character is expected after a dash."); | |
545 | __push_char('-'); | |
79b576cc | 546 | } |
e280b6ff | 547 | } |
e280b6ff | 548 | } |
4dae67e0 | 549 | else if (_M_match_token(_ScannerT::_S_token_quoted_class)) |
4aebb4e4 TS |
550 | { |
551 | __flush(); | |
552 | __matcher._M_add_character_class(_M_value, | |
553 | _M_ctype.is(_CtypeT::upper, | |
554 | _M_value[0])); | |
555 | } | |
7c812a2a | 556 | else |
236d76c4 TS |
557 | __throw_regex_error(regex_constants::error_brack, |
558 | "Unexpected character in bracket expression."); | |
f9ce3c16 TS |
559 | |
560 | return true; | |
6cb784b6 TS |
561 | } |
562 | ||
ddf41e9d | 563 | template<typename _TraitsT> |
33fbbb76 | 564 | bool |
ddf41e9d | 565 | _Compiler<_TraitsT>:: |
33fbbb76 TS |
566 | _M_try_char() |
567 | { | |
568 | bool __is_char = false; | |
569 | if (_M_match_token(_ScannerT::_S_token_oct_num)) | |
570 | { | |
571 | __is_char = true; | |
572 | _M_value.assign(1, _M_cur_int_value(8)); | |
573 | } | |
574 | else if (_M_match_token(_ScannerT::_S_token_hex_num)) | |
575 | { | |
576 | __is_char = true; | |
577 | _M_value.assign(1, _M_cur_int_value(16)); | |
578 | } | |
579 | else if (_M_match_token(_ScannerT::_S_token_ord_char)) | |
580 | __is_char = true; | |
581 | return __is_char; | |
582 | } | |
583 | ||
ddf41e9d | 584 | template<typename _TraitsT> |
7c812a2a | 585 | bool |
ddf41e9d | 586 | _Compiler<_TraitsT>:: |
7c812a2a TS |
587 | _M_match_token(_TokenT token) |
588 | { | |
589 | if (token == _M_scanner._M_get_token()) | |
590 | { | |
591 | _M_value = _M_scanner._M_get_value(); | |
592 | _M_scanner._M_advance(); | |
593 | return true; | |
594 | } | |
595 | return false; | |
596 | } | |
597 | ||
ddf41e9d | 598 | template<typename _TraitsT> |
6cb784b6 | 599 | int |
ddf41e9d | 600 | _Compiler<_TraitsT>:: |
6cb784b6 TS |
601 | _M_cur_int_value(int __radix) |
602 | { | |
6cb43087 | 603 | long __v = 0; |
6cb784b6 | 604 | for (typename _StringT::size_type __i = 0; |
33fbbb76 TS |
605 | __i < _M_value.length(); ++__i) |
606 | __v =__v * __radix + _M_traits.value(_M_value[__i], __radix); | |
6cb784b6 TS |
607 | return __v; |
608 | } | |
609 | ||
ddf41e9d | 610 | template<typename _TraitsT, bool __icase, bool __collate> |
7d9d2185 | 611 | bool |
ddf41e9d TS |
612 | _BracketMatcher<_TraitsT, __icase, __collate>:: |
613 | _M_apply(_CharT __ch, false_type) const | |
6cb784b6 | 614 | { |
974afa58 TS |
615 | return [this, __ch] |
616 | { | |
617 | if (std::binary_search(_M_char_set.begin(), _M_char_set.end(), | |
618 | _M_translator._M_translate(__ch))) | |
619 | return true; | |
620 | auto __s = _M_translator._M_transform(__ch); | |
621 | for (auto& __it : _M_range_set) | |
622 | if (_M_translator._M_match_range(__it.first, __it.second, __s)) | |
623 | return true; | |
624 | if (_M_traits.isctype(__ch, _M_class_set)) | |
625 | return true; | |
626 | if (std::find(_M_equiv_set.begin(), _M_equiv_set.end(), | |
627 | _M_traits.transform_primary(&__ch, &__ch+1)) | |
628 | != _M_equiv_set.end()) | |
629 | return true; | |
630 | for (auto& __it : _M_neg_class_set) | |
631 | if (!_M_traits.isctype(__ch, __it)) | |
632 | return true; | |
633 | return false; | |
634 | }() ^ _M_is_non_matching; | |
6cb784b6 | 635 | } |
4a15d842 | 636 | } // namespace __detail |
6cb784b6 TS |
637 | |
638 | _GLIBCXX_END_NAMESPACE_VERSION | |
6cb784b6 | 639 | } // namespace |