You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

561 lines
16KB

  1. // class template regex -*- C++ -*-
  2. // Copyright (C) 2010-2020 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 __is_contiguous_iter : is_pointer<_Tp>::type { };
  125. template<typename _Tp, typename _Cont>
  126. struct
  127. __is_contiguous_iter<__gnu_cxx::__normal_iterator<_Tp*, _Cont>>
  128. : true_type { };
  129. template<typename _Iter, typename _TraitsT>
  130. using __enable_if_contiguous_iter
  131. = __enable_if_t< __is_contiguous_iter<_Iter>::value,
  132. std::shared_ptr<const _NFA<_TraitsT>> >;
  133. template<typename _Iter, typename _TraitsT>
  134. using __disable_if_contiguous_iter
  135. = __enable_if_t< !__is_contiguous_iter<_Iter>::value,
  136. std::shared_ptr<const _NFA<_TraitsT>> >;
  137. template<typename _TraitsT, typename _FwdIter>
  138. inline __enable_if_contiguous_iter<_FwdIter, _TraitsT>
  139. __compile_nfa(_FwdIter __first, _FwdIter __last,
  140. const typename _TraitsT::locale_type& __loc,
  141. regex_constants::syntax_option_type __flags)
  142. {
  143. size_t __len = __last - __first;
  144. const auto* __cfirst = __len ? std::__addressof(*__first) : nullptr;
  145. using _Cmplr = _Compiler<_TraitsT>;
  146. return _Cmplr(__cfirst, __cfirst + __len, __loc, __flags)._M_get_nfa();
  147. }
  148. template<typename _TraitsT, typename _FwdIter>
  149. inline __disable_if_contiguous_iter<_FwdIter, _TraitsT>
  150. __compile_nfa(_FwdIter __first, _FwdIter __last,
  151. const typename _TraitsT::locale_type& __loc,
  152. regex_constants::syntax_option_type __flags)
  153. {
  154. const basic_string<typename _TraitsT::char_type> __str(__first, __last);
  155. return __compile_nfa<_TraitsT>(__str.data(), __str.data() + __str.size(),
  156. __loc, __flags);
  157. }
  158. // [28.13.14]
  159. template<typename _TraitsT, bool __icase, bool __collate>
  160. class _RegexTranslatorBase
  161. {
  162. public:
  163. typedef typename _TraitsT::char_type _CharT;
  164. typedef typename _TraitsT::string_type _StringT;
  165. typedef _StringT _StrTransT;
  166. explicit
  167. _RegexTranslatorBase(const _TraitsT& __traits)
  168. : _M_traits(__traits)
  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. _StrTransT
  181. _M_transform(_CharT __ch) const
  182. {
  183. _StrTransT __str(1, __ch);
  184. return _M_traits.transform(__str.begin(), __str.end());
  185. }
  186. // See LWG 523. It's not efficiently implementable when _TraitsT is not
  187. // std::regex_traits<>, and __collate is true. See specializations for
  188. // implementations of other cases.
  189. bool
  190. _M_match_range(const _StrTransT& __first, const _StrTransT& __last,
  191. const _StrTransT& __s) const
  192. { return __first <= __s && __s <= __last; }
  193. protected:
  194. bool _M_in_range_icase(_CharT __first, _CharT __last, _CharT __ch) const
  195. {
  196. typedef std::ctype<_CharT> __ctype_type;
  197. const auto& __fctyp = use_facet<__ctype_type>(this->_M_traits.getloc());
  198. auto __lower = __fctyp.tolower(__ch);
  199. auto __upper = __fctyp.toupper(__ch);
  200. return (__first <= __lower && __lower <= __last)
  201. || (__first <= __upper && __upper <= __last);
  202. }
  203. const _TraitsT& _M_traits;
  204. };
  205. template<typename _TraitsT, bool __icase, bool __collate>
  206. class _RegexTranslator
  207. : public _RegexTranslatorBase<_TraitsT, __icase, __collate>
  208. {
  209. public:
  210. typedef _RegexTranslatorBase<_TraitsT, __icase, __collate> _Base;
  211. using _Base::_Base;
  212. };
  213. template<typename _TraitsT, bool __icase>
  214. class _RegexTranslator<_TraitsT, __icase, false>
  215. : public _RegexTranslatorBase<_TraitsT, __icase, false>
  216. {
  217. public:
  218. typedef _RegexTranslatorBase<_TraitsT, __icase, false> _Base;
  219. typedef typename _Base::_CharT _CharT;
  220. typedef _CharT _StrTransT;
  221. using _Base::_Base;
  222. _StrTransT
  223. _M_transform(_CharT __ch) const
  224. { return __ch; }
  225. bool
  226. _M_match_range(_CharT __first, _CharT __last, _CharT __ch) const
  227. {
  228. if (!__icase)
  229. return __first <= __ch && __ch <= __last;
  230. return this->_M_in_range_icase(__first, __last, __ch);
  231. }
  232. };
  233. template<typename _CharType>
  234. class _RegexTranslator<std::regex_traits<_CharType>, true, true>
  235. : public _RegexTranslatorBase<std::regex_traits<_CharType>, true, true>
  236. {
  237. public:
  238. typedef _RegexTranslatorBase<std::regex_traits<_CharType>, true, true>
  239. _Base;
  240. typedef typename _Base::_CharT _CharT;
  241. typedef typename _Base::_StrTransT _StrTransT;
  242. using _Base::_Base;
  243. bool
  244. _M_match_range(const _StrTransT& __first, const _StrTransT& __last,
  245. const _StrTransT& __str) const
  246. {
  247. __glibcxx_assert(__first.size() == 1);
  248. __glibcxx_assert(__last.size() == 1);
  249. __glibcxx_assert(__str.size() == 1);
  250. return this->_M_in_range_icase(__first[0], __last[0], __str[0]);
  251. }
  252. };
  253. template<typename _TraitsT>
  254. class _RegexTranslator<_TraitsT, false, false>
  255. {
  256. public:
  257. typedef typename _TraitsT::char_type _CharT;
  258. typedef _CharT _StrTransT;
  259. explicit
  260. _RegexTranslator(const _TraitsT&)
  261. { }
  262. _CharT
  263. _M_translate(_CharT __ch) const
  264. { return __ch; }
  265. _StrTransT
  266. _M_transform(_CharT __ch) const
  267. { return __ch; }
  268. bool
  269. _M_match_range(_CharT __first, _CharT __last, _CharT __ch) const
  270. { return __first <= __ch && __ch <= __last; }
  271. };
  272. template<typename _TraitsT, bool __is_ecma, bool __icase, bool __collate>
  273. struct _AnyMatcher;
  274. template<typename _TraitsT, bool __icase, bool __collate>
  275. struct _AnyMatcher<_TraitsT, false, __icase, __collate>
  276. {
  277. typedef _RegexTranslator<_TraitsT, __icase, __collate> _TransT;
  278. typedef typename _TransT::_CharT _CharT;
  279. explicit
  280. _AnyMatcher(const _TraitsT& __traits)
  281. : _M_translator(__traits)
  282. { }
  283. bool
  284. operator()(_CharT __ch) const
  285. {
  286. static auto __nul = _M_translator._M_translate('\0');
  287. return _M_translator._M_translate(__ch) != __nul;
  288. }
  289. _TransT _M_translator;
  290. };
  291. template<typename _TraitsT, bool __icase, bool __collate>
  292. struct _AnyMatcher<_TraitsT, true, __icase, __collate>
  293. {
  294. typedef _RegexTranslator<_TraitsT, __icase, __collate> _TransT;
  295. typedef typename _TransT::_CharT _CharT;
  296. explicit
  297. _AnyMatcher(const _TraitsT& __traits)
  298. : _M_translator(__traits)
  299. { }
  300. bool
  301. operator()(_CharT __ch) const
  302. { return _M_apply(__ch, typename is_same<_CharT, char>::type()); }
  303. bool
  304. _M_apply(_CharT __ch, true_type) const
  305. {
  306. auto __c = _M_translator._M_translate(__ch);
  307. auto __n = _M_translator._M_translate('\n');
  308. auto __r = _M_translator._M_translate('\r');
  309. return __c != __n && __c != __r;
  310. }
  311. bool
  312. _M_apply(_CharT __ch, false_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. auto __u2028 = _M_translator._M_translate(u'\u2028');
  318. auto __u2029 = _M_translator._M_translate(u'\u2029');
  319. return __c != __n && __c != __r && __c != __u2028 && __c != __u2029;
  320. }
  321. _TransT _M_translator;
  322. };
  323. template<typename _TraitsT, bool __icase, bool __collate>
  324. struct _CharMatcher
  325. {
  326. typedef _RegexTranslator<_TraitsT, __icase, __collate> _TransT;
  327. typedef typename _TransT::_CharT _CharT;
  328. _CharMatcher(_CharT __ch, const _TraitsT& __traits)
  329. : _M_translator(__traits), _M_ch(_M_translator._M_translate(__ch))
  330. { }
  331. bool
  332. operator()(_CharT __ch) const
  333. { return _M_ch == _M_translator._M_translate(__ch); }
  334. _TransT _M_translator;
  335. _CharT _M_ch;
  336. };
  337. /// Matches a character range (bracket expression)
  338. template<typename _TraitsT, bool __icase, bool __collate>
  339. struct _BracketMatcher
  340. {
  341. public:
  342. typedef _RegexTranslator<_TraitsT, __icase, __collate> _TransT;
  343. typedef typename _TransT::_CharT _CharT;
  344. typedef typename _TransT::_StrTransT _StrTransT;
  345. typedef typename _TraitsT::string_type _StringT;
  346. typedef typename _TraitsT::char_class_type _CharClassT;
  347. public:
  348. _BracketMatcher(bool __is_non_matching,
  349. const _TraitsT& __traits)
  350. : _M_class_set(0), _M_translator(__traits), _M_traits(__traits),
  351. _M_is_non_matching(__is_non_matching)
  352. { }
  353. bool
  354. operator()(_CharT __ch) const
  355. {
  356. _GLIBCXX_DEBUG_ASSERT(_M_is_ready);
  357. return _M_apply(__ch, _UseCache());
  358. }
  359. void
  360. _M_add_char(_CharT __c)
  361. {
  362. _M_char_set.push_back(_M_translator._M_translate(__c));
  363. _GLIBCXX_DEBUG_ONLY(_M_is_ready = false);
  364. }
  365. _StringT
  366. _M_add_collate_element(const _StringT& __s)
  367. {
  368. auto __st = _M_traits.lookup_collatename(__s.data(),
  369. __s.data() + __s.size());
  370. if (__st.empty())
  371. __throw_regex_error(regex_constants::error_collate,
  372. "Invalid collate element.");
  373. _M_char_set.push_back(_M_translator._M_translate(__st[0]));
  374. _GLIBCXX_DEBUG_ONLY(_M_is_ready = false);
  375. return __st;
  376. }
  377. void
  378. _M_add_equivalence_class(const _StringT& __s)
  379. {
  380. auto __st = _M_traits.lookup_collatename(__s.data(),
  381. __s.data() + __s.size());
  382. if (__st.empty())
  383. __throw_regex_error(regex_constants::error_collate,
  384. "Invalid equivalence class.");
  385. __st = _M_traits.transform_primary(__st.data(),
  386. __st.data() + __st.size());
  387. _M_equiv_set.push_back(__st);
  388. _GLIBCXX_DEBUG_ONLY(_M_is_ready = false);
  389. }
  390. // __neg should be true for \D, \S and \W only.
  391. void
  392. _M_add_character_class(const _StringT& __s, bool __neg)
  393. {
  394. auto __mask = _M_traits.lookup_classname(__s.data(),
  395. __s.data() + __s.size(),
  396. __icase);
  397. if (__mask == 0)
  398. __throw_regex_error(regex_constants::error_collate,
  399. "Invalid character class.");
  400. if (!__neg)
  401. _M_class_set |= __mask;
  402. else
  403. _M_neg_class_set.push_back(__mask);
  404. _GLIBCXX_DEBUG_ONLY(_M_is_ready = false);
  405. }
  406. void
  407. _M_make_range(_CharT __l, _CharT __r)
  408. {
  409. if (__l > __r)
  410. __throw_regex_error(regex_constants::error_range,
  411. "Invalid range in bracket expression.");
  412. _M_range_set.push_back(make_pair(_M_translator._M_transform(__l),
  413. _M_translator._M_transform(__r)));
  414. _GLIBCXX_DEBUG_ONLY(_M_is_ready = false);
  415. }
  416. void
  417. _M_ready()
  418. {
  419. std::sort(_M_char_set.begin(), _M_char_set.end());
  420. auto __end = std::unique(_M_char_set.begin(), _M_char_set.end());
  421. _M_char_set.erase(__end, _M_char_set.end());
  422. _M_make_cache(_UseCache());
  423. _GLIBCXX_DEBUG_ONLY(_M_is_ready = true);
  424. }
  425. private:
  426. // Currently we only use the cache for char
  427. typedef typename std::is_same<_CharT, char>::type _UseCache;
  428. static constexpr size_t
  429. _S_cache_size =
  430. 1ul << (sizeof(_CharT) * __CHAR_BIT__ * int(_UseCache::value));
  431. struct _Dummy { };
  432. typedef typename std::conditional<_UseCache::value,
  433. std::bitset<_S_cache_size>,
  434. _Dummy>::type _CacheT;
  435. typedef typename std::make_unsigned<_CharT>::type _UnsignedCharT;
  436. bool
  437. _M_apply(_CharT __ch, false_type) const;
  438. bool
  439. _M_apply(_CharT __ch, true_type) const
  440. { return _M_cache[static_cast<_UnsignedCharT>(__ch)]; }
  441. void
  442. _M_make_cache(true_type)
  443. {
  444. for (unsigned __i = 0; __i < _M_cache.size(); __i++)
  445. _M_cache[__i] = _M_apply(static_cast<_CharT>(__i), false_type());
  446. }
  447. void
  448. _M_make_cache(false_type)
  449. { }
  450. private:
  451. std::vector<_CharT> _M_char_set;
  452. std::vector<_StringT> _M_equiv_set;
  453. std::vector<pair<_StrTransT, _StrTransT>> _M_range_set;
  454. std::vector<_CharClassT> _M_neg_class_set;
  455. _CharClassT _M_class_set;
  456. _TransT _M_translator;
  457. const _TraitsT& _M_traits;
  458. bool _M_is_non_matching;
  459. _CacheT _M_cache;
  460. #ifdef _GLIBCXX_DEBUG
  461. bool _M_is_ready = false;
  462. #endif
  463. };
  464. //@} regex-detail
  465. } // namespace __detail
  466. _GLIBCXX_END_NAMESPACE_VERSION
  467. } // namespace std
  468. #include <bits/regex_compiler.tcc>