regex_compiler.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. // class template regex -*- C++ -*-
  2. // Copyright (C) 2010-2018 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /**
  21. * @file bits/regex_compiler.h
  22. * This is an internal header file, included by other library headers.
  23. * Do not attempt to use it directly. @headername{regex}
  24. */
  25. namespace std _GLIBCXX_VISIBILITY(default)
  26. {
  27. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  28. _GLIBCXX_BEGIN_NAMESPACE_CXX11
  29. template<typename>
  30. class regex_traits;
  31. _GLIBCXX_END_NAMESPACE_CXX11
  32. namespace __detail
  33. {
  34. /**
  35. * @addtogroup regex-detail
  36. * @{
  37. */
  38. template<typename, bool, bool>
  39. struct _BracketMatcher;
  40. /**
  41. * @brief Builds an NFA from an input iterator range.
  42. *
  43. * The %_TraitsT type should fulfill requirements [28.3].
  44. */
  45. template<typename _TraitsT>
  46. class _Compiler
  47. {
  48. public:
  49. typedef typename _TraitsT::char_type _CharT;
  50. typedef const _CharT* _IterT;
  51. typedef _NFA<_TraitsT> _RegexT;
  52. typedef regex_constants::syntax_option_type _FlagT;
  53. _Compiler(_IterT __b, _IterT __e,
  54. const typename _TraitsT::locale_type& __traits, _FlagT __flags);
  55. shared_ptr<const _RegexT>
  56. _M_get_nfa()
  57. { return std::move(_M_nfa); }
  58. private:
  59. typedef _Scanner<_CharT> _ScannerT;
  60. typedef typename _TraitsT::string_type _StringT;
  61. typedef typename _ScannerT::_TokenT _TokenT;
  62. typedef _StateSeq<_TraitsT> _StateSeqT;
  63. typedef std::stack<_StateSeqT> _StackT;
  64. typedef std::ctype<_CharT> _CtypeT;
  65. // accepts a specific token or returns false.
  66. bool
  67. _M_match_token(_TokenT __token);
  68. void
  69. _M_disjunction();
  70. void
  71. _M_alternative();
  72. bool
  73. _M_term();
  74. bool
  75. _M_assertion();
  76. bool
  77. _M_quantifier();
  78. bool
  79. _M_atom();
  80. bool
  81. _M_bracket_expression();
  82. template<bool __icase, bool __collate>
  83. void
  84. _M_insert_any_matcher_ecma();
  85. template<bool __icase, bool __collate>
  86. void
  87. _M_insert_any_matcher_posix();
  88. template<bool __icase, bool __collate>
  89. void
  90. _M_insert_char_matcher();
  91. template<bool __icase, bool __collate>
  92. void
  93. _M_insert_character_class_matcher();
  94. template<bool __icase, bool __collate>
  95. void
  96. _M_insert_bracket_matcher(bool __neg);
  97. // Returns true if successfully matched one term and should continue.
  98. // Returns false if the compiler should move on.
  99. template<bool __icase, bool __collate>
  100. bool
  101. _M_expression_term(pair<bool, _CharT>& __last_char,
  102. _BracketMatcher<_TraitsT, __icase, __collate>&
  103. __matcher);
  104. int
  105. _M_cur_int_value(int __radix);
  106. bool
  107. _M_try_char();
  108. _StateSeqT
  109. _M_pop()
  110. {
  111. auto ret = _M_stack.top();
  112. _M_stack.pop();
  113. return ret;
  114. }
  115. _FlagT _M_flags;
  116. _ScannerT _M_scanner;
  117. shared_ptr<_RegexT> _M_nfa;
  118. _StringT _M_value;
  119. _StackT _M_stack;
  120. const _TraitsT& _M_traits;
  121. const _CtypeT& _M_ctype;
  122. };
  123. template<typename _Tp>
  124. struct __has_contiguous_iter : std::false_type { };
  125. template<typename _Ch, typename _Tr, typename _Alloc>
  126. struct __has_contiguous_iter<std::basic_string<_Ch, _Tr, _Alloc>>
  127. : std::true_type
  128. { };
  129. template<typename _Tp, typename _Alloc>
  130. struct __has_contiguous_iter<std::vector<_Tp, _Alloc>>
  131. : std::true_type
  132. { };
  133. template<typename _Tp>
  134. struct __is_contiguous_normal_iter : std::false_type { };
  135. template<typename _CharT>
  136. struct __is_contiguous_normal_iter<_CharT*> : std::true_type { };
  137. template<typename _Tp, typename _Cont>
  138. struct
  139. __is_contiguous_normal_iter<__gnu_cxx::__normal_iterator<_Tp, _Cont>>
  140. : __has_contiguous_iter<_Cont>::type
  141. { };
  142. template<typename _Iter, typename _TraitsT>
  143. using __enable_if_contiguous_normal_iter
  144. = typename enable_if< __is_contiguous_normal_iter<_Iter>::value,
  145. std::shared_ptr<const _NFA<_TraitsT>> >::type;
  146. template<typename _Iter, typename _TraitsT>
  147. using __disable_if_contiguous_normal_iter
  148. = typename enable_if< !__is_contiguous_normal_iter<_Iter>::value,
  149. std::shared_ptr<const _NFA<_TraitsT>> >::type;
  150. template<typename _TraitsT, typename _FwdIter>
  151. inline __enable_if_contiguous_normal_iter<_FwdIter, _TraitsT>
  152. __compile_nfa(_FwdIter __first, _FwdIter __last,
  153. const typename _TraitsT::locale_type& __loc,
  154. regex_constants::syntax_option_type __flags)
  155. {
  156. size_t __len = __last - __first;
  157. const auto* __cfirst = __len ? std::__addressof(*__first) : nullptr;
  158. using _Cmplr = _Compiler<_TraitsT>;
  159. return _Cmplr(__cfirst, __cfirst + __len, __loc, __flags)._M_get_nfa();
  160. }
  161. template<typename _TraitsT, typename _FwdIter>
  162. inline __disable_if_contiguous_normal_iter<_FwdIter, _TraitsT>
  163. __compile_nfa(_FwdIter __first, _FwdIter __last,
  164. const typename _TraitsT::locale_type& __loc,
  165. regex_constants::syntax_option_type __flags)
  166. {
  167. const basic_string<typename _TraitsT::char_type> __str(__first, __last);
  168. return __compile_nfa<_TraitsT>(__str.data(), __str.data() + __str.size(),
  169. __loc, __flags);
  170. }
  171. // [28.13.14]
  172. template<typename _TraitsT, bool __icase, bool __collate>
  173. class _RegexTranslatorBase
  174. {
  175. public:
  176. typedef typename _TraitsT::char_type _CharT;
  177. typedef typename _TraitsT::string_type _StringT;
  178. typedef _StringT _StrTransT;
  179. explicit
  180. _RegexTranslatorBase(const _TraitsT& __traits)
  181. : _M_traits(__traits)
  182. { }
  183. _CharT
  184. _M_translate(_CharT __ch) const
  185. {
  186. if (__icase)
  187. return _M_traits.translate_nocase(__ch);
  188. else if (__collate)
  189. return _M_traits.translate(__ch);
  190. else
  191. return __ch;
  192. }
  193. _StrTransT
  194. _M_transform(_CharT __ch) const
  195. {
  196. _StrTransT __str(1, __ch);
  197. return _M_traits.transform(__str.begin(), __str.end());
  198. }
  199. // See LWG 523. It's not efficiently implementable when _TraitsT is not
  200. // std::regex_traits<>, and __collate is true. See specializations for
  201. // implementations of other cases.
  202. bool
  203. _M_match_range(const _StrTransT& __first, const _StrTransT& __last,
  204. const _StrTransT& __s) const
  205. { return __first <= __s && __s <= __last; }
  206. protected:
  207. bool _M_in_range_icase(_CharT __first, _CharT __last, _CharT __ch) const
  208. {
  209. typedef std::ctype<_CharT> __ctype_type;
  210. const auto& __fctyp = use_facet<__ctype_type>(this->_M_traits.getloc());
  211. auto __lower = __fctyp.tolower(__ch);
  212. auto __upper = __fctyp.toupper(__ch);
  213. return (__first <= __lower && __lower <= __last)
  214. || (__first <= __upper && __upper <= __last);
  215. }
  216. const _TraitsT& _M_traits;
  217. };
  218. template<typename _TraitsT, bool __icase, bool __collate>
  219. class _RegexTranslator
  220. : public _RegexTranslatorBase<_TraitsT, __icase, __collate>
  221. {
  222. public:
  223. typedef _RegexTranslatorBase<_TraitsT, __icase, __collate> _Base;
  224. using _Base::_Base;
  225. };
  226. template<typename _TraitsT, bool __icase>
  227. class _RegexTranslator<_TraitsT, __icase, false>
  228. : public _RegexTranslatorBase<_TraitsT, __icase, false>
  229. {
  230. public:
  231. typedef _RegexTranslatorBase<_TraitsT, __icase, false> _Base;
  232. typedef typename _Base::_CharT _CharT;
  233. typedef _CharT _StrTransT;
  234. using _Base::_Base;
  235. _StrTransT
  236. _M_transform(_CharT __ch) const
  237. { return __ch; }
  238. bool
  239. _M_match_range(_CharT __first, _CharT __last, _CharT __ch) const
  240. {
  241. if (!__icase)
  242. return __first <= __ch && __ch <= __last;
  243. return this->_M_in_range_icase(__first, __last, __ch);
  244. }
  245. };
  246. template<typename _CharType>
  247. class _RegexTranslator<std::regex_traits<_CharType>, true, true>
  248. : public _RegexTranslatorBase<std::regex_traits<_CharType>, true, true>
  249. {
  250. public:
  251. typedef _RegexTranslatorBase<std::regex_traits<_CharType>, true, true>
  252. _Base;
  253. typedef typename _Base::_CharT _CharT;
  254. typedef typename _Base::_StrTransT _StrTransT;
  255. using _Base::_Base;
  256. bool
  257. _M_match_range(const _StrTransT& __first, const _StrTransT& __last,
  258. const _StrTransT& __str) const
  259. {
  260. __glibcxx_assert(__first.size() == 1);
  261. __glibcxx_assert(__last.size() == 1);
  262. __glibcxx_assert(__str.size() == 1);
  263. return this->_M_in_range_icase(__first[0], __last[0], __str[0]);
  264. }
  265. };
  266. template<typename _TraitsT>
  267. class _RegexTranslator<_TraitsT, false, false>
  268. {
  269. public:
  270. typedef typename _TraitsT::char_type _CharT;
  271. typedef _CharT _StrTransT;
  272. explicit
  273. _RegexTranslator(const _TraitsT&)
  274. { }
  275. _CharT
  276. _M_translate(_CharT __ch) const
  277. { return __ch; }
  278. _StrTransT
  279. _M_transform(_CharT __ch) const
  280. { return __ch; }
  281. bool
  282. _M_match_range(_CharT __first, _CharT __last, _CharT __ch) const
  283. { return __first <= __ch && __ch <= __last; }
  284. };
  285. template<typename _TraitsT, bool __is_ecma, bool __icase, bool __collate>
  286. struct _AnyMatcher;
  287. template<typename _TraitsT, bool __icase, bool __collate>
  288. struct _AnyMatcher<_TraitsT, false, __icase, __collate>
  289. {
  290. typedef _RegexTranslator<_TraitsT, __icase, __collate> _TransT;
  291. typedef typename _TransT::_CharT _CharT;
  292. explicit
  293. _AnyMatcher(const _TraitsT& __traits)
  294. : _M_translator(__traits)
  295. { }
  296. bool
  297. operator()(_CharT __ch) const
  298. {
  299. static auto __nul = _M_translator._M_translate('\0');
  300. return _M_translator._M_translate(__ch) != __nul;
  301. }
  302. _TransT _M_translator;
  303. };
  304. template<typename _TraitsT, bool __icase, bool __collate>
  305. struct _AnyMatcher<_TraitsT, true, __icase, __collate>
  306. {
  307. typedef _RegexTranslator<_TraitsT, __icase, __collate> _TransT;
  308. typedef typename _TransT::_CharT _CharT;
  309. explicit
  310. _AnyMatcher(const _TraitsT& __traits)
  311. : _M_translator(__traits)
  312. { }
  313. bool
  314. operator()(_CharT __ch) const
  315. { return _M_apply(__ch, typename is_same<_CharT, char>::type()); }
  316. bool
  317. _M_apply(_CharT __ch, true_type) const
  318. {
  319. auto __c = _M_translator._M_translate(__ch);
  320. auto __n = _M_translator._M_translate('\n');
  321. auto __r = _M_translator._M_translate('\r');
  322. return __c != __n && __c != __r;
  323. }
  324. bool
  325. _M_apply(_CharT __ch, false_type) const
  326. {
  327. auto __c = _M_translator._M_translate(__ch);
  328. auto __n = _M_translator._M_translate('\n');
  329. auto __r = _M_translator._M_translate('\r');
  330. auto __u2028 = _M_translator._M_translate(u'\u2028');
  331. auto __u2029 = _M_translator._M_translate(u'\u2029');
  332. return __c != __n && __c != __r && __c != __u2028 && __c != __u2029;
  333. }
  334. _TransT _M_translator;
  335. };
  336. template<typename _TraitsT, bool __icase, bool __collate>
  337. struct _CharMatcher
  338. {
  339. typedef _RegexTranslator<_TraitsT, __icase, __collate> _TransT;
  340. typedef typename _TransT::_CharT _CharT;
  341. _CharMatcher(_CharT __ch, const _TraitsT& __traits)
  342. : _M_translator(__traits), _M_ch(_M_translator._M_translate(__ch))
  343. { }
  344. bool
  345. operator()(_CharT __ch) const
  346. { return _M_ch == _M_translator._M_translate(__ch); }
  347. _TransT _M_translator;
  348. _CharT _M_ch;
  349. };
  350. /// Matches a character range (bracket expression)
  351. template<typename _TraitsT, bool __icase, bool __collate>
  352. struct _BracketMatcher
  353. {
  354. public:
  355. typedef _RegexTranslator<_TraitsT, __icase, __collate> _TransT;
  356. typedef typename _TransT::_CharT _CharT;
  357. typedef typename _TransT::_StrTransT _StrTransT;
  358. typedef typename _TraitsT::string_type _StringT;
  359. typedef typename _TraitsT::char_class_type _CharClassT;
  360. public:
  361. _BracketMatcher(bool __is_non_matching,
  362. const _TraitsT& __traits)
  363. : _M_class_set(0), _M_translator(__traits), _M_traits(__traits),
  364. _M_is_non_matching(__is_non_matching)
  365. { }
  366. bool
  367. operator()(_CharT __ch) const
  368. {
  369. _GLIBCXX_DEBUG_ASSERT(_M_is_ready);
  370. return _M_apply(__ch, _UseCache());
  371. }
  372. void
  373. _M_add_char(_CharT __c)
  374. {
  375. _M_char_set.push_back(_M_translator._M_translate(__c));
  376. _GLIBCXX_DEBUG_ONLY(_M_is_ready = false);
  377. }
  378. _StringT
  379. _M_add_collate_element(const _StringT& __s)
  380. {
  381. auto __st = _M_traits.lookup_collatename(__s.data(),
  382. __s.data() + __s.size());
  383. if (__st.empty())
  384. __throw_regex_error(regex_constants::error_collate,
  385. "Invalid collate element.");
  386. _M_char_set.push_back(_M_translator._M_translate(__st[0]));
  387. _GLIBCXX_DEBUG_ONLY(_M_is_ready = false);
  388. return __st;
  389. }
  390. void
  391. _M_add_equivalence_class(const _StringT& __s)
  392. {
  393. auto __st = _M_traits.lookup_collatename(__s.data(),
  394. __s.data() + __s.size());
  395. if (__st.empty())
  396. __throw_regex_error(regex_constants::error_collate,
  397. "Invalid equivalence class.");
  398. __st = _M_traits.transform_primary(__st.data(),
  399. __st.data() + __st.size());
  400. _M_equiv_set.push_back(__st);
  401. _GLIBCXX_DEBUG_ONLY(_M_is_ready = false);
  402. }
  403. // __neg should be true for \D, \S and \W only.
  404. void
  405. _M_add_character_class(const _StringT& __s, bool __neg)
  406. {
  407. auto __mask = _M_traits.lookup_classname(__s.data(),
  408. __s.data() + __s.size(),
  409. __icase);
  410. if (__mask == 0)
  411. __throw_regex_error(regex_constants::error_collate,
  412. "Invalid character class.");
  413. if (!__neg)
  414. _M_class_set |= __mask;
  415. else
  416. _M_neg_class_set.push_back(__mask);
  417. _GLIBCXX_DEBUG_ONLY(_M_is_ready = false);
  418. }
  419. void
  420. _M_make_range(_CharT __l, _CharT __r)
  421. {
  422. if (__l > __r)
  423. __throw_regex_error(regex_constants::error_range,
  424. "Invalid range in bracket expression.");
  425. _M_range_set.push_back(make_pair(_M_translator._M_transform(__l),
  426. _M_translator._M_transform(__r)));
  427. _GLIBCXX_DEBUG_ONLY(_M_is_ready = false);
  428. }
  429. void
  430. _M_ready()
  431. {
  432. std::sort(_M_char_set.begin(), _M_char_set.end());
  433. auto __end = std::unique(_M_char_set.begin(), _M_char_set.end());
  434. _M_char_set.erase(__end, _M_char_set.end());
  435. _M_make_cache(_UseCache());
  436. _GLIBCXX_DEBUG_ONLY(_M_is_ready = true);
  437. }
  438. private:
  439. // Currently we only use the cache for char
  440. typedef typename std::is_same<_CharT, char>::type _UseCache;
  441. static constexpr size_t
  442. _S_cache_size()
  443. {
  444. return 1ul << (sizeof(_CharT) * __CHAR_BIT__ * int(_UseCache::value));
  445. }
  446. struct _Dummy { };
  447. typedef typename std::conditional<_UseCache::value,
  448. std::bitset<_S_cache_size()>,
  449. _Dummy>::type _CacheT;
  450. typedef typename std::make_unsigned<_CharT>::type _UnsignedCharT;
  451. bool
  452. _M_apply(_CharT __ch, false_type) const;
  453. bool
  454. _M_apply(_CharT __ch, true_type) const
  455. { return _M_cache[static_cast<_UnsignedCharT>(__ch)]; }
  456. void
  457. _M_make_cache(true_type)
  458. {
  459. for (unsigned __i = 0; __i < _M_cache.size(); __i++)
  460. _M_cache[__i] = _M_apply(static_cast<_CharT>(__i), false_type());
  461. }
  462. void
  463. _M_make_cache(false_type)
  464. { }
  465. private:
  466. std::vector<_CharT> _M_char_set;
  467. std::vector<_StringT> _M_equiv_set;
  468. std::vector<pair<_StrTransT, _StrTransT>> _M_range_set;
  469. std::vector<_CharClassT> _M_neg_class_set;
  470. _CharClassT _M_class_set;
  471. _TransT _M_translator;
  472. const _TraitsT& _M_traits;
  473. bool _M_is_non_matching;
  474. _CacheT _M_cache;
  475. #ifdef _GLIBCXX_DEBUG
  476. bool _M_is_ready = false;
  477. #endif
  478. };
  479. //@} regex-detail
  480. } // namespace __detail
  481. _GLIBCXX_END_NAMESPACE_VERSION
  482. } // namespace std
  483. #include <bits/regex_compiler.tcc>