1 // class template regex -*- C++ -*-
3 // Copyright (C) 2013-2015 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 _CharT>
57 _Scanner(typename _Scanner::_IterT __begin,
58 typename _Scanner::_IterT __end,
59 _FlagT __flags, std::locale __loc)
60 : _ScannerBase(__flags),
61 _M_current(__begin), _M_end(__end),
62 _M_ctype(std::use_facet<_CtypeT>(__loc)),
63 _M_eat_escape(_M_is_ecma()
64 ? &_Scanner::_M_eat_escape_ecma
65 : &_Scanner::_M_eat_escape_posix)
68 template<typename _CharT>
73 if (_M_current == _M_end)
75 _M_token = _S_token_eof;
79 if (_M_state == _S_state_normal)
81 else if (_M_state == _S_state_in_bracket)
83 else if (_M_state == _S_state_in_brace)
87 _GLIBCXX_DEBUG_ASSERT(false);
91 // Differences between styles:
92 // 1) "\(", "\)", "\{" in basic. It's not escaping.
93 // 2) "(?:", "(?=", "(?!" in ECMAScript.
94 template<typename _CharT>
99 auto __c = *_M_current++;
102 if (std::strchr(_M_spec_char, _M_ctype.narrow(__c, '\0')) == nullptr)
104 _M_token = _S_token_ord_char;
105 _M_value.assign(1, __c);
110 if (_M_current == _M_end)
111 __throw_regex_error(regex_constants::error_escape);
114 || (*_M_current != '('
115 && *_M_current != ')'
116 && *_M_current != '{'))
118 (this->*_M_eat_escape)();
125 if (_M_is_ecma() && *_M_current == '?')
127 if (++_M_current == _M_end)
128 __throw_regex_error(regex_constants::error_paren);
130 if (*_M_current == ':')
133 _M_token = _S_token_subexpr_no_group_begin;
135 else if (*_M_current == '=')
138 _M_token = _S_token_subexpr_lookahead_begin;
139 _M_value.assign(1, 'p');
141 else if (*_M_current == '!')
144 _M_token = _S_token_subexpr_lookahead_begin;
145 _M_value.assign(1, 'n');
148 __throw_regex_error(regex_constants::error_paren);
150 else if (_M_flags & regex_constants::nosubs)
151 _M_token = _S_token_subexpr_no_group_begin;
153 _M_token = _S_token_subexpr_begin;
156 _M_token = _S_token_subexpr_end;
159 _M_state = _S_state_in_bracket;
160 _M_at_bracket_start = true;
161 if (_M_current != _M_end && *_M_current == '^')
163 _M_token = _S_token_bracket_neg_begin;
167 _M_token = _S_token_bracket_begin;
171 _M_state = _S_state_in_brace;
172 _M_token = _S_token_interval_begin;
174 else if (((__pos = std::strchr(_M_spec_char, _M_ctype.narrow(__c, '\0')))
179 || (_M_is_grep() && __c == '\n'))
181 auto __it = _M_token_tbl;
182 auto __narrowc = _M_ctype.narrow(__c, '\0');
183 for (; __it->first != '\0'; ++__it)
184 if (__it->first == __narrowc)
186 _M_token = __it->second;
189 _GLIBCXX_DEBUG_ASSERT(false);
193 _M_token = _S_token_ord_char;
194 _M_value.assign(1, __c);
198 // Differences between styles:
199 // 1) different semantics of "[]" and "[^]".
200 // 2) Escaping in bracket expr.
201 template<typename _CharT>
206 if (_M_current == _M_end)
207 __throw_regex_error(regex_constants::error_brack);
209 auto __c = *_M_current++;
213 if (_M_current == _M_end)
214 __throw_regex_error(regex_constants::error_brack);
216 if (*_M_current == '.')
218 _M_token = _S_token_collsymbol;
219 _M_eat_class(*_M_current++);
221 else if (*_M_current == ':')
223 _M_token = _S_token_char_class_name;
224 _M_eat_class(*_M_current++);
226 else if (*_M_current == '=')
228 _M_token = _S_token_equiv_class_name;
229 _M_eat_class(*_M_current++);
233 _M_token = _S_token_ord_char;
234 _M_value.assign(1, __c);
237 // In POSIX, when encountering "[]" or "[^]", the ']' is interpreted
238 // literally. So "[]]" and "[^]]" are valid regexes. See the testcases
239 // `*/empty_range.cc`.
240 else if (__c == ']' && (_M_is_ecma() || !_M_at_bracket_start))
242 _M_token = _S_token_bracket_end;
243 _M_state = _S_state_normal;
245 // ECMAScript and awk permits escaping in bracket.
246 else if (__c == '\\' && (_M_is_ecma() || _M_is_awk()))
247 (this->*_M_eat_escape)();
250 _M_token = _S_token_ord_char;
251 _M_value.assign(1, __c);
253 _M_at_bracket_start = false;
256 // Differences between styles:
257 // 1) "\}" in basic style.
258 template<typename _CharT>
263 if (_M_current == _M_end)
264 __throw_regex_error(regex_constants::error_brace);
266 auto __c = *_M_current++;
268 if (_M_ctype.is(_CtypeT::digit, __c))
270 _M_token = _S_token_dup_count;
271 _M_value.assign(1, __c);
272 while (_M_current != _M_end
273 && _M_ctype.is(_CtypeT::digit, *_M_current))
274 _M_value += *_M_current++;
277 _M_token = _S_token_comma;
279 else if (_M_is_basic())
281 if (__c == '\\' && _M_current != _M_end && *_M_current == '}')
283 _M_state = _S_state_normal;
284 _M_token = _S_token_interval_end;
288 __throw_regex_error(regex_constants::error_badbrace);
292 _M_state = _S_state_normal;
293 _M_token = _S_token_interval_end;
296 __throw_regex_error(regex_constants::error_badbrace);
299 template<typename _CharT>
304 if (_M_current == _M_end)
305 __throw_regex_error(regex_constants::error_escape);
307 auto __c = *_M_current++;
308 auto __pos = _M_find_escape(_M_ctype.narrow(__c, '\0'));
310 if (__pos != nullptr && (__c != 'b' || _M_state == _S_state_in_bracket))
312 _M_token = _S_token_ord_char;
313 _M_value.assign(1, *__pos);
317 _M_token = _S_token_word_bound;
318 _M_value.assign(1, 'p');
322 _M_token = _S_token_word_bound;
323 _M_value.assign(1, 'n');
333 _M_token = _S_token_quoted_class;
334 _M_value.assign(1, __c);
338 if (_M_current == _M_end)
339 __throw_regex_error(regex_constants::error_escape);
340 _M_token = _S_token_ord_char;
341 _M_value.assign(1, *_M_current++);
343 else if (__c == 'x' || __c == 'u')
346 for (int __i = 0; __i < (__c == 'x' ? 2 : 4); __i++)
348 if (_M_current == _M_end
349 || !_M_ctype.is(_CtypeT::xdigit, *_M_current))
350 __throw_regex_error(regex_constants::error_escape);
351 _M_value += *_M_current++;
353 _M_token = _S_token_hex_num;
355 // ECMAScript recognizes multi-digit back-references.
356 else if (_M_ctype.is(_CtypeT::digit, __c))
358 _M_value.assign(1, __c);
359 while (_M_current != _M_end
360 && _M_ctype.is(_CtypeT::digit, *_M_current))
361 _M_value += *_M_current++;
362 _M_token = _S_token_backref;
366 _M_token = _S_token_ord_char;
367 _M_value.assign(1, __c);
371 // Differences between styles:
372 // 1) Extended doesn't support backref, but basic does.
373 template<typename _CharT>
376 _M_eat_escape_posix()
378 if (_M_current == _M_end)
379 __throw_regex_error(regex_constants::error_escape);
381 auto __c = *_M_current;
382 auto __pos = std::strchr(_M_spec_char, _M_ctype.narrow(__c, '\0'));
384 if (__pos != nullptr && *__pos != '\0')
386 _M_token = _S_token_ord_char;
387 _M_value.assign(1, __c);
389 // We MUST judge awk before handling backrefs. There's no backref in awk.
390 else if (_M_is_awk())
395 else if (_M_is_basic() && _M_ctype.is(_CtypeT::digit, __c) && __c != '0')
397 _M_token = _S_token_backref;
398 _M_value.assign(1, __c);
402 #ifdef __STRICT_ANSI__
403 // POSIX says it is undefined to escape ordinary characters
404 __throw_regex_error(regex_constants::error_escape);
406 _M_token = _S_token_ord_char;
407 _M_value.assign(1, __c);
413 template<typename _CharT>
418 auto __c = *_M_current++;
419 auto __pos = _M_find_escape(_M_ctype.narrow(__c, '\0'));
421 if (__pos != nullptr)
423 _M_token = _S_token_ord_char;
424 _M_value.assign(1, *__pos);
426 // \ddd for oct representation
427 else if (_M_ctype.is(_CtypeT::digit, __c)
431 _M_value.assign(1, __c);
434 && _M_current != _M_end
435 && _M_ctype.is(_CtypeT::digit, *_M_current)
436 && *_M_current != '8'
437 && *_M_current != '9';
439 _M_value += *_M_current++;
440 _M_token = _S_token_oct_num;
444 __throw_regex_error(regex_constants::error_escape);
447 // Eats a character class or throws an exception.
448 // __ch could be ':', '.' or '=', _M_current is the char after ']' when
450 template<typename _CharT>
453 _M_eat_class(char __ch)
455 for (_M_value.clear(); _M_current != _M_end && *_M_current != __ch;)
456 _M_value += *_M_current++;
457 if (_M_current == _M_end
458 || *_M_current++ != __ch
459 || _M_current == _M_end // skip __ch
460 || *_M_current++ != ']') // skip ']'
463 __throw_regex_error(regex_constants::error_ctype);
465 __throw_regex_error(regex_constants::error_collate);
469 #ifdef _GLIBCXX_DEBUG
470 template<typename _CharT>
473 _M_print(std::ostream& ostr)
477 case _S_token_anychar:
478 ostr << "any-character\n";
480 case _S_token_backref:
483 case _S_token_bracket_begin:
484 ostr << "bracket-begin\n";
486 case _S_token_bracket_neg_begin:
487 ostr << "bracket-neg-begin\n";
489 case _S_token_bracket_end:
490 ostr << "bracket-end\n";
492 case _S_token_char_class_name:
493 ostr << "char-class-name \"" << _M_value << "\"\n";
495 case _S_token_closure0:
496 ostr << "closure0\n";
498 case _S_token_closure1:
499 ostr << "closure1\n";
501 case _S_token_collsymbol:
502 ostr << "collsymbol \"" << _M_value << "\"\n";
507 case _S_token_dup_count:
508 ostr << "dup count: " << _M_value << "\n";
513 case _S_token_equiv_class_name:
514 ostr << "equiv-class-name \"" << _M_value << "\"\n";
516 case _S_token_interval_begin:
517 ostr << "interval begin\n";
519 case _S_token_interval_end:
520 ostr << "interval end\n";
522 case _S_token_line_begin:
523 ostr << "line begin\n";
525 case _S_token_line_end:
526 ostr << "line end\n";
534 case _S_token_ord_char:
535 ostr << "ordinary character: \"" << _M_value << "\"\n";
537 case _S_token_subexpr_begin:
538 ostr << "subexpr begin\n";
540 case _S_token_subexpr_no_group_begin:
541 ostr << "no grouping subexpr begin\n";
543 case _S_token_subexpr_lookahead_begin:
544 ostr << "lookahead subexpr begin\n";
546 case _S_token_subexpr_end:
547 ostr << "subexpr end\n";
549 case _S_token_unknown:
550 ostr << "-- unknown token --\n";
552 case _S_token_oct_num:
553 ostr << "oct number " << _M_value << "\n";
555 case _S_token_hex_num:
556 ostr << "hex number " << _M_value << "\n";
558 case _S_token_quoted_class:
559 ostr << "quoted class " << "\\" << _M_value << "\n";
562 _GLIBCXX_DEBUG_ASSERT(false);
568 _GLIBCXX_END_NAMESPACE_VERSION
569 } // namespace __detail