perl_matcher.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  2. *
  3. * Copyright (c) 2002
  4. * John Maddock
  5. *
  6. * Use, modification and distribution are subject to the
  7. * Boost Software License, Version 1.0. (See accompanying file
  8. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. *
  10. */
  11. #ifndef BOOST_REGEX_MATCHER_HPP
  12. #define BOOST_REGEX_MATCHER_HPP
  13. #include <boost/regex/v5/iterator_category.hpp>
  14. #ifdef BOOST_REGEX_MSVC
  15. # pragma warning(push)
  16. #pragma warning(disable : 4251 4459)
  17. #if BOOST_REGEX_MSVC < 1700
  18. # pragma warning(disable : 4231)
  19. #endif
  20. # if BOOST_REGEX_MSVC < 1600
  21. # pragma warning(disable : 4660)
  22. # endif
  23. #if BOOST_REGEX_MSVC < 1910
  24. #pragma warning(disable:4800)
  25. #endif
  26. #endif
  27. namespace boost{
  28. namespace BOOST_REGEX_DETAIL_NS{
  29. //
  30. // error checking API:
  31. //
  32. inline void verify_options(boost::regex_constants::syntax_option_type, match_flag_type mf)
  33. {
  34. //
  35. // can't mix match_extra with POSIX matching rules:
  36. //
  37. if ((mf & match_extra) && (mf & match_posix))
  38. {
  39. std::logic_error msg("Usage Error: Can't mix regular expression captures with POSIX matching rules");
  40. #ifndef BOOST_REGEX_STANDALONE
  41. throw_exception(msg);
  42. #else
  43. throw msg;
  44. #endif
  45. }
  46. }
  47. //
  48. // function can_start:
  49. //
  50. template <class charT>
  51. inline bool can_start(charT c, const unsigned char* map, unsigned char mask)
  52. {
  53. return ((c < static_cast<charT>(0)) ? true : ((c >= static_cast<charT>(1 << CHAR_BIT)) ? true : map[c] & mask));
  54. }
  55. inline bool can_start(char c, const unsigned char* map, unsigned char mask)
  56. {
  57. return map[(unsigned char)c] & mask;
  58. }
  59. inline bool can_start(signed char c, const unsigned char* map, unsigned char mask)
  60. {
  61. return map[(unsigned char)c] & mask;
  62. }
  63. inline bool can_start(unsigned char c, const unsigned char* map, unsigned char mask)
  64. {
  65. return map[c] & mask;
  66. }
  67. inline bool can_start(unsigned short c, const unsigned char* map, unsigned char mask)
  68. {
  69. return ((c >= (1 << CHAR_BIT)) ? true : map[c] & mask);
  70. }
  71. #if defined(WCHAR_MIN) && (WCHAR_MIN == 0) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
  72. inline bool can_start(wchar_t c, const unsigned char* map, unsigned char mask)
  73. {
  74. return ((c >= static_cast<wchar_t>(1u << CHAR_BIT)) ? true : map[c] & mask);
  75. }
  76. #endif
  77. #if !defined(BOOST_NO_INTRINSIC_WCHAR_T)
  78. inline bool can_start(unsigned int c, const unsigned char* map, unsigned char mask)
  79. {
  80. return (((c >= static_cast<unsigned int>(1u << CHAR_BIT)) ? true : map[c] & mask));
  81. }
  82. #endif
  83. template <class C, class T, class A>
  84. inline int string_compare(const std::basic_string<C,T,A>& s, const C* p)
  85. {
  86. if(0 == *p)
  87. {
  88. if(s.empty() || ((s.size() == 1) && (s[0] == 0)))
  89. return 0;
  90. }
  91. return s.compare(p);
  92. }
  93. template <class Seq, class C>
  94. inline int string_compare(const Seq& s, const C* p)
  95. {
  96. std::size_t i = 0;
  97. while((i < s.size()) && (p[i] == s[i]))
  98. {
  99. ++i;
  100. }
  101. return (i == s.size()) ? -(int)p[i] : (int)s[i] - (int)p[i];
  102. }
  103. # define STR_COMP(s,p) string_compare(s,p)
  104. template<class charT>
  105. inline const charT* re_skip_past_null(const charT* p)
  106. {
  107. while (*p != static_cast<charT>(0)) ++p;
  108. return ++p;
  109. }
  110. template <class iterator, class charT, class traits_type, class char_classT>
  111. iterator re_is_set_member(iterator next,
  112. iterator last,
  113. const re_set_long<char_classT>* set_,
  114. const regex_data<charT, traits_type>& e, bool icase)
  115. {
  116. const charT* p = reinterpret_cast<const charT*>(set_+1);
  117. iterator ptr;
  118. unsigned int i;
  119. //bool icase = e.m_flags & regex_constants::icase;
  120. if(next == last) return next;
  121. typedef typename traits_type::string_type traits_string_type;
  122. const ::boost::regex_traits_wrapper<traits_type>& traits_inst = *(e.m_ptraits);
  123. // dwa 9/13/00 suppress incorrect MSVC warning - it claims this is never
  124. // referenced
  125. (void)traits_inst;
  126. // try and match a single character, could be a multi-character
  127. // collating element...
  128. for(i = 0; i < set_->csingles; ++i)
  129. {
  130. ptr = next;
  131. if(*p == static_cast<charT>(0))
  132. {
  133. // treat null string as special case:
  134. if(traits_inst.translate(*ptr, icase))
  135. {
  136. ++p;
  137. continue;
  138. }
  139. return set_->isnot ? next : (ptr == next) ? ++next : ptr;
  140. }
  141. else
  142. {
  143. while(*p && (ptr != last))
  144. {
  145. if(traits_inst.translate(*ptr, icase) != *p)
  146. break;
  147. ++p;
  148. ++ptr;
  149. }
  150. if(*p == static_cast<charT>(0)) // if null we've matched
  151. return set_->isnot ? next : (ptr == next) ? ++next : ptr;
  152. p = re_skip_past_null(p); // skip null
  153. }
  154. }
  155. charT col = traits_inst.translate(*next, icase);
  156. if(set_->cranges || set_->cequivalents)
  157. {
  158. traits_string_type s1;
  159. //
  160. // try and match a range, NB only a single character can match
  161. if(set_->cranges)
  162. {
  163. if((e.m_flags & regex_constants::collate) == 0)
  164. s1.assign(1, col);
  165. else
  166. {
  167. charT a[2] = { col, charT(0), };
  168. s1 = traits_inst.transform(a, a + 1);
  169. }
  170. for(i = 0; i < set_->cranges; ++i)
  171. {
  172. if(STR_COMP(s1, p) >= 0)
  173. {
  174. do{ ++p; }while(*p);
  175. ++p;
  176. if(STR_COMP(s1, p) <= 0)
  177. return set_->isnot ? next : ++next;
  178. }
  179. else
  180. {
  181. // skip first string
  182. do{ ++p; }while(*p);
  183. ++p;
  184. }
  185. // skip second string
  186. do{ ++p; }while(*p);
  187. ++p;
  188. }
  189. }
  190. //
  191. // try and match an equivalence class, NB only a single character can match
  192. if(set_->cequivalents)
  193. {
  194. charT a[2] = { col, charT(0), };
  195. s1 = traits_inst.transform_primary(a, a +1);
  196. for(i = 0; i < set_->cequivalents; ++i)
  197. {
  198. if(STR_COMP(s1, p) == 0)
  199. return set_->isnot ? next : ++next;
  200. // skip string
  201. do{ ++p; }while(*p);
  202. ++p;
  203. }
  204. }
  205. }
  206. if(traits_inst.isctype(col, set_->cclasses) == true)
  207. return set_->isnot ? next : ++next;
  208. if((set_->cnclasses != 0) && (traits_inst.isctype(col, set_->cnclasses) == false))
  209. return set_->isnot ? next : ++next;
  210. return set_->isnot ? ++next : next;
  211. }
  212. template <class BidiIterator>
  213. class repeater_count
  214. {
  215. repeater_count** stack;
  216. repeater_count* next;
  217. int state_id;
  218. std::size_t count; // the number of iterations so far
  219. BidiIterator start_pos; // where the last repeat started
  220. repeater_count* unwind_until(int n, repeater_count* p, int current_recursion_id)
  221. {
  222. while(p && (p->state_id != n))
  223. {
  224. if(-2 - current_recursion_id == p->state_id)
  225. return 0;
  226. p = p->next;
  227. if(p && (p->state_id < 0))
  228. {
  229. p = unwind_until(p->state_id, p, current_recursion_id);
  230. if(!p)
  231. return p;
  232. p = p->next;
  233. }
  234. }
  235. return p;
  236. }
  237. public:
  238. repeater_count(repeater_count** s) : stack(s), next(0), state_id(-1), count(0), start_pos() {}
  239. repeater_count(int i, repeater_count** s, BidiIterator start, int current_recursion_id)
  240. : start_pos(start)
  241. {
  242. state_id = i;
  243. stack = s;
  244. next = *stack;
  245. *stack = this;
  246. if((state_id > next->state_id) && (next->state_id >= 0))
  247. count = 0;
  248. else
  249. {
  250. repeater_count* p = next;
  251. p = unwind_until(state_id, p, current_recursion_id);
  252. if(p)
  253. {
  254. count = p->count;
  255. start_pos = p->start_pos;
  256. }
  257. else
  258. count = 0;
  259. }
  260. }
  261. ~repeater_count()
  262. {
  263. if(next)
  264. *stack = next;
  265. }
  266. std::size_t get_count() { return count; }
  267. int get_id() { return state_id; }
  268. std::size_t operator++() { return ++count; }
  269. bool check_null_repeat(const BidiIterator& pos, std::size_t max)
  270. {
  271. // this is called when we are about to start a new repeat,
  272. // if the last one was NULL move our count to max,
  273. // otherwise save the current position.
  274. bool result = (count == 0) ? false : (pos == start_pos);
  275. if(result)
  276. count = max;
  277. else
  278. start_pos = pos;
  279. return result;
  280. }
  281. };
  282. struct saved_state;
  283. enum saved_state_type
  284. {
  285. saved_type_end = 0,
  286. saved_type_paren = 1,
  287. saved_type_recurse = 2,
  288. saved_type_assertion = 3,
  289. saved_state_alt = 4,
  290. saved_state_repeater_count = 5,
  291. saved_state_extra_block = 6,
  292. saved_state_greedy_single_repeat = 7,
  293. saved_state_rep_slow_dot = 8,
  294. saved_state_rep_fast_dot = 9,
  295. saved_state_rep_char = 10,
  296. saved_state_rep_short_set = 11,
  297. saved_state_rep_long_set = 12,
  298. saved_state_non_greedy_long_repeat = 13,
  299. saved_state_count = 14
  300. };
  301. #ifdef BOOST_REGEX_MSVC
  302. # pragma warning(push)
  303. #if BOOST_REGEX_MSVC >= 1800
  304. #pragma warning(disable:26495)
  305. #endif
  306. #endif
  307. template <class Results>
  308. struct recursion_info
  309. {
  310. typedef typename Results::value_type value_type;
  311. typedef typename value_type::iterator iterator;
  312. int idx;
  313. const re_syntax_base* preturn_address;
  314. Results results;
  315. repeater_count<iterator>* repeater_stack;
  316. iterator location_of_start;
  317. };
  318. #ifdef BOOST_REGEX_MSVC
  319. # pragma warning(pop)
  320. #endif
  321. template <class BidiIterator, class Allocator, class traits>
  322. class perl_matcher
  323. {
  324. public:
  325. typedef typename traits::char_type char_type;
  326. typedef perl_matcher<BidiIterator, Allocator, traits> self_type;
  327. typedef bool (self_type::*matcher_proc_type)();
  328. typedef std::size_t traits_size_type;
  329. typedef typename is_byte<char_type>::width_type width_type;
  330. typedef typename std::iterator_traits<BidiIterator>::difference_type difference_type;
  331. typedef match_results<BidiIterator, Allocator> results_type;
  332. perl_matcher(BidiIterator first, BidiIterator end,
  333. match_results<BidiIterator, Allocator>& what,
  334. const basic_regex<char_type, traits>& e,
  335. match_flag_type f,
  336. BidiIterator l_base)
  337. : m_result(what), base(first), last(end),
  338. position(first), backstop(l_base), re(e), traits_inst(e.get_traits()),
  339. m_independent(false), next_count(&rep_obj), rep_obj(&next_count)
  340. , m_recursions(0)
  341. {
  342. construct_init(e, f);
  343. }
  344. bool match();
  345. bool find();
  346. void setf(match_flag_type f)
  347. { m_match_flags |= f; }
  348. void unsetf(match_flag_type f)
  349. { m_match_flags &= ~f; }
  350. private:
  351. void construct_init(const basic_regex<char_type, traits>& e, match_flag_type f);
  352. bool find_imp();
  353. bool match_imp();
  354. void estimate_max_state_count(std::random_access_iterator_tag*);
  355. void estimate_max_state_count(void*);
  356. bool match_prefix();
  357. bool match_all_states();
  358. // match procs, stored in s_match_vtable:
  359. bool match_startmark();
  360. bool match_endmark();
  361. bool match_literal();
  362. bool match_start_line();
  363. bool match_end_line();
  364. bool match_wild();
  365. bool match_match();
  366. bool match_word_boundary();
  367. bool match_within_word();
  368. bool match_word_start();
  369. bool match_word_end();
  370. bool match_buffer_start();
  371. bool match_buffer_end();
  372. bool match_backref();
  373. bool match_long_set();
  374. bool match_set();
  375. bool match_jump();
  376. bool match_alt();
  377. bool match_rep();
  378. bool match_combining();
  379. bool match_soft_buffer_end();
  380. bool match_restart_continue();
  381. bool match_long_set_repeat();
  382. bool match_set_repeat();
  383. bool match_char_repeat();
  384. bool match_dot_repeat_fast();
  385. bool match_dot_repeat_slow();
  386. bool match_dot_repeat_dispatch()
  387. {
  388. return ::boost::is_random_access_iterator<BidiIterator>::value ? match_dot_repeat_fast() : match_dot_repeat_slow();
  389. }
  390. bool match_backstep();
  391. bool match_assert_backref();
  392. bool match_toggle_case();
  393. bool match_recursion();
  394. bool match_fail();
  395. bool match_accept();
  396. bool match_commit();
  397. bool match_then();
  398. bool skip_until_paren(int index, bool match = true);
  399. // find procs stored in s_find_vtable:
  400. bool find_restart_any();
  401. bool find_restart_word();
  402. bool find_restart_line();
  403. bool find_restart_buf();
  404. bool find_restart_lit();
  405. private:
  406. // final result structure to be filled in:
  407. match_results<BidiIterator, Allocator>& m_result;
  408. // temporary result for POSIX matches:
  409. std::unique_ptr<match_results<BidiIterator, Allocator> > m_temp_match;
  410. // pointer to actual result structure to fill in:
  411. match_results<BidiIterator, Allocator>* m_presult;
  412. // start of sequence being searched:
  413. BidiIterator base;
  414. // end of sequence being searched:
  415. BidiIterator last;
  416. // current character being examined:
  417. BidiIterator position;
  418. // where to restart next search after failed match attempt:
  419. BidiIterator restart;
  420. // where the current search started from, acts as base for $` during grep:
  421. BidiIterator search_base;
  422. // how far we can go back when matching lookbehind:
  423. BidiIterator backstop;
  424. // the expression being examined:
  425. const basic_regex<char_type, traits>& re;
  426. // the expression's traits class:
  427. const ::boost::regex_traits_wrapper<traits>& traits_inst;
  428. // the next state in the machine being matched:
  429. const re_syntax_base* pstate;
  430. // matching flags in use:
  431. match_flag_type m_match_flags;
  432. // how many states we have examined so far:
  433. std::ptrdiff_t state_count;
  434. // max number of states to examine before giving up:
  435. std::ptrdiff_t max_state_count;
  436. // whether we should ignore case or not:
  437. bool icase;
  438. // set to true when (position == last), indicates that we may have a partial match:
  439. bool m_has_partial_match;
  440. // set to true whenever we get a match:
  441. bool m_has_found_match;
  442. // set to true whenever we're inside an independent sub-expression:
  443. bool m_independent;
  444. // the current repeat being examined:
  445. repeater_count<BidiIterator>* next_count;
  446. // the first repeat being examined (top of linked list):
  447. repeater_count<BidiIterator> rep_obj;
  448. // the mask to pass when matching word boundaries:
  449. typename traits::char_class_type m_word_mask;
  450. // the bitmask to use when determining whether a match_any matches a newline or not:
  451. unsigned char match_any_mask;
  452. // recursion information:
  453. std::vector<recursion_info<results_type> > recursion_stack;
  454. //
  455. // additional members for non-recursive version:
  456. //
  457. typedef bool (self_type::*unwind_proc_type)(bool);
  458. void extend_stack();
  459. bool unwind(bool);
  460. bool unwind_end(bool);
  461. bool unwind_paren(bool);
  462. bool unwind_recursion_stopper(bool);
  463. bool unwind_assertion(bool);
  464. bool unwind_alt(bool);
  465. bool unwind_repeater_counter(bool);
  466. bool unwind_extra_block(bool);
  467. bool unwind_greedy_single_repeat(bool);
  468. bool unwind_slow_dot_repeat(bool);
  469. bool unwind_fast_dot_repeat(bool);
  470. bool unwind_char_repeat(bool);
  471. bool unwind_short_set_repeat(bool);
  472. bool unwind_long_set_repeat(bool);
  473. bool unwind_non_greedy_repeat(bool);
  474. bool unwind_recursion(bool);
  475. bool unwind_recursion_pop(bool);
  476. bool unwind_commit(bool);
  477. bool unwind_then(bool);
  478. bool unwind_case(bool);
  479. void destroy_single_repeat();
  480. void push_matched_paren(int index, const sub_match<BidiIterator>& sub);
  481. void push_recursion_stopper();
  482. void push_assertion(const re_syntax_base* ps, bool positive);
  483. void push_alt(const re_syntax_base* ps);
  484. void push_repeater_count(int i, repeater_count<BidiIterator>** s);
  485. void push_single_repeat(std::size_t c, const re_repeat* r, BidiIterator last_position, int state_id);
  486. void push_non_greedy_repeat(const re_syntax_base* ps);
  487. void push_recursion(int idx, const re_syntax_base* p, results_type* presults, results_type* presults2);
  488. void push_recursion_pop();
  489. void push_case_change(bool);
  490. // pointer to base of stack:
  491. saved_state* m_stack_base;
  492. // pointer to current stack position:
  493. saved_state* m_backup_state;
  494. // how many memory blocks have we used up?:
  495. unsigned used_block_count;
  496. // determines what value to return when unwinding from recursion,
  497. // allows for mixed recursive/non-recursive algorithm:
  498. bool m_recursive_result;
  499. // We have unwound to a lookahead/lookbehind, used by COMMIT/PRUNE/SKIP:
  500. bool m_unwound_lookahead;
  501. // We have unwound to an alternative, used by THEN:
  502. bool m_unwound_alt;
  503. // We are unwinding a commit - used by independent subs to determine whether to stop there or carry on unwinding:
  504. //bool m_unwind_commit;
  505. // Recursion limit:
  506. unsigned m_recursions;
  507. #ifdef BOOST_REGEX_MSVC
  508. # pragma warning(push)
  509. #if BOOST_REGEX_MSVC >= 1800
  510. #pragma warning(disable:26495)
  511. #endif
  512. #endif
  513. // these operations aren't allowed, so are declared private,
  514. // bodies are provided to keep explicit-instantiation requests happy:
  515. perl_matcher& operator=(const perl_matcher&)
  516. {
  517. return *this;
  518. }
  519. perl_matcher(const perl_matcher& that)
  520. : m_result(that.m_result), re(that.re), traits_inst(that.traits_inst), rep_obj(0) {}
  521. #ifdef BOOST_REGEX_MSVC
  522. # pragma warning(pop)
  523. #endif
  524. };
  525. } // namespace BOOST_REGEX_DETAIL_NS
  526. #ifdef BOOST_REGEX_MSVC
  527. # pragma warning(pop)
  528. #endif
  529. } // namespace boost
  530. //
  531. // include the implementation of perl_matcher:
  532. //
  533. #include <boost/regex/v5/perl_matcher_non_recursive.hpp>
  534. // this one has to be last:
  535. #include <boost/regex/v5/perl_matcher_common.hpp>
  536. #endif