perl_matcher_non_recursive.hpp 62 KB

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