1 // class template regex -*- C++ -*-
3 // Copyright (C) 2013-2014 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)
86 _GLIBCXX_DEBUG_ASSERT(false);
89 // Differences between styles:
90 // 1) "\(", "\)", "\{" in basic. It's not escaping.
91 // 2) "(?:", "(?=", "(?!" in ECMAScript.
92 template<typename _CharT>
97 auto __c = *_M_current++;
100 if (std::strchr(_M_spec_char, _M_ctype.narrow(__c, '\0')) == nullptr)
102 _M_token = _S_token_ord_char;
103 _M_value.assign(1, __c);
108 if (_M_current == _M_end)
109 __throw_regex_error(regex_constants::error_escape);
112 || (*_M_current != '('
113 && *_M_current != ')'
114 && *_M_current != '{'))
116 (this->*_M_eat_escape)();
123 if (_M_is_ecma() && *_M_current == '?')
125 if (++_M_current == _M_end)
126 __throw_regex_error(regex_constants::error_paren);
128 if (*_M_current == ':')
131 _M_token = _S_token_subexpr_no_group_begin;
133 else if (*_M_current == '=')
136 _M_token = _S_token_subexpr_lookahead_begin;
137 _M_value.assign(1, 'p');
139 else if (*_M_current == '!')
142 _M_token = _S_token_subexpr_lookahead_begin;
143 _M_value.assign(1, 'n');
146 __throw_regex_error(regex_constants::error_paren);
148 else if (_M_flags & regex_constants::nosubs)
149 _M_token = _S_token_subexpr_no_group_begin;
151 _M_token = _S_token_subexpr_begin;
154 _M_token = _S_token_subexpr_end;
157 _M_state = _S_state_in_bracket;
158 _M_at_bracket_start = true;
159 if (_M_current != _M_end && *_M_current == '^')
161 _M_token = _S_token_bracket_neg_begin;
165 _M_token = _S_token_bracket_begin;
169 _M_state = _S_state_in_brace;
170 _M_token = _S_token_interval_begin;
172 else if (((__pos = std::strchr(_M_spec_char, _M_ctype.narrow(__c, '\0')))
177 || (_M_is_grep() && __c == '\n'))
179 auto __it = _M_token_tbl;
180 auto __narrowc = _M_ctype.narrow(__c, '\0');
181 for (; __it->first != '\0'; ++__it)
182 if (__it->first == __narrowc)
184 _M_token = __it->second;
187 _GLIBCXX_DEBUG_ASSERT(false);
191 _M_token = _S_token_ord_char;
192 _M_value.assign(1, __c);
196 // Differences between styles:
197 // 1) different semantics of "[]" and "[^]".
198 // 2) Escaping in bracket expr.
199 template<typename _CharT>
204 if (_M_current == _M_end)
205 __throw_regex_error(regex_constants::error_brack);
207 auto __c = *_M_current++;
211 if (_M_current == _M_end)
212 __throw_regex_error(regex_constants::error_brack);
214 if (*_M_current == '.')
216 _M_token = _S_token_collsymbol;
217 _M_eat_class(*_M_current++);
219 else if (*_M_current == ':')
221 _M_token = _S_token_char_class_name;
222 _M_eat_class(*_M_current++);
224 else if (*_M_current == '=')
226 _M_token = _S_token_equiv_class_name;
227 _M_eat_class(*_M_current++);
231 _M_token = _S_token_ord_char;
232 _M_value.assign(1, __c);
235 // In POSIX, when encountering "[]" or "[^]", the ']' is interpreted
236 // literally. So "[]]" or "[^]]" is valid regex. See the testcases
237 // `*/empty_range.cc`.
238 else if (__c == ']' && (_M_is_ecma() || !_M_at_bracket_start))
240 _M_token = _S_token_bracket_end;
241 _M_state = _S_state_normal;
243 // ECMAScirpt and awk permmits escaping in bracket.
244 else if (__c == '\\' && (_M_is_ecma() || _M_is_awk()))
245 (this->*_M_eat_escape)();
248 _M_token = _S_token_ord_char;
249 _M_value.assign(1, __c);
251 _M_at_bracket_start = false;
254 // Differences between styles:
255 // 1) "\}" in basic style.
256 template<typename _CharT>
261 if (_M_current == _M_end)
262 __throw_regex_error(regex_constants::error_brace);
264 auto __c = *_M_current++;
266 if (_M_ctype.is(_CtypeT::digit, __c))
268 _M_token = _S_token_dup_count;
269 _M_value.assign(1, __c);
270 while (_M_current != _M_end
271 && _M_ctype.is(_CtypeT::digit, *_M_current))
272 _M_value += *_M_current++;
275 _M_token = _S_token_comma;
277 else if (_M_is_basic())
279 if (__c == '\\' && _M_current != _M_end && *_M_current == '}')
281 _M_state = _S_state_normal;
282 _M_token = _S_token_interval_end;
286 __throw_regex_error(regex_constants::error_badbrace);
290 _M_state = _S_state_normal;
291 _M_token = _S_token_interval_end;
294 __throw_regex_error(regex_constants::error_badbrace);
297 template<typename _CharT>
302 if (_M_current == _M_end)
303 __throw_regex_error(regex_constants::error_escape);
305 auto __c = *_M_current++;
306 auto __pos = _M_find_escape(_M_ctype.narrow(__c, '\0'));
308 if (__pos != nullptr && (__c != 'b' || _M_state == _S_state_in_bracket))
310 _M_token = _S_token_ord_char;
311 _M_value.assign(1, *__pos);
315 _M_token = _S_token_word_bound;
316 _M_value.assign(1, 'p');
320 _M_token = _S_token_word_bound;
321 _M_value.assign(1, 'n');
331 _M_token = _S_token_quoted_class;
332 _M_value.assign(1, __c);
336 if (_M_current == _M_end)
337 __throw_regex_error(regex_constants::error_escape);
338 _M_token = _S_token_ord_char;
339 _M_value.assign(1, *_M_current++);
341 else if (__c == 'x' || __c == 'u')
344 for (int i = 0; i < (__c == 'x' ? 2 : 4); i++)
346 if (_M_current == _M_end
347 || !_M_ctype.is(_CtypeT::xdigit, *_M_current))
348 __throw_regex_error(regex_constants::error_escape);
349 _M_value += *_M_current++;
351 _M_token = _S_token_hex_num;
353 // ECMAScript recongnizes multi-digit back-references.
354 else if (_M_ctype.is(_CtypeT::digit, __c))
356 _M_value.assign(1, __c);
357 while (_M_current != _M_end
358 && _M_ctype.is(_CtypeT::digit, *_M_current))
359 _M_value += *_M_current++;
360 _M_token = _S_token_backref;
364 _M_token = _S_token_ord_char;
365 _M_value.assign(1, __c);
369 // Differences between styles:
370 // 1) Extended doesn't support backref, but basic does.
371 template<typename _CharT>
374 _M_eat_escape_posix()
376 if (_M_current == _M_end)
377 __throw_regex_error(regex_constants::error_escape);
379 auto __c = *_M_current;
380 auto __pos = std::strchr(_M_spec_char, _M_ctype.narrow(__c, '\0'));
382 if (__pos != nullptr && *__pos != '\0')
384 _M_token = _S_token_ord_char;
385 _M_value.assign(1, __c);
387 // We MUST judge awk before handling backrefs. There's no backref in awk.
388 else if (_M_is_awk())
393 else if (_M_is_basic() && _M_ctype.is(_CtypeT::digit, __c) && __c != '0')
395 _M_token = _S_token_backref;
396 _M_value.assign(1, __c);
400 #ifdef __STRICT_ANSI__
401 __throw_regex_error(regex_constants::error_escape);
403 _M_token = _S_token_ord_char;
404 _M_value.assign(1, __c);
410 template<typename _CharT>
415 auto __c = *_M_current++;
416 auto __pos = _M_find_escape(_M_ctype.narrow(__c, '\0'));
418 if (__pos != nullptr)
420 _M_token = _S_token_ord_char;
421 _M_value.assign(1, *__pos);
423 // \ddd for oct representation
424 else if (_M_ctype.is(_CtypeT::digit, __c)
428 _M_value.assign(1, __c);
431 && _M_current != _M_end
432 && _M_ctype.is(_CtypeT::digit, *_M_current)
433 && *_M_current != '8'
434 && *_M_current != '9';
436 _M_value += *_M_current++;
437 _M_token = _S_token_oct_num;
441 __throw_regex_error(regex_constants::error_escape);
444 // Eats a character class or throwns an exception.
445 // __ch cound be ':', '.' or '=', _M_current is the char after ']' when
447 template<typename _CharT>
450 _M_eat_class(char __ch)
452 for (_M_value.clear(); _M_current != _M_end && *_M_current != __ch;)
453 _M_value += *_M_current++;
454 if (_M_current == _M_end
455 || *_M_current++ != __ch
456 || _M_current == _M_end // skip __ch
457 || *_M_current++ != ']') // skip ']'
460 __throw_regex_error(regex_constants::error_ctype);
462 __throw_regex_error(regex_constants::error_collate);
466 #ifdef _GLIBCXX_DEBUG
467 template<typename _CharT>
470 _M_print(std::ostream& ostr)
474 case _S_token_anychar:
475 ostr << "any-character\n";
477 case _S_token_backref:
480 case _S_token_bracket_begin:
481 ostr << "bracket-begin\n";
483 case _S_token_bracket_neg_begin:
484 ostr << "bracket-neg-begin\n";
486 case _S_token_bracket_end:
487 ostr << "bracket-end\n";
489 case _S_token_char_class_name:
490 ostr << "char-class-name \"" << _M_value << "\"\n";
492 case _S_token_closure0:
493 ostr << "closure0\n";
495 case _S_token_closure1:
496 ostr << "closure1\n";
498 case _S_token_collsymbol:
499 ostr << "collsymbol \"" << _M_value << "\"\n";
504 case _S_token_dup_count:
505 ostr << "dup count: " << _M_value << "\n";
510 case _S_token_equiv_class_name:
511 ostr << "equiv-class-name \"" << _M_value << "\"\n";
513 case _S_token_interval_begin:
514 ostr << "interval begin\n";
516 case _S_token_interval_end:
517 ostr << "interval end\n";
519 case _S_token_line_begin:
520 ostr << "line begin\n";
522 case _S_token_line_end:
523 ostr << "line end\n";
531 case _S_token_ord_char:
532 ostr << "ordinary character: \"" << _M_value << "\"\n";
534 case _S_token_subexpr_begin:
535 ostr << "subexpr begin\n";
537 case _S_token_subexpr_no_group_begin:
538 ostr << "no grouping subexpr begin\n";
540 case _S_token_subexpr_lookahead_begin:
541 ostr << "lookahead subexpr begin\n";
543 case _S_token_subexpr_end:
544 ostr << "subexpr end\n";
546 case _S_token_unknown:
547 ostr << "-- unknown token --\n";
549 case _S_token_oct_num:
550 ostr << "oct number " << _M_value << "\n";
552 case _S_token_hex_num:
553 ostr << "hex number " << _M_value << "\n";
555 case _S_token_quoted_class:
556 ostr << "quoted class " << "\\" << _M_value << "\n";
559 _GLIBCXX_DEBUG_ASSERT(false);
565 _GLIBCXX_END_NAMESPACE_VERSION
566 } // namespace __detail