Rebase.
[official-gcc.git] / libstdc++-v3 / include / bits / regex_scanner.tcc
blob818e47b5670005ffb24be9598adf37ff3c7f1c99
1 // class template regex -*- C++ -*-
3 // Copyright (C) 2013-2014 Free Software Foundation, Inc.
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.
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/>.
25 /**
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}
29  */
31 // FIXME make comments doxygen format.
33 // N3376 specified 6 regex styles: ECMAScript, basic, extended, grep, egrep
34 // and awk
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
38 //    back-reference.
40 // References:
42 // ECMAScript: ECMA-262 15.10
44 // basic, extended:
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)
51 namespace __detail
53 _GLIBCXX_BEGIN_NAMESPACE_VERSION
55   template<typename _CharT>
56     _Scanner<_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)
66     { _M_advance(); }
68   template<typename _CharT>
69     void
70     _Scanner<_CharT>::
71     _M_advance()
72     {
73       if (_M_current == _M_end)
74         {
75           _M_token = _S_token_eof;
76           return;
77         }
79       if (_M_state == _S_state_normal)
80         _M_scan_normal();
81       else if (_M_state == _S_state_in_bracket)
82         _M_scan_in_bracket();
83       else if (_M_state == _S_state_in_brace)
84         _M_scan_in_brace();
85       else
86         _GLIBCXX_DEBUG_ASSERT(false);
87     }
89   // Differences between styles:
90   // 1) "\(", "\)", "\{" in basic. It's not escaping.
91   // 2) "(?:", "(?=", "(?!" in ECMAScript.
92   template<typename _CharT>
93     void
94     _Scanner<_CharT>::
95     _M_scan_normal()
96     {
97       auto __c = *_M_current++;
98       const char* __pos;
100       if (__c == '\\')
101         {
102           if (_M_current == _M_end)
103             __throw_regex_error(regex_constants::error_escape);
105           if (!_M_is_basic()
106               || (*_M_current != '('
107                   && *_M_current != ')'
108                   && *_M_current != '{'))
109             {
110               (this->*_M_eat_escape)();
111               return;
112             }
113           __c = *_M_current++;
114         }
115       if (__c == '(')
116         {
117           if (_M_is_ecma() && *_M_current == '?')
118             {
119               if (++_M_current == _M_end)
120                 __throw_regex_error(regex_constants::error_paren);
122               if (*_M_current == ':')
123                 {
124                   ++_M_current;
125                   _M_token = _S_token_subexpr_no_group_begin;
126                 }
127               else if (*_M_current == '=')
128                 {
129                   ++_M_current;
130                   _M_token = _S_token_subexpr_lookahead_begin;
131                   _M_value.assign(1, 'p');
132                 }
133               else if (*_M_current == '!')
134                 {
135                   ++_M_current;
136                   _M_token = _S_token_subexpr_lookahead_begin;
137                   _M_value.assign(1, 'n');
138                 }
139               else
140                 __throw_regex_error(regex_constants::error_paren);
141             }
142           else if (_M_flags & regex_constants::nosubs)
143             _M_token = _S_token_subexpr_no_group_begin;
144           else
145             _M_token = _S_token_subexpr_begin;
146         }
147       else if (__c == ')')
148         _M_token = _S_token_subexpr_end;
149       else if (__c == '[')
150         {
151           _M_state = _S_state_in_bracket;
152           _M_at_bracket_start = true;
153           if (_M_current != _M_end && *_M_current == '^')
154             {
155               _M_token = _S_token_bracket_neg_begin;
156               ++_M_current;
157             }
158           else
159             _M_token = _S_token_bracket_begin;
160         }
161       else if (__c == '{')
162         {
163           _M_state = _S_state_in_brace;
164           _M_token = _S_token_interval_begin;
165         }
166       else if (((__pos = std::strchr(_M_spec_char, _M_ctype.narrow(__c, '\0')))
167                   != nullptr
168                 && *__pos != '\0'
169                 && __c != ']'
170                 && __c != '}')
171                || (_M_is_grep() && __c == '\n'))
172         {
173           auto __it = _M_token_tbl;
174           auto __narrowc = _M_ctype.narrow(__c, '\0');
175           for (; __it->first != '\0'; ++__it)
176             if (__it->first == __narrowc)
177               {
178                 _M_token = __it->second;
179                 return;
180               }
181           _GLIBCXX_DEBUG_ASSERT(false);
182         }
183       else
184         {
185           _M_token = _S_token_ord_char;
186           _M_value.assign(1, __c);
187         }
188     }
190   // Differences between styles:
191   // 1) different semantics of "[]" and "[^]".
192   // 2) Escaping in bracket expr.
193   template<typename _CharT>
194     void
195     _Scanner<_CharT>::
196     _M_scan_in_bracket()
197     {
198       if (_M_current == _M_end)
199         __throw_regex_error(regex_constants::error_brack);
201       auto __c = *_M_current++;
203       if (__c == '[')
204         {
205           if (_M_current == _M_end)
206             __throw_regex_error(regex_constants::error_brack);
208           if (*_M_current == '.')
209             {
210               _M_token = _S_token_collsymbol;
211               _M_eat_class(*_M_current++);
212             }
213           else if (*_M_current == ':')
214             {
215               _M_token = _S_token_char_class_name;
216               _M_eat_class(*_M_current++);
217             }
218           else if (*_M_current == '=')
219             {
220               _M_token = _S_token_equiv_class_name;
221               _M_eat_class(*_M_current++);
222             }
223           else
224             {
225               _M_token = _S_token_ord_char;
226               _M_value.assign(1, __c);
227             }
228         }
229       // In POSIX, when encountering "[]" or "[^]", the ']' is interpreted
230       // literally. So "[]]" and "[^]]" are valid regexes. See the testcases
231       // `*/empty_range.cc`.
232       else if (__c == ']' && (_M_is_ecma() || !_M_at_bracket_start))
233         {
234           _M_token = _S_token_bracket_end;
235           _M_state = _S_state_normal;
236         }
237       // ECMAScript and awk permits escaping in bracket.
238       else if (__c == '\\' && (_M_is_ecma() || _M_is_awk()))
239         (this->*_M_eat_escape)();
240       else
241         {
242           _M_token = _S_token_ord_char;
243           _M_value.assign(1, __c);
244         }
245       _M_at_bracket_start = false;
246     }
248   // Differences between styles:
249   // 1) "\}" in basic style.
250   template<typename _CharT>
251     void
252     _Scanner<_CharT>::
253     _M_scan_in_brace()
254     {
255       if (_M_current == _M_end)
256         __throw_regex_error(regex_constants::error_brace);
258       auto __c = *_M_current++;
260       if (_M_ctype.is(_CtypeT::digit, __c))
261         {
262           _M_token = _S_token_dup_count;
263           _M_value.assign(1, __c);
264           while (_M_current != _M_end
265                  && _M_ctype.is(_CtypeT::digit, *_M_current))
266             _M_value += *_M_current++;
267         }
268       else if (__c == ',')
269         _M_token = _S_token_comma;
270       // basic use \}.
271       else if (_M_is_basic())
272         {
273           if (__c == '\\' && _M_current != _M_end && *_M_current == '}')
274             {
275               _M_state = _S_state_normal;
276               _M_token = _S_token_interval_end;
277               ++_M_current;
278             }
279           else
280             __throw_regex_error(regex_constants::error_badbrace);
281         }
282       else if (__c == '}')
283         {
284           _M_state = _S_state_normal;
285           _M_token = _S_token_interval_end;
286         }
287       else
288         __throw_regex_error(regex_constants::error_badbrace);
289     }
291   template<typename _CharT>
292     void
293     _Scanner<_CharT>::
294     _M_eat_escape_ecma()
295     {
296       if (_M_current == _M_end)
297         __throw_regex_error(regex_constants::error_escape);
299       auto __c = *_M_current++;
300       auto __pos = _M_find_escape(_M_ctype.narrow(__c, '\0'));
302       if (__pos != nullptr && (__c != 'b' || _M_state == _S_state_in_bracket))
303         {
304           _M_token = _S_token_ord_char;
305           _M_value.assign(1, *__pos);
306         }
307       else if (__c == 'b')
308         {
309           _M_token = _S_token_word_bound;
310           _M_value.assign(1, 'p');
311         }
312       else if (__c == 'B')
313         {
314           _M_token = _S_token_word_bound;
315           _M_value.assign(1, 'n');
316         }
317       // N3376 28.13
318       else if (__c == 'd'
319                || __c == 'D'
320                || __c == 's'
321                || __c == 'S'
322                || __c == 'w'
323                || __c == 'W')
324         {
325           _M_token = _S_token_quoted_class;
326           _M_value.assign(1, __c);
327         }
328       else if (__c == 'c')
329         {
330           if (_M_current == _M_end)
331             __throw_regex_error(regex_constants::error_escape);
332           _M_token = _S_token_ord_char;
333           _M_value.assign(1, *_M_current++);
334         }
335       else if (__c == 'x' || __c == 'u')
336         {
337           _M_value.erase();
338           for (int __i = 0; __i < (__c == 'x' ? 2 : 4); __i++)
339             {
340               if (_M_current == _M_end
341                   || !_M_ctype.is(_CtypeT::xdigit, *_M_current))
342                 __throw_regex_error(regex_constants::error_escape);
343               _M_value += *_M_current++;
344             }
345           _M_token = _S_token_hex_num;
346         }
347       // ECMAScript recognizes multi-digit back-references.
348       else if (_M_ctype.is(_CtypeT::digit, __c))
349         {
350           _M_value.assign(1, __c);
351           while (_M_current != _M_end
352                  && _M_ctype.is(_CtypeT::digit, *_M_current))
353             _M_value += *_M_current++;
354           _M_token = _S_token_backref;
355         }
356       else
357         {
358           _M_token = _S_token_ord_char;
359           _M_value.assign(1, __c);
360         }
361     }
363   // Differences between styles:
364   // 1) Extended doesn't support backref, but basic does.
365   template<typename _CharT>
366     void
367     _Scanner<_CharT>::
368     _M_eat_escape_posix()
369     {
370       if (_M_current == _M_end)
371         __throw_regex_error(regex_constants::error_escape);
373       auto __c = *_M_current;
374       auto __pos = std::strchr(_M_spec_char, _M_ctype.narrow(__c, '\0'));
376       if (__pos != nullptr && *__pos != '\0')
377         {
378           _M_token = _S_token_ord_char;
379           _M_value.assign(1, __c);
380         }
381       // We MUST judge awk before handling backrefs. There's no backref in awk.
382       else if (_M_is_awk())
383         {
384           _M_eat_escape_awk();
385           return;
386         }
387       else if (_M_is_basic() && _M_ctype.is(_CtypeT::digit, __c) && __c != '0')
388         {
389           _M_token = _S_token_backref;
390           _M_value.assign(1, __c);
391         }
392       else
393         {
394 #ifdef __STRICT_ANSI__
395           // POSIX says it is undefined to escape ordinary characters
396           __throw_regex_error(regex_constants::error_escape);
397 #else
398           _M_token = _S_token_ord_char;
399           _M_value.assign(1, __c);
400 #endif
401         }
402       ++_M_current;
403     }
405   template<typename _CharT>
406     void
407     _Scanner<_CharT>::
408     _M_eat_escape_awk()
409     {
410       auto __c = *_M_current++;
411       auto __pos = _M_find_escape(_M_ctype.narrow(__c, '\0'));
413       if (__pos != nullptr)
414         {
415           _M_token = _S_token_ord_char;
416           _M_value.assign(1, *__pos);
417         }
418       // \ddd for oct representation
419       else if (_M_ctype.is(_CtypeT::digit, __c)
420                && __c != '8'
421                && __c != '9')
422         {
423           _M_value.assign(1,  __c);
424           for (int __i = 0;
425                __i < 2
426                && _M_current != _M_end
427                && _M_ctype.is(_CtypeT::digit, *_M_current)
428                && *_M_current != '8'
429                && *_M_current != '9';
430                __i++)
431             _M_value += *_M_current++;
432           _M_token = _S_token_oct_num;
433           return;
434         }
435       else
436         __throw_regex_error(regex_constants::error_escape);
437     }
439   // Eats a character class or throws an exception.
440   // __ch could be ':', '.' or '=', _M_current is the char after ']' when
441   // returning.
442   template<typename _CharT>
443     void
444     _Scanner<_CharT>::
445     _M_eat_class(char __ch)
446     {
447       for (_M_value.clear(); _M_current != _M_end && *_M_current != __ch;)
448         _M_value += *_M_current++;
449       if (_M_current == _M_end
450           || *_M_current++ != __ch
451           || _M_current == _M_end // skip __ch
452           || *_M_current++ != ']') // skip ']'
453         {
454           if (__ch == ':')
455             __throw_regex_error(regex_constants::error_ctype);
456           else
457             __throw_regex_error(regex_constants::error_collate);
458         }
459     }
461 #ifdef _GLIBCXX_DEBUG
462   template<typename _CharT>
463     std::ostream&
464     _Scanner<_CharT>::
465     _M_print(std::ostream& ostr)
466     {
467       switch (_M_token)
468       {
469       case _S_token_anychar:
470         ostr << "any-character\n";
471         break;
472       case _S_token_backref:
473         ostr << "backref\n";
474         break;
475       case _S_token_bracket_begin:
476         ostr << "bracket-begin\n";
477         break;
478       case _S_token_bracket_neg_begin:
479         ostr << "bracket-neg-begin\n";
480         break;
481       case _S_token_bracket_end:
482         ostr << "bracket-end\n";
483         break;
484       case _S_token_char_class_name:
485         ostr << "char-class-name \"" << _M_value << "\"\n";
486         break;
487       case _S_token_closure0:
488         ostr << "closure0\n";
489         break;
490       case _S_token_closure1:
491         ostr << "closure1\n";
492         break;
493       case _S_token_collsymbol:
494         ostr << "collsymbol \"" << _M_value << "\"\n";
495         break;
496       case _S_token_comma:
497         ostr << "comma\n";
498         break;
499       case _S_token_dup_count:
500         ostr << "dup count: " << _M_value << "\n";
501         break;
502       case _S_token_eof:
503         ostr << "EOF\n";
504         break;
505       case _S_token_equiv_class_name:
506         ostr << "equiv-class-name \"" << _M_value << "\"\n";
507         break;
508       case _S_token_interval_begin:
509         ostr << "interval begin\n";
510         break;
511       case _S_token_interval_end:
512         ostr << "interval end\n";
513         break;
514       case _S_token_line_begin:
515         ostr << "line begin\n";
516         break;
517       case _S_token_line_end:
518         ostr << "line end\n";
519         break;
520       case _S_token_opt:
521         ostr << "opt\n";
522         break;
523       case _S_token_or:
524         ostr << "or\n";
525         break;
526       case _S_token_ord_char:
527         ostr << "ordinary character: \"" << _M_value << "\"\n";
528         break;
529       case _S_token_subexpr_begin:
530         ostr << "subexpr begin\n";
531         break;
532       case _S_token_subexpr_no_group_begin:
533         ostr << "no grouping subexpr begin\n";
534         break;
535       case _S_token_subexpr_lookahead_begin:
536         ostr << "lookahead subexpr begin\n";
537         break;
538       case _S_token_subexpr_end:
539         ostr << "subexpr end\n";
540         break;
541       case _S_token_unknown:
542         ostr << "-- unknown token --\n";
543         break;
544       case _S_token_oct_num:
545         ostr << "oct number " << _M_value << "\n";
546         break;
547       case _S_token_hex_num:
548         ostr << "hex number " << _M_value << "\n";
549         break;
550       case _S_token_quoted_class:
551         ostr << "quoted class " << "\\" << _M_value << "\n";
552         break;
553       default:
554         _GLIBCXX_DEBUG_ASSERT(false);
555       }
556       return ostr;
557     }
558 #endif
560 _GLIBCXX_END_NAMESPACE_VERSION
561 } // namespace __detail
562 } // namespace