libstdc++
regex_compiler.h
Go to the documentation of this file.
1 // class template regex -*- C++ -*-
2 
3 // Copyright (C) 2010-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.
10 
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.
15 
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.
19 
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/>.
24 
25 /**
26  * @file bits/regex_compiler.h
27  * This is an internal header file, included by other library headers.
28  * Do not attempt to use it directly. @headername{regex}
29  */
30 
31 namespace std _GLIBCXX_VISIBILITY(default)
32 {
33 namespace __detail
34 {
35 _GLIBCXX_BEGIN_NAMESPACE_VERSION
36 
37  /**
38  * @addtogroup regex-detail
39  * @{
40  */
41 
42  template<typename, bool, bool>
44 
45  /// Builds an NFA from an input iterator interval.
46  template<typename _TraitsT>
47  class _Compiler
48  {
49  public:
50  typedef typename _TraitsT::char_type _CharT;
51  typedef const _CharT* _IterT;
52  typedef _NFA<_TraitsT> _RegexT;
54 
55  _Compiler(_IterT __b, _IterT __e,
56  const _TraitsT& __traits, _FlagT __flags);
57 
59  _M_get_nfa()
60  { return make_shared<_RegexT>(std::move(_M_nfa)); }
61 
62  private:
63  typedef _Scanner<_CharT> _ScannerT;
64  typedef typename _TraitsT::string_type _StringT;
65  typedef typename _ScannerT::_TokenT _TokenT;
69 
70  // accepts a specific token or returns false.
71  bool
72  _M_match_token(_TokenT __token);
73 
74  void
75  _M_disjunction();
76 
77  void
78  _M_alternative();
79 
80  bool
81  _M_term();
82 
83  bool
84  _M_assertion();
85 
86  bool
87  _M_quantifier();
88 
89  bool
90  _M_atom();
91 
92  bool
93  _M_bracket_expression();
94 
95  template<bool __icase, bool __collate>
96  void
97  _M_insert_any_matcher_ecma();
98 
99  template<bool __icase, bool __collate>
100  void
101  _M_insert_any_matcher_posix();
102 
103  template<bool __icase, bool __collate>
104  void
105  _M_insert_char_matcher();
106 
107  template<bool __icase, bool __collate>
108  void
109  _M_insert_character_class_matcher();
110 
111  template<bool __icase, bool __collate>
112  void
113  _M_insert_bracket_matcher(bool __neg);
114 
115  template<bool __icase, bool __collate>
116  void
118  __matcher);
119 
120  int
121  _M_cur_int_value(int __radix);
122 
123  bool
124  _M_try_char();
125 
126  _StateSeqT
127  _M_pop()
128  {
129  auto ret = _M_stack.top();
130  _M_stack.pop();
131  return ret;
132  }
133 
134  _FlagT _M_flags;
135  const _TraitsT& _M_traits;
136  const _CtypeT& _M_ctype;
137  _ScannerT _M_scanner;
138  _RegexT _M_nfa;
139  _StringT _M_value;
140  _StackT _M_stack;
141  };
142 
143  template<typename _TraitsT>
145  __compile_nfa(const typename _TraitsT::char_type* __first,
146  const typename _TraitsT::char_type* __last,
147  const _TraitsT& __traits,
149  {
150  using _Cmplr = _Compiler<_TraitsT>;
151  return _Cmplr(__first, __last, __traits, __flags)._M_get_nfa();
152  }
153 
154  // [28.13.14]
155  template<typename _TraitsT, bool __icase, bool __collate>
156  class _RegexTranslator
157  {
158  public:
159  typedef typename _TraitsT::char_type _CharT;
160  typedef typename _TraitsT::string_type _StringT;
161  typedef typename std::conditional<__collate,
162  _StringT,
163  _CharT>::type _StrTransT;
164 
165  explicit
166  _RegexTranslator(const _TraitsT& __traits)
167  : _M_traits(__traits)
168  { }
169 
170  _CharT
171  _M_translate(_CharT __ch) const
172  {
173  if (__icase)
174  return _M_traits.translate_nocase(__ch);
175  else if (__collate)
176  return _M_traits.translate(__ch);
177  else
178  return __ch;
179  }
180 
181  _StrTransT
182  _M_transform(_CharT __ch) const
183  {
184  return _M_transform_impl(__ch, typename integral_constant<bool,
185  __collate>::type());
186  }
187 
188  private:
189  _StrTransT
190  _M_transform_impl(_CharT __ch, false_type) const
191  { return __ch; }
192 
193  _StrTransT
194  _M_transform_impl(_CharT __ch, true_type) const
195  {
196  _StrTransT __str = _StrTransT(1, _M_translate(__ch));
197  return _M_traits.transform(__str.begin(), __str.end());
198  }
199 
200  const _TraitsT& _M_traits;
201  };
202 
203  template<typename _TraitsT>
204  class _RegexTranslator<_TraitsT, false, false>
205  {
206  public:
207  typedef typename _TraitsT::char_type _CharT;
208  typedef _CharT _StrTransT;
209 
210  explicit
211  _RegexTranslator(const _TraitsT& __traits)
212  { }
213 
214  _CharT
215  _M_translate(_CharT __ch) const
216  { return __ch; }
217 
218  _StrTransT
219  _M_transform(_CharT __ch) const
220  { return __ch; }
221  };
222 
223  template<typename _TraitsT, bool __is_ecma, bool __icase, bool __collate>
224  struct _AnyMatcher;
225 
226  template<typename _TraitsT, bool __icase, bool __collate>
227  struct _AnyMatcher<_TraitsT, false, __icase, __collate>
228  {
229  typedef _RegexTranslator<_TraitsT, __icase, __collate> _TransT;
230  typedef typename _TransT::_CharT _CharT;
231 
232  explicit
233  _AnyMatcher(const _TraitsT& __traits)
234  : _M_translator(__traits)
235  { }
236 
237  bool
238  operator()(_CharT __ch) const
239  {
240  static auto __nul = _M_translator._M_translate('\0');
241  return _M_translator._M_translate(__ch) != __nul;
242  }
243 
244  _TransT _M_translator;
245  };
246 
247  template<typename _TraitsT, bool __icase, bool __collate>
248  struct _AnyMatcher<_TraitsT, true, __icase, __collate>
249  {
250  typedef _RegexTranslator<_TraitsT, __icase, __collate> _TransT;
251  typedef typename _TransT::_CharT _CharT;
252 
253  explicit
254  _AnyMatcher(const _TraitsT& __traits)
255  : _M_translator(__traits)
256  { }
257 
258  bool
259  operator()(_CharT __ch) const
260  { return _M_apply(__ch, typename is_same<_CharT, char>::type()); }
261 
262  bool
263  _M_apply(_CharT __ch, true_type) const
264  {
265  auto __c = _M_translator._M_translate(__ch);
266  auto __n = _M_translator._M_translate('\n');
267  auto __r = _M_translator._M_translate('\r');
268  return __c != __n && __c != __r;
269  }
270 
271  bool
272  _M_apply(_CharT __ch, false_type) const
273  {
274  auto __c = _M_translator._M_translate(__ch);
275  auto __n = _M_translator._M_translate('\n');
276  auto __r = _M_translator._M_translate('\r');
277  auto __u2028 = _M_translator._M_translate(u'\u2028');
278  auto __u2029 = _M_translator._M_translate(u'\u2029');
279  return __c != __n && __c != __r && __c != __u2028 && __c != __u2029;
280  }
281 
282  _TransT _M_translator;
283  };
284 
285  template<typename _TraitsT, bool __icase, bool __collate>
286  struct _CharMatcher
287  {
288  typedef _RegexTranslator<_TraitsT, __icase, __collate> _TransT;
289  typedef typename _TransT::_CharT _CharT;
290 
291  _CharMatcher(_CharT __ch, const _TraitsT& __traits)
292  : _M_translator(__traits), _M_ch(_M_translator._M_translate(__ch))
293  { }
294 
295  bool
296  operator()(_CharT __ch) const
297  { return _M_ch == _M_translator._M_translate(__ch); }
298 
299  _TransT _M_translator;
300  _CharT _M_ch;
301  };
302 
303  /// Matches a character range (bracket expression)
304  template<typename _TraitsT, bool __icase, bool __collate>
305  struct _BracketMatcher
306  {
307  public:
308  typedef _RegexTranslator<_TraitsT, __icase, __collate> _TransT;
309  typedef typename _TransT::_CharT _CharT;
310  typedef typename _TransT::_StrTransT _StrTransT;
311  typedef typename _TraitsT::string_type _StringT;
312  typedef typename _TraitsT::char_class_type _CharClassT;
313 
314  public:
315  _BracketMatcher(bool __is_non_matching,
316  const _TraitsT& __traits)
317  : _M_class_set(0), _M_translator(__traits), _M_traits(__traits),
318  _M_is_non_matching(__is_non_matching)
319 #ifdef _GLIBCXX_DEBUG
320  , _M_is_ready(false)
321 #endif
322  { }
323 
324  bool
325  operator()(_CharT __ch) const
326  {
327  _GLIBCXX_DEBUG_ASSERT(_M_is_ready);
328  return _M_apply(__ch, _IsChar());
329  }
330 
331  void
332  _M_add_char(_CharT __c)
333  {
334  _M_char_set.push_back(_M_translator._M_translate(__c));
335 #ifdef _GLIBCXX_DEBUG
336  _M_is_ready = false;
337 #endif
338  }
339 
340  void
341  _M_add_collating_element(const _StringT& __s)
342  {
343  auto __st = _M_traits.lookup_collatename(__s.data(),
344  __s.data() + __s.size());
345  if (__st.empty())
346  __throw_regex_error(regex_constants::error_collate);
347  _M_char_set.push_back(_M_translator._M_translate(__st[0]));
348 #ifdef _GLIBCXX_DEBUG
349  _M_is_ready = false;
350 #endif
351  }
352 
353  void
354  _M_add_equivalence_class(const _StringT& __s)
355  {
356  auto __st = _M_traits.lookup_collatename(__s.data(),
357  __s.data() + __s.size());
358  if (__st.empty())
359  __throw_regex_error(regex_constants::error_collate);
360  __st = _M_traits.transform_primary(__st.data(),
361  __st.data() + __st.size());
362  _M_equiv_set.push_back(__st);
363 #ifdef _GLIBCXX_DEBUG
364  _M_is_ready = false;
365 #endif
366  }
367 
368  void
369  _M_add_character_class(const _StringT& __s)
370  {
371  auto __mask = _M_traits.lookup_classname(__s.data(),
372  __s.data() + __s.size(),
373  __icase);
374  if (__mask == 0)
375  __throw_regex_error(regex_constants::error_ctype);
376  _M_class_set |= __mask;
377 #ifdef _GLIBCXX_DEBUG
378  _M_is_ready = false;
379 #endif
380  }
381 
382  void
383  _M_make_range(_CharT __l, _CharT __r)
384  {
385  _M_range_set.push_back(make_pair(_M_translator._M_transform(__l),
386  _M_translator._M_transform(__r)));
387 #ifdef _GLIBCXX_DEBUG
388  _M_is_ready = false;
389 #endif
390  }
391 
392  void
393  _M_ready()
394  {
395  _M_make_cache(_IsChar());
396 #ifdef _GLIBCXX_DEBUG
397  _M_is_ready = true;
398 #endif
399  }
400 
401  private:
402  typedef typename is_same<_CharT, char>::type _IsChar;
403  struct _Dummy { };
404  typedef typename conditional<_IsChar::value,
405  std::bitset<1 << (8 * sizeof(_CharT))>,
406  _Dummy>::type _CacheT;
407  typedef typename make_unsigned<_CharT>::type _UnsignedCharT;
408 
409  private:
410  bool
411  _M_apply(_CharT __ch, false_type) const;
412 
413  bool
414  _M_apply(_CharT __ch, true_type) const
415  { return _M_cache[static_cast<_UnsignedCharT>(__ch)]; }
416 
417  void
418  _M_make_cache(true_type)
419  {
420  for (int __i = 0; __i < _M_cache.size(); __i++)
421  _M_cache[static_cast<_UnsignedCharT>(__i)] =
422  _M_apply(__i, false_type());
423  }
424 
425  void
426  _M_make_cache(false_type)
427  { }
428 
429  private:
430  _CacheT _M_cache;
431  std::vector<_CharT> _M_char_set;
432  std::vector<_StringT> _M_equiv_set;
434  _CharClassT _M_class_set;
435  _TransT _M_translator;
436  const _TraitsT& _M_traits;
437  bool _M_is_non_matching;
438 #ifdef _GLIBCXX_DEBUG
439  bool _M_is_ready;
440 #endif
441  };
442 
443  //@} regex-detail
444 _GLIBCXX_END_NAMESPACE_VERSION
445 } // namespace __detail
446 } // namespace std
447 
448 #include <bits/regex_compiler.tcc>
constexpr error_type error_collate(_S_error_collate)
Describes a sequence of one or more _State, its current start and end(s). This structure contains fra...
integral_constant< bool, true > true_type
The type used as a compile-time boolean with true value.
Definition: type_traits:72
constexpr error_type error_ctype(_S_error_ctype)
void push_back(const value_type &__x)
Add data to the end of the vector.
Definition: stl_vector.h:913
reference top()
Definition: stl_stack.h:159
Builds an NFA from an input iterator interval.
void pop()
Removes first element.
Definition: stl_stack.h:212
struct _Scanner. Scans an input range for regex tokens.
syntax_option_type
This is a bitmask type indicating how to interpret the regex.
constexpr pair< typename __decay_and_strip< _T1 >::__type, typename __decay_and_strip< _T2 >::__type > make_pair(_T1 &&__x, _T2 &&__y)
A convenience wrapper for creating a pair from two objects.
Definition: stl_pair.h:276
integral_constant< bool, false > false_type
The type used as a compile-time boolean with false value.
Definition: type_traits:75
Primary class template ctype facet.This template class defines classification and conversion function...
A smart pointer with reference-counted copy semantics.
Definition: shared_ptr.h:93
Matches a character range (bracket expression)