regex_compiler.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. // class template regex -*- C++ -*-
  2. // Copyright (C) 2010-2022 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 _NFA<_TraitsT> _RegexT;
  51. typedef regex_constants::syntax_option_type _FlagT;
  52. _Compiler(const _CharT* __b, const _CharT* __e,
  53. const typename _TraitsT::locale_type& __traits, _FlagT __flags);
  54. shared_ptr<const _RegexT>
  55. _M_get_nfa() noexcept
  56. { return std::move(_M_nfa); }
  57. private:
  58. typedef _Scanner<_CharT> _ScannerT;
  59. typedef typename _TraitsT::string_type _StringT;
  60. typedef typename _ScannerT::_TokenT _TokenT;
  61. typedef _StateSeq<_TraitsT> _StateSeqT;
  62. typedef std::stack<_StateSeqT> _StackT;
  63. typedef std::ctype<_CharT> _CtypeT;
  64. // accepts a specific token or returns false.
  65. bool
  66. _M_match_token(_TokenT __token);
  67. void
  68. _M_disjunction();
  69. void
  70. _M_alternative();
  71. bool
  72. _M_term();
  73. bool
  74. _M_assertion();
  75. bool
  76. _M_quantifier();
  77. bool
  78. _M_atom();
  79. bool
  80. _M_bracket_expression();
  81. template<bool __icase, bool __collate>
  82. void
  83. _M_insert_any_matcher_ecma();
  84. template<bool __icase, bool __collate>
  85. void
  86. _M_insert_any_matcher_posix();
  87. template<bool __icase, bool __collate>
  88. void
  89. _M_insert_char_matcher();
  90. template<bool __icase, bool __collate>
  91. void
  92. _M_insert_character_class_matcher();
  93. template<bool __icase, bool __collate>
  94. void
  95. _M_insert_bracket_matcher(bool __neg);
  96. // Cache of the last atom seen in a bracketed range expression.
  97. struct _BracketState
  98. {
  99. enum class _Type : char { _None, _Char, _Class } _M_type = _Type::_None;
  100. _CharT _M_char = _CharT();
  101. void
  102. set(_CharT __c) noexcept { _M_type = _Type::_Char; _M_char = __c; }
  103. _GLIBCXX_NODISCARD _CharT
  104. get() const noexcept { return _M_char; }
  105. void
  106. reset(_Type __t = _Type::_None) noexcept { _M_type = __t; }
  107. explicit operator bool() const noexcept
  108. { return _M_type != _Type::_None; }
  109. // Previous token was a single character.
  110. _GLIBCXX_NODISCARD bool
  111. _M_is_char() const noexcept { return _M_type == _Type::_Char; }
  112. // Previous token was a character class, equivalent class,
  113. // collating symbol etc.
  114. _GLIBCXX_NODISCARD bool
  115. _M_is_class() const noexcept { return _M_type == _Type::_Class; }
  116. };
  117. template<bool __icase, bool __collate>
  118. using _BracketMatcher
  119. = std::__detail::_BracketMatcher<_TraitsT, __icase, __collate>;
  120. // Returns true if successfully parsed one term and should continue
  121. // compiling a bracket expression.
  122. // Returns false if the compiler should move on.
  123. template<bool __icase, bool __collate>
  124. bool
  125. _M_expression_term(_BracketState& __last_char,
  126. _BracketMatcher<__icase, __collate>& __matcher);
  127. int
  128. _M_cur_int_value(int __radix);
  129. bool
  130. _M_try_char();
  131. _StateSeqT
  132. _M_pop()
  133. {
  134. auto ret = _M_stack.top();
  135. _M_stack.pop();
  136. return ret;
  137. }
  138. static _FlagT
  139. _S_validate(_FlagT __f)
  140. {
  141. using namespace regex_constants;
  142. switch (__f & (ECMAScript|basic|extended|awk|grep|egrep))
  143. {
  144. case ECMAScript:
  145. case basic:
  146. case extended:
  147. case awk:
  148. case grep:
  149. case egrep:
  150. return __f;
  151. case _FlagT(0):
  152. return __f | ECMAScript;
  153. default:
  154. std::__throw_regex_error(_S_grammar, "conflicting grammar options");
  155. }
  156. }
  157. _FlagT _M_flags;
  158. _ScannerT _M_scanner;
  159. shared_ptr<_RegexT> _M_nfa;
  160. _StringT _M_value;
  161. _StackT _M_stack;
  162. const _TraitsT& _M_traits;
  163. const _CtypeT& _M_ctype;
  164. };
  165. // [28.13.14]
  166. template<typename _TraitsT, bool __icase, bool __collate>
  167. class _RegexTranslatorBase
  168. {
  169. public:
  170. typedef typename _TraitsT::char_type _CharT;
  171. typedef typename _TraitsT::string_type _StringT;
  172. typedef _StringT _StrTransT;
  173. explicit
  174. _RegexTranslatorBase(const _TraitsT& __traits)
  175. : _M_traits(__traits)
  176. { }
  177. _CharT
  178. _M_translate(_CharT __ch) const
  179. {
  180. if _GLIBCXX17_CONSTEXPR (__icase)
  181. return _M_traits.translate_nocase(__ch);
  182. else if _GLIBCXX17_CONSTEXPR (__collate)
  183. return _M_traits.translate(__ch);
  184. else
  185. return __ch;
  186. }
  187. _StrTransT
  188. _M_transform(_CharT __ch) const
  189. {
  190. _StrTransT __str(1, __ch);
  191. return _M_traits.transform(__str.begin(), __str.end());
  192. }
  193. // See LWG 523. It's not efficiently implementable when _TraitsT is not
  194. // std::regex_traits<>, and __collate is true. See specializations for
  195. // implementations of other cases.
  196. bool
  197. _M_match_range(const _StrTransT& __first, const _StrTransT& __last,
  198. const _StrTransT& __s) const
  199. { return __first <= __s && __s <= __last; }
  200. protected:
  201. bool _M_in_range_icase(_CharT __first, _CharT __last, _CharT __ch) const
  202. {
  203. typedef std::ctype<_CharT> __ctype_type;
  204. const auto& __fctyp = use_facet<__ctype_type>(this->_M_traits.getloc());
  205. auto __lower = __fctyp.tolower(__ch);
  206. auto __upper = __fctyp.toupper(__ch);
  207. return (__first <= __lower && __lower <= __last)
  208. || (__first <= __upper && __upper <= __last);
  209. }
  210. const _TraitsT& _M_traits;
  211. };
  212. template<typename _TraitsT, bool __icase, bool __collate>
  213. class _RegexTranslator
  214. : public _RegexTranslatorBase<_TraitsT, __icase, __collate>
  215. {
  216. public:
  217. typedef _RegexTranslatorBase<_TraitsT, __icase, __collate> _Base;
  218. using _Base::_Base;
  219. };
  220. template<typename _TraitsT, bool __icase>
  221. class _RegexTranslator<_TraitsT, __icase, false>
  222. : public _RegexTranslatorBase<_TraitsT, __icase, false>
  223. {
  224. public:
  225. typedef _RegexTranslatorBase<_TraitsT, __icase, false> _Base;
  226. typedef typename _Base::_CharT _CharT;
  227. typedef _CharT _StrTransT;
  228. using _Base::_Base;
  229. _StrTransT
  230. _M_transform(_CharT __ch) const
  231. { return __ch; }
  232. bool
  233. _M_match_range(_CharT __first, _CharT __last, _CharT __ch) const
  234. {
  235. if _GLIBCXX17_CONSTEXPR (!__icase)
  236. return __first <= __ch && __ch <= __last;
  237. else
  238. return this->_M_in_range_icase(__first, __last, __ch);
  239. }
  240. };
  241. template<typename _CharType>
  242. class _RegexTranslator<std::regex_traits<_CharType>, true, true>
  243. : public _RegexTranslatorBase<std::regex_traits<_CharType>, true, true>
  244. {
  245. public:
  246. typedef _RegexTranslatorBase<std::regex_traits<_CharType>, true, true>
  247. _Base;
  248. typedef typename _Base::_CharT _CharT;
  249. typedef typename _Base::_StrTransT _StrTransT;
  250. using _Base::_Base;
  251. bool
  252. _M_match_range(const _StrTransT& __first, const _StrTransT& __last,
  253. const _StrTransT& __str) const
  254. {
  255. __glibcxx_assert(__first.size() == 1);
  256. __glibcxx_assert(__last.size() == 1);
  257. __glibcxx_assert(__str.size() == 1);
  258. return this->_M_in_range_icase(__first[0], __last[0], __str[0]);
  259. }
  260. };
  261. template<typename _TraitsT>
  262. class _RegexTranslator<_TraitsT, false, false>
  263. {
  264. public:
  265. typedef typename _TraitsT::char_type _CharT;
  266. typedef _CharT _StrTransT;
  267. explicit
  268. _RegexTranslator(const _TraitsT&)
  269. { }
  270. _CharT
  271. _M_translate(_CharT __ch) const
  272. { return __ch; }
  273. _StrTransT
  274. _M_transform(_CharT __ch) const
  275. { return __ch; }
  276. bool
  277. _M_match_range(_CharT __first, _CharT __last, _CharT __ch) const
  278. { return __first <= __ch && __ch <= __last; }
  279. };
  280. template<typename _TraitsT, bool __is_ecma, bool __icase, bool __collate>
  281. struct _AnyMatcher;
  282. template<typename _TraitsT, bool __icase, bool __collate>
  283. struct _AnyMatcher<_TraitsT, false, __icase, __collate>
  284. {
  285. typedef _RegexTranslator<_TraitsT, __icase, __collate> _TransT;
  286. typedef typename _TransT::_CharT _CharT;
  287. explicit
  288. _AnyMatcher(const _TraitsT& __traits)
  289. : _M_translator(__traits)
  290. { }
  291. bool
  292. operator()(_CharT __ch) const
  293. {
  294. static auto __nul = _M_translator._M_translate('\0');
  295. return _M_translator._M_translate(__ch) != __nul;
  296. }
  297. _TransT _M_translator;
  298. };
  299. template<typename _TraitsT, bool __icase, bool __collate>
  300. struct _AnyMatcher<_TraitsT, true, __icase, __collate>
  301. {
  302. typedef _RegexTranslator<_TraitsT, __icase, __collate> _TransT;
  303. typedef typename _TransT::_CharT _CharT;
  304. explicit
  305. _AnyMatcher(const _TraitsT& __traits)
  306. : _M_translator(__traits)
  307. { }
  308. bool
  309. operator()(_CharT __ch) const
  310. { return _M_apply(__ch, typename is_same<_CharT, char>::type()); }
  311. bool
  312. _M_apply(_CharT __ch, true_type) const
  313. {
  314. auto __c = _M_translator._M_translate(__ch);
  315. auto __n = _M_translator._M_translate('\n');
  316. auto __r = _M_translator._M_translate('\r');
  317. return __c != __n && __c != __r;
  318. }
  319. bool
  320. _M_apply(_CharT __ch, false_type) const
  321. {
  322. auto __c = _M_translator._M_translate(__ch);
  323. auto __n = _M_translator._M_translate('\n');
  324. auto __r = _M_translator._M_translate('\r');
  325. auto __u2028 = _M_translator._M_translate(u'\u2028');
  326. auto __u2029 = _M_translator._M_translate(u'\u2029');
  327. return __c != __n && __c != __r && __c != __u2028 && __c != __u2029;
  328. }
  329. _TransT _M_translator;
  330. };
  331. template<typename _TraitsT, bool __icase, bool __collate>
  332. struct _CharMatcher
  333. {
  334. typedef _RegexTranslator<_TraitsT, __icase, __collate> _TransT;
  335. typedef typename _TransT::_CharT _CharT;
  336. _CharMatcher(_CharT __ch, const _TraitsT& __traits)
  337. : _M_translator(__traits), _M_ch(_M_translator._M_translate(__ch))
  338. { }
  339. bool
  340. operator()(_CharT __ch) const
  341. { return _M_ch == _M_translator._M_translate(__ch); }
  342. _TransT _M_translator;
  343. _CharT _M_ch;
  344. };
  345. /// Matches a character range (bracket expression)
  346. template<typename _TraitsT, bool __icase, bool __collate>
  347. struct _BracketMatcher
  348. {
  349. public:
  350. typedef _RegexTranslator<_TraitsT, __icase, __collate> _TransT;
  351. typedef typename _TransT::_CharT _CharT;
  352. typedef typename _TransT::_StrTransT _StrTransT;
  353. typedef typename _TraitsT::string_type _StringT;
  354. typedef typename _TraitsT::char_class_type _CharClassT;
  355. public:
  356. _BracketMatcher(bool __is_non_matching,
  357. const _TraitsT& __traits)
  358. : _M_class_set(0), _M_translator(__traits), _M_traits(__traits),
  359. _M_is_non_matching(__is_non_matching)
  360. { }
  361. bool
  362. operator()(_CharT __ch) const
  363. {
  364. _GLIBCXX_DEBUG_ASSERT(_M_is_ready);
  365. return _M_apply(__ch, _UseCache());
  366. }
  367. void
  368. _M_add_char(_CharT __c)
  369. {
  370. _M_char_set.push_back(_M_translator._M_translate(__c));
  371. _GLIBCXX_DEBUG_ONLY(_M_is_ready = false);
  372. }
  373. _StringT
  374. _M_add_collate_element(const _StringT& __s)
  375. {
  376. auto __st = _M_traits.lookup_collatename(__s.data(),
  377. __s.data() + __s.size());
  378. if (__st.empty())
  379. __throw_regex_error(regex_constants::error_collate,
  380. "Invalid collate element.");
  381. _M_char_set.push_back(_M_translator._M_translate(__st[0]));
  382. _GLIBCXX_DEBUG_ONLY(_M_is_ready = false);
  383. return __st;
  384. }
  385. void
  386. _M_add_equivalence_class(const _StringT& __s)
  387. {
  388. auto __st = _M_traits.lookup_collatename(__s.data(),
  389. __s.data() + __s.size());
  390. if (__st.empty())
  391. __throw_regex_error(regex_constants::error_collate,
  392. "Invalid equivalence class.");
  393. __st = _M_traits.transform_primary(__st.data(),
  394. __st.data() + __st.size());
  395. _M_equiv_set.push_back(__st);
  396. _GLIBCXX_DEBUG_ONLY(_M_is_ready = false);
  397. }
  398. // __neg should be true for \D, \S and \W only.
  399. void
  400. _M_add_character_class(const _StringT& __s, bool __neg)
  401. {
  402. auto __mask = _M_traits.lookup_classname(__s.data(),
  403. __s.data() + __s.size(),
  404. __icase);
  405. if (__mask == 0)
  406. __throw_regex_error(regex_constants::error_collate,
  407. "Invalid character class.");
  408. if (!__neg)
  409. _M_class_set |= __mask;
  410. else
  411. _M_neg_class_set.push_back(__mask);
  412. _GLIBCXX_DEBUG_ONLY(_M_is_ready = false);
  413. }
  414. void
  415. _M_make_range(_CharT __l, _CharT __r)
  416. {
  417. if (__l > __r)
  418. __throw_regex_error(regex_constants::error_range,
  419. "Invalid range in bracket expression.");
  420. _M_range_set.push_back(make_pair(_M_translator._M_transform(__l),
  421. _M_translator._M_transform(__r)));
  422. _GLIBCXX_DEBUG_ONLY(_M_is_ready = false);
  423. }
  424. void
  425. _M_ready()
  426. {
  427. std::sort(_M_char_set.begin(), _M_char_set.end());
  428. auto __end = std::unique(_M_char_set.begin(), _M_char_set.end());
  429. _M_char_set.erase(__end, _M_char_set.end());
  430. _M_make_cache(_UseCache());
  431. _GLIBCXX_DEBUG_ONLY(_M_is_ready = true);
  432. }
  433. private:
  434. // Currently we only use the cache for char
  435. using _UseCache = typename std::is_same<_CharT, char>::type;
  436. static constexpr size_t
  437. _S_cache_size =
  438. 1ul << (sizeof(_CharT) * __CHAR_BIT__ * int(_UseCache::value));
  439. struct _Dummy { };
  440. using _CacheT = std::__conditional_t<_UseCache::value,
  441. std::bitset<_S_cache_size>,
  442. _Dummy>;
  443. using _UnsignedCharT = typename std::make_unsigned<_CharT>::type;
  444. bool
  445. _M_apply(_CharT __ch, false_type) const;
  446. bool
  447. _M_apply(_CharT __ch, true_type) const
  448. { return _M_cache[static_cast<_UnsignedCharT>(__ch)]; }
  449. void
  450. _M_make_cache(true_type)
  451. {
  452. for (unsigned __i = 0; __i < _M_cache.size(); __i++)
  453. _M_cache[__i] = _M_apply(static_cast<_CharT>(__i), false_type());
  454. }
  455. void
  456. _M_make_cache(false_type)
  457. { }
  458. private:
  459. _GLIBCXX_STD_C::vector<_CharT> _M_char_set;
  460. _GLIBCXX_STD_C::vector<_StringT> _M_equiv_set;
  461. _GLIBCXX_STD_C::vector<pair<_StrTransT, _StrTransT>> _M_range_set;
  462. _GLIBCXX_STD_C::vector<_CharClassT> _M_neg_class_set;
  463. _CharClassT _M_class_set;
  464. _TransT _M_translator;
  465. const _TraitsT& _M_traits;
  466. bool _M_is_non_matching;
  467. _CacheT _M_cache;
  468. #ifdef _GLIBCXX_DEBUG
  469. bool _M_is_ready = false;
  470. #endif
  471. };
  472. ///@} regex-detail
  473. } // namespace __detail
  474. _GLIBCXX_END_NAMESPACE_VERSION
  475. } // namespace std
  476. #include <bits/regex_compiler.tcc>