perl_matcher.hpp 18 KB

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