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_scanner.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 // N3376 specified 6 regex styles: ECMAScript, basic, extended, grep, egrep
35 // 1) grep is basic except '\n' is treated as '|'
36 // 2) egrep is extended except '\n' is treated as '|'
37 // 3) awk is extended except special escaping rules, and there's no
42 // ECMAScript: ECMA-262 15.10
45 // http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap09.html
47 // awk: http://pubs.opengroup.org/onlinepubs/000095399/utilities/awk.html
49 namespace std _GLIBCXX_VISIBILITY(default)
53 _GLIBCXX_BEGIN_NAMESPACE_VERSION
55 template<typename _FwdIter>
57 _Scanner(_FwdIter __begin, _FwdIter __end,
58 _FlagT __flags, std::locale __loc)
59 : _M_current(__begin) , _M_end(__end) , _M_flags(__flags),
60 _M_ctype(std::use_facet<_CtypeT>(__loc)), _M_state(_S_state_normal),
61 _M_at_bracket_start(false),
64 {'^', _S_token_line_begin},
65 {'$', _S_token_line_end},
66 {'.', _S_token_anychar},
67 {'*', _S_token_closure0},
68 {'+', _S_token_closure1},
97 _M_escape_map(_M_is_ecma()
126 _M_extended_spec_char
141 _M_eat_escape(_M_is_ecma()
142 ? &_Scanner::_M_eat_escape_ecma
143 : &_Scanner::_M_eat_escape_posix),
144 _M_spec_char(_M_is_ecma()
148 : _M_extended_spec_char)
151 template<typename _FwdIter>
156 if (_M_current == _M_end)
158 _M_token = _S_token_eof;
162 if (_M_state == _S_state_normal)
164 else if (_M_state == _S_state_in_bracket)
165 _M_scan_in_bracket();
166 else if (_M_state == _S_state_in_brace)
169 _GLIBCXX_DEBUG_ASSERT(false);
172 // Differences between styles:
173 // 1) "\(", "\)", "\{" in basic. It's not escaping.
174 // 2) "(?:", "(?=", "(?!" in ECMAScript.
175 template<typename _FwdIter>
180 auto __c = *_M_current++;
184 if (_M_current == _M_end)
185 __throw_regex_error(regex_constants::error_escape);
188 || (*_M_current != '('
189 && *_M_current != ')'
190 && *_M_current != '{'))
192 (this->*_M_eat_escape)();
199 if (_M_is_ecma() && *_M_current == '?')
201 if (++_M_current == _M_end)
202 __throw_regex_error(regex_constants::error_paren);
204 if (*_M_current == ':')
207 _M_token = _S_token_subexpr_no_group_begin;
209 else if (*_M_current == '=')
212 _M_token = _S_token_subexpr_lookahead_begin;
213 _M_value.assign(1, 'p');
215 else if (*_M_current == '!')
218 _M_token = _S_token_subexpr_lookahead_begin;
219 _M_value.assign(1, 'n');
222 __throw_regex_error(regex_constants::error_paren);
225 _M_token = _S_token_subexpr_begin;
228 _M_token = _S_token_subexpr_end;
231 _M_state = _S_state_in_bracket;
232 _M_at_bracket_start = true;
233 if (_M_current != _M_end && *_M_current == '^')
235 _M_token = _S_token_bracket_neg_begin;
239 _M_token = _S_token_bracket_begin;
243 _M_state = _S_state_in_brace;
244 _M_token = _S_token_interval_begin;
246 else if (_M_spec_char.count(__c)
249 || (_M_is_grep() && __c == '\n'))
250 _M_token = _M_token_map.at(__c);
253 _M_token = _S_token_ord_char;
254 _M_value.assign(1, __c);
258 // Differences between styles:
259 // 1) different semantics of "[]" and "[^]".
260 // 2) Escaping in bracket expr.
261 template<typename _FwdIter>
266 if (_M_current == _M_end)
267 __throw_regex_error(regex_constants::error_brack);
269 auto __c = *_M_current++;
273 if (_M_current == _M_end)
274 __throw_regex_error(regex_constants::error_brack);
276 if (*_M_current == '.')
278 _M_token = _S_token_collsymbol;
279 _M_eat_class(*_M_current++);
281 else if (*_M_current == ':')
283 _M_token = _S_token_char_class_name;
284 _M_eat_class(*_M_current++);
286 else if (*_M_current == '=')
288 _M_token = _S_token_equiv_class_name;
289 _M_eat_class(*_M_current++);
293 _M_token = _S_token_ord_char;
294 _M_value.assign(1, __c);
297 // In POSIX, when encountering "[]" or "[^]", the ']' is interpreted
298 // literally. So "[]]" or "[^]]" is valid regex. See the testcases
299 // `*/empty_range.cc`.
300 else if (__c == ']' && (_M_is_ecma() || !_M_at_bracket_start))
302 _M_token = _S_token_bracket_end;
303 _M_state = _S_state_normal;
305 // ECMAScirpt and awk permmits escaping in bracket.
306 else if (__c == '\\' && (_M_is_ecma() || _M_is_awk()))
307 (this->*_M_eat_escape)();
310 _M_token = _S_token_ord_char;
311 _M_value.assign(1, __c);
313 _M_at_bracket_start = false;
316 // Differences between styles:
317 // 1) "\}" in basic style.
318 template<typename _FwdIter>
323 if (_M_current == _M_end)
324 __throw_regex_error(regex_constants::error_brace);
326 auto __c = *_M_current++;
328 if (_M_ctype.is(_CtypeT::digit, __c))
330 _M_token = _S_token_dup_count;
331 _M_value.assign(1, __c);
332 while (_M_current != _M_end
333 && _M_ctype.is(_CtypeT::digit, *_M_current))
334 _M_value += *_M_current++;
337 _M_token = _S_token_comma;
339 else if (_M_is_basic())
341 if (__c == '\\' && _M_current != _M_end && *_M_current == '}')
343 _M_state = _S_state_normal;
344 _M_token = _S_token_interval_end;
348 __throw_regex_error(regex_constants::error_badbrace);
352 _M_state = _S_state_normal;
353 _M_token = _S_token_interval_end;
356 __throw_regex_error(regex_constants::error_badbrace);
359 template<typename _FwdIter>
364 if (_M_current == _M_end)
365 __throw_regex_error(regex_constants::error_escape);
367 auto __c = *_M_current++;
369 if (_M_escape_map.count(__c)
370 && (__c != 'b' || _M_state == _S_state_in_bracket))
372 _M_token = _S_token_ord_char;
373 _M_value.assign(1, _M_escape_map.at(__c));
377 _M_token = _S_token_word_bound;
378 _M_value.assign(1, 'p');
382 _M_token = _S_token_word_bound;
383 _M_value.assign(1, 'n');
393 _M_token = _S_token_quoted_class;
394 _M_value.assign(1, __c);
398 if (_M_current == _M_end)
399 __throw_regex_error(regex_constants::error_escape);
400 _M_token = _S_token_ord_char;
401 _M_value.assign(1, *_M_current++);
403 else if (__c == 'x' || __c == 'u')
406 for (int i = 0; i < (__c == 'x' ? 2 : 4); i++)
408 if (_M_current == _M_end
409 || !_M_ctype.is(_CtypeT::xdigit, *_M_current))
410 __throw_regex_error(regex_constants::error_escape);
411 _M_value += *_M_current++;
413 _M_token = _S_token_hex_num;
415 // ECMAScript recongnizes multi-digit back-references.
416 else if (_M_ctype.is(_CtypeT::digit, __c))
418 _M_value.assign(1, __c);
419 while (_M_current != _M_end
420 && _M_ctype.is(_CtypeT::digit, *_M_current))
421 _M_value += *_M_current++;
422 _M_token = _S_token_backref;
426 _M_token = _S_token_ord_char;
427 _M_value.assign(1, __c);
431 // Differences between styles:
432 // 1) Extended doesn't support backref, but basic does.
433 template<typename _FwdIter>
436 _M_eat_escape_posix()
438 if (_M_current == _M_end)
439 __throw_regex_error(regex_constants::error_escape);
441 auto __c = *_M_current;
443 if (_M_spec_char.count(__c))
445 _M_token = _S_token_ord_char;
446 _M_value.assign(1, __c);
448 // We MUST judge awk before handling backrefs. There's no backref in awk.
449 else if (_M_is_awk())
454 else if (_M_is_basic() && _M_ctype.is(_CtypeT::digit, __c) && __c != '0')
456 _M_token = _S_token_backref;
457 _M_value.assign(1, __c);
460 __throw_regex_error(regex_constants::error_escape);
464 template<typename _FwdIter>
469 auto __c = *_M_current++;
471 if (_M_escape_map.count(__c))
473 _M_token = _S_token_ord_char;
474 _M_value.assign(1, _M_escape_map.at(__c));
476 // \ddd for oct representation
477 else if (_M_ctype.is(_CtypeT::digit, __c)
481 _M_value.assign(1, __c);
484 && _M_current != _M_end
485 && _M_ctype.is(_CtypeT::digit, *_M_current)
486 && *_M_current != '8'
487 && *_M_current != '9';
489 _M_value += *_M_current++;
490 _M_token = _S_token_oct_num;
494 __throw_regex_error(regex_constants::error_escape);
497 // Eats a character class or throwns an exception.
498 // __ch cound be ':', '.' or '=', _M_current is the char after ']' when
500 template<typename _FwdIter>
503 _M_eat_class(char __ch)
505 for (_M_value.clear(); _M_current != _M_end && *_M_current != __ch;)
506 _M_value += *_M_current++;
507 if (_M_current == _M_end
508 || *_M_current++ != __ch
509 || _M_current == _M_end // skip __ch
510 || *_M_current++ != ']') // skip ']'
512 __throw_regex_error(regex_constants::error_ctype);
514 __throw_regex_error(regex_constants::error_collate);
517 #ifdef _GLIBCXX_DEBUG
518 template<typename _FwdIter>
521 _M_print(std::ostream& ostr)
525 case _S_token_anychar:
526 ostr << "any-character\n";
528 case _S_token_backref:
531 case _S_token_bracket_begin:
532 ostr << "bracket-begin\n";
534 case _S_token_bracket_neg_begin:
535 ostr << "bracket-neg-begin\n";
537 case _S_token_bracket_end:
538 ostr << "bracket-end\n";
540 case _S_token_char_class_name:
541 ostr << "char-class-name \"" << _M_value << "\"\n";
543 case _S_token_closure0:
544 ostr << "closure0\n";
546 case _S_token_closure1:
547 ostr << "closure1\n";
549 case _S_token_collsymbol:
550 ostr << "collsymbol \"" << _M_value << "\"\n";
555 case _S_token_dup_count:
556 ostr << "dup count: " << _M_value << "\n";
561 case _S_token_equiv_class_name:
562 ostr << "equiv-class-name \"" << _M_value << "\"\n";
564 case _S_token_interval_begin:
565 ostr << "interval begin\n";
567 case _S_token_interval_end:
568 ostr << "interval end\n";
570 case _S_token_line_begin:
571 ostr << "line begin\n";
573 case _S_token_line_end:
574 ostr << "line end\n";
582 case _S_token_ord_char:
583 ostr << "ordinary character: \"" << _M_value << "\"\n";
585 case _S_token_subexpr_begin:
586 ostr << "subexpr begin\n";
588 case _S_token_subexpr_no_group_begin:
589 ostr << "no grouping subexpr begin\n";
591 case _S_token_subexpr_lookahead_begin:
592 ostr << "lookahead subexpr begin\n";
594 case _S_token_subexpr_end:
595 ostr << "subexpr end\n";
597 case _S_token_unknown:
598 ostr << "-- unknown token --\n";
600 case _S_token_oct_num:
601 ostr << "oct number " << _M_value << "\n";
603 case _S_token_hex_num:
604 ostr << "hex number " << _M_value << "\n";
606 case _S_token_quoted_class:
607 ostr << "quoted class " << "\\" << _M_value << "\n";
610 _GLIBCXX_DEBUG_ASSERT(false);
616 _GLIBCXX_END_NAMESPACE_VERSION
617 } // namespace __detail