libstdc++

regex_compiler.h

Go to the documentation of this file.
00001 // class template regex -*- C++ -*-
00002 
00003 // Copyright (C) 2010-2015 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /**
00026  *  @file bits/regex_compiler.h
00027  *  This is an internal header file, included by other library headers.
00028  *  Do not attempt to use it directly. @headername{regex}
00029  */
00030 
00031 namespace std _GLIBCXX_VISIBILITY(default)
00032 {
00033 namespace __detail
00034 {
00035 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00036 
00037   /**
00038    * @addtogroup regex-detail
00039    * @{
00040    */
00041 
00042   template<typename, bool, bool>
00043     struct _BracketMatcher;
00044 
00045   /**
00046    * @brief Builds an NFA from an input iterator range.
00047    *
00048    * The %_TraitsT type should fulfill requirements [28.3].
00049    */
00050   template<typename _TraitsT>
00051     class _Compiler
00052     {
00053     public:
00054       typedef typename _TraitsT::char_type        _CharT;
00055       typedef const _CharT*                       _IterT;
00056       typedef _NFA<_TraitsT>                      _RegexT;
00057       typedef regex_constants::syntax_option_type _FlagT;
00058 
00059       _Compiler(_IterT __b, _IterT __e,
00060                 const typename _TraitsT::locale_type& __traits, _FlagT __flags);
00061 
00062       shared_ptr<const _RegexT>
00063       _M_get_nfa()
00064       { return std::move(_M_nfa); }
00065 
00066     private:
00067       typedef _Scanner<_CharT>               _ScannerT;
00068       typedef typename _TraitsT::string_type _StringT;
00069       typedef typename _ScannerT::_TokenT    _TokenT;
00070       typedef _StateSeq<_TraitsT>            _StateSeqT;
00071       typedef std::stack<_StateSeqT>         _StackT;
00072       typedef std::ctype<_CharT>             _CtypeT;
00073 
00074       // accepts a specific token or returns false.
00075       bool
00076       _M_match_token(_TokenT __token);
00077 
00078       void
00079       _M_disjunction();
00080 
00081       void
00082       _M_alternative();
00083 
00084       bool
00085       _M_term();
00086 
00087       bool
00088       _M_assertion();
00089 
00090       bool
00091       _M_quantifier();
00092 
00093       bool
00094       _M_atom();
00095 
00096       bool
00097       _M_bracket_expression();
00098 
00099       template<bool __icase, bool __collate>
00100         void
00101         _M_insert_any_matcher_ecma();
00102 
00103       template<bool __icase, bool __collate>
00104         void
00105         _M_insert_any_matcher_posix();
00106 
00107       template<bool __icase, bool __collate>
00108         void
00109         _M_insert_char_matcher();
00110 
00111       template<bool __icase, bool __collate>
00112         void
00113         _M_insert_character_class_matcher();
00114 
00115       template<bool __icase, bool __collate>
00116         void
00117         _M_insert_bracket_matcher(bool __neg);
00118 
00119       template<bool __icase, bool __collate>
00120         void
00121         _M_expression_term(pair<bool, _CharT>& __last_char,
00122                            _BracketMatcher<_TraitsT, __icase, __collate>&
00123                            __matcher);
00124 
00125       int
00126       _M_cur_int_value(int __radix);
00127 
00128       bool
00129       _M_try_char();
00130 
00131       _StateSeqT
00132       _M_pop()
00133       {
00134         auto ret = _M_stack.top();
00135         _M_stack.pop();
00136         return ret;
00137       }
00138 
00139       _FlagT              _M_flags;
00140       _ScannerT           _M_scanner;
00141       shared_ptr<_RegexT> _M_nfa;
00142       _StringT            _M_value;
00143       _StackT             _M_stack;
00144       const _TraitsT&     _M_traits;
00145       const _CtypeT&      _M_ctype;
00146     };
00147 
00148   template<typename _Tp>
00149     struct __has_contiguous_iter : std::false_type { };
00150 
00151   template<typename _Ch, typename _Tr, typename _Alloc>
00152     struct __has_contiguous_iter<std::basic_string<_Ch, _Tr, _Alloc>>
00153     : std::true_type
00154     { };
00155 
00156   template<typename _Tp, typename _Alloc>
00157     struct __has_contiguous_iter<std::vector<_Tp, _Alloc>>
00158     : std::true_type
00159     { };
00160 
00161   template<typename _Tp>
00162     struct __is_contiguous_normal_iter : std::false_type { };
00163 
00164   template<typename _CharT>
00165     struct __is_contiguous_normal_iter<_CharT*> : std::true_type { };
00166 
00167   template<typename _Tp, typename _Cont>
00168     struct
00169     __is_contiguous_normal_iter<__gnu_cxx::__normal_iterator<_Tp, _Cont>>
00170     : __has_contiguous_iter<_Cont>::type
00171     { };
00172 
00173   template<typename _Iter, typename _TraitsT>
00174     using __enable_if_contiguous_normal_iter
00175       = typename enable_if< __is_contiguous_normal_iter<_Iter>::value,
00176                            std::shared_ptr<const _NFA<_TraitsT>> >::type;
00177 
00178   template<typename _Iter, typename _TraitsT>
00179     using __disable_if_contiguous_normal_iter
00180       = typename enable_if< !__is_contiguous_normal_iter<_Iter>::value,
00181                            std::shared_ptr<const _NFA<_TraitsT>> >::type;
00182 
00183   template<typename _FwdIter, typename _TraitsT>
00184     inline __enable_if_contiguous_normal_iter<_FwdIter, _TraitsT>
00185     __compile_nfa(_FwdIter __first, _FwdIter __last,
00186                   const typename _TraitsT::locale_type& __loc,
00187                   regex_constants::syntax_option_type __flags)
00188     {
00189       size_t __len = __last - __first;
00190       const auto* __cfirst = __len ? std::__addressof(*__first) : nullptr;
00191       using _Cmplr = _Compiler<_TraitsT>;
00192       return _Cmplr(__cfirst, __cfirst + __len, __loc, __flags)._M_get_nfa();
00193     }
00194 
00195   template<typename _FwdIter, typename _TraitsT>
00196     inline __disable_if_contiguous_normal_iter<_FwdIter, _TraitsT>
00197     __compile_nfa(_FwdIter __first, _FwdIter __last,
00198                   const typename _TraitsT::locale_type& __loc,
00199                   regex_constants::syntax_option_type __flags)
00200     {
00201       basic_string<typename _TraitsT::char_type> __str(__first, __last);
00202       return __compile_nfa(__str.data(), __str.data() + __str.size(), __loc,
00203           __flags);
00204     }
00205 
00206   // [28.13.14]
00207   template<typename _TraitsT, bool __icase, bool __collate>
00208     class _RegexTranslator
00209     {
00210     public:
00211       typedef typename _TraitsT::char_type            _CharT;
00212       typedef typename _TraitsT::string_type          _StringT;
00213       typedef typename std::conditional<__collate,
00214                                         _StringT,
00215                                         _CharT>::type _StrTransT;
00216 
00217       explicit
00218       _RegexTranslator(const _TraitsT& __traits)
00219       : _M_traits(__traits)
00220       { }
00221 
00222       _CharT
00223       _M_translate(_CharT __ch) const
00224       {
00225         if (__icase)
00226           return _M_traits.translate_nocase(__ch);
00227         else if (__collate)
00228           return _M_traits.translate(__ch);
00229         else
00230           return __ch;
00231       }
00232 
00233       _StrTransT
00234       _M_transform(_CharT __ch) const
00235       {
00236         return _M_transform_impl(__ch, typename integral_constant<bool,
00237                                  __collate>::type());
00238       }
00239 
00240     private:
00241       _StrTransT
00242       _M_transform_impl(_CharT __ch, false_type) const
00243       { return __ch; }
00244 
00245       _StrTransT
00246       _M_transform_impl(_CharT __ch, true_type) const
00247       {
00248         _StrTransT __str = _StrTransT(1, _M_translate(__ch));
00249         return _M_traits.transform(__str.begin(), __str.end());
00250       }
00251 
00252       const _TraitsT& _M_traits;
00253     };
00254 
00255   template<typename _TraitsT>
00256     class _RegexTranslator<_TraitsT, false, false>
00257     {
00258     public:
00259       typedef typename _TraitsT::char_type _CharT;
00260       typedef _CharT                       _StrTransT;
00261 
00262       explicit
00263       _RegexTranslator(const _TraitsT&)
00264       { }
00265 
00266       _CharT
00267       _M_translate(_CharT __ch) const
00268       { return __ch; }
00269 
00270       _StrTransT
00271       _M_transform(_CharT __ch) const
00272       { return __ch; }
00273     };
00274 
00275   template<typename _TraitsT, bool __is_ecma, bool __icase, bool __collate>
00276     struct _AnyMatcher;
00277 
00278   template<typename _TraitsT, bool __icase, bool __collate>
00279     struct _AnyMatcher<_TraitsT, false, __icase, __collate>
00280     {
00281       typedef _RegexTranslator<_TraitsT, __icase, __collate> _TransT;
00282       typedef typename _TransT::_CharT                       _CharT;
00283 
00284       explicit
00285       _AnyMatcher(const _TraitsT& __traits)
00286       : _M_translator(__traits)
00287       { }
00288 
00289       bool
00290       operator()(_CharT __ch) const
00291       {
00292         static auto __nul = _M_translator._M_translate('\0');
00293         return _M_translator._M_translate(__ch) != __nul;
00294       }
00295 
00296       _TransT _M_translator;
00297     };
00298 
00299   template<typename _TraitsT, bool __icase, bool __collate>
00300     struct _AnyMatcher<_TraitsT, true, __icase, __collate>
00301     {
00302       typedef _RegexTranslator<_TraitsT, __icase, __collate> _TransT;
00303       typedef typename _TransT::_CharT                       _CharT;
00304 
00305       explicit
00306       _AnyMatcher(const _TraitsT& __traits)
00307       : _M_translator(__traits)
00308       { }
00309 
00310       bool
00311       operator()(_CharT __ch) const
00312       { return _M_apply(__ch, typename is_same<_CharT, char>::type()); }
00313 
00314       bool
00315       _M_apply(_CharT __ch, true_type) const
00316       {
00317         auto __c = _M_translator._M_translate(__ch);
00318         auto __n = _M_translator._M_translate('\n');
00319         auto __r = _M_translator._M_translate('\r');
00320         return __c != __n && __c != __r;
00321       }
00322 
00323       bool
00324       _M_apply(_CharT __ch, false_type) const
00325       {
00326         auto __c = _M_translator._M_translate(__ch);
00327         auto __n = _M_translator._M_translate('\n');
00328         auto __r = _M_translator._M_translate('\r');
00329         auto __u2028 = _M_translator._M_translate(u'\u2028');
00330         auto __u2029 = _M_translator._M_translate(u'\u2029');
00331         return __c != __n && __c != __r && __c != __u2028 && __c != __u2029;
00332       }
00333 
00334       _TransT _M_translator;
00335     };
00336 
00337   template<typename _TraitsT, bool __icase, bool __collate>
00338     struct _CharMatcher
00339     {
00340       typedef _RegexTranslator<_TraitsT, __icase, __collate> _TransT;
00341       typedef typename _TransT::_CharT                       _CharT;
00342 
00343       _CharMatcher(_CharT __ch, const _TraitsT& __traits)
00344       : _M_translator(__traits), _M_ch(_M_translator._M_translate(__ch))
00345       { }
00346 
00347       bool
00348       operator()(_CharT __ch) const
00349       { return _M_ch == _M_translator._M_translate(__ch); }
00350 
00351       _TransT _M_translator;
00352       _CharT  _M_ch;
00353     };
00354 
00355   /// Matches a character range (bracket expression)
00356   template<typename _TraitsT, bool __icase, bool __collate>
00357     struct _BracketMatcher
00358     {
00359     public:
00360       typedef _RegexTranslator<_TraitsT, __icase, __collate> _TransT;
00361       typedef typename _TransT::_CharT                       _CharT;
00362       typedef typename _TransT::_StrTransT                   _StrTransT;
00363       typedef typename _TraitsT::string_type                 _StringT;
00364       typedef typename _TraitsT::char_class_type             _CharClassT;
00365 
00366     public:
00367       _BracketMatcher(bool __is_non_matching,
00368                       const _TraitsT& __traits)
00369       : _M_class_set(0), _M_translator(__traits), _M_traits(__traits),
00370       _M_is_non_matching(__is_non_matching)
00371 #ifdef _GLIBCXX_DEBUG
00372       , _M_is_ready(false)
00373 #endif
00374       { }
00375 
00376       bool
00377       operator()(_CharT __ch) const
00378       {
00379         _GLIBCXX_DEBUG_ASSERT(_M_is_ready);
00380         return _M_apply(__ch, _UseCache());
00381       }
00382 
00383       void
00384       _M_add_char(_CharT __c)
00385       {
00386         _M_char_set.push_back(_M_translator._M_translate(__c));
00387 #ifdef _GLIBCXX_DEBUG
00388         _M_is_ready = false;
00389 #endif
00390       }
00391 
00392       void
00393       _M_add_collating_element(const _StringT& __s)
00394       {
00395         auto __st = _M_traits.lookup_collatename(__s.data(),
00396                                                  __s.data() + __s.size());
00397         if (__st.empty())
00398           __throw_regex_error(regex_constants::error_collate);
00399         _M_char_set.push_back(_M_translator._M_translate(__st[0]));
00400 #ifdef _GLIBCXX_DEBUG
00401         _M_is_ready = false;
00402 #endif
00403       }
00404 
00405       void
00406       _M_add_equivalence_class(const _StringT& __s)
00407       {
00408         auto __st = _M_traits.lookup_collatename(__s.data(),
00409                                                  __s.data() + __s.size());
00410         if (__st.empty())
00411           __throw_regex_error(regex_constants::error_collate);
00412         __st = _M_traits.transform_primary(__st.data(),
00413                                            __st.data() + __st.size());
00414         _M_equiv_set.push_back(__st);
00415 #ifdef _GLIBCXX_DEBUG
00416         _M_is_ready = false;
00417 #endif
00418       }
00419 
00420       // __neg should be true for \D, \S and \W only.
00421       void
00422       _M_add_character_class(const _StringT& __s, bool __neg)
00423       {
00424         auto __mask = _M_traits.lookup_classname(__s.data(),
00425                                                  __s.data() + __s.size(),
00426                                                  __icase);
00427         if (__mask == 0)
00428           __throw_regex_error(regex_constants::error_ctype);
00429         if (!__neg)
00430           _M_class_set |= __mask;
00431         else
00432           _M_neg_class_set.push_back(__mask);
00433 #ifdef _GLIBCXX_DEBUG
00434         _M_is_ready = false;
00435 #endif
00436       }
00437 
00438       void
00439       _M_make_range(_CharT __l, _CharT __r)
00440       {
00441         if (__l > __r)
00442           __throw_regex_error(regex_constants::error_range);
00443         _M_range_set.push_back(make_pair(_M_translator._M_transform(__l),
00444                                          _M_translator._M_transform(__r)));
00445 #ifdef _GLIBCXX_DEBUG
00446         _M_is_ready = false;
00447 #endif
00448       }
00449 
00450       void
00451       _M_ready()
00452       {
00453         std::sort(_M_char_set.begin(), _M_char_set.end());
00454         auto __end = std::unique(_M_char_set.begin(), _M_char_set.end());
00455         _M_char_set.erase(__end, _M_char_set.end());
00456         _M_make_cache(_UseCache());
00457 #ifdef _GLIBCXX_DEBUG
00458         _M_is_ready = true;
00459 #endif
00460       }
00461 
00462     private:
00463       // Currently we only use the cache for char
00464       typedef typename std::is_same<_CharT, char>::type _UseCache;
00465 
00466       static constexpr size_t
00467       _S_cache_size()
00468       {
00469         return 1ul << (sizeof(_CharT) * __CHAR_BIT__ * int(_UseCache::value));
00470       }
00471 
00472       struct _Dummy { };
00473       typedef typename std::conditional<_UseCache::value,
00474                                         std::bitset<_S_cache_size()>,
00475                                         _Dummy>::type _CacheT;
00476       typedef typename std::make_unsigned<_CharT>::type _UnsignedCharT;
00477 
00478       bool
00479       _M_apply(_CharT __ch, false_type) const;
00480 
00481       bool
00482       _M_apply(_CharT __ch, true_type) const
00483       { return _M_cache[static_cast<_UnsignedCharT>(__ch)]; }
00484 
00485       void
00486       _M_make_cache(true_type)
00487       {
00488         for (unsigned __i = 0; __i < _M_cache.size(); __i++)
00489           _M_cache[__i] = _M_apply(static_cast<_CharT>(__i), false_type());
00490       }
00491 
00492       void
00493       _M_make_cache(false_type)
00494       { }
00495 
00496     private:
00497       std::vector<_CharT>                       _M_char_set;
00498       std::vector<_StringT>                     _M_equiv_set;
00499       std::vector<pair<_StrTransT, _StrTransT>> _M_range_set;
00500       std::vector<_CharClassT>                  _M_neg_class_set;
00501       _CharClassT                               _M_class_set;
00502       _TransT                                   _M_translator;
00503       const _TraitsT&                           _M_traits;
00504       bool                                      _M_is_non_matching;
00505       _CacheT                                   _M_cache;
00506 #ifdef _GLIBCXX_DEBUG
00507       bool                                      _M_is_ready;
00508 #endif
00509     };
00510 
00511  //@} regex-detail
00512 _GLIBCXX_END_NAMESPACE_VERSION
00513 } // namespace __detail
00514 } // namespace std
00515 
00516 #include <bits/regex_compiler.tcc>