No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

642 líneas
18KB

  1. // class template regex -*- C++ -*-
  2. // Copyright (C) 2013-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.tcc
  22. * This is an internal header file, included by other library headers.
  23. * Do not attempt to use it directly. @headername{regex}
  24. */
  25. // FIXME make comments doxygen format.
  26. /*
  27. // This compiler refers to "Regular Expression Matching Can Be Simple And Fast"
  28. // (http://swtch.com/~rsc/regexp/regexp1.html),
  29. // but doesn't strictly follow it.
  30. //
  31. // When compiling, states are *chained* instead of tree- or graph-constructed.
  32. // It's more like structured programs: there's if statement and loop statement.
  33. //
  34. // For alternative structure (say "a|b"), aka "if statement", two branches
  35. // should be constructed. However, these two shall merge to an "end_tag" at
  36. // the end of this operator:
  37. //
  38. // branch1
  39. // / \
  40. // => begin_tag end_tag =>
  41. // \ /
  42. // branch2
  43. //
  44. // This is the difference between this implementation and that in Russ's
  45. // article.
  46. //
  47. // That's why we introduced dummy node here ------ "end_tag" is a dummy node.
  48. // All dummy nodes will be eliminated at the end of compilation.
  49. */
  50. namespace std _GLIBCXX_VISIBILITY(default)
  51. {
  52. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  53. namespace __detail
  54. {
  55. template<typename _TraitsT>
  56. _Compiler<_TraitsT>::
  57. _Compiler(_IterT __b, _IterT __e,
  58. const typename _TraitsT::locale_type& __loc, _FlagT __flags)
  59. : _M_flags((__flags
  60. & (regex_constants::ECMAScript
  61. | regex_constants::basic
  62. | regex_constants::extended
  63. | regex_constants::grep
  64. | regex_constants::egrep
  65. | regex_constants::awk))
  66. ? __flags
  67. : __flags | regex_constants::ECMAScript),
  68. _M_scanner(__b, __e, _M_flags, __loc),
  69. _M_nfa(make_shared<_RegexT>(__loc, _M_flags)),
  70. _M_traits(_M_nfa->_M_traits),
  71. _M_ctype(std::use_facet<_CtypeT>(__loc))
  72. {
  73. _StateSeqT __r(*_M_nfa, _M_nfa->_M_start());
  74. __r._M_append(_M_nfa->_M_insert_subexpr_begin());
  75. this->_M_disjunction();
  76. if (!_M_match_token(_ScannerT::_S_token_eof))
  77. __throw_regex_error(regex_constants::error_paren);
  78. __r._M_append(_M_pop());
  79. __glibcxx_assert(_M_stack.empty());
  80. __r._M_append(_M_nfa->_M_insert_subexpr_end());
  81. __r._M_append(_M_nfa->_M_insert_accept());
  82. _M_nfa->_M_eliminate_dummy();
  83. }
  84. template<typename _TraitsT>
  85. void
  86. _Compiler<_TraitsT>::
  87. _M_disjunction()
  88. {
  89. this->_M_alternative();
  90. while (_M_match_token(_ScannerT::_S_token_or))
  91. {
  92. _StateSeqT __alt1 = _M_pop();
  93. this->_M_alternative();
  94. _StateSeqT __alt2 = _M_pop();
  95. auto __end = _M_nfa->_M_insert_dummy();
  96. __alt1._M_append(__end);
  97. __alt2._M_append(__end);
  98. // __alt2 is state._M_next, __alt1 is state._M_alt. The executor
  99. // executes _M_alt before _M_next, as well as executing left
  100. // alternative before right one.
  101. _M_stack.push(_StateSeqT(*_M_nfa,
  102. _M_nfa->_M_insert_alt(
  103. __alt2._M_start, __alt1._M_start, false),
  104. __end));
  105. }
  106. }
  107. template<typename _TraitsT>
  108. void
  109. _Compiler<_TraitsT>::
  110. _M_alternative()
  111. {
  112. if (this->_M_term())
  113. {
  114. _StateSeqT __re = _M_pop();
  115. this->_M_alternative();
  116. __re._M_append(_M_pop());
  117. _M_stack.push(__re);
  118. }
  119. else
  120. _M_stack.push(_StateSeqT(*_M_nfa, _M_nfa->_M_insert_dummy()));
  121. }
  122. template<typename _TraitsT>
  123. bool
  124. _Compiler<_TraitsT>::
  125. _M_term()
  126. {
  127. if (this->_M_assertion())
  128. return true;
  129. if (this->_M_atom())
  130. {
  131. while (this->_M_quantifier());
  132. return true;
  133. }
  134. return false;
  135. }
  136. template<typename _TraitsT>
  137. bool
  138. _Compiler<_TraitsT>::
  139. _M_assertion()
  140. {
  141. if (_M_match_token(_ScannerT::_S_token_line_begin))
  142. _M_stack.push(_StateSeqT(*_M_nfa, _M_nfa->_M_insert_line_begin()));
  143. else if (_M_match_token(_ScannerT::_S_token_line_end))
  144. _M_stack.push(_StateSeqT(*_M_nfa, _M_nfa->_M_insert_line_end()));
  145. else if (_M_match_token(_ScannerT::_S_token_word_bound))
  146. // _M_value[0] == 'n' means it's negative, say "not word boundary".
  147. _M_stack.push(_StateSeqT(*_M_nfa, _M_nfa->
  148. _M_insert_word_bound(_M_value[0] == 'n')));
  149. else if (_M_match_token(_ScannerT::_S_token_subexpr_lookahead_begin))
  150. {
  151. auto __neg = _M_value[0] == 'n';
  152. this->_M_disjunction();
  153. if (!_M_match_token(_ScannerT::_S_token_subexpr_end))
  154. __throw_regex_error(regex_constants::error_paren,
  155. "Parenthesis is not closed.");
  156. auto __tmp = _M_pop();
  157. __tmp._M_append(_M_nfa->_M_insert_accept());
  158. _M_stack.push(
  159. _StateSeqT(
  160. *_M_nfa,
  161. _M_nfa->_M_insert_lookahead(__tmp._M_start, __neg)));
  162. }
  163. else
  164. return false;
  165. return true;
  166. }
  167. template<typename _TraitsT>
  168. bool
  169. _Compiler<_TraitsT>::
  170. _M_quantifier()
  171. {
  172. bool __neg = (_M_flags & regex_constants::ECMAScript);
  173. auto __init = [this, &__neg]()
  174. {
  175. if (_M_stack.empty())
  176. __throw_regex_error(regex_constants::error_badrepeat,
  177. "Nothing to repeat before a quantifier.");
  178. __neg = __neg && _M_match_token(_ScannerT::_S_token_opt);
  179. };
  180. if (_M_match_token(_ScannerT::_S_token_closure0))
  181. {
  182. __init();
  183. auto __e = _M_pop();
  184. _StateSeqT __r(*_M_nfa,
  185. _M_nfa->_M_insert_repeat(_S_invalid_state_id,
  186. __e._M_start, __neg));
  187. __e._M_append(__r);
  188. _M_stack.push(__r);
  189. }
  190. else if (_M_match_token(_ScannerT::_S_token_closure1))
  191. {
  192. __init();
  193. auto __e = _M_pop();
  194. __e._M_append(_M_nfa->_M_insert_repeat(_S_invalid_state_id,
  195. __e._M_start, __neg));
  196. _M_stack.push(__e);
  197. }
  198. else if (_M_match_token(_ScannerT::_S_token_opt))
  199. {
  200. __init();
  201. auto __e = _M_pop();
  202. auto __end = _M_nfa->_M_insert_dummy();
  203. _StateSeqT __r(*_M_nfa,
  204. _M_nfa->_M_insert_repeat(_S_invalid_state_id,
  205. __e._M_start, __neg));
  206. __e._M_append(__end);
  207. __r._M_append(__end);
  208. _M_stack.push(__r);
  209. }
  210. else if (_M_match_token(_ScannerT::_S_token_interval_begin))
  211. {
  212. if (_M_stack.empty())
  213. __throw_regex_error(regex_constants::error_badrepeat,
  214. "Nothing to repeat before a quantifier.");
  215. if (!_M_match_token(_ScannerT::_S_token_dup_count))
  216. __throw_regex_error(regex_constants::error_badbrace,
  217. "Unexpected token in brace expression.");
  218. _StateSeqT __r(_M_pop());
  219. _StateSeqT __e(*_M_nfa, _M_nfa->_M_insert_dummy());
  220. long __min_rep = _M_cur_int_value(10);
  221. bool __infi = false;
  222. long __n;
  223. // {3
  224. if (_M_match_token(_ScannerT::_S_token_comma))
  225. if (_M_match_token(_ScannerT::_S_token_dup_count)) // {3,7}
  226. __n = _M_cur_int_value(10) - __min_rep;
  227. else
  228. __infi = true;
  229. else
  230. __n = 0;
  231. if (!_M_match_token(_ScannerT::_S_token_interval_end))
  232. __throw_regex_error(regex_constants::error_brace,
  233. "Unexpected end of brace expression.");
  234. __neg = __neg && _M_match_token(_ScannerT::_S_token_opt);
  235. for (long __i = 0; __i < __min_rep; ++__i)
  236. __e._M_append(__r._M_clone());
  237. if (__infi)
  238. {
  239. auto __tmp = __r._M_clone();
  240. _StateSeqT __s(*_M_nfa,
  241. _M_nfa->_M_insert_repeat(_S_invalid_state_id,
  242. __tmp._M_start, __neg));
  243. __tmp._M_append(__s);
  244. __e._M_append(__s);
  245. }
  246. else
  247. {
  248. if (__n < 0)
  249. __throw_regex_error(regex_constants::error_badbrace,
  250. "Invalid range in brace expression.");
  251. auto __end = _M_nfa->_M_insert_dummy();
  252. // _M_alt is the "match more" branch, and _M_next is the
  253. // "match less" one. Switch _M_alt and _M_next of all created
  254. // nodes. This is a hack but IMO works well.
  255. std::stack<_StateIdT> __stack;
  256. for (long __i = 0; __i < __n; ++__i)
  257. {
  258. auto __tmp = __r._M_clone();
  259. auto __alt = _M_nfa->_M_insert_repeat(__tmp._M_start,
  260. __end, __neg);
  261. __stack.push(__alt);
  262. __e._M_append(_StateSeqT(*_M_nfa, __alt, __tmp._M_end));
  263. }
  264. __e._M_append(__end);
  265. while (!__stack.empty())
  266. {
  267. auto& __tmp = (*_M_nfa)[__stack.top()];
  268. __stack.pop();
  269. std::swap(__tmp._M_next, __tmp._M_alt);
  270. }
  271. }
  272. _M_stack.push(__e);
  273. }
  274. else
  275. return false;
  276. return true;
  277. }
  278. #define __INSERT_REGEX_MATCHER(__func, ...)\
  279. do {\
  280. if (!(_M_flags & regex_constants::icase))\
  281. if (!(_M_flags & regex_constants::collate))\
  282. __func<false, false>(__VA_ARGS__);\
  283. else\
  284. __func<false, true>(__VA_ARGS__);\
  285. else\
  286. if (!(_M_flags & regex_constants::collate))\
  287. __func<true, false>(__VA_ARGS__);\
  288. else\
  289. __func<true, true>(__VA_ARGS__);\
  290. } while (false)
  291. template<typename _TraitsT>
  292. bool
  293. _Compiler<_TraitsT>::
  294. _M_atom()
  295. {
  296. if (_M_match_token(_ScannerT::_S_token_anychar))
  297. {
  298. if (!(_M_flags & regex_constants::ECMAScript))
  299. __INSERT_REGEX_MATCHER(_M_insert_any_matcher_posix);
  300. else
  301. __INSERT_REGEX_MATCHER(_M_insert_any_matcher_ecma);
  302. }
  303. else if (_M_try_char())
  304. __INSERT_REGEX_MATCHER(_M_insert_char_matcher);
  305. else if (_M_match_token(_ScannerT::_S_token_backref))
  306. _M_stack.push(_StateSeqT(*_M_nfa, _M_nfa->
  307. _M_insert_backref(_M_cur_int_value(10))));
  308. else if (_M_match_token(_ScannerT::_S_token_quoted_class))
  309. __INSERT_REGEX_MATCHER(_M_insert_character_class_matcher);
  310. else if (_M_match_token(_ScannerT::_S_token_subexpr_no_group_begin))
  311. {
  312. _StateSeqT __r(*_M_nfa, _M_nfa->_M_insert_dummy());
  313. this->_M_disjunction();
  314. if (!_M_match_token(_ScannerT::_S_token_subexpr_end))
  315. __throw_regex_error(regex_constants::error_paren,
  316. "Parenthesis is not closed.");
  317. __r._M_append(_M_pop());
  318. _M_stack.push(__r);
  319. }
  320. else if (_M_match_token(_ScannerT::_S_token_subexpr_begin))
  321. {
  322. _StateSeqT __r(*_M_nfa, _M_nfa->_M_insert_subexpr_begin());
  323. this->_M_disjunction();
  324. if (!_M_match_token(_ScannerT::_S_token_subexpr_end))
  325. __throw_regex_error(regex_constants::error_paren,
  326. "Parenthesis is not closed.");
  327. __r._M_append(_M_pop());
  328. __r._M_append(_M_nfa->_M_insert_subexpr_end());
  329. _M_stack.push(__r);
  330. }
  331. else if (!_M_bracket_expression())
  332. return false;
  333. return true;
  334. }
  335. template<typename _TraitsT>
  336. bool
  337. _Compiler<_TraitsT>::
  338. _M_bracket_expression()
  339. {
  340. bool __neg =
  341. _M_match_token(_ScannerT::_S_token_bracket_neg_begin);
  342. if (!(__neg || _M_match_token(_ScannerT::_S_token_bracket_begin)))
  343. return false;
  344. __INSERT_REGEX_MATCHER(_M_insert_bracket_matcher, __neg);
  345. return true;
  346. }
  347. #undef __INSERT_REGEX_MATCHER
  348. template<typename _TraitsT>
  349. template<bool __icase, bool __collate>
  350. void
  351. _Compiler<_TraitsT>::
  352. _M_insert_any_matcher_ecma()
  353. {
  354. _M_stack.push(_StateSeqT(*_M_nfa,
  355. _M_nfa->_M_insert_matcher
  356. (_AnyMatcher<_TraitsT, true, __icase, __collate>
  357. (_M_traits))));
  358. }
  359. template<typename _TraitsT>
  360. template<bool __icase, bool __collate>
  361. void
  362. _Compiler<_TraitsT>::
  363. _M_insert_any_matcher_posix()
  364. {
  365. _M_stack.push(_StateSeqT(*_M_nfa,
  366. _M_nfa->_M_insert_matcher
  367. (_AnyMatcher<_TraitsT, false, __icase, __collate>
  368. (_M_traits))));
  369. }
  370. template<typename _TraitsT>
  371. template<bool __icase, bool __collate>
  372. void
  373. _Compiler<_TraitsT>::
  374. _M_insert_char_matcher()
  375. {
  376. _M_stack.push(_StateSeqT(*_M_nfa,
  377. _M_nfa->_M_insert_matcher
  378. (_CharMatcher<_TraitsT, __icase, __collate>
  379. (_M_value[0], _M_traits))));
  380. }
  381. template<typename _TraitsT>
  382. template<bool __icase, bool __collate>
  383. void
  384. _Compiler<_TraitsT>::
  385. _M_insert_character_class_matcher()
  386. {
  387. __glibcxx_assert(_M_value.size() == 1);
  388. _BracketMatcher<_TraitsT, __icase, __collate> __matcher
  389. (_M_ctype.is(_CtypeT::upper, _M_value[0]), _M_traits);
  390. __matcher._M_add_character_class(_M_value, false);
  391. __matcher._M_ready();
  392. _M_stack.push(_StateSeqT(*_M_nfa,
  393. _M_nfa->_M_insert_matcher(std::move(__matcher))));
  394. }
  395. template<typename _TraitsT>
  396. template<bool __icase, bool __collate>
  397. void
  398. _Compiler<_TraitsT>::
  399. _M_insert_bracket_matcher(bool __neg)
  400. {
  401. _BracketMatcher<_TraitsT, __icase, __collate> __matcher(__neg, _M_traits);
  402. pair<bool, _CharT> __last_char; // Optional<_CharT>
  403. __last_char.first = false;
  404. if (!(_M_flags & regex_constants::ECMAScript))
  405. {
  406. if (_M_try_char())
  407. {
  408. __last_char.first = true;
  409. __last_char.second = _M_value[0];
  410. }
  411. else if (_M_match_token(_ScannerT::_S_token_bracket_dash))
  412. {
  413. __last_char.first = true;
  414. __last_char.second = '-';
  415. }
  416. }
  417. while (_M_expression_term(__last_char, __matcher));
  418. if (__last_char.first)
  419. __matcher._M_add_char(__last_char.second);
  420. __matcher._M_ready();
  421. _M_stack.push(_StateSeqT(
  422. *_M_nfa,
  423. _M_nfa->_M_insert_matcher(std::move(__matcher))));
  424. }
  425. template<typename _TraitsT>
  426. template<bool __icase, bool __collate>
  427. bool
  428. _Compiler<_TraitsT>::
  429. _M_expression_term(pair<bool, _CharT>& __last_char,
  430. _BracketMatcher<_TraitsT, __icase, __collate>& __matcher)
  431. {
  432. if (_M_match_token(_ScannerT::_S_token_bracket_end))
  433. return false;
  434. const auto __push_char = [&](_CharT __ch)
  435. {
  436. if (__last_char.first)
  437. __matcher._M_add_char(__last_char.second);
  438. else
  439. __last_char.first = true;
  440. __last_char.second = __ch;
  441. };
  442. const auto __flush = [&]
  443. {
  444. if (__last_char.first)
  445. {
  446. __matcher._M_add_char(__last_char.second);
  447. __last_char.first = false;
  448. }
  449. };
  450. if (_M_match_token(_ScannerT::_S_token_collsymbol))
  451. {
  452. auto __symbol = __matcher._M_add_collate_element(_M_value);
  453. if (__symbol.size() == 1)
  454. __push_char(__symbol[0]);
  455. else
  456. __flush();
  457. }
  458. else if (_M_match_token(_ScannerT::_S_token_equiv_class_name))
  459. {
  460. __flush();
  461. __matcher._M_add_equivalence_class(_M_value);
  462. }
  463. else if (_M_match_token(_ScannerT::_S_token_char_class_name))
  464. {
  465. __flush();
  466. __matcher._M_add_character_class(_M_value, false);
  467. }
  468. else if (_M_try_char())
  469. __push_char(_M_value[0]);
  470. // POSIX doesn't allow '-' as a start-range char (say [a-z--0]),
  471. // except when the '-' is the first or last character in the bracket
  472. // expression ([--0]). ECMAScript treats all '-' after a range as a
  473. // normal character. Also see above, where _M_expression_term gets called.
  474. //
  475. // As a result, POSIX rejects [-----], but ECMAScript doesn't.
  476. // Boost (1.57.0) always uses POSIX style even in its ECMAScript syntax.
  477. // Clang (3.5) always uses ECMAScript style even in its POSIX syntax.
  478. //
  479. // It turns out that no one reads BNFs ;)
  480. else if (_M_match_token(_ScannerT::_S_token_bracket_dash))
  481. {
  482. if (!__last_char.first)
  483. {
  484. if (!(_M_flags & regex_constants::ECMAScript))
  485. {
  486. if (_M_match_token(_ScannerT::_S_token_bracket_end))
  487. {
  488. __push_char('-');
  489. return false;
  490. }
  491. __throw_regex_error(
  492. regex_constants::error_range,
  493. "Unexpected dash in bracket expression. For POSIX syntax, "
  494. "a dash is not treated literally only when it is at "
  495. "beginning or end.");
  496. }
  497. __push_char('-');
  498. }
  499. else
  500. {
  501. if (_M_try_char())
  502. {
  503. __matcher._M_make_range(__last_char.second, _M_value[0]);
  504. __last_char.first = false;
  505. }
  506. else if (_M_match_token(_ScannerT::_S_token_bracket_dash))
  507. {
  508. __matcher._M_make_range(__last_char.second, '-');
  509. __last_char.first = false;
  510. }
  511. else
  512. {
  513. if (_M_scanner._M_get_token()
  514. != _ScannerT::_S_token_bracket_end)
  515. __throw_regex_error(
  516. regex_constants::error_range,
  517. "Character is expected after a dash.");
  518. __push_char('-');
  519. }
  520. }
  521. }
  522. else if (_M_match_token(_ScannerT::_S_token_quoted_class))
  523. {
  524. __flush();
  525. __matcher._M_add_character_class(_M_value,
  526. _M_ctype.is(_CtypeT::upper,
  527. _M_value[0]));
  528. }
  529. else
  530. __throw_regex_error(regex_constants::error_brack,
  531. "Unexpected character in bracket expression.");
  532. return true;
  533. }
  534. template<typename _TraitsT>
  535. bool
  536. _Compiler<_TraitsT>::
  537. _M_try_char()
  538. {
  539. bool __is_char = false;
  540. if (_M_match_token(_ScannerT::_S_token_oct_num))
  541. {
  542. __is_char = true;
  543. _M_value.assign(1, _M_cur_int_value(8));
  544. }
  545. else if (_M_match_token(_ScannerT::_S_token_hex_num))
  546. {
  547. __is_char = true;
  548. _M_value.assign(1, _M_cur_int_value(16));
  549. }
  550. else if (_M_match_token(_ScannerT::_S_token_ord_char))
  551. __is_char = true;
  552. return __is_char;
  553. }
  554. template<typename _TraitsT>
  555. bool
  556. _Compiler<_TraitsT>::
  557. _M_match_token(_TokenT token)
  558. {
  559. if (token == _M_scanner._M_get_token())
  560. {
  561. _M_value = _M_scanner._M_get_value();
  562. _M_scanner._M_advance();
  563. return true;
  564. }
  565. return false;
  566. }
  567. template<typename _TraitsT>
  568. int
  569. _Compiler<_TraitsT>::
  570. _M_cur_int_value(int __radix)
  571. {
  572. long __v = 0;
  573. for (typename _StringT::size_type __i = 0;
  574. __i < _M_value.length(); ++__i)
  575. __v =__v * __radix + _M_traits.value(_M_value[__i], __radix);
  576. return __v;
  577. }
  578. template<typename _TraitsT, bool __icase, bool __collate>
  579. bool
  580. _BracketMatcher<_TraitsT, __icase, __collate>::
  581. _M_apply(_CharT __ch, false_type) const
  582. {
  583. return [this, __ch]
  584. {
  585. if (std::binary_search(_M_char_set.begin(), _M_char_set.end(),
  586. _M_translator._M_translate(__ch)))
  587. return true;
  588. auto __s = _M_translator._M_transform(__ch);
  589. for (auto& __it : _M_range_set)
  590. if (_M_translator._M_match_range(__it.first, __it.second, __s))
  591. return true;
  592. if (_M_traits.isctype(__ch, _M_class_set))
  593. return true;
  594. if (std::find(_M_equiv_set.begin(), _M_equiv_set.end(),
  595. _M_traits.transform_primary(&__ch, &__ch+1))
  596. != _M_equiv_set.end())
  597. return true;
  598. for (auto& __it : _M_neg_class_set)
  599. if (!_M_traits.isctype(__ch, __it))
  600. return true;
  601. return false;
  602. }() ^ _M_is_non_matching;
  603. }
  604. } // namespace __detail
  605. _GLIBCXX_END_NAMESPACE_VERSION
  606. } // namespace