perl_matcher_non_recursive.hpp 60 KB

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