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_state(_S_state_normal), _M_current(__begin), _M_end(__end),
61 _M_ctype(std::use_facet<_CtypeT>(__loc)),
62 _M_at_bracket_start(false),
65 {'^', _S_token_line_begin},
66 {'$', _S_token_line_end},
67 {'.', _S_token_anychar},
68 {'*', _S_token_closure0},
69 {'+', _S_token_closure1},
124 _M_extended_spec_char
139 _M_escape_map(_M_is_ecma()
141 : _M_awk_escape_map),
142 _M_spec_char(_M_is_ecma()
146 : _M_extended_spec_char),
147 _M_eat_escape(_M_is_ecma()
148 ? &_Scanner::_M_eat_escape_ecma
149 : &_Scanner::_M_eat_escape_posix)
152 template<typename _FwdIter>
157 if (_M_current == _M_end)
159 _M_token = _S_token_eof;
163 if (_M_state == _S_state_normal)
165 else if (_M_state == _S_state_in_bracket)
166 _M_scan_in_bracket();
167 else if (_M_state == _S_state_in_brace)
170 _GLIBCXX_DEBUG_ASSERT(false);
173 // Differences between styles:
174 // 1) "\(", "\)", "\{" in basic. It's not escaping.
175 // 2) "(?:", "(?=", "(?!" in ECMAScript.
176 template<typename _FwdIter>
181 auto __c = *_M_current++;
185 if (_M_current == _M_end)
186 __throw_regex_error(regex_constants::error_escape);
189 || (*_M_current != '('
190 && *_M_current != ')'
191 && *_M_current != '{'))
193 (this->*_M_eat_escape)();
200 if (_M_is_ecma() && *_M_current == '?')
202 if (++_M_current == _M_end)
203 __throw_regex_error(regex_constants::error_paren);
205 if (*_M_current == ':')
208 _M_token = _S_token_subexpr_no_group_begin;
210 else if (*_M_current == '=')
213 _M_token = _S_token_subexpr_lookahead_begin;
214 _M_value.assign(1, 'p');
216 else if (*_M_current == '!')
219 _M_token = _S_token_subexpr_lookahead_begin;
220 _M_value.assign(1, 'n');
223 __throw_regex_error(regex_constants::error_paren);
226 _M_token = _S_token_subexpr_begin;
229 _M_token = _S_token_subexpr_end;
232 _M_state = _S_state_in_bracket;
233 _M_at_bracket_start = true;
234 if (_M_current != _M_end && *_M_current == '^')
236 _M_token = _S_token_bracket_neg_begin;
240 _M_token = _S_token_bracket_begin;
244 _M_state = _S_state_in_brace;
245 _M_token = _S_token_interval_begin;
247 else if ((_M_spec_char.count(_M_ctype.narrow(__c, '\0'))
250 || (_M_is_grep() && __c == '\n'))
251 _M_token = _M_token_map.at(__c);
254 _M_token = _S_token_ord_char;
255 _M_value.assign(1, __c);
259 // Differences between styles:
260 // 1) different semantics of "[]" and "[^]".
261 // 2) Escaping in bracket expr.
262 template<typename _FwdIter>
267 if (_M_current == _M_end)
268 __throw_regex_error(regex_constants::error_brack);
270 auto __c = *_M_current++;
274 if (_M_current == _M_end)
275 __throw_regex_error(regex_constants::error_brack);
277 if (*_M_current == '.')
279 _M_token = _S_token_collsymbol;
280 _M_eat_class(*_M_current++);
282 else if (*_M_current == ':')
284 _M_token = _S_token_char_class_name;
285 _M_eat_class(*_M_current++);
287 else if (*_M_current == '=')
289 _M_token = _S_token_equiv_class_name;
290 _M_eat_class(*_M_current++);
294 _M_token = _S_token_ord_char;
295 _M_value.assign(1, __c);
298 // In POSIX, when encountering "[]" or "[^]", the ']' is interpreted
299 // literally. So "[]]" or "[^]]" is valid regex. See the testcases
300 // `*/empty_range.cc`.
301 else if (__c == ']' && (_M_is_ecma() || !_M_at_bracket_start))
303 _M_token = _S_token_bracket_end;
304 _M_state = _S_state_normal;
306 // ECMAScirpt and awk permmits escaping in bracket.
307 else if (__c == '\\' && (_M_is_ecma() || _M_is_awk()))
308 (this->*_M_eat_escape)();
311 _M_token = _S_token_ord_char;
312 _M_value.assign(1, __c);
314 _M_at_bracket_start = false;
317 // Differences between styles:
318 // 1) "\}" in basic style.
319 template<typename _FwdIter>
324 if (_M_current == _M_end)
325 __throw_regex_error(regex_constants::error_brace);
327 auto __c = *_M_current++;
329 if (_M_ctype.is(_CtypeT::digit, __c))
331 _M_token = _S_token_dup_count;
332 _M_value.assign(1, __c);
333 while (_M_current != _M_end
334 && _M_ctype.is(_CtypeT::digit, *_M_current))
335 _M_value += *_M_current++;
338 _M_token = _S_token_comma;
340 else if (_M_is_basic())
342 if (__c == '\\' && _M_current != _M_end && *_M_current == '}')
344 _M_state = _S_state_normal;
345 _M_token = _S_token_interval_end;
349 __throw_regex_error(regex_constants::error_badbrace);
353 _M_state = _S_state_normal;
354 _M_token = _S_token_interval_end;
357 __throw_regex_error(regex_constants::error_badbrace);
360 template<typename _FwdIter>
365 if (_M_current == _M_end)
366 __throw_regex_error(regex_constants::error_escape);
368 auto __c = *_M_current++;
370 if (_M_escape_map.count(_M_ctype.narrow(__c, '\0'))
371 && (__c != 'b' || _M_state == _S_state_in_bracket))
373 _M_token = _S_token_ord_char;
374 _M_value.assign(1, _M_escape_map.at(__c));
378 _M_token = _S_token_word_bound;
379 _M_value.assign(1, 'p');
383 _M_token = _S_token_word_bound;
384 _M_value.assign(1, 'n');
394 _M_token = _S_token_quoted_class;
395 _M_value.assign(1, __c);
399 if (_M_current == _M_end)
400 __throw_regex_error(regex_constants::error_escape);
401 _M_token = _S_token_ord_char;
402 _M_value.assign(1, *_M_current++);
404 else if (__c == 'x' || __c == 'u')
407 for (int i = 0; i < (__c == 'x' ? 2 : 4); i++)
409 if (_M_current == _M_end
410 || !_M_ctype.is(_CtypeT::xdigit, *_M_current))
411 __throw_regex_error(regex_constants::error_escape);
412 _M_value += *_M_current++;
414 _M_token = _S_token_hex_num;
416 // ECMAScript recongnizes multi-digit back-references.
417 else if (_M_ctype.is(_CtypeT::digit, __c))
419 _M_value.assign(1, __c);
420 while (_M_current != _M_end
421 && _M_ctype.is(_CtypeT::digit, *_M_current))
422 _M_value += *_M_current++;
423 _M_token = _S_token_backref;
427 _M_token = _S_token_ord_char;
428 _M_value.assign(1, __c);
432 // Differences between styles:
433 // 1) Extended doesn't support backref, but basic does.
434 template<typename _FwdIter>
437 _M_eat_escape_posix()
439 if (_M_current == _M_end)
440 __throw_regex_error(regex_constants::error_escape);
442 auto __c = *_M_current;
444 if (_M_spec_char.count(_M_ctype.narrow(__c, '\0')))
446 _M_token = _S_token_ord_char;
447 _M_value.assign(1, __c);
449 // We MUST judge awk before handling backrefs. There's no backref in awk.
450 else if (_M_is_awk())
455 else if (_M_is_basic() && _M_ctype.is(_CtypeT::digit, __c) && __c != '0')
457 _M_token = _S_token_backref;
458 _M_value.assign(1, __c);
462 #ifdef __STRICT_ANSI__
463 __throw_regex_error(regex_constants::error_escape);
465 _M_token = _S_token_ord_char;
466 _M_value.assign(1, __c);
472 template<typename _FwdIter>
477 auto __c = *_M_current++;
479 if (_M_escape_map.count(_M_ctype.narrow(__c, '\0')))
481 _M_token = _S_token_ord_char;
482 _M_value.assign(1, _M_escape_map.at(__c));
484 // \ddd for oct representation
485 else if (_M_ctype.is(_CtypeT::digit, __c)
489 _M_value.assign(1, __c);
492 && _M_current != _M_end
493 && _M_ctype.is(_CtypeT::digit, *_M_current)
494 && *_M_current != '8'
495 && *_M_current != '9';
497 _M_value += *_M_current++;
498 _M_token = _S_token_oct_num;
502 __throw_regex_error(regex_constants::error_escape);
505 // Eats a character class or throwns an exception.
506 // __ch cound be ':', '.' or '=', _M_current is the char after ']' when
508 template<typename _FwdIter>
511 _M_eat_class(char __ch)
513 for (_M_value.clear(); _M_current != _M_end && *_M_current != __ch;)
514 _M_value += *_M_current++;
515 if (_M_current == _M_end
516 || *_M_current++ != __ch
517 || _M_current == _M_end // skip __ch
518 || *_M_current++ != ']') // skip ']'
521 __throw_regex_error(regex_constants::error_ctype);
523 __throw_regex_error(regex_constants::error_collate);
527 #ifdef _GLIBCXX_DEBUG
528 template<typename _FwdIter>
531 _M_print(std::ostream& ostr)
535 case _S_token_anychar:
536 ostr << "any-character\n";
538 case _S_token_backref:
541 case _S_token_bracket_begin:
542 ostr << "bracket-begin\n";
544 case _S_token_bracket_neg_begin:
545 ostr << "bracket-neg-begin\n";
547 case _S_token_bracket_end:
548 ostr << "bracket-end\n";
550 case _S_token_char_class_name:
551 ostr << "char-class-name \"" << _M_value << "\"\n";
553 case _S_token_closure0:
554 ostr << "closure0\n";
556 case _S_token_closure1:
557 ostr << "closure1\n";
559 case _S_token_collsymbol:
560 ostr << "collsymbol \"" << _M_value << "\"\n";
565 case _S_token_dup_count:
566 ostr << "dup count: " << _M_value << "\n";
571 case _S_token_equiv_class_name:
572 ostr << "equiv-class-name \"" << _M_value << "\"\n";
574 case _S_token_interval_begin:
575 ostr << "interval begin\n";
577 case _S_token_interval_end:
578 ostr << "interval end\n";
580 case _S_token_line_begin:
581 ostr << "line begin\n";
583 case _S_token_line_end:
584 ostr << "line end\n";
592 case _S_token_ord_char:
593 ostr << "ordinary character: \"" << _M_value << "\"\n";
595 case _S_token_subexpr_begin:
596 ostr << "subexpr begin\n";
598 case _S_token_subexpr_no_group_begin:
599 ostr << "no grouping subexpr begin\n";
601 case _S_token_subexpr_lookahead_begin:
602 ostr << "lookahead subexpr begin\n";
604 case _S_token_subexpr_end:
605 ostr << "subexpr end\n";
607 case _S_token_unknown:
608 ostr << "-- unknown token --\n";
610 case _S_token_oct_num:
611 ostr << "oct number " << _M_value << "\n";
613 case _S_token_hex_num:
614 ostr << "hex number " << _M_value << "\n";
616 case _S_token_quoted_class:
617 ostr << "quoted class " << "\\" << _M_value << "\n";
620 _GLIBCXX_DEBUG_ASSERT(false);
626 _GLIBCXX_END_NAMESPACE_VERSION
627 } // namespace __detail