libstdc++

regex_compiler.h

Go to the documentation of this file.
00001 // class template regex -*- C++ -*-
00002 
00003 // Copyright (C) 2010-2016 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       // Returns true if successfully matched one term and should continue.
00120       // Returns false if the compiler should move on.
00121       template<bool __icase, bool __collate>
00122         bool
00123         _M_expression_term(pair<bool, _CharT>& __last_char,
00124                            _BracketMatcher<_TraitsT, __icase, __collate>&
00125                            __matcher);
00126 
00127       int
00128       _M_cur_int_value(int __radix);
00129 
00130       bool
00131       _M_try_char();
00132 
00133       _StateSeqT
00134       _M_pop()
00135       {
00136         auto ret = _M_stack.top();
00137         _M_stack.pop();
00138         return ret;
00139       }
00140 
00141       _FlagT              _M_flags;
00142       _ScannerT           _M_scanner;
00143       shared_ptr<_RegexT> _M_nfa;
00144       _StringT            _M_value;
00145       _StackT             _M_stack;
00146       const _TraitsT&     _M_traits;
00147       const _CtypeT&      _M_ctype;
00148     };
00149 
00150   template<typename _Tp>
00151     struct __has_contiguous_iter : std::false_type { };
00152 
00153   template<typename _Ch, typename _Tr, typename _Alloc>
00154     struct __has_contiguous_iter<std::basic_string<_Ch, _Tr, _Alloc>>
00155     : std::true_type
00156     { };
00157 
00158   template<typename _Tp, typename _Alloc>
00159     struct __has_contiguous_iter<std::vector<_Tp, _Alloc>>
00160     : std::true_type
00161     { };
00162 
00163   template<typename _Tp>
00164     struct __is_contiguous_normal_iter : std::false_type { };
00165 
00166   template<typename _CharT>
00167     struct __is_contiguous_normal_iter<_CharT*> : std::true_type { };
00168 
00169   template<typename _Tp, typename _Cont>
00170     struct
00171     __is_contiguous_normal_iter<__gnu_cxx::__normal_iterator<_Tp, _Cont>>
00172     : __has_contiguous_iter<_Cont>::type
00173     { };
00174 
00175   template<typename _Iter, typename _TraitsT>
00176     using __enable_if_contiguous_normal_iter
00177       = typename enable_if< __is_contiguous_normal_iter<_Iter>::value,
00178                            std::shared_ptr<const _NFA<_TraitsT>> >::type;
00179 
00180   template<typename _Iter, typename _TraitsT>
00181     using __disable_if_contiguous_normal_iter
00182       = typename enable_if< !__is_contiguous_normal_iter<_Iter>::value,
00183                            std::shared_ptr<const _NFA<_TraitsT>> >::type;
00184 
00185   template<typename _FwdIter, typename _TraitsT>
00186     inline __enable_if_contiguous_normal_iter<_FwdIter, _TraitsT>
00187     __compile_nfa(_FwdIter __first, _FwdIter __last,
00188                   const typename _TraitsT::locale_type& __loc,
00189                   regex_constants::syntax_option_type __flags)
00190     {
00191       size_t __len = __last - __first;
00192       const auto* __cfirst = __len ? std::__addressof(*__first) : nullptr;
00193       using _Cmplr = _Compiler<_TraitsT>;
00194       return _Cmplr(__cfirst, __cfirst + __len, __loc, __flags)._M_get_nfa();
00195     }
00196 
00197   template<typename _FwdIter, typename _TraitsT>
00198     inline __disable_if_contiguous_normal_iter<_FwdIter, _TraitsT>
00199     __compile_nfa(_FwdIter __first, _FwdIter __last,
00200                   const typename _TraitsT::locale_type& __loc,
00201                   regex_constants::syntax_option_type __flags)
00202     {
00203       using char_type = typename _TraitsT::char_type;
00204       const basic_string<char_type> __str(__first, __last);
00205       return __compile_nfa<const char_type*, _TraitsT>(__str.data(),
00206           __str.data() + __str.size(), __loc, __flags);
00207     }
00208 
00209   // [28.13.14]
00210   template<typename _TraitsT, bool __icase, bool __collate>
00211     class _RegexTranslator
00212     {
00213     public:
00214       typedef typename _TraitsT::char_type            _CharT;
00215       typedef typename _TraitsT::string_type          _StringT;
00216       typedef typename std::conditional<__collate,
00217                                         _StringT,
00218                                         _CharT>::type _StrTransT;
00219 
00220       explicit
00221       _RegexTranslator(const _TraitsT& __traits)
00222       : _M_traits(__traits)
00223       { }
00224 
00225       _CharT
00226       _M_translate(_CharT __ch) const
00227       {
00228         if (__icase)
00229           return _M_traits.translate_nocase(__ch);
00230         else if (__collate)
00231           return _M_traits.translate(__ch);
00232         else
00233           return __ch;
00234       }
00235 
00236       _StrTransT
00237       _M_transform(_CharT __ch) const
00238       {
00239         return _M_transform_impl(__ch, typename integral_constant<bool,
00240                                  __collate>::type());
00241       }
00242 
00243     private:
00244       _StrTransT
00245       _M_transform_impl(_CharT __ch, false_type) const
00246       { return __ch; }
00247 
00248       _StrTransT
00249       _M_transform_impl(_CharT __ch, true_type) const
00250       {
00251         _StrTransT __str = _StrTransT(1, _M_translate(__ch));
00252         return _M_traits.transform(__str.begin(), __str.end());
00253       }
00254 
00255       const _TraitsT& _M_traits;
00256     };
00257 
00258   template<typename _TraitsT>
00259     class _RegexTranslator<_TraitsT, false, false>
00260     {
00261     public:
00262       typedef typename _TraitsT::char_type _CharT;
00263       typedef _CharT                       _StrTransT;
00264 
00265       explicit
00266       _RegexTranslator(const _TraitsT&)
00267       { }
00268 
00269       _CharT
00270       _M_translate(_CharT __ch) const
00271       { return __ch; }
00272 
00273       _StrTransT
00274       _M_transform(_CharT __ch) const
00275       { return __ch; }
00276     };
00277 
00278   template<typename _TraitsT, bool __is_ecma, bool __icase, bool __collate>
00279     struct _AnyMatcher;
00280 
00281   template<typename _TraitsT, bool __icase, bool __collate>
00282     struct _AnyMatcher<_TraitsT, false, __icase, __collate>
00283     {
00284       typedef _RegexTranslator<_TraitsT, __icase, __collate> _TransT;
00285       typedef typename _TransT::_CharT                       _CharT;
00286 
00287       explicit
00288       _AnyMatcher(const _TraitsT& __traits)
00289       : _M_translator(__traits)
00290       { }
00291 
00292       bool
00293       operator()(_CharT __ch) const
00294       {
00295         static auto __nul = _M_translator._M_translate('\0');
00296         return _M_translator._M_translate(__ch) != __nul;
00297       }
00298 
00299       _TransT _M_translator;
00300     };
00301 
00302   template<typename _TraitsT, bool __icase, bool __collate>
00303     struct _AnyMatcher<_TraitsT, true, __icase, __collate>
00304     {
00305       typedef _RegexTranslator<_TraitsT, __icase, __collate> _TransT;
00306       typedef typename _TransT::_CharT                       _CharT;
00307 
00308       explicit
00309       _AnyMatcher(const _TraitsT& __traits)
00310       : _M_translator(__traits)
00311       { }
00312 
00313       bool
00314       operator()(_CharT __ch) const
00315       { return _M_apply(__ch, typename is_same<_CharT, char>::type()); }
00316 
00317       bool
00318       _M_apply(_CharT __ch, true_type) const
00319       {
00320         auto __c = _M_translator._M_translate(__ch);
00321         auto __n = _M_translator._M_translate('\n');
00322         auto __r = _M_translator._M_translate('\r');
00323         return __c != __n && __c != __r;
00324       }
00325 
00326       bool
00327       _M_apply(_CharT __ch, false_type) const
00328       {
00329         auto __c = _M_translator._M_translate(__ch);
00330         auto __n = _M_translator._M_translate('\n');
00331         auto __r = _M_translator._M_translate('\r');
00332         auto __u2028 = _M_translator._M_translate(u'\u2028');
00333         auto __u2029 = _M_translator._M_translate(u'\u2029');
00334         return __c != __n && __c != __r && __c != __u2028 && __c != __u2029;
00335       }
00336 
00337       _TransT _M_translator;
00338     };
00339 
00340   template<typename _TraitsT, bool __icase, bool __collate>
00341     struct _CharMatcher
00342     {
00343       typedef _RegexTranslator<_TraitsT, __icase, __collate> _TransT;
00344       typedef typename _TransT::_CharT                       _CharT;
00345 
00346       _CharMatcher(_CharT __ch, const _TraitsT& __traits)
00347       : _M_translator(__traits), _M_ch(_M_translator._M_translate(__ch))
00348       { }
00349 
00350       bool
00351       operator()(_CharT __ch) const
00352       { return _M_ch == _M_translator._M_translate(__ch); }
00353 
00354       _TransT _M_translator;
00355       _CharT  _M_ch;
00356     };
00357 
00358   /// Matches a character range (bracket expression)
00359   template<typename _TraitsT, bool __icase, bool __collate>
00360     struct _BracketMatcher
00361     {
00362     public:
00363       typedef _RegexTranslator<_TraitsT, __icase, __collate> _TransT;
00364       typedef typename _TransT::_CharT                       _CharT;
00365       typedef typename _TransT::_StrTransT                   _StrTransT;
00366       typedef typename _TraitsT::string_type                 _StringT;
00367       typedef typename _TraitsT::char_class_type             _CharClassT;
00368 
00369     public:
00370       _BracketMatcher(bool __is_non_matching,
00371                       const _TraitsT& __traits)
00372       : _M_class_set(0), _M_translator(__traits), _M_traits(__traits),
00373       _M_is_non_matching(__is_non_matching)
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         _GLIBCXX_DEBUG_ONLY(_M_is_ready = false);
00388       }
00389 
00390       _StringT
00391       _M_add_collate_element(const _StringT& __s)
00392       {
00393         auto __st = _M_traits.lookup_collatename(__s.data(),
00394                                                  __s.data() + __s.size());
00395         if (__st.empty())
00396           __throw_regex_error(regex_constants::error_collate,
00397                               "Invalid collate element.");
00398         _M_char_set.push_back(_M_translator._M_translate(__st[0]));
00399         _GLIBCXX_DEBUG_ONLY(_M_is_ready = false);
00400         return __st;
00401       }
00402 
00403       void
00404       _M_add_equivalence_class(const _StringT& __s)
00405       {
00406         auto __st = _M_traits.lookup_collatename(__s.data(),
00407                                                  __s.data() + __s.size());
00408         if (__st.empty())
00409           __throw_regex_error(regex_constants::error_collate,
00410                               "Invalid equivalence class.");
00411         __st = _M_traits.transform_primary(__st.data(),
00412                                            __st.data() + __st.size());
00413         _M_equiv_set.push_back(__st);
00414         _GLIBCXX_DEBUG_ONLY(_M_is_ready = false);
00415       }
00416 
00417       // __neg should be true for \D, \S and \W only.
00418       void
00419       _M_add_character_class(const _StringT& __s, bool __neg)
00420       {
00421         auto __mask = _M_traits.lookup_classname(__s.data(),
00422                                                  __s.data() + __s.size(),
00423                                                  __icase);
00424         if (__mask == 0)
00425           __throw_regex_error(regex_constants::error_collate,
00426                               "Invalid character class.");
00427         if (!__neg)
00428           _M_class_set |= __mask;
00429         else
00430           _M_neg_class_set.push_back(__mask);
00431         _GLIBCXX_DEBUG_ONLY(_M_is_ready = false);
00432       }
00433 
00434       void
00435       _M_make_range(_CharT __l, _CharT __r)
00436       {
00437         if (__l > __r)
00438           __throw_regex_error(regex_constants::error_range,
00439                               "Invalid range in bracket expression.");
00440         _M_range_set.push_back(make_pair(_M_translator._M_transform(__l),
00441                                          _M_translator._M_transform(__r)));
00442         _GLIBCXX_DEBUG_ONLY(_M_is_ready = false);
00443       }
00444 
00445       void
00446       _M_ready()
00447       {
00448         std::sort(_M_char_set.begin(), _M_char_set.end());
00449         auto __end = std::unique(_M_char_set.begin(), _M_char_set.end());
00450         _M_char_set.erase(__end, _M_char_set.end());
00451         _M_make_cache(_UseCache());
00452         _GLIBCXX_DEBUG_ONLY(_M_is_ready = true);
00453       }
00454 
00455     private:
00456       // Currently we only use the cache for char
00457       typedef typename std::is_same<_CharT, char>::type _UseCache;
00458 
00459       static constexpr size_t
00460       _S_cache_size()
00461       {
00462         return 1ul << (sizeof(_CharT) * __CHAR_BIT__ * int(_UseCache::value));
00463       }
00464 
00465       struct _Dummy { };
00466       typedef typename std::conditional<_UseCache::value,
00467                                         std::bitset<_S_cache_size()>,
00468                                         _Dummy>::type _CacheT;
00469       typedef typename std::make_unsigned<_CharT>::type _UnsignedCharT;
00470 
00471       bool
00472       _M_apply(_CharT __ch, false_type) const;
00473 
00474       bool
00475       _M_apply(_CharT __ch, true_type) const
00476       { return _M_cache[static_cast<_UnsignedCharT>(__ch)]; }
00477 
00478       void
00479       _M_make_cache(true_type)
00480       {
00481         for (unsigned __i = 0; __i < _M_cache.size(); __i++)
00482           _M_cache[__i] = _M_apply(static_cast<_CharT>(__i), false_type());
00483       }
00484 
00485       void
00486       _M_make_cache(false_type)
00487       { }
00488 
00489     private:
00490       std::vector<_CharT>                       _M_char_set;
00491       std::vector<_StringT>                     _M_equiv_set;
00492       std::vector<pair<_StrTransT, _StrTransT>> _M_range_set;
00493       std::vector<_CharClassT>                  _M_neg_class_set;
00494       _CharClassT                               _M_class_set;
00495       _TransT                                   _M_translator;
00496       const _TraitsT&                           _M_traits;
00497       bool                                      _M_is_non_matching;
00498       _CacheT                                   _M_cache;
00499 #ifdef _GLIBCXX_DEBUG
00500       bool                                      _M_is_ready = false;
00501 #endif
00502     };
00503 
00504  //@} regex-detail
00505 _GLIBCXX_END_NAMESPACE_VERSION
00506 } // namespace __detail
00507 } // namespace std
00508 
00509 #include <bits/regex_compiler.tcc>