perl_matcher_non_recursive.hpp 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947
  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. /*
  12. * LOCATION: see http://www.boost.org for most recent version.
  13. * FILE perl_matcher_common.cpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: Definitions of perl_matcher member functions that are
  16. * specific to the non-recursive implementation.
  17. */
  18. #ifndef BOOST_REGEX_V4_PERL_MATCHER_NON_RECURSIVE_HPP
  19. #define BOOST_REGEX_V4_PERL_MATCHER_NON_RECURSIVE_HPP
  20. #include <boost/regex/v4/mem_block_cache.hpp>
  21. #ifdef BOOST_MSVC
  22. #pragma warning(push)
  23. #pragma warning(disable: 4103)
  24. #endif
  25. #ifdef BOOST_HAS_ABI_HEADERS
  26. # include BOOST_ABI_PREFIX
  27. #endif
  28. #ifdef BOOST_MSVC
  29. #pragma warning(pop)
  30. #endif
  31. #ifdef BOOST_MSVC
  32. # pragma warning(push)
  33. # pragma warning(disable: 4706)
  34. #if BOOST_MSVC < 1910
  35. #pragma warning(disable:4800)
  36. #endif
  37. #endif
  38. namespace boost{
  39. namespace BOOST_REGEX_DETAIL_NS{
  40. template <class T>
  41. inline void inplace_destroy(T* p)
  42. {
  43. (void)p; // warning suppression
  44. p->~T();
  45. }
  46. struct saved_state
  47. {
  48. union{
  49. unsigned int state_id;
  50. // this padding ensures correct alignment on 64-bit platforms:
  51. std::size_t padding1;
  52. std::ptrdiff_t padding2;
  53. void* padding3;
  54. };
  55. saved_state(unsigned i) : state_id(i) {}
  56. };
  57. template <class BidiIterator>
  58. struct saved_matched_paren : public saved_state
  59. {
  60. int index;
  61. sub_match<BidiIterator> sub;
  62. saved_matched_paren(int i, const sub_match<BidiIterator>& s) : saved_state(1), index(i), sub(s){}
  63. };
  64. template <class BidiIterator>
  65. struct saved_position : public saved_state
  66. {
  67. const re_syntax_base* pstate;
  68. BidiIterator position;
  69. saved_position(const re_syntax_base* ps, BidiIterator pos, int i) : saved_state(i), pstate(ps), position(pos){}
  70. };
  71. template <class BidiIterator>
  72. struct saved_assertion : public saved_position<BidiIterator>
  73. {
  74. bool positive;
  75. saved_assertion(bool p, const re_syntax_base* ps, BidiIterator pos)
  76. : saved_position<BidiIterator>(ps, pos, saved_type_assertion), positive(p){}
  77. };
  78. template <class BidiIterator>
  79. struct saved_repeater : public saved_state
  80. {
  81. repeater_count<BidiIterator> count;
  82. saved_repeater(int i, repeater_count<BidiIterator>** s, BidiIterator start, int current_recursion_id)
  83. : saved_state(saved_state_repeater_count), count(i, s, start, current_recursion_id){}
  84. };
  85. struct saved_extra_block : public saved_state
  86. {
  87. saved_state *base, *end;
  88. saved_extra_block(saved_state* b, saved_state* e)
  89. : saved_state(saved_state_extra_block), base(b), end(e) {}
  90. };
  91. struct save_state_init
  92. {
  93. saved_state** stack;
  94. save_state_init(saved_state** base, saved_state** end)
  95. : stack(base)
  96. {
  97. *base = static_cast<saved_state*>(get_mem_block());
  98. *end = reinterpret_cast<saved_state*>(reinterpret_cast<char*>(*base)+BOOST_REGEX_BLOCKSIZE);
  99. --(*end);
  100. (void) new (*end)saved_state(0);
  101. BOOST_REGEX_ASSERT(*end > *base);
  102. }
  103. ~save_state_init()
  104. {
  105. put_mem_block(*stack);
  106. *stack = 0;
  107. }
  108. };
  109. template <class BidiIterator>
  110. struct saved_single_repeat : public saved_state
  111. {
  112. std::size_t count;
  113. const re_repeat* rep;
  114. BidiIterator last_position;
  115. saved_single_repeat(std::size_t c, const re_repeat* r, BidiIterator lp, int arg_id)
  116. : saved_state(arg_id), count(c), rep(r), last_position(lp){}
  117. };
  118. template <class Results>
  119. struct saved_recursion : public saved_state
  120. {
  121. saved_recursion(int idx, const re_syntax_base* p, Results* pr, Results* pr2)
  122. : saved_state(14), recursion_id(idx), preturn_address(p), internal_results(*pr), prior_results(*pr2) {}
  123. int recursion_id;
  124. const re_syntax_base* preturn_address;
  125. Results internal_results, prior_results;
  126. };
  127. struct saved_change_case : public saved_state
  128. {
  129. bool icase;
  130. saved_change_case(bool c) : saved_state(18), icase(c) {}
  131. };
  132. struct incrementer
  133. {
  134. incrementer(unsigned* pu) : m_pu(pu) { ++*m_pu; }
  135. ~incrementer() { --*m_pu; }
  136. bool operator > (unsigned i) { return *m_pu > i; }
  137. private:
  138. unsigned* m_pu;
  139. };
  140. template <class BidiIterator, class Allocator, class traits>
  141. bool perl_matcher<BidiIterator, Allocator, traits>::match_all_states()
  142. {
  143. static matcher_proc_type const s_match_vtable[34] =
  144. {
  145. (&perl_matcher<BidiIterator, Allocator, traits>::match_startmark),
  146. &perl_matcher<BidiIterator, Allocator, traits>::match_endmark,
  147. &perl_matcher<BidiIterator, Allocator, traits>::match_literal,
  148. &perl_matcher<BidiIterator, Allocator, traits>::match_start_line,
  149. &perl_matcher<BidiIterator, Allocator, traits>::match_end_line,
  150. &perl_matcher<BidiIterator, Allocator, traits>::match_wild,
  151. &perl_matcher<BidiIterator, Allocator, traits>::match_match,
  152. &perl_matcher<BidiIterator, Allocator, traits>::match_word_boundary,
  153. &perl_matcher<BidiIterator, Allocator, traits>::match_within_word,
  154. &perl_matcher<BidiIterator, Allocator, traits>::match_word_start,
  155. &perl_matcher<BidiIterator, Allocator, traits>::match_word_end,
  156. &perl_matcher<BidiIterator, Allocator, traits>::match_buffer_start,
  157. &perl_matcher<BidiIterator, Allocator, traits>::match_buffer_end,
  158. &perl_matcher<BidiIterator, Allocator, traits>::match_backref,
  159. &perl_matcher<BidiIterator, Allocator, traits>::match_long_set,
  160. &perl_matcher<BidiIterator, Allocator, traits>::match_set,
  161. &perl_matcher<BidiIterator, Allocator, traits>::match_jump,
  162. &perl_matcher<BidiIterator, Allocator, traits>::match_alt,
  163. &perl_matcher<BidiIterator, Allocator, traits>::match_rep,
  164. &perl_matcher<BidiIterator, Allocator, traits>::match_combining,
  165. &perl_matcher<BidiIterator, Allocator, traits>::match_soft_buffer_end,
  166. &perl_matcher<BidiIterator, Allocator, traits>::match_restart_continue,
  167. // Although this next line *should* be evaluated at compile time, in practice
  168. // some compilers (VC++) emit run-time initialisation which breaks thread
  169. // safety, so use a dispatch function instead:
  170. //(::boost::is_random_access_iterator<BidiIterator>::value ? &perl_matcher<BidiIterator, Allocator, traits>::match_dot_repeat_fast : &perl_matcher<BidiIterator, Allocator, traits>::match_dot_repeat_slow),
  171. &perl_matcher<BidiIterator, Allocator, traits>::match_dot_repeat_dispatch,
  172. &perl_matcher<BidiIterator, Allocator, traits>::match_char_repeat,
  173. &perl_matcher<BidiIterator, Allocator, traits>::match_set_repeat,
  174. &perl_matcher<BidiIterator, Allocator, traits>::match_long_set_repeat,
  175. &perl_matcher<BidiIterator, Allocator, traits>::match_backstep,
  176. &perl_matcher<BidiIterator, Allocator, traits>::match_assert_backref,
  177. &perl_matcher<BidiIterator, Allocator, traits>::match_toggle_case,
  178. &perl_matcher<BidiIterator, Allocator, traits>::match_recursion,
  179. &perl_matcher<BidiIterator, Allocator, traits>::match_fail,
  180. &perl_matcher<BidiIterator, Allocator, traits>::match_accept,
  181. &perl_matcher<BidiIterator, Allocator, traits>::match_commit,
  182. &perl_matcher<BidiIterator, Allocator, traits>::match_then,
  183. };
  184. incrementer inc(&m_recursions);
  185. if(inc > 80)
  186. raise_error(traits_inst, regex_constants::error_complexity);
  187. push_recursion_stopper();
  188. do{
  189. while(pstate)
  190. {
  191. matcher_proc_type proc = s_match_vtable[pstate->type];
  192. ++state_count;
  193. if(!(this->*proc)())
  194. {
  195. if(state_count > max_state_count)
  196. raise_error(traits_inst, regex_constants::error_complexity);
  197. if((m_match_flags & match_partial) && (position == last) && (position != search_base))
  198. m_has_partial_match = true;
  199. bool successful_unwind = unwind(false);
  200. if((m_match_flags & match_partial) && (position == last) && (position != search_base))
  201. m_has_partial_match = true;
  202. if(!successful_unwind)
  203. return m_recursive_result;
  204. }
  205. }
  206. }while(unwind(true));
  207. return m_recursive_result;
  208. }
  209. template <class BidiIterator, class Allocator, class traits>
  210. void perl_matcher<BidiIterator, Allocator, traits>::extend_stack()
  211. {
  212. if(used_block_count)
  213. {
  214. --used_block_count;
  215. saved_state* stack_base;
  216. saved_state* backup_state;
  217. stack_base = static_cast<saved_state*>(get_mem_block());
  218. backup_state = reinterpret_cast<saved_state*>(reinterpret_cast<char*>(stack_base)+BOOST_REGEX_BLOCKSIZE);
  219. saved_extra_block* block = static_cast<saved_extra_block*>(backup_state);
  220. --block;
  221. (void) new (block) saved_extra_block(m_stack_base, m_backup_state);
  222. m_stack_base = stack_base;
  223. m_backup_state = block;
  224. }
  225. else
  226. raise_error(traits_inst, regex_constants::error_stack);
  227. }
  228. template <class BidiIterator, class Allocator, class traits>
  229. inline void perl_matcher<BidiIterator, Allocator, traits>::push_matched_paren(int index, const sub_match<BidiIterator>& sub)
  230. {
  231. //BOOST_REGEX_ASSERT(index);
  232. saved_matched_paren<BidiIterator>* pmp = static_cast<saved_matched_paren<BidiIterator>*>(m_backup_state);
  233. --pmp;
  234. if(pmp < m_stack_base)
  235. {
  236. extend_stack();
  237. pmp = static_cast<saved_matched_paren<BidiIterator>*>(m_backup_state);
  238. --pmp;
  239. }
  240. (void) new (pmp)saved_matched_paren<BidiIterator>(index, sub);
  241. m_backup_state = pmp;
  242. }
  243. template <class BidiIterator, class Allocator, class traits>
  244. inline void perl_matcher<BidiIterator, Allocator, traits>::push_case_change(bool c)
  245. {
  246. //BOOST_REGEX_ASSERT(index);
  247. saved_change_case* pmp = static_cast<saved_change_case*>(m_backup_state);
  248. --pmp;
  249. if(pmp < m_stack_base)
  250. {
  251. extend_stack();
  252. pmp = static_cast<saved_change_case*>(m_backup_state);
  253. --pmp;
  254. }
  255. (void) new (pmp)saved_change_case(c);
  256. m_backup_state = pmp;
  257. }
  258. template <class BidiIterator, class Allocator, class traits>
  259. inline void perl_matcher<BidiIterator, Allocator, traits>::push_recursion_stopper()
  260. {
  261. saved_state* pmp = m_backup_state;
  262. --pmp;
  263. if(pmp < m_stack_base)
  264. {
  265. extend_stack();
  266. pmp = m_backup_state;
  267. --pmp;
  268. }
  269. (void) new (pmp)saved_state(saved_type_recurse);
  270. m_backup_state = pmp;
  271. }
  272. template <class BidiIterator, class Allocator, class traits>
  273. inline void perl_matcher<BidiIterator, Allocator, traits>::push_assertion(const re_syntax_base* ps, bool positive)
  274. {
  275. saved_assertion<BidiIterator>* pmp = static_cast<saved_assertion<BidiIterator>*>(m_backup_state);
  276. --pmp;
  277. if(pmp < m_stack_base)
  278. {
  279. extend_stack();
  280. pmp = static_cast<saved_assertion<BidiIterator>*>(m_backup_state);
  281. --pmp;
  282. }
  283. (void) new (pmp)saved_assertion<BidiIterator>(positive, ps, position);
  284. m_backup_state = pmp;
  285. }
  286. template <class BidiIterator, class Allocator, class traits>
  287. inline void perl_matcher<BidiIterator, Allocator, traits>::push_alt(const re_syntax_base* ps)
  288. {
  289. saved_position<BidiIterator>* pmp = static_cast<saved_position<BidiIterator>*>(m_backup_state);
  290. --pmp;
  291. if(pmp < m_stack_base)
  292. {
  293. extend_stack();
  294. pmp = static_cast<saved_position<BidiIterator>*>(m_backup_state);
  295. --pmp;
  296. }
  297. (void) new (pmp)saved_position<BidiIterator>(ps, position, saved_state_alt);
  298. m_backup_state = pmp;
  299. }
  300. template <class BidiIterator, class Allocator, class traits>
  301. inline void perl_matcher<BidiIterator, Allocator, traits>::push_non_greedy_repeat(const re_syntax_base* ps)
  302. {
  303. saved_position<BidiIterator>* pmp = static_cast<saved_position<BidiIterator>*>(m_backup_state);
  304. --pmp;
  305. if(pmp < m_stack_base)
  306. {
  307. extend_stack();
  308. pmp = static_cast<saved_position<BidiIterator>*>(m_backup_state);
  309. --pmp;
  310. }
  311. (void) new (pmp)saved_position<BidiIterator>(ps, position, saved_state_non_greedy_long_repeat);
  312. m_backup_state = pmp;
  313. }
  314. template <class BidiIterator, class Allocator, class traits>
  315. inline void perl_matcher<BidiIterator, Allocator, traits>::push_repeater_count(int i, repeater_count<BidiIterator>** s)
  316. {
  317. saved_repeater<BidiIterator>* pmp = static_cast<saved_repeater<BidiIterator>*>(m_backup_state);
  318. --pmp;
  319. if(pmp < m_stack_base)
  320. {
  321. extend_stack();
  322. pmp = static_cast<saved_repeater<BidiIterator>*>(m_backup_state);
  323. --pmp;
  324. }
  325. (void) new (pmp)saved_repeater<BidiIterator>(i, s, position, this->recursion_stack.empty() ? (INT_MIN + 3) : this->recursion_stack.back().idx);
  326. m_backup_state = pmp;
  327. }
  328. template <class BidiIterator, class Allocator, class traits>
  329. inline void perl_matcher<BidiIterator, Allocator, traits>::push_single_repeat(std::size_t c, const re_repeat* r, BidiIterator last_position, int state_id)
  330. {
  331. saved_single_repeat<BidiIterator>* pmp = static_cast<saved_single_repeat<BidiIterator>*>(m_backup_state);
  332. --pmp;
  333. if(pmp < m_stack_base)
  334. {
  335. extend_stack();
  336. pmp = static_cast<saved_single_repeat<BidiIterator>*>(m_backup_state);
  337. --pmp;
  338. }
  339. (void) new (pmp)saved_single_repeat<BidiIterator>(c, r, last_position, state_id);
  340. m_backup_state = pmp;
  341. }
  342. template <class BidiIterator, class Allocator, class traits>
  343. inline void perl_matcher<BidiIterator, Allocator, traits>::push_recursion(int idx, const re_syntax_base* p, results_type* presults, results_type* presults2)
  344. {
  345. saved_recursion<results_type>* pmp = static_cast<saved_recursion<results_type>*>(m_backup_state);
  346. --pmp;
  347. if(pmp < m_stack_base)
  348. {
  349. extend_stack();
  350. pmp = static_cast<saved_recursion<results_type>*>(m_backup_state);
  351. --pmp;
  352. }
  353. (void) new (pmp)saved_recursion<results_type>(idx, p, presults, presults2);
  354. m_backup_state = pmp;
  355. }
  356. template <class BidiIterator, class Allocator, class traits>
  357. bool perl_matcher<BidiIterator, Allocator, traits>::match_toggle_case()
  358. {
  359. // change our case sensitivity:
  360. push_case_change(this->icase);
  361. this->icase = static_cast<const re_case*>(pstate)->icase;
  362. pstate = pstate->next.p;
  363. return true;
  364. }
  365. template <class BidiIterator, class Allocator, class traits>
  366. bool perl_matcher<BidiIterator, Allocator, traits>::match_startmark()
  367. {
  368. int index = static_cast<const re_brace*>(pstate)->index;
  369. icase = static_cast<const re_brace*>(pstate)->icase;
  370. switch(index)
  371. {
  372. case 0:
  373. pstate = pstate->next.p;
  374. break;
  375. case -1:
  376. case -2:
  377. {
  378. // forward lookahead assert:
  379. const re_syntax_base* next_pstate = static_cast<const re_jump*>(pstate->next.p)->alt.p->next.p;
  380. pstate = pstate->next.p->next.p;
  381. push_assertion(next_pstate, index == -1);
  382. break;
  383. }
  384. case -3:
  385. {
  386. // independent sub-expression, currently this is always recursive:
  387. bool old_independent = m_independent;
  388. m_independent = true;
  389. const re_syntax_base* next_pstate = static_cast<const re_jump*>(pstate->next.p)->alt.p->next.p;
  390. pstate = pstate->next.p->next.p;
  391. bool r = false;
  392. #if !defined(BOOST_NO_EXCEPTIONS)
  393. try{
  394. #endif
  395. r = match_all_states();
  396. if(!r && !m_independent)
  397. {
  398. // Must be unwinding from a COMMIT/SKIP/PRUNE and the independent
  399. // sub failed, need to unwind everything else:
  400. while(unwind(false));
  401. return false;
  402. }
  403. #if !defined(BOOST_NO_EXCEPTIONS)
  404. }
  405. catch(...)
  406. {
  407. pstate = next_pstate;
  408. // unwind all pushed states, apart from anything else this
  409. // ensures that all the states are correctly destructed
  410. // not just the memory freed.
  411. while(unwind(true)) {}
  412. throw;
  413. }
  414. #endif
  415. pstate = next_pstate;
  416. m_independent = old_independent;
  417. #ifdef BOOST_REGEX_MATCH_EXTRA
  418. if(r && (m_match_flags & match_extra))
  419. {
  420. //
  421. // our captures have been stored in *m_presult
  422. // we need to unpack them, and insert them
  423. // back in the right order when we unwind the stack:
  424. //
  425. match_results<BidiIterator, Allocator> temp_match(*m_presult);
  426. unsigned i;
  427. for(i = 0; i < temp_match.size(); ++i)
  428. (*m_presult)[i].get_captures().clear();
  429. // match everything else:
  430. #if !defined(BOOST_NO_EXCEPTIONS)
  431. try{
  432. #endif
  433. r = match_all_states();
  434. #if !defined(BOOST_NO_EXCEPTIONS)
  435. }
  436. catch(...)
  437. {
  438. pstate = next_pstate;
  439. // unwind all pushed states, apart from anything else this
  440. // ensures that all the states are correctly destructed
  441. // not just the memory freed.
  442. while(unwind(true)) {}
  443. throw;
  444. }
  445. #endif
  446. // now place the stored captures back:
  447. for(i = 0; i < temp_match.size(); ++i)
  448. {
  449. typedef typename sub_match<BidiIterator>::capture_sequence_type seq;
  450. seq& s1 = (*m_presult)[i].get_captures();
  451. const seq& s2 = temp_match[i].captures();
  452. s1.insert(
  453. s1.end(),
  454. s2.begin(),
  455. s2.end());
  456. }
  457. }
  458. #endif
  459. return r;
  460. }
  461. case -4:
  462. {
  463. // conditional expression:
  464. const re_alt* alt = static_cast<const re_alt*>(pstate->next.p);
  465. BOOST_REGEX_ASSERT(alt->type == syntax_element_alt);
  466. pstate = alt->next.p;
  467. if(pstate->type == syntax_element_assert_backref)
  468. {
  469. if(!match_assert_backref())
  470. pstate = alt->alt.p;
  471. break;
  472. }
  473. else
  474. {
  475. // zero width assertion, have to match this recursively:
  476. BOOST_REGEX_ASSERT(pstate->type == syntax_element_startmark);
  477. bool negated = static_cast<const re_brace*>(pstate)->index == -2;
  478. BidiIterator saved_position = position;
  479. const re_syntax_base* next_pstate = static_cast<const re_jump*>(pstate->next.p)->alt.p->next.p;
  480. pstate = pstate->next.p->next.p;
  481. #if !defined(BOOST_NO_EXCEPTIONS)
  482. try{
  483. #endif
  484. bool r = match_all_states();
  485. position = saved_position;
  486. if(negated)
  487. r = !r;
  488. if(r)
  489. pstate = next_pstate;
  490. else
  491. pstate = alt->alt.p;
  492. #if !defined(BOOST_NO_EXCEPTIONS)
  493. }
  494. catch(...)
  495. {
  496. pstate = next_pstate;
  497. // unwind all pushed states, apart from anything else this
  498. // ensures that all the states are correctly destructed
  499. // not just the memory freed.
  500. while(unwind(true)){}
  501. throw;
  502. }
  503. #endif
  504. break;
  505. }
  506. }
  507. case -5:
  508. {
  509. push_matched_paren(0, (*m_presult)[0]);
  510. m_presult->set_first(position, 0, true);
  511. pstate = pstate->next.p;
  512. break;
  513. }
  514. default:
  515. {
  516. BOOST_REGEX_ASSERT(index > 0);
  517. if((m_match_flags & match_nosubs) == 0)
  518. {
  519. push_matched_paren(index, (*m_presult)[index]);
  520. m_presult->set_first(position, index);
  521. }
  522. pstate = pstate->next.p;
  523. break;
  524. }
  525. }
  526. return true;
  527. }
  528. template <class BidiIterator, class Allocator, class traits>
  529. bool perl_matcher<BidiIterator, Allocator, traits>::match_alt()
  530. {
  531. bool take_first, take_second;
  532. const re_alt* jmp = static_cast<const re_alt*>(pstate);
  533. // find out which of these two alternatives we need to take:
  534. if(position == last)
  535. {
  536. take_first = jmp->can_be_null & mask_take;
  537. take_second = jmp->can_be_null & mask_skip;
  538. }
  539. else
  540. {
  541. take_first = can_start(*position, jmp->_map, (unsigned char)mask_take);
  542. take_second = can_start(*position, jmp->_map, (unsigned char)mask_skip);
  543. }
  544. if(take_first)
  545. {
  546. // we can take the first alternative,
  547. // see if we need to push next alternative:
  548. if(take_second)
  549. {
  550. push_alt(jmp->alt.p);
  551. }
  552. pstate = pstate->next.p;
  553. return true;
  554. }
  555. if(take_second)
  556. {
  557. pstate = jmp->alt.p;
  558. return true;
  559. }
  560. return false; // neither option is possible
  561. }
  562. template <class BidiIterator, class Allocator, class traits>
  563. bool perl_matcher<BidiIterator, Allocator, traits>::match_rep()
  564. {
  565. #ifdef BOOST_MSVC
  566. #pragma warning(push)
  567. #pragma warning(disable:4127 4244)
  568. #endif
  569. #ifdef BOOST_BORLANDC
  570. #pragma option push -w-8008 -w-8066 -w-8004
  571. #endif
  572. const re_repeat* rep = static_cast<const re_repeat*>(pstate);
  573. // find out which of these two alternatives we need to take:
  574. bool take_first, take_second;
  575. if(position == last)
  576. {
  577. take_first = rep->can_be_null & mask_take;
  578. take_second = rep->can_be_null & mask_skip;
  579. }
  580. else
  581. {
  582. take_first = can_start(*position, rep->_map, (unsigned char)mask_take);
  583. take_second = can_start(*position, rep->_map, (unsigned char)mask_skip);
  584. }
  585. if((m_backup_state->state_id != saved_state_repeater_count)
  586. || (static_cast<saved_repeater<BidiIterator>*>(m_backup_state)->count.get_id() != rep->state_id)
  587. || (next_count->get_id() != rep->state_id))
  588. {
  589. // we're moving to a different repeat from the last
  590. // one, so set up a counter object:
  591. push_repeater_count(rep->state_id, &next_count);
  592. }
  593. //
  594. // If we've had at least one repeat already, and the last one
  595. // matched the NULL string then set the repeat count to
  596. // maximum:
  597. //
  598. next_count->check_null_repeat(position, rep->max);
  599. if(next_count->get_count() < rep->min)
  600. {
  601. // we must take the repeat:
  602. if(take_first)
  603. {
  604. // increase the counter:
  605. ++(*next_count);
  606. pstate = rep->next.p;
  607. return true;
  608. }
  609. return false;
  610. }
  611. bool greedy = (rep->greedy) && (!(m_match_flags & regex_constants::match_any) || m_independent);
  612. if(greedy)
  613. {
  614. // try and take the repeat if we can:
  615. if((next_count->get_count() < rep->max) && take_first)
  616. {
  617. if(take_second)
  618. {
  619. // store position in case we fail:
  620. push_alt(rep->alt.p);
  621. }
  622. // increase the counter:
  623. ++(*next_count);
  624. pstate = rep->next.p;
  625. return true;
  626. }
  627. else if(take_second)
  628. {
  629. pstate = rep->alt.p;
  630. return true;
  631. }
  632. return false; // can't take anything, fail...
  633. }
  634. else // non-greedy
  635. {
  636. // try and skip the repeat if we can:
  637. if(take_second)
  638. {
  639. if((next_count->get_count() < rep->max) && take_first)
  640. {
  641. // store position in case we fail:
  642. push_non_greedy_repeat(rep->next.p);
  643. }
  644. pstate = rep->alt.p;
  645. return true;
  646. }
  647. if((next_count->get_count() < rep->max) && take_first)
  648. {
  649. // increase the counter:
  650. ++(*next_count);
  651. pstate = rep->next.p;
  652. return true;
  653. }
  654. }
  655. return false;
  656. #ifdef BOOST_BORLANDC
  657. #pragma option pop
  658. #endif
  659. #ifdef BOOST_MSVC
  660. #pragma warning(pop)
  661. #endif
  662. }
  663. template <class BidiIterator, class Allocator, class traits>
  664. bool perl_matcher<BidiIterator, Allocator, traits>::match_dot_repeat_slow()
  665. {
  666. std::size_t count = 0;
  667. const re_repeat* rep = static_cast<const re_repeat*>(pstate);
  668. re_syntax_base* psingle = rep->next.p;
  669. // match compulsory repeats first:
  670. while(count < rep->min)
  671. {
  672. pstate = psingle;
  673. if(!match_wild())
  674. return false;
  675. ++count;
  676. }
  677. bool greedy = (rep->greedy) && (!(m_match_flags & regex_constants::match_any) || m_independent);
  678. if(greedy)
  679. {
  680. // repeat for as long as we can:
  681. while(count < rep->max)
  682. {
  683. pstate = psingle;
  684. if(!match_wild())
  685. break;
  686. ++count;
  687. }
  688. // remember where we got to if this is a leading repeat:
  689. if((rep->leading) && (count < rep->max))
  690. restart = position;
  691. // push backtrack info if available:
  692. if(count - rep->min)
  693. push_single_repeat(count, rep, position, saved_state_greedy_single_repeat);
  694. // jump to next state:
  695. pstate = rep->alt.p;
  696. return true;
  697. }
  698. else
  699. {
  700. // non-greedy, push state and return true if we can skip:
  701. if(count < rep->max)
  702. push_single_repeat(count, rep, position, saved_state_rep_slow_dot);
  703. pstate = rep->alt.p;
  704. return (position == last) ? (rep->can_be_null & mask_skip) : can_start(*position, rep->_map, mask_skip);
  705. }
  706. }
  707. template <class BidiIterator, class Allocator, class traits>
  708. bool perl_matcher<BidiIterator, Allocator, traits>::match_dot_repeat_fast()
  709. {
  710. if(m_match_flags & match_not_dot_null)
  711. return match_dot_repeat_slow();
  712. if((static_cast<const re_dot*>(pstate->next.p)->mask & match_any_mask) == 0)
  713. return match_dot_repeat_slow();
  714. const re_repeat* rep = static_cast<const re_repeat*>(pstate);
  715. bool greedy = (rep->greedy) && (!(m_match_flags & regex_constants::match_any) || m_independent);
  716. std::size_t count = static_cast<std::size_t>((std::min)(static_cast<std::size_t>(::boost::BOOST_REGEX_DETAIL_NS::distance(position, last)), greedy ? rep->max : rep->min));
  717. if(rep->min > count)
  718. {
  719. position = last;
  720. return false; // not enough text left to match
  721. }
  722. std::advance(position, count);
  723. if(greedy)
  724. {
  725. if((rep->leading) && (count < rep->max))
  726. restart = position;
  727. // push backtrack info if available:
  728. if(count - rep->min)
  729. push_single_repeat(count, rep, position, saved_state_greedy_single_repeat);
  730. // jump to next state:
  731. pstate = rep->alt.p;
  732. return true;
  733. }
  734. else
  735. {
  736. // non-greedy, push state and return true if we can skip:
  737. if(count < rep->max)
  738. push_single_repeat(count, rep, position, saved_state_rep_fast_dot);
  739. pstate = rep->alt.p;
  740. return (position == last) ? (rep->can_be_null & mask_skip) : can_start(*position, rep->_map, mask_skip);
  741. }
  742. }
  743. template <class BidiIterator, class Allocator, class traits>
  744. bool perl_matcher<BidiIterator, Allocator, traits>::match_char_repeat()
  745. {
  746. #ifdef BOOST_MSVC
  747. #pragma warning(push)
  748. #pragma warning(disable:4127)
  749. #endif
  750. #ifdef BOOST_BORLANDC
  751. #pragma option push -w-8008 -w-8066 -w-8004
  752. #endif
  753. const re_repeat* rep = static_cast<const re_repeat*>(pstate);
  754. BOOST_REGEX_ASSERT(1 == static_cast<const re_literal*>(rep->next.p)->length);
  755. const char_type what = *reinterpret_cast<const char_type*>(static_cast<const re_literal*>(rep->next.p) + 1);
  756. std::size_t count = 0;
  757. //
  758. // start by working out how much we can skip:
  759. //
  760. bool greedy = (rep->greedy) && (!(m_match_flags & regex_constants::match_any) || m_independent);
  761. std::size_t desired = greedy ? rep->max : rep->min;
  762. if(::boost::is_random_access_iterator<BidiIterator>::value)
  763. {
  764. BidiIterator end = position;
  765. // Move end forward by "desired", preferably without using distance or advance if we can
  766. // as these can be slow for some iterator types.
  767. std::size_t len = (desired == (std::numeric_limits<std::size_t>::max)()) ? 0u : ::boost::BOOST_REGEX_DETAIL_NS::distance(position, last);
  768. if(desired >= len)
  769. end = last;
  770. else
  771. std::advance(end, desired);
  772. BidiIterator origin(position);
  773. while((position != end) && (traits_inst.translate(*position, icase) == what))
  774. {
  775. ++position;
  776. }
  777. count = (unsigned)::boost::BOOST_REGEX_DETAIL_NS::distance(origin, position);
  778. }
  779. else
  780. {
  781. while((count < desired) && (position != last) && (traits_inst.translate(*position, icase) == what))
  782. {
  783. ++position;
  784. ++count;
  785. }
  786. }
  787. if(count < rep->min)
  788. return false;
  789. if(greedy)
  790. {
  791. if((rep->leading) && (count < rep->max))
  792. restart = position;
  793. // push backtrack info if available:
  794. if(count - rep->min)
  795. push_single_repeat(count, rep, position, saved_state_greedy_single_repeat);
  796. // jump to next state:
  797. pstate = rep->alt.p;
  798. return true;
  799. }
  800. else
  801. {
  802. // non-greedy, push state and return true if we can skip:
  803. if(count < rep->max)
  804. push_single_repeat(count, rep, position, saved_state_rep_char);
  805. pstate = rep->alt.p;
  806. return (position == last) ? (rep->can_be_null & mask_skip) : can_start(*position, rep->_map, mask_skip);
  807. }
  808. #ifdef BOOST_BORLANDC
  809. #pragma option pop
  810. #endif
  811. #ifdef BOOST_MSVC
  812. #pragma warning(pop)
  813. #endif
  814. }
  815. template <class BidiIterator, class Allocator, class traits>
  816. bool perl_matcher<BidiIterator, Allocator, traits>::match_set_repeat()
  817. {
  818. #ifdef BOOST_MSVC
  819. #pragma warning(push)
  820. #pragma warning(disable:4127)
  821. #endif
  822. #ifdef BOOST_BORLANDC
  823. #pragma option push -w-8008 -w-8066 -w-8004
  824. #endif
  825. const re_repeat* rep = static_cast<const re_repeat*>(pstate);
  826. const unsigned char* map = static_cast<const re_set*>(rep->next.p)->_map;
  827. std::size_t count = 0;
  828. //
  829. // start by working out how much we can skip:
  830. //
  831. bool greedy = (rep->greedy) && (!(m_match_flags & regex_constants::match_any) || m_independent);
  832. std::size_t desired = greedy ? rep->max : rep->min;
  833. if(::boost::is_random_access_iterator<BidiIterator>::value)
  834. {
  835. BidiIterator end = position;
  836. // Move end forward by "desired", preferably without using distance or advance if we can
  837. // as these can be slow for some iterator types.
  838. std::size_t len = (desired == (std::numeric_limits<std::size_t>::max)()) ? 0u : ::boost::BOOST_REGEX_DETAIL_NS::distance(position, last);
  839. if(desired >= len)
  840. end = last;
  841. else
  842. std::advance(end, desired);
  843. BidiIterator origin(position);
  844. while((position != end) && map[static_cast<unsigned char>(traits_inst.translate(*position, icase))])
  845. {
  846. ++position;
  847. }
  848. count = (unsigned)::boost::BOOST_REGEX_DETAIL_NS::distance(origin, position);
  849. }
  850. else
  851. {
  852. while((count < desired) && (position != last) && map[static_cast<unsigned char>(traits_inst.translate(*position, icase))])
  853. {
  854. ++position;
  855. ++count;
  856. }
  857. }
  858. if(count < rep->min)
  859. return false;
  860. if(greedy)
  861. {
  862. if((rep->leading) && (count < rep->max))
  863. restart = position;
  864. // push backtrack info if available:
  865. if(count - rep->min)
  866. push_single_repeat(count, rep, position, saved_state_greedy_single_repeat);
  867. // jump to next state:
  868. pstate = rep->alt.p;
  869. return true;
  870. }
  871. else
  872. {
  873. // non-greedy, push state and return true if we can skip:
  874. if(count < rep->max)
  875. push_single_repeat(count, rep, position, saved_state_rep_short_set);
  876. pstate = rep->alt.p;
  877. return (position == last) ? (rep->can_be_null & mask_skip) : can_start(*position, rep->_map, mask_skip);
  878. }
  879. #ifdef BOOST_BORLANDC
  880. #pragma option pop
  881. #endif
  882. #ifdef BOOST_MSVC
  883. #pragma warning(pop)
  884. #endif
  885. }
  886. template <class BidiIterator, class Allocator, class traits>
  887. bool perl_matcher<BidiIterator, Allocator, traits>::match_long_set_repeat()
  888. {
  889. #ifdef BOOST_MSVC
  890. #pragma warning(push)
  891. #pragma warning(disable:4127)
  892. #endif
  893. #ifdef BOOST_BORLANDC
  894. #pragma option push -w-8008 -w-8066 -w-8004
  895. #endif
  896. typedef typename traits::char_class_type m_type;
  897. const re_repeat* rep = static_cast<const re_repeat*>(pstate);
  898. const re_set_long<m_type>* set = static_cast<const re_set_long<m_type>*>(pstate->next.p);
  899. std::size_t count = 0;
  900. //
  901. // start by working out how much we can skip:
  902. //
  903. bool greedy = (rep->greedy) && (!(m_match_flags & regex_constants::match_any) || m_independent);
  904. std::size_t desired = greedy ? rep->max : rep->min;
  905. if(::boost::is_random_access_iterator<BidiIterator>::value)
  906. {
  907. BidiIterator end = position;
  908. // Move end forward by "desired", preferably without using distance or advance if we can
  909. // as these can be slow for some iterator types.
  910. std::size_t len = (desired == (std::numeric_limits<std::size_t>::max)()) ? 0u : ::boost::BOOST_REGEX_DETAIL_NS::distance(position, last);
  911. if(desired >= len)
  912. end = last;
  913. else
  914. std::advance(end, desired);
  915. BidiIterator origin(position);
  916. while((position != end) && (position != re_is_set_member(position, last, set, re.get_data(), icase)))
  917. {
  918. ++position;
  919. }
  920. count = (unsigned)::boost::BOOST_REGEX_DETAIL_NS::distance(origin, position);
  921. }
  922. else
  923. {
  924. while((count < desired) && (position != last) && (position != re_is_set_member(position, last, set, re.get_data(), icase)))
  925. {
  926. ++position;
  927. ++count;
  928. }
  929. }
  930. if(count < rep->min)
  931. return false;
  932. if(greedy)
  933. {
  934. if((rep->leading) && (count < rep->max))
  935. restart = position;
  936. // push backtrack info if available:
  937. if(count - rep->min)
  938. push_single_repeat(count, rep, position, saved_state_greedy_single_repeat);
  939. // jump to next state:
  940. pstate = rep->alt.p;
  941. return true;
  942. }
  943. else
  944. {
  945. // non-greedy, push state and return true if we can skip:
  946. if(count < rep->max)
  947. push_single_repeat(count, rep, position, saved_state_rep_long_set);
  948. pstate = rep->alt.p;
  949. return (position == last) ? (rep->can_be_null & mask_skip) : can_start(*position, rep->_map, mask_skip);
  950. }
  951. #ifdef BOOST_BORLANDC
  952. #pragma option pop
  953. #endif
  954. #ifdef BOOST_MSVC
  955. #pragma warning(pop)
  956. #endif
  957. }
  958. template <class BidiIterator, class Allocator, class traits>
  959. bool perl_matcher<BidiIterator, Allocator, traits>::match_recursion()
  960. {
  961. BOOST_REGEX_ASSERT(pstate->type == syntax_element_recurse);
  962. //
  963. // See if we've seen this recursion before at this location, if we have then
  964. // we need to prevent infinite recursion:
  965. //
  966. for(typename std::vector<recursion_info<results_type> >::reverse_iterator i = recursion_stack.rbegin(); i != recursion_stack.rend(); ++i)
  967. {
  968. if(i->idx == static_cast<const re_brace*>(static_cast<const re_jump*>(pstate)->alt.p)->index)
  969. {
  970. if(i->location_of_start == position)
  971. return false;
  972. break;
  973. }
  974. }
  975. //
  976. // Backup call stack:
  977. //
  978. push_recursion_pop();
  979. //
  980. // Set new call stack:
  981. //
  982. if(recursion_stack.capacity() == 0)
  983. {
  984. recursion_stack.reserve(50);
  985. }
  986. recursion_stack.push_back(recursion_info<results_type>());
  987. recursion_stack.back().preturn_address = pstate->next.p;
  988. recursion_stack.back().results = *m_presult;
  989. pstate = static_cast<const re_jump*>(pstate)->alt.p;
  990. recursion_stack.back().idx = static_cast<const re_brace*>(pstate)->index;
  991. recursion_stack.back().location_of_start = position;
  992. //if(static_cast<const re_recurse*>(pstate)->state_id > 0)
  993. {
  994. push_repeater_count(-(2 + static_cast<const re_brace*>(pstate)->index), &next_count);
  995. }
  996. return true;
  997. }
  998. template <class BidiIterator, class Allocator, class traits>
  999. bool perl_matcher<BidiIterator, Allocator, traits>::match_endmark()
  1000. {
  1001. int index = static_cast<const re_brace*>(pstate)->index;
  1002. icase = static_cast<const re_brace*>(pstate)->icase;
  1003. if(index > 0)
  1004. {
  1005. if((m_match_flags & match_nosubs) == 0)
  1006. {
  1007. m_presult->set_second(position, index);
  1008. }
  1009. if(!recursion_stack.empty())
  1010. {
  1011. if(index == recursion_stack.back().idx)
  1012. {
  1013. pstate = recursion_stack.back().preturn_address;
  1014. *m_presult = recursion_stack.back().results;
  1015. push_recursion(recursion_stack.back().idx, recursion_stack.back().preturn_address, m_presult, &recursion_stack.back().results);
  1016. recursion_stack.pop_back();
  1017. push_repeater_count(-(2 + index), &next_count);
  1018. }
  1019. }
  1020. }
  1021. else if((index < 0) && (index != -4))
  1022. {
  1023. // matched forward lookahead:
  1024. pstate = 0;
  1025. return true;
  1026. }
  1027. pstate = pstate->next.p;
  1028. return true;
  1029. }
  1030. template <class BidiIterator, class Allocator, class traits>
  1031. bool perl_matcher<BidiIterator, Allocator, traits>::match_match()
  1032. {
  1033. if(!recursion_stack.empty())
  1034. {
  1035. BOOST_REGEX_ASSERT(0 == recursion_stack.back().idx);
  1036. pstate = recursion_stack.back().preturn_address;
  1037. push_recursion(recursion_stack.back().idx, recursion_stack.back().preturn_address, m_presult, &recursion_stack.back().results);
  1038. *m_presult = recursion_stack.back().results;
  1039. recursion_stack.pop_back();
  1040. return true;
  1041. }
  1042. if((m_match_flags & match_not_null) && (position == (*m_presult)[0].first))
  1043. return false;
  1044. if((m_match_flags & match_all) && (position != last))
  1045. return false;
  1046. if((m_match_flags & regex_constants::match_not_initial_null) && (position == search_base))
  1047. return false;
  1048. m_presult->set_second(position);
  1049. pstate = 0;
  1050. m_has_found_match = true;
  1051. if((m_match_flags & match_posix) == match_posix)
  1052. {
  1053. m_result.maybe_assign(*m_presult);
  1054. if((m_match_flags & match_any) == 0)
  1055. return false;
  1056. }
  1057. #ifdef BOOST_REGEX_MATCH_EXTRA
  1058. if(match_extra & m_match_flags)
  1059. {
  1060. for(unsigned i = 0; i < m_presult->size(); ++i)
  1061. if((*m_presult)[i].matched)
  1062. ((*m_presult)[i]).get_captures().push_back((*m_presult)[i]);
  1063. }
  1064. #endif
  1065. return true;
  1066. }
  1067. template <class BidiIterator, class Allocator, class traits>
  1068. bool perl_matcher<BidiIterator, Allocator, traits>::match_commit()
  1069. {
  1070. // Ideally we would just junk all the states that are on the stack,
  1071. // however we might not unwind correctly in that case, so for now,
  1072. // just mark that we don't backtrack into whatever is left (or rather
  1073. // we'll unwind it unconditionally without pausing to try other matches).
  1074. switch(static_cast<const re_commit*>(pstate)->action)
  1075. {
  1076. case commit_commit:
  1077. restart = last;
  1078. break;
  1079. case commit_skip:
  1080. if(base != position)
  1081. {
  1082. restart = position;
  1083. // Have to decrement restart since it will get incremented again later:
  1084. --restart;
  1085. }
  1086. break;
  1087. case commit_prune:
  1088. break;
  1089. }
  1090. saved_state* pmp = m_backup_state;
  1091. --pmp;
  1092. if(pmp < m_stack_base)
  1093. {
  1094. extend_stack();
  1095. pmp = m_backup_state;
  1096. --pmp;
  1097. }
  1098. (void) new (pmp)saved_state(16);
  1099. m_backup_state = pmp;
  1100. pstate = pstate->next.p;
  1101. return true;
  1102. }
  1103. template <class BidiIterator, class Allocator, class traits>
  1104. bool perl_matcher<BidiIterator, Allocator, traits>::match_then()
  1105. {
  1106. // Just leave a mark that we need to skip to next alternative:
  1107. saved_state* pmp = m_backup_state;
  1108. --pmp;
  1109. if(pmp < m_stack_base)
  1110. {
  1111. extend_stack();
  1112. pmp = m_backup_state;
  1113. --pmp;
  1114. }
  1115. (void) new (pmp)saved_state(17);
  1116. m_backup_state = pmp;
  1117. pstate = pstate->next.p;
  1118. return true;
  1119. }
  1120. template <class BidiIterator, class Allocator, class traits>
  1121. bool perl_matcher<BidiIterator, Allocator, traits>::skip_until_paren(int index, bool have_match)
  1122. {
  1123. while(pstate)
  1124. {
  1125. if(pstate->type == syntax_element_endmark)
  1126. {
  1127. if(static_cast<const re_brace*>(pstate)->index == index)
  1128. {
  1129. if(have_match)
  1130. return this->match_endmark();
  1131. pstate = pstate->next.p;
  1132. return true;
  1133. }
  1134. else
  1135. {
  1136. // Unenclosed closing ), occurs when (*ACCEPT) is inside some other
  1137. // parenthesis which may or may not have other side effects associated with it.
  1138. const re_syntax_base* sp = pstate;
  1139. match_endmark();
  1140. if(!pstate)
  1141. {
  1142. unwind(true);
  1143. // unwind may leave pstate NULL if we've unwound a forward lookahead, in which
  1144. // case just move to the next state and keep looking...
  1145. if (!pstate)
  1146. pstate = sp->next.p;
  1147. }
  1148. }
  1149. continue;
  1150. }
  1151. else if(pstate->type == syntax_element_match)
  1152. return true;
  1153. else if(pstate->type == syntax_element_startmark)
  1154. {
  1155. int idx = static_cast<const re_brace*>(pstate)->index;
  1156. pstate = pstate->next.p;
  1157. skip_until_paren(idx, false);
  1158. continue;
  1159. }
  1160. pstate = pstate->next.p;
  1161. }
  1162. return true;
  1163. }
  1164. /****************************************************************************
  1165. Unwind and associated procedures follow, these perform what normal stack
  1166. unwinding does in the recursive implementation.
  1167. ****************************************************************************/
  1168. template <class BidiIterator, class Allocator, class traits>
  1169. bool perl_matcher<BidiIterator, Allocator, traits>::unwind(bool have_match)
  1170. {
  1171. static unwind_proc_type const s_unwind_table[19] =
  1172. {
  1173. &perl_matcher<BidiIterator, Allocator, traits>::unwind_end,
  1174. &perl_matcher<BidiIterator, Allocator, traits>::unwind_paren,
  1175. &perl_matcher<BidiIterator, Allocator, traits>::unwind_recursion_stopper,
  1176. &perl_matcher<BidiIterator, Allocator, traits>::unwind_assertion,
  1177. &perl_matcher<BidiIterator, Allocator, traits>::unwind_alt,
  1178. &perl_matcher<BidiIterator, Allocator, traits>::unwind_repeater_counter,
  1179. &perl_matcher<BidiIterator, Allocator, traits>::unwind_extra_block,
  1180. &perl_matcher<BidiIterator, Allocator, traits>::unwind_greedy_single_repeat,
  1181. &perl_matcher<BidiIterator, Allocator, traits>::unwind_slow_dot_repeat,
  1182. &perl_matcher<BidiIterator, Allocator, traits>::unwind_fast_dot_repeat,
  1183. &perl_matcher<BidiIterator, Allocator, traits>::unwind_char_repeat,
  1184. &perl_matcher<BidiIterator, Allocator, traits>::unwind_short_set_repeat,
  1185. &perl_matcher<BidiIterator, Allocator, traits>::unwind_long_set_repeat,
  1186. &perl_matcher<BidiIterator, Allocator, traits>::unwind_non_greedy_repeat,
  1187. &perl_matcher<BidiIterator, Allocator, traits>::unwind_recursion,
  1188. &perl_matcher<BidiIterator, Allocator, traits>::unwind_recursion_pop,
  1189. &perl_matcher<BidiIterator, Allocator, traits>::unwind_commit,
  1190. &perl_matcher<BidiIterator, Allocator, traits>::unwind_then,
  1191. &perl_matcher<BidiIterator, Allocator, traits>::unwind_case,
  1192. };
  1193. m_recursive_result = have_match;
  1194. m_unwound_lookahead = false;
  1195. m_unwound_alt = false;
  1196. unwind_proc_type unwinder;
  1197. bool cont;
  1198. //
  1199. // keep unwinding our stack until we have something to do:
  1200. //
  1201. do
  1202. {
  1203. unwinder = s_unwind_table[m_backup_state->state_id];
  1204. cont = (this->*unwinder)(m_recursive_result);
  1205. }while(cont);
  1206. //
  1207. // return true if we have more states to try:
  1208. //
  1209. return pstate ? true : false;
  1210. }
  1211. template <class BidiIterator, class Allocator, class traits>
  1212. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_end(bool)
  1213. {
  1214. pstate = 0; // nothing left to search
  1215. return false; // end of stack nothing more to search
  1216. }
  1217. template <class BidiIterator, class Allocator, class traits>
  1218. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_case(bool)
  1219. {
  1220. saved_change_case* pmp = static_cast<saved_change_case*>(m_backup_state);
  1221. icase = pmp->icase;
  1222. boost::BOOST_REGEX_DETAIL_NS::inplace_destroy(pmp++);
  1223. m_backup_state = pmp;
  1224. return true;
  1225. }
  1226. template <class BidiIterator, class Allocator, class traits>
  1227. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_paren(bool have_match)
  1228. {
  1229. saved_matched_paren<BidiIterator>* pmp = static_cast<saved_matched_paren<BidiIterator>*>(m_backup_state);
  1230. // restore previous values if no match was found:
  1231. if(!have_match)
  1232. {
  1233. m_presult->set_first(pmp->sub.first, pmp->index, pmp->index == 0);
  1234. m_presult->set_second(pmp->sub.second, pmp->index, pmp->sub.matched, pmp->index == 0);
  1235. }
  1236. #ifdef BOOST_REGEX_MATCH_EXTRA
  1237. //
  1238. // we have a match, push the capture information onto the stack:
  1239. //
  1240. else if(pmp->sub.matched && (match_extra & m_match_flags))
  1241. ((*m_presult)[pmp->index]).get_captures().push_back(pmp->sub);
  1242. #endif
  1243. // unwind stack:
  1244. m_backup_state = pmp+1;
  1245. boost::BOOST_REGEX_DETAIL_NS::inplace_destroy(pmp);
  1246. return true; // keep looking
  1247. }
  1248. template <class BidiIterator, class Allocator, class traits>
  1249. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_recursion_stopper(bool)
  1250. {
  1251. boost::BOOST_REGEX_DETAIL_NS::inplace_destroy(m_backup_state++);
  1252. pstate = 0; // nothing left to search
  1253. return false; // end of stack nothing more to search
  1254. }
  1255. template <class BidiIterator, class Allocator, class traits>
  1256. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_assertion(bool r)
  1257. {
  1258. saved_assertion<BidiIterator>* pmp = static_cast<saved_assertion<BidiIterator>*>(m_backup_state);
  1259. pstate = pmp->pstate;
  1260. position = pmp->position;
  1261. bool result = (r == pmp->positive);
  1262. m_recursive_result = pmp->positive ? r : !r;
  1263. boost::BOOST_REGEX_DETAIL_NS::inplace_destroy(pmp++);
  1264. m_backup_state = pmp;
  1265. m_unwound_lookahead = true;
  1266. return !result; // return false if the assertion was matched to stop search.
  1267. }
  1268. template <class BidiIterator, class Allocator, class traits>
  1269. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_alt(bool r)
  1270. {
  1271. saved_position<BidiIterator>* pmp = static_cast<saved_position<BidiIterator>*>(m_backup_state);
  1272. if(!r)
  1273. {
  1274. pstate = pmp->pstate;
  1275. position = pmp->position;
  1276. }
  1277. boost::BOOST_REGEX_DETAIL_NS::inplace_destroy(pmp++);
  1278. m_backup_state = pmp;
  1279. m_unwound_alt = !r;
  1280. return r;
  1281. }
  1282. template <class BidiIterator, class Allocator, class traits>
  1283. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_repeater_counter(bool)
  1284. {
  1285. ++used_block_count;
  1286. saved_repeater<BidiIterator>* pmp = static_cast<saved_repeater<BidiIterator>*>(m_backup_state);
  1287. boost::BOOST_REGEX_DETAIL_NS::inplace_destroy(pmp++);
  1288. m_backup_state = pmp;
  1289. return true; // keep looking
  1290. }
  1291. template <class BidiIterator, class Allocator, class traits>
  1292. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_extra_block(bool)
  1293. {
  1294. saved_extra_block* pmp = static_cast<saved_extra_block*>(m_backup_state);
  1295. void* condemmed = m_stack_base;
  1296. m_stack_base = pmp->base;
  1297. m_backup_state = pmp->end;
  1298. boost::BOOST_REGEX_DETAIL_NS::inplace_destroy(pmp);
  1299. put_mem_block(condemmed);
  1300. return true; // keep looking
  1301. }
  1302. template <class BidiIterator, class Allocator, class traits>
  1303. inline void perl_matcher<BidiIterator, Allocator, traits>::destroy_single_repeat()
  1304. {
  1305. saved_single_repeat<BidiIterator>* p = static_cast<saved_single_repeat<BidiIterator>*>(m_backup_state);
  1306. boost::BOOST_REGEX_DETAIL_NS::inplace_destroy(p++);
  1307. m_backup_state = p;
  1308. }
  1309. template <class BidiIterator, class Allocator, class traits>
  1310. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_greedy_single_repeat(bool r)
  1311. {
  1312. saved_single_repeat<BidiIterator>* pmp = static_cast<saved_single_repeat<BidiIterator>*>(m_backup_state);
  1313. // if we have a match, just discard this state:
  1314. if(r)
  1315. {
  1316. destroy_single_repeat();
  1317. return true;
  1318. }
  1319. const re_repeat* rep = pmp->rep;
  1320. std::size_t count = pmp->count;
  1321. BOOST_REGEX_ASSERT(rep->next.p != 0);
  1322. BOOST_REGEX_ASSERT(rep->alt.p != 0);
  1323. count -= rep->min;
  1324. if((m_match_flags & match_partial) && (position == last))
  1325. m_has_partial_match = true;
  1326. BOOST_REGEX_ASSERT(count);
  1327. position = pmp->last_position;
  1328. // backtrack till we can skip out:
  1329. do
  1330. {
  1331. --position;
  1332. --count;
  1333. ++state_count;
  1334. }while(count && !can_start(*position, rep->_map, mask_skip));
  1335. // if we've hit base, destroy this state:
  1336. if(count == 0)
  1337. {
  1338. destroy_single_repeat();
  1339. if(!can_start(*position, rep->_map, mask_skip))
  1340. return true;
  1341. }
  1342. else
  1343. {
  1344. pmp->count = count + rep->min;
  1345. pmp->last_position = position;
  1346. }
  1347. pstate = rep->alt.p;
  1348. return false;
  1349. }
  1350. template <class BidiIterator, class Allocator, class traits>
  1351. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_slow_dot_repeat(bool r)
  1352. {
  1353. saved_single_repeat<BidiIterator>* pmp = static_cast<saved_single_repeat<BidiIterator>*>(m_backup_state);
  1354. // if we have a match, just discard this state:
  1355. if(r)
  1356. {
  1357. destroy_single_repeat();
  1358. return true;
  1359. }
  1360. const re_repeat* rep = pmp->rep;
  1361. std::size_t count = pmp->count;
  1362. BOOST_REGEX_ASSERT(rep->type == syntax_element_dot_rep);
  1363. BOOST_REGEX_ASSERT(rep->next.p != 0);
  1364. BOOST_REGEX_ASSERT(rep->alt.p != 0);
  1365. BOOST_REGEX_ASSERT(rep->next.p->type == syntax_element_wild);
  1366. BOOST_REGEX_ASSERT(count < rep->max);
  1367. pstate = rep->next.p;
  1368. position = pmp->last_position;
  1369. if(position != last)
  1370. {
  1371. // wind forward until we can skip out of the repeat:
  1372. do
  1373. {
  1374. if(!match_wild())
  1375. {
  1376. // failed repeat match, discard this state and look for another:
  1377. destroy_single_repeat();
  1378. return true;
  1379. }
  1380. ++count;
  1381. ++state_count;
  1382. pstate = rep->next.p;
  1383. }while((count < rep->max) && (position != last) && !can_start(*position, rep->_map, mask_skip));
  1384. }
  1385. if(position == last)
  1386. {
  1387. // can't repeat any more, remove the pushed state:
  1388. destroy_single_repeat();
  1389. if((m_match_flags & match_partial) && (position == last) && (position != search_base))
  1390. m_has_partial_match = true;
  1391. if(0 == (rep->can_be_null & mask_skip))
  1392. return true;
  1393. }
  1394. else if(count == rep->max)
  1395. {
  1396. // can't repeat any more, remove the pushed state:
  1397. destroy_single_repeat();
  1398. if(!can_start(*position, rep->_map, mask_skip))
  1399. return true;
  1400. }
  1401. else
  1402. {
  1403. pmp->count = count;
  1404. pmp->last_position = position;
  1405. }
  1406. pstate = rep->alt.p;
  1407. return false;
  1408. }
  1409. template <class BidiIterator, class Allocator, class traits>
  1410. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_fast_dot_repeat(bool r)
  1411. {
  1412. saved_single_repeat<BidiIterator>* pmp = static_cast<saved_single_repeat<BidiIterator>*>(m_backup_state);
  1413. // if we have a match, just discard this state:
  1414. if(r)
  1415. {
  1416. destroy_single_repeat();
  1417. return true;
  1418. }
  1419. const re_repeat* rep = pmp->rep;
  1420. std::size_t count = pmp->count;
  1421. BOOST_REGEX_ASSERT(count < rep->max);
  1422. position = pmp->last_position;
  1423. if(position != last)
  1424. {
  1425. // wind forward until we can skip out of the repeat:
  1426. do
  1427. {
  1428. ++position;
  1429. ++count;
  1430. ++state_count;
  1431. }while((count < rep->max) && (position != last) && !can_start(*position, rep->_map, mask_skip));
  1432. }
  1433. // remember where we got to if this is a leading repeat:
  1434. if((rep->leading) && (count < rep->max))
  1435. restart = position;
  1436. if(position == last)
  1437. {
  1438. // can't repeat any more, remove the pushed state:
  1439. destroy_single_repeat();
  1440. if((m_match_flags & match_partial) && (position == last) && (position != search_base))
  1441. m_has_partial_match = true;
  1442. if(0 == (rep->can_be_null & mask_skip))
  1443. return true;
  1444. }
  1445. else if(count == rep->max)
  1446. {
  1447. // can't repeat any more, remove the pushed state:
  1448. destroy_single_repeat();
  1449. if(!can_start(*position, rep->_map, mask_skip))
  1450. return true;
  1451. }
  1452. else
  1453. {
  1454. pmp->count = count;
  1455. pmp->last_position = position;
  1456. }
  1457. pstate = rep->alt.p;
  1458. return false;
  1459. }
  1460. template <class BidiIterator, class Allocator, class traits>
  1461. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_char_repeat(bool r)
  1462. {
  1463. saved_single_repeat<BidiIterator>* pmp = static_cast<saved_single_repeat<BidiIterator>*>(m_backup_state);
  1464. // if we have a match, just discard this state:
  1465. if(r)
  1466. {
  1467. destroy_single_repeat();
  1468. return true;
  1469. }
  1470. const re_repeat* rep = pmp->rep;
  1471. std::size_t count = pmp->count;
  1472. pstate = rep->next.p;
  1473. const char_type what = *reinterpret_cast<const char_type*>(static_cast<const re_literal*>(pstate) + 1);
  1474. position = pmp->last_position;
  1475. BOOST_REGEX_ASSERT(rep->type == syntax_element_char_rep);
  1476. BOOST_REGEX_ASSERT(rep->next.p != 0);
  1477. BOOST_REGEX_ASSERT(rep->alt.p != 0);
  1478. BOOST_REGEX_ASSERT(rep->next.p->type == syntax_element_literal);
  1479. BOOST_REGEX_ASSERT(count < rep->max);
  1480. if(position != last)
  1481. {
  1482. // wind forward until we can skip out of the repeat:
  1483. do
  1484. {
  1485. if(traits_inst.translate(*position, icase) != what)
  1486. {
  1487. // failed repeat match, discard this state and look for another:
  1488. destroy_single_repeat();
  1489. return true;
  1490. }
  1491. ++count;
  1492. ++ position;
  1493. ++state_count;
  1494. pstate = rep->next.p;
  1495. }while((count < rep->max) && (position != last) && !can_start(*position, rep->_map, mask_skip));
  1496. }
  1497. // remember where we got to if this is a leading repeat:
  1498. if((rep->leading) && (count < rep->max))
  1499. restart = position;
  1500. if(position == last)
  1501. {
  1502. // can't repeat any more, remove the pushed state:
  1503. destroy_single_repeat();
  1504. if((m_match_flags & match_partial) && (position == last) && (position != search_base))
  1505. m_has_partial_match = true;
  1506. if(0 == (rep->can_be_null & mask_skip))
  1507. return true;
  1508. }
  1509. else if(count == rep->max)
  1510. {
  1511. // can't repeat any more, remove the pushed state:
  1512. destroy_single_repeat();
  1513. if(!can_start(*position, rep->_map, mask_skip))
  1514. return true;
  1515. }
  1516. else
  1517. {
  1518. pmp->count = count;
  1519. pmp->last_position = position;
  1520. }
  1521. pstate = rep->alt.p;
  1522. return false;
  1523. }
  1524. template <class BidiIterator, class Allocator, class traits>
  1525. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_short_set_repeat(bool r)
  1526. {
  1527. saved_single_repeat<BidiIterator>* pmp = static_cast<saved_single_repeat<BidiIterator>*>(m_backup_state);
  1528. // if we have a match, just discard this state:
  1529. if(r)
  1530. {
  1531. destroy_single_repeat();
  1532. return true;
  1533. }
  1534. const re_repeat* rep = pmp->rep;
  1535. std::size_t count = pmp->count;
  1536. pstate = rep->next.p;
  1537. const unsigned char* map = static_cast<const re_set*>(rep->next.p)->_map;
  1538. position = pmp->last_position;
  1539. BOOST_REGEX_ASSERT(rep->type == syntax_element_short_set_rep);
  1540. BOOST_REGEX_ASSERT(rep->next.p != 0);
  1541. BOOST_REGEX_ASSERT(rep->alt.p != 0);
  1542. BOOST_REGEX_ASSERT(rep->next.p->type == syntax_element_set);
  1543. BOOST_REGEX_ASSERT(count < rep->max);
  1544. if(position != last)
  1545. {
  1546. // wind forward until we can skip out of the repeat:
  1547. do
  1548. {
  1549. if(!map[static_cast<unsigned char>(traits_inst.translate(*position, icase))])
  1550. {
  1551. // failed repeat match, discard this state and look for another:
  1552. destroy_single_repeat();
  1553. return true;
  1554. }
  1555. ++count;
  1556. ++ position;
  1557. ++state_count;
  1558. pstate = rep->next.p;
  1559. }while((count < rep->max) && (position != last) && !can_start(*position, rep->_map, mask_skip));
  1560. }
  1561. // remember where we got to if this is a leading repeat:
  1562. if((rep->leading) && (count < rep->max))
  1563. restart = position;
  1564. if(position == last)
  1565. {
  1566. // can't repeat any more, remove the pushed state:
  1567. destroy_single_repeat();
  1568. if((m_match_flags & match_partial) && (position == last) && (position != search_base))
  1569. m_has_partial_match = true;
  1570. if(0 == (rep->can_be_null & mask_skip))
  1571. return true;
  1572. }
  1573. else if(count == rep->max)
  1574. {
  1575. // can't repeat any more, remove the pushed state:
  1576. destroy_single_repeat();
  1577. if(!can_start(*position, rep->_map, mask_skip))
  1578. return true;
  1579. }
  1580. else
  1581. {
  1582. pmp->count = count;
  1583. pmp->last_position = position;
  1584. }
  1585. pstate = rep->alt.p;
  1586. return false;
  1587. }
  1588. template <class BidiIterator, class Allocator, class traits>
  1589. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_long_set_repeat(bool r)
  1590. {
  1591. typedef typename traits::char_class_type m_type;
  1592. saved_single_repeat<BidiIterator>* pmp = static_cast<saved_single_repeat<BidiIterator>*>(m_backup_state);
  1593. // if we have a match, just discard this state:
  1594. if(r)
  1595. {
  1596. destroy_single_repeat();
  1597. return true;
  1598. }
  1599. const re_repeat* rep = pmp->rep;
  1600. std::size_t count = pmp->count;
  1601. pstate = rep->next.p;
  1602. const re_set_long<m_type>* set = static_cast<const re_set_long<m_type>*>(pstate);
  1603. position = pmp->last_position;
  1604. BOOST_REGEX_ASSERT(rep->type == syntax_element_long_set_rep);
  1605. BOOST_REGEX_ASSERT(rep->next.p != 0);
  1606. BOOST_REGEX_ASSERT(rep->alt.p != 0);
  1607. BOOST_REGEX_ASSERT(rep->next.p->type == syntax_element_long_set);
  1608. BOOST_REGEX_ASSERT(count < rep->max);
  1609. if(position != last)
  1610. {
  1611. // wind forward until we can skip out of the repeat:
  1612. do
  1613. {
  1614. if(position == re_is_set_member(position, last, set, re.get_data(), icase))
  1615. {
  1616. // failed repeat match, discard this state and look for another:
  1617. destroy_single_repeat();
  1618. return true;
  1619. }
  1620. ++position;
  1621. ++count;
  1622. ++state_count;
  1623. pstate = rep->next.p;
  1624. }while((count < rep->max) && (position != last) && !can_start(*position, rep->_map, mask_skip));
  1625. }
  1626. // remember where we got to if this is a leading repeat:
  1627. if((rep->leading) && (count < rep->max))
  1628. restart = position;
  1629. if(position == last)
  1630. {
  1631. // can't repeat any more, remove the pushed state:
  1632. destroy_single_repeat();
  1633. if((m_match_flags & match_partial) && (position == last) && (position != search_base))
  1634. m_has_partial_match = true;
  1635. if(0 == (rep->can_be_null & mask_skip))
  1636. return true;
  1637. }
  1638. else if(count == rep->max)
  1639. {
  1640. // can't repeat any more, remove the pushed state:
  1641. destroy_single_repeat();
  1642. if(!can_start(*position, rep->_map, mask_skip))
  1643. return true;
  1644. }
  1645. else
  1646. {
  1647. pmp->count = count;
  1648. pmp->last_position = position;
  1649. }
  1650. pstate = rep->alt.p;
  1651. return false;
  1652. }
  1653. template <class BidiIterator, class Allocator, class traits>
  1654. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_non_greedy_repeat(bool r)
  1655. {
  1656. saved_position<BidiIterator>* pmp = static_cast<saved_position<BidiIterator>*>(m_backup_state);
  1657. if(!r)
  1658. {
  1659. position = pmp->position;
  1660. pstate = pmp->pstate;
  1661. ++(*next_count);
  1662. }
  1663. boost::BOOST_REGEX_DETAIL_NS::inplace_destroy(pmp++);
  1664. m_backup_state = pmp;
  1665. return r;
  1666. }
  1667. template <class BidiIterator, class Allocator, class traits>
  1668. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_recursion(bool r)
  1669. {
  1670. // We are backtracking back inside a recursion, need to push the info
  1671. // back onto the recursion stack, and do so unconditionally, otherwise
  1672. // we can get mismatched pushes and pops...
  1673. saved_recursion<results_type>* pmp = static_cast<saved_recursion<results_type>*>(m_backup_state);
  1674. if (!r)
  1675. {
  1676. recursion_stack.push_back(recursion_info<results_type>());
  1677. recursion_stack.back().idx = pmp->recursion_id;
  1678. recursion_stack.back().preturn_address = pmp->preturn_address;
  1679. recursion_stack.back().results = pmp->prior_results;
  1680. recursion_stack.back().location_of_start = position;
  1681. *m_presult = pmp->internal_results;
  1682. }
  1683. boost::BOOST_REGEX_DETAIL_NS::inplace_destroy(pmp++);
  1684. m_backup_state = pmp;
  1685. return true;
  1686. }
  1687. template <class BidiIterator, class Allocator, class traits>
  1688. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_recursion_pop(bool r)
  1689. {
  1690. // Backtracking out of a recursion, we must pop state off the recursion
  1691. // stack unconditionally to ensure matched pushes and pops:
  1692. saved_state* pmp = static_cast<saved_state*>(m_backup_state);
  1693. if (!r && !recursion_stack.empty())
  1694. {
  1695. *m_presult = recursion_stack.back().results;
  1696. position = recursion_stack.back().location_of_start;
  1697. recursion_stack.pop_back();
  1698. }
  1699. boost::BOOST_REGEX_DETAIL_NS::inplace_destroy(pmp++);
  1700. m_backup_state = pmp;
  1701. return true;
  1702. }
  1703. template <class BidiIterator, class Allocator, class traits>
  1704. void perl_matcher<BidiIterator, Allocator, traits>::push_recursion_pop()
  1705. {
  1706. saved_state* pmp = static_cast<saved_state*>(m_backup_state);
  1707. --pmp;
  1708. if(pmp < m_stack_base)
  1709. {
  1710. extend_stack();
  1711. pmp = static_cast<saved_state*>(m_backup_state);
  1712. --pmp;
  1713. }
  1714. (void) new (pmp)saved_state(15);
  1715. m_backup_state = pmp;
  1716. }
  1717. template <class BidiIterator, class Allocator, class traits>
  1718. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_commit(bool b)
  1719. {
  1720. boost::BOOST_REGEX_DETAIL_NS::inplace_destroy(m_backup_state++);
  1721. while(unwind(b) && !m_unwound_lookahead){}
  1722. if(m_unwound_lookahead && pstate)
  1723. {
  1724. //
  1725. // If we stop because we just unwound an assertion, put the
  1726. // commit state back on the stack again:
  1727. //
  1728. m_unwound_lookahead = false;
  1729. saved_state* pmp = m_backup_state;
  1730. --pmp;
  1731. if(pmp < m_stack_base)
  1732. {
  1733. extend_stack();
  1734. pmp = m_backup_state;
  1735. --pmp;
  1736. }
  1737. (void) new (pmp)saved_state(16);
  1738. m_backup_state = pmp;
  1739. }
  1740. // This prevents us from stopping when we exit from an independent sub-expression:
  1741. m_independent = false;
  1742. return false;
  1743. }
  1744. template <class BidiIterator, class Allocator, class traits>
  1745. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_then(bool b)
  1746. {
  1747. // Unwind everything till we hit an alternative:
  1748. boost::BOOST_REGEX_DETAIL_NS::inplace_destroy(m_backup_state++);
  1749. bool result = false;
  1750. while((result = unwind(b)) && !m_unwound_alt){}
  1751. // We're now pointing at the next alternative, need one more backtrack
  1752. // since *all* the other alternatives must fail once we've reached a THEN clause:
  1753. if(result && m_unwound_alt)
  1754. unwind(b);
  1755. return false;
  1756. }
  1757. /*
  1758. template <class BidiIterator, class Allocator, class traits>
  1759. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_parenthesis_pop(bool r)
  1760. {
  1761. saved_state* pmp = static_cast<saved_state*>(m_backup_state);
  1762. if(!r)
  1763. {
  1764. --parenthesis_stack_position;
  1765. }
  1766. boost::BOOST_REGEX_DETAIL_NS::inplace_destroy(pmp++);
  1767. m_backup_state = pmp;
  1768. return true;
  1769. }
  1770. template <class BidiIterator, class Allocator, class traits>
  1771. void perl_matcher<BidiIterator, Allocator, traits>::push_parenthesis_pop()
  1772. {
  1773. saved_state* pmp = static_cast<saved_state*>(m_backup_state);
  1774. --pmp;
  1775. if(pmp < m_stack_base)
  1776. {
  1777. extend_stack();
  1778. pmp = static_cast<saved_state*>(m_backup_state);
  1779. --pmp;
  1780. }
  1781. (void) new (pmp)saved_state(16);
  1782. m_backup_state = pmp;
  1783. }
  1784. template <class BidiIterator, class Allocator, class traits>
  1785. bool perl_matcher<BidiIterator, Allocator, traits>::unwind_parenthesis_push(bool r)
  1786. {
  1787. saved_position<BidiIterator>* pmp = static_cast<saved_position<BidiIterator>*>(m_backup_state);
  1788. if(!r)
  1789. {
  1790. parenthesis_stack[parenthesis_stack_position++] = pmp->position;
  1791. }
  1792. boost::BOOST_REGEX_DETAIL_NS::inplace_destroy(pmp++);
  1793. m_backup_state = pmp;
  1794. return true;
  1795. }
  1796. template <class BidiIterator, class Allocator, class traits>
  1797. inline void perl_matcher<BidiIterator, Allocator, traits>::push_parenthesis_push(BidiIterator p)
  1798. {
  1799. saved_position<BidiIterator>* pmp = static_cast<saved_position<BidiIterator>*>(m_backup_state);
  1800. --pmp;
  1801. if(pmp < m_stack_base)
  1802. {
  1803. extend_stack();
  1804. pmp = static_cast<saved_position<BidiIterator>*>(m_backup_state);
  1805. --pmp;
  1806. }
  1807. (void) new (pmp)saved_position<BidiIterator>(0, p, 17);
  1808. m_backup_state = pmp;
  1809. }
  1810. */
  1811. } // namespace BOOST_REGEX_DETAIL_NS
  1812. } // namespace boost
  1813. #ifdef BOOST_MSVC
  1814. # pragma warning(pop)
  1815. #endif
  1816. #ifdef BOOST_MSVC
  1817. #pragma warning(push)
  1818. #pragma warning(disable: 4103)
  1819. #endif
  1820. #ifdef BOOST_HAS_ABI_HEADERS
  1821. # include BOOST_ABI_SUFFIX
  1822. #endif
  1823. #ifdef BOOST_MSVC
  1824. #pragma warning(pop)
  1825. #endif
  1826. #endif