perl_matcher.hpp 18 KB

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