perl_matcher_non_recursive.hpp 62 KB

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