perl_matcher_non_recursive.hpp 60 KB

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