basic_regex_parser.hpp 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164
  1. /*
  2. *
  3. * Copyright (c) 2004
  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 basic_regex_parser.cpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: Declares template class basic_regex_parser.
  16. */
  17. #ifndef BOOST_REGEX_V4_BASIC_REGEX_PARSER_HPP
  18. #define BOOST_REGEX_V4_BASIC_REGEX_PARSER_HPP
  19. #ifdef BOOST_MSVC
  20. #pragma warning(push)
  21. #pragma warning(disable: 4103)
  22. #if BOOST_MSVC >= 1800
  23. #pragma warning(disable: 26812)
  24. #endif
  25. #endif
  26. #ifdef BOOST_HAS_ABI_HEADERS
  27. # include BOOST_ABI_PREFIX
  28. #endif
  29. #ifdef BOOST_MSVC
  30. #pragma warning(pop)
  31. #endif
  32. namespace boost{
  33. namespace BOOST_REGEX_DETAIL_NS{
  34. #ifdef BOOST_MSVC
  35. #pragma warning(push)
  36. #pragma warning(disable:4244)
  37. #if BOOST_MSVC < 1910
  38. #pragma warning(disable:4800)
  39. #endif
  40. #endif
  41. inline boost::intmax_t umax(mpl::false_ const&)
  42. {
  43. // Get out clause here, just in case numeric_limits is unspecialized:
  44. return std::numeric_limits<boost::intmax_t>::is_specialized ? (std::numeric_limits<boost::intmax_t>::max)() : INT_MAX;
  45. }
  46. inline boost::intmax_t umax(mpl::true_ const&)
  47. {
  48. return (std::numeric_limits<std::size_t>::max)();
  49. }
  50. inline boost::intmax_t umax()
  51. {
  52. return umax(mpl::bool_<std::numeric_limits<boost::intmax_t>::digits >= std::numeric_limits<std::size_t>::digits>());
  53. }
  54. template <class charT, class traits>
  55. class basic_regex_parser : public basic_regex_creator<charT, traits>
  56. {
  57. public:
  58. basic_regex_parser(regex_data<charT, traits>* data);
  59. void parse(const charT* p1, const charT* p2, unsigned flags);
  60. void fail(regex_constants::error_type error_code, std::ptrdiff_t position);
  61. void fail(regex_constants::error_type error_code, std::ptrdiff_t position, std::string message, std::ptrdiff_t start_pos);
  62. void fail(regex_constants::error_type error_code, std::ptrdiff_t position, const std::string& message)
  63. {
  64. fail(error_code, position, message, position);
  65. }
  66. bool parse_all();
  67. bool parse_basic();
  68. bool parse_extended();
  69. bool parse_literal();
  70. bool parse_open_paren();
  71. bool parse_basic_escape();
  72. bool parse_extended_escape();
  73. bool parse_match_any();
  74. bool parse_repeat(std::size_t low = 0, std::size_t high = (std::numeric_limits<std::size_t>::max)());
  75. bool parse_repeat_range(bool isbasic);
  76. bool parse_alt();
  77. bool parse_set();
  78. bool parse_backref();
  79. void parse_set_literal(basic_char_set<charT, traits>& char_set);
  80. bool parse_inner_set(basic_char_set<charT, traits>& char_set);
  81. bool parse_QE();
  82. bool parse_perl_extension();
  83. bool parse_perl_verb();
  84. bool match_verb(const char*);
  85. bool add_emacs_code(bool negate);
  86. bool unwind_alts(std::ptrdiff_t last_paren_start);
  87. digraph<charT> get_next_set_literal(basic_char_set<charT, traits>& char_set);
  88. charT unescape_character();
  89. regex_constants::syntax_option_type parse_options();
  90. private:
  91. typedef bool (basic_regex_parser::*parser_proc_type)();
  92. typedef typename traits::string_type string_type;
  93. typedef typename traits::char_class_type char_class_type;
  94. parser_proc_type m_parser_proc; // the main parser to use
  95. const charT* m_base; // the start of the string being parsed
  96. const charT* m_end; // the end of the string being parsed
  97. const charT* m_position; // our current parser position
  98. unsigned m_mark_count; // how many sub-expressions we have
  99. int m_mark_reset; // used to indicate that we're inside a (?|...) block.
  100. unsigned m_max_mark; // largest mark count seen inside a (?|...) block.
  101. std::ptrdiff_t m_paren_start; // where the last seen ')' began (where repeats are inserted).
  102. std::ptrdiff_t m_alt_insert_point; // where to insert the next alternative
  103. bool m_has_case_change; // true if somewhere in the current block the case has changed
  104. unsigned m_recursion_count; // How many times we've called parse_all.
  105. #if defined(BOOST_MSVC) && defined(_M_IX86)
  106. // This is an ugly warning suppression workaround (for warnings *inside* std::vector
  107. // that can not otherwise be suppressed)...
  108. BOOST_STATIC_ASSERT(sizeof(long) >= sizeof(void*));
  109. std::vector<long> m_alt_jumps; // list of alternative in the current scope.
  110. #else
  111. std::vector<std::ptrdiff_t> m_alt_jumps; // list of alternative in the current scope.
  112. #endif
  113. basic_regex_parser& operator=(const basic_regex_parser&);
  114. basic_regex_parser(const basic_regex_parser&);
  115. };
  116. template <class charT, class traits>
  117. basic_regex_parser<charT, traits>::basic_regex_parser(regex_data<charT, traits>* data)
  118. : basic_regex_creator<charT, traits>(data), m_parser_proc(), m_base(0), m_end(0), m_position(0),
  119. m_mark_count(0), m_mark_reset(-1), m_max_mark(0), m_paren_start(0), m_alt_insert_point(0), m_has_case_change(false), m_recursion_count(0)
  120. {
  121. }
  122. template <class charT, class traits>
  123. void basic_regex_parser<charT, traits>::parse(const charT* p1, const charT* p2, unsigned l_flags)
  124. {
  125. // pass l_flags on to base class:
  126. this->init(l_flags);
  127. // set up pointers:
  128. m_position = m_base = p1;
  129. m_end = p2;
  130. // empty strings are errors:
  131. if((p1 == p2) &&
  132. (
  133. ((l_flags & regbase::main_option_type) != regbase::perl_syntax_group)
  134. || (l_flags & regbase::no_empty_expressions)
  135. )
  136. )
  137. {
  138. fail(regex_constants::error_empty, 0);
  139. return;
  140. }
  141. // select which parser to use:
  142. switch(l_flags & regbase::main_option_type)
  143. {
  144. case regbase::perl_syntax_group:
  145. {
  146. m_parser_proc = &basic_regex_parser<charT, traits>::parse_extended;
  147. //
  148. // Add a leading paren with index zero to give recursions a target:
  149. //
  150. re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_startmark, sizeof(re_brace)));
  151. br->index = 0;
  152. br->icase = this->flags() & regbase::icase;
  153. break;
  154. }
  155. case regbase::basic_syntax_group:
  156. m_parser_proc = &basic_regex_parser<charT, traits>::parse_basic;
  157. break;
  158. case regbase::literal:
  159. m_parser_proc = &basic_regex_parser<charT, traits>::parse_literal;
  160. break;
  161. default:
  162. // Oops, someone has managed to set more than one of the main option flags,
  163. // so this must be an error:
  164. fail(regex_constants::error_unknown, 0, "An invalid combination of regular expression syntax flags was used.");
  165. return;
  166. }
  167. // parse all our characters:
  168. bool result = parse_all();
  169. //
  170. // Unwind our alternatives:
  171. //
  172. unwind_alts(-1);
  173. // reset l_flags as a global scope (?imsx) may have altered them:
  174. this->flags(l_flags);
  175. // if we haven't gobbled up all the characters then we must
  176. // have had an unexpected ')' :
  177. if(!result)
  178. {
  179. fail(regex_constants::error_paren, ::boost::BOOST_REGEX_DETAIL_NS::distance(m_base, m_position), "Found a closing ) with no corresponding opening parenthesis.");
  180. return;
  181. }
  182. // if an error has been set then give up now:
  183. if(this->m_pdata->m_status)
  184. return;
  185. // fill in our sub-expression count:
  186. this->m_pdata->m_mark_count = 1u + (std::size_t)m_mark_count;
  187. this->finalize(p1, p2);
  188. }
  189. template <class charT, class traits>
  190. void basic_regex_parser<charT, traits>::fail(regex_constants::error_type error_code, std::ptrdiff_t position)
  191. {
  192. // get the error message:
  193. std::string message = this->m_pdata->m_ptraits->error_string(error_code);
  194. fail(error_code, position, message);
  195. }
  196. template <class charT, class traits>
  197. void basic_regex_parser<charT, traits>::fail(regex_constants::error_type error_code, std::ptrdiff_t position, std::string message, std::ptrdiff_t start_pos)
  198. {
  199. if(0 == this->m_pdata->m_status) // update the error code if not already set
  200. this->m_pdata->m_status = error_code;
  201. m_position = m_end; // don't bother parsing anything else
  202. #ifndef BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS
  203. //
  204. // Augment error message with the regular expression text:
  205. //
  206. if(start_pos == position)
  207. start_pos = (std::max)(static_cast<std::ptrdiff_t>(0), position - static_cast<std::ptrdiff_t>(10));
  208. std::ptrdiff_t end_pos = (std::min)(position + static_cast<std::ptrdiff_t>(10), static_cast<std::ptrdiff_t>(m_end - m_base));
  209. if(error_code != regex_constants::error_empty)
  210. {
  211. if((start_pos != 0) || (end_pos != (m_end - m_base)))
  212. message += " The error occurred while parsing the regular expression fragment: '";
  213. else
  214. message += " The error occurred while parsing the regular expression: '";
  215. if(start_pos != end_pos)
  216. {
  217. message += std::string(m_base + start_pos, m_base + position);
  218. message += ">>>HERE>>>";
  219. message += std::string(m_base + position, m_base + end_pos);
  220. }
  221. message += "'.";
  222. }
  223. #endif
  224. #ifndef BOOST_NO_EXCEPTIONS
  225. if(0 == (this->flags() & regex_constants::no_except))
  226. {
  227. boost::regex_error e(message, error_code, position);
  228. e.raise();
  229. }
  230. #else
  231. (void)position; // suppress warnings.
  232. #endif
  233. }
  234. template <class charT, class traits>
  235. bool basic_regex_parser<charT, traits>::parse_all()
  236. {
  237. if (++m_recursion_count > 400)
  238. {
  239. // exceeded internal limits
  240. fail(boost::regex_constants::error_complexity, m_position - m_base, "Exceeded nested brace limit.");
  241. }
  242. bool result = true;
  243. while(result && (m_position != m_end))
  244. {
  245. result = (this->*m_parser_proc)();
  246. }
  247. --m_recursion_count;
  248. return result;
  249. }
  250. #ifdef BOOST_MSVC
  251. #pragma warning(push)
  252. #pragma warning(disable:4702)
  253. #endif
  254. template <class charT, class traits>
  255. bool basic_regex_parser<charT, traits>::parse_basic()
  256. {
  257. switch(this->m_traits.syntax_type(*m_position))
  258. {
  259. case regex_constants::syntax_escape:
  260. return parse_basic_escape();
  261. case regex_constants::syntax_dot:
  262. return parse_match_any();
  263. case regex_constants::syntax_caret:
  264. ++m_position;
  265. this->append_state(syntax_element_start_line);
  266. break;
  267. case regex_constants::syntax_dollar:
  268. ++m_position;
  269. this->append_state(syntax_element_end_line);
  270. break;
  271. case regex_constants::syntax_star:
  272. if(!(this->m_last_state) || (this->m_last_state->type == syntax_element_start_line))
  273. return parse_literal();
  274. else
  275. {
  276. ++m_position;
  277. return parse_repeat();
  278. }
  279. case regex_constants::syntax_plus:
  280. if(!(this->m_last_state) || (this->m_last_state->type == syntax_element_start_line) || !(this->flags() & regbase::emacs_ex))
  281. return parse_literal();
  282. else
  283. {
  284. ++m_position;
  285. return parse_repeat(1);
  286. }
  287. case regex_constants::syntax_question:
  288. if(!(this->m_last_state) || (this->m_last_state->type == syntax_element_start_line) || !(this->flags() & regbase::emacs_ex))
  289. return parse_literal();
  290. else
  291. {
  292. ++m_position;
  293. return parse_repeat(0, 1);
  294. }
  295. case regex_constants::syntax_open_set:
  296. return parse_set();
  297. case regex_constants::syntax_newline:
  298. if(this->flags() & regbase::newline_alt)
  299. return parse_alt();
  300. else
  301. return parse_literal();
  302. default:
  303. return parse_literal();
  304. }
  305. return true;
  306. }
  307. #ifdef BOOST_MSVC
  308. # pragma warning(push)
  309. #if BOOST_MSVC >= 1800
  310. #pragma warning(disable:26812)
  311. #endif
  312. #endif
  313. template <class charT, class traits>
  314. bool basic_regex_parser<charT, traits>::parse_extended()
  315. {
  316. bool result = true;
  317. switch(this->m_traits.syntax_type(*m_position))
  318. {
  319. case regex_constants::syntax_open_mark:
  320. return parse_open_paren();
  321. case regex_constants::syntax_close_mark:
  322. return false;
  323. case regex_constants::syntax_escape:
  324. return parse_extended_escape();
  325. case regex_constants::syntax_dot:
  326. return parse_match_any();
  327. case regex_constants::syntax_caret:
  328. ++m_position;
  329. this->append_state(
  330. (this->flags() & regex_constants::no_mod_m ? syntax_element_buffer_start : syntax_element_start_line));
  331. break;
  332. case regex_constants::syntax_dollar:
  333. ++m_position;
  334. this->append_state(
  335. (this->flags() & regex_constants::no_mod_m ? syntax_element_buffer_end : syntax_element_end_line));
  336. break;
  337. case regex_constants::syntax_star:
  338. if(m_position == this->m_base)
  339. {
  340. fail(regex_constants::error_badrepeat, 0, "The repeat operator \"*\" cannot start a regular expression.");
  341. return false;
  342. }
  343. ++m_position;
  344. return parse_repeat();
  345. case regex_constants::syntax_question:
  346. if(m_position == this->m_base)
  347. {
  348. fail(regex_constants::error_badrepeat, 0, "The repeat operator \"?\" cannot start a regular expression.");
  349. return false;
  350. }
  351. ++m_position;
  352. return parse_repeat(0,1);
  353. case regex_constants::syntax_plus:
  354. if(m_position == this->m_base)
  355. {
  356. fail(regex_constants::error_badrepeat, 0, "The repeat operator \"+\" cannot start a regular expression.");
  357. return false;
  358. }
  359. ++m_position;
  360. return parse_repeat(1);
  361. case regex_constants::syntax_open_brace:
  362. ++m_position;
  363. return parse_repeat_range(false);
  364. case regex_constants::syntax_close_brace:
  365. if((this->flags() & regbase::no_perl_ex) == regbase::no_perl_ex)
  366. {
  367. fail(regex_constants::error_brace, this->m_position - this->m_base, "Found a closing repetition operator } with no corresponding {.");
  368. return false;
  369. }
  370. result = parse_literal();
  371. break;
  372. case regex_constants::syntax_or:
  373. return parse_alt();
  374. case regex_constants::syntax_open_set:
  375. return parse_set();
  376. case regex_constants::syntax_newline:
  377. if(this->flags() & regbase::newline_alt)
  378. return parse_alt();
  379. else
  380. return parse_literal();
  381. case regex_constants::syntax_hash:
  382. //
  383. // If we have a mod_x flag set, then skip until
  384. // we get to a newline character:
  385. //
  386. if((this->flags()
  387. & (regbase::no_perl_ex|regbase::mod_x))
  388. == regbase::mod_x)
  389. {
  390. while((m_position != m_end) && !is_separator(*m_position++)){}
  391. return true;
  392. }
  393. BOOST_FALLTHROUGH;
  394. default:
  395. result = parse_literal();
  396. break;
  397. }
  398. return result;
  399. }
  400. #ifdef BOOST_MSVC
  401. # pragma warning(pop)
  402. #endif
  403. #ifdef BOOST_MSVC
  404. #pragma warning(pop)
  405. #endif
  406. template <class charT, class traits>
  407. bool basic_regex_parser<charT, traits>::parse_literal()
  408. {
  409. // append this as a literal provided it's not a space character
  410. // or the perl option regbase::mod_x is not set:
  411. if(
  412. ((this->flags()
  413. & (regbase::main_option_type|regbase::mod_x|regbase::no_perl_ex))
  414. != regbase::mod_x)
  415. || !this->m_traits.isctype(*m_position, this->m_mask_space))
  416. this->append_literal(*m_position);
  417. ++m_position;
  418. return true;
  419. }
  420. template <class charT, class traits>
  421. bool basic_regex_parser<charT, traits>::parse_open_paren()
  422. {
  423. //
  424. // skip the '(' and error check:
  425. //
  426. if(++m_position == m_end)
  427. {
  428. fail(regex_constants::error_paren, m_position - m_base);
  429. return false;
  430. }
  431. //
  432. // begin by checking for a perl-style (?...) extension:
  433. //
  434. if(
  435. ((this->flags() & (regbase::main_option_type | regbase::no_perl_ex)) == 0)
  436. || ((this->flags() & (regbase::main_option_type | regbase::emacs_ex)) == (regbase::basic_syntax_group|regbase::emacs_ex))
  437. )
  438. {
  439. if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_question)
  440. return parse_perl_extension();
  441. if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_star)
  442. return parse_perl_verb();
  443. }
  444. //
  445. // update our mark count, and append the required state:
  446. //
  447. unsigned markid = 0;
  448. if(0 == (this->flags() & regbase::nosubs))
  449. {
  450. markid = ++m_mark_count;
  451. #ifndef BOOST_NO_STD_DISTANCE
  452. if(this->flags() & regbase::save_subexpression_location)
  453. this->m_pdata->m_subs.push_back(std::pair<std::size_t, std::size_t>(std::distance(m_base, m_position) - 1, 0));
  454. #else
  455. if(this->flags() & regbase::save_subexpression_location)
  456. this->m_pdata->m_subs.push_back(std::pair<std::size_t, std::size_t>((m_position - m_base) - 1, 0));
  457. #endif
  458. }
  459. re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_startmark, sizeof(re_brace)));
  460. pb->index = markid;
  461. pb->icase = this->flags() & regbase::icase;
  462. std::ptrdiff_t last_paren_start = this->getoffset(pb);
  463. // back up insertion point for alternations, and set new point:
  464. std::ptrdiff_t last_alt_point = m_alt_insert_point;
  465. this->m_pdata->m_data.align();
  466. m_alt_insert_point = this->m_pdata->m_data.size();
  467. //
  468. // back up the current flags in case we have a nested (?imsx) group:
  469. //
  470. regex_constants::syntax_option_type opts = this->flags();
  471. bool old_case_change = m_has_case_change;
  472. m_has_case_change = false; // no changes to this scope as yet...
  473. //
  474. // Back up branch reset data in case we have a nested (?|...)
  475. //
  476. int mark_reset = m_mark_reset;
  477. m_mark_reset = -1;
  478. //
  479. // now recursively add more states, this will terminate when we get to a
  480. // matching ')' :
  481. //
  482. parse_all();
  483. //
  484. // Unwind pushed alternatives:
  485. //
  486. if(0 == unwind_alts(last_paren_start))
  487. return false;
  488. //
  489. // restore flags:
  490. //
  491. if(m_has_case_change)
  492. {
  493. // the case has changed in one or more of the alternatives
  494. // within the scoped (...) block: we have to add a state
  495. // to reset the case sensitivity:
  496. static_cast<re_case*>(
  497. this->append_state(syntax_element_toggle_case, sizeof(re_case))
  498. )->icase = opts & regbase::icase;
  499. }
  500. this->flags(opts);
  501. m_has_case_change = old_case_change;
  502. //
  503. // restore branch reset:
  504. //
  505. m_mark_reset = mark_reset;
  506. //
  507. // we either have a ')' or we have run out of characters prematurely:
  508. //
  509. if(m_position == m_end)
  510. {
  511. this->fail(regex_constants::error_paren, ::boost::BOOST_REGEX_DETAIL_NS::distance(m_base, m_end));
  512. return false;
  513. }
  514. if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
  515. return false;
  516. #ifndef BOOST_NO_STD_DISTANCE
  517. if(markid && (this->flags() & regbase::save_subexpression_location))
  518. this->m_pdata->m_subs.at(markid - 1).second = std::distance(m_base, m_position);
  519. #else
  520. if(markid && (this->flags() & regbase::save_subexpression_location))
  521. this->m_pdata->m_subs.at(markid - 1).second = (m_position - m_base);
  522. #endif
  523. ++m_position;
  524. //
  525. // append closing parenthesis state:
  526. //
  527. pb = static_cast<re_brace*>(this->append_state(syntax_element_endmark, sizeof(re_brace)));
  528. pb->index = markid;
  529. pb->icase = this->flags() & regbase::icase;
  530. this->m_paren_start = last_paren_start;
  531. //
  532. // restore the alternate insertion point:
  533. //
  534. this->m_alt_insert_point = last_alt_point;
  535. //
  536. // allow backrefs to this mark:
  537. //
  538. if(markid > 0)
  539. this->m_backrefs.set(markid);
  540. return true;
  541. }
  542. template <class charT, class traits>
  543. bool basic_regex_parser<charT, traits>::parse_basic_escape()
  544. {
  545. if(++m_position == m_end)
  546. {
  547. fail(regex_constants::error_paren, m_position - m_base);
  548. return false;
  549. }
  550. bool result = true;
  551. switch(this->m_traits.escape_syntax_type(*m_position))
  552. {
  553. case regex_constants::syntax_open_mark:
  554. return parse_open_paren();
  555. case regex_constants::syntax_close_mark:
  556. return false;
  557. case regex_constants::syntax_plus:
  558. if(this->flags() & regex_constants::bk_plus_qm)
  559. {
  560. ++m_position;
  561. return parse_repeat(1);
  562. }
  563. else
  564. return parse_literal();
  565. case regex_constants::syntax_question:
  566. if(this->flags() & regex_constants::bk_plus_qm)
  567. {
  568. ++m_position;
  569. return parse_repeat(0, 1);
  570. }
  571. else
  572. return parse_literal();
  573. case regex_constants::syntax_open_brace:
  574. if(this->flags() & regbase::no_intervals)
  575. return parse_literal();
  576. ++m_position;
  577. return parse_repeat_range(true);
  578. case regex_constants::syntax_close_brace:
  579. if(this->flags() & regbase::no_intervals)
  580. return parse_literal();
  581. fail(regex_constants::error_brace, this->m_position - this->m_base, "Found a closing repetition operator } with no corresponding {.");
  582. return false;
  583. case regex_constants::syntax_or:
  584. if(this->flags() & regbase::bk_vbar)
  585. return parse_alt();
  586. else
  587. result = parse_literal();
  588. break;
  589. case regex_constants::syntax_digit:
  590. return parse_backref();
  591. case regex_constants::escape_type_start_buffer:
  592. if(this->flags() & regbase::emacs_ex)
  593. {
  594. ++m_position;
  595. this->append_state(syntax_element_buffer_start);
  596. }
  597. else
  598. result = parse_literal();
  599. break;
  600. case regex_constants::escape_type_end_buffer:
  601. if(this->flags() & regbase::emacs_ex)
  602. {
  603. ++m_position;
  604. this->append_state(syntax_element_buffer_end);
  605. }
  606. else
  607. result = parse_literal();
  608. break;
  609. case regex_constants::escape_type_word_assert:
  610. if(this->flags() & regbase::emacs_ex)
  611. {
  612. ++m_position;
  613. this->append_state(syntax_element_word_boundary);
  614. }
  615. else
  616. result = parse_literal();
  617. break;
  618. case regex_constants::escape_type_not_word_assert:
  619. if(this->flags() & regbase::emacs_ex)
  620. {
  621. ++m_position;
  622. this->append_state(syntax_element_within_word);
  623. }
  624. else
  625. result = parse_literal();
  626. break;
  627. case regex_constants::escape_type_left_word:
  628. if(this->flags() & regbase::emacs_ex)
  629. {
  630. ++m_position;
  631. this->append_state(syntax_element_word_start);
  632. }
  633. else
  634. result = parse_literal();
  635. break;
  636. case regex_constants::escape_type_right_word:
  637. if(this->flags() & regbase::emacs_ex)
  638. {
  639. ++m_position;
  640. this->append_state(syntax_element_word_end);
  641. }
  642. else
  643. result = parse_literal();
  644. break;
  645. default:
  646. if(this->flags() & regbase::emacs_ex)
  647. {
  648. bool negate = true;
  649. switch(*m_position)
  650. {
  651. case 'w':
  652. negate = false;
  653. BOOST_FALLTHROUGH;
  654. case 'W':
  655. {
  656. basic_char_set<charT, traits> char_set;
  657. if(negate)
  658. char_set.negate();
  659. char_set.add_class(this->m_word_mask);
  660. if(0 == this->append_set(char_set))
  661. {
  662. fail(regex_constants::error_ctype, m_position - m_base);
  663. return false;
  664. }
  665. ++m_position;
  666. return true;
  667. }
  668. case 's':
  669. negate = false;
  670. BOOST_FALLTHROUGH;
  671. case 'S':
  672. return add_emacs_code(negate);
  673. case 'c':
  674. case 'C':
  675. // not supported yet:
  676. fail(regex_constants::error_escape, m_position - m_base, "The \\c and \\C escape sequences are not supported by POSIX basic regular expressions: try the Perl syntax instead.");
  677. return false;
  678. default:
  679. break;
  680. }
  681. }
  682. result = parse_literal();
  683. break;
  684. }
  685. return result;
  686. }
  687. template <class charT, class traits>
  688. bool basic_regex_parser<charT, traits>::parse_extended_escape()
  689. {
  690. ++m_position;
  691. if(m_position == m_end)
  692. {
  693. fail(regex_constants::error_escape, m_position - m_base, "Incomplete escape sequence found.");
  694. return false;
  695. }
  696. bool negate = false; // in case this is a character class escape: \w \d etc
  697. switch(this->m_traits.escape_syntax_type(*m_position))
  698. {
  699. case regex_constants::escape_type_not_class:
  700. negate = true;
  701. BOOST_FALLTHROUGH;
  702. case regex_constants::escape_type_class:
  703. {
  704. escape_type_class_jump:
  705. typedef typename traits::char_class_type m_type;
  706. m_type m = this->m_traits.lookup_classname(m_position, m_position+1);
  707. if(m != 0)
  708. {
  709. basic_char_set<charT, traits> char_set;
  710. if(negate)
  711. char_set.negate();
  712. char_set.add_class(m);
  713. if(0 == this->append_set(char_set))
  714. {
  715. fail(regex_constants::error_ctype, m_position - m_base);
  716. return false;
  717. }
  718. ++m_position;
  719. return true;
  720. }
  721. //
  722. // not a class, just a regular unknown escape:
  723. //
  724. this->append_literal(unescape_character());
  725. break;
  726. }
  727. case regex_constants::syntax_digit:
  728. return parse_backref();
  729. case regex_constants::escape_type_left_word:
  730. ++m_position;
  731. this->append_state(syntax_element_word_start);
  732. break;
  733. case regex_constants::escape_type_right_word:
  734. ++m_position;
  735. this->append_state(syntax_element_word_end);
  736. break;
  737. case regex_constants::escape_type_start_buffer:
  738. ++m_position;
  739. this->append_state(syntax_element_buffer_start);
  740. break;
  741. case regex_constants::escape_type_end_buffer:
  742. ++m_position;
  743. this->append_state(syntax_element_buffer_end);
  744. break;
  745. case regex_constants::escape_type_word_assert:
  746. ++m_position;
  747. this->append_state(syntax_element_word_boundary);
  748. break;
  749. case regex_constants::escape_type_not_word_assert:
  750. ++m_position;
  751. this->append_state(syntax_element_within_word);
  752. break;
  753. case regex_constants::escape_type_Z:
  754. ++m_position;
  755. this->append_state(syntax_element_soft_buffer_end);
  756. break;
  757. case regex_constants::escape_type_Q:
  758. return parse_QE();
  759. case regex_constants::escape_type_C:
  760. return parse_match_any();
  761. case regex_constants::escape_type_X:
  762. ++m_position;
  763. this->append_state(syntax_element_combining);
  764. break;
  765. case regex_constants::escape_type_G:
  766. ++m_position;
  767. this->append_state(syntax_element_restart_continue);
  768. break;
  769. case regex_constants::escape_type_not_property:
  770. negate = true;
  771. BOOST_FALLTHROUGH;
  772. case regex_constants::escape_type_property:
  773. {
  774. ++m_position;
  775. char_class_type m;
  776. if(m_position == m_end)
  777. {
  778. fail(regex_constants::error_escape, m_position - m_base, "Incomplete property escape found.");
  779. return false;
  780. }
  781. // maybe have \p{ddd}
  782. if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_open_brace)
  783. {
  784. const charT* base = m_position;
  785. // skip forward until we find enclosing brace:
  786. while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_brace))
  787. ++m_position;
  788. if(m_position == m_end)
  789. {
  790. fail(regex_constants::error_escape, m_position - m_base, "Closing } missing from property escape sequence.");
  791. return false;
  792. }
  793. m = this->m_traits.lookup_classname(++base, m_position++);
  794. }
  795. else
  796. {
  797. m = this->m_traits.lookup_classname(m_position, m_position+1);
  798. ++m_position;
  799. }
  800. if(m != 0)
  801. {
  802. basic_char_set<charT, traits> char_set;
  803. if(negate)
  804. char_set.negate();
  805. char_set.add_class(m);
  806. if(0 == this->append_set(char_set))
  807. {
  808. fail(regex_constants::error_ctype, m_position - m_base);
  809. return false;
  810. }
  811. return true;
  812. }
  813. fail(regex_constants::error_ctype, m_position - m_base, "Escape sequence was neither a valid property nor a valid character class name.");
  814. return false;
  815. }
  816. case regex_constants::escape_type_reset_start_mark:
  817. if(0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
  818. {
  819. re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_startmark, sizeof(re_brace)));
  820. pb->index = -5;
  821. pb->icase = this->flags() & regbase::icase;
  822. this->m_pdata->m_data.align();
  823. ++m_position;
  824. return true;
  825. }
  826. goto escape_type_class_jump;
  827. case regex_constants::escape_type_line_ending:
  828. if(0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
  829. {
  830. const charT* e = get_escape_R_string<charT>();
  831. const charT* old_position = m_position;
  832. const charT* old_end = m_end;
  833. const charT* old_base = m_base;
  834. m_position = e;
  835. m_base = e;
  836. m_end = e + traits::length(e);
  837. bool r = parse_all();
  838. m_position = ++old_position;
  839. m_end = old_end;
  840. m_base = old_base;
  841. return r;
  842. }
  843. goto escape_type_class_jump;
  844. case regex_constants::escape_type_extended_backref:
  845. if(0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
  846. {
  847. bool have_brace = false;
  848. bool negative = false;
  849. static const char incomplete_message[] = "Incomplete \\g escape found.";
  850. if(++m_position == m_end)
  851. {
  852. fail(regex_constants::error_escape, m_position - m_base, incomplete_message);
  853. return false;
  854. }
  855. // maybe have \g{ddd}
  856. regex_constants::syntax_type syn = this->m_traits.syntax_type(*m_position);
  857. regex_constants::syntax_type syn_end = 0;
  858. if((syn == regex_constants::syntax_open_brace)
  859. || (syn == regex_constants::escape_type_left_word)
  860. || (syn == regex_constants::escape_type_end_buffer))
  861. {
  862. if(++m_position == m_end)
  863. {
  864. fail(regex_constants::error_escape, m_position - m_base, incomplete_message);
  865. return false;
  866. }
  867. have_brace = true;
  868. switch(syn)
  869. {
  870. case regex_constants::syntax_open_brace:
  871. syn_end = regex_constants::syntax_close_brace;
  872. break;
  873. case regex_constants::escape_type_left_word:
  874. syn_end = regex_constants::escape_type_right_word;
  875. break;
  876. default:
  877. syn_end = regex_constants::escape_type_end_buffer;
  878. break;
  879. }
  880. }
  881. negative = (*m_position == static_cast<charT>('-'));
  882. if((negative) && (++m_position == m_end))
  883. {
  884. fail(regex_constants::error_escape, m_position - m_base, incomplete_message);
  885. return false;
  886. }
  887. const charT* pc = m_position;
  888. boost::intmax_t i = this->m_traits.toi(pc, m_end, 10);
  889. if((i < 0) && syn_end)
  890. {
  891. // Check for a named capture, get the leftmost one if there is more than one:
  892. const charT* base = m_position;
  893. while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != syn_end))
  894. {
  895. ++m_position;
  896. }
  897. i = hash_value_from_capture_name(base, m_position);
  898. pc = m_position;
  899. }
  900. if(negative)
  901. i = 1 + (static_cast<boost::intmax_t>(m_mark_count) - i);
  902. if(((i < hash_value_mask) && (i > 0) && (this->m_backrefs.test(i))) || ((i >= hash_value_mask) && (this->m_pdata->get_id(i) > 0) && (this->m_backrefs.test(this->m_pdata->get_id(i)))))
  903. {
  904. m_position = pc;
  905. re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_backref, sizeof(re_brace)));
  906. pb->index = i;
  907. pb->icase = this->flags() & regbase::icase;
  908. }
  909. else
  910. {
  911. fail(regex_constants::error_backref, m_position - m_base);
  912. return false;
  913. }
  914. m_position = pc;
  915. if(have_brace)
  916. {
  917. if((m_position == m_end) || (this->m_traits.syntax_type(*m_position) != syn_end))
  918. {
  919. fail(regex_constants::error_escape, m_position - m_base, incomplete_message);
  920. return false;
  921. }
  922. ++m_position;
  923. }
  924. return true;
  925. }
  926. goto escape_type_class_jump;
  927. case regex_constants::escape_type_control_v:
  928. if(0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
  929. goto escape_type_class_jump;
  930. BOOST_FALLTHROUGH;
  931. default:
  932. this->append_literal(unescape_character());
  933. break;
  934. }
  935. return true;
  936. }
  937. template <class charT, class traits>
  938. bool basic_regex_parser<charT, traits>::parse_match_any()
  939. {
  940. //
  941. // we have a '.' that can match any character:
  942. //
  943. ++m_position;
  944. static_cast<re_dot*>(
  945. this->append_state(syntax_element_wild, sizeof(re_dot))
  946. )->mask = static_cast<unsigned char>(this->flags() & regbase::no_mod_s
  947. ? BOOST_REGEX_DETAIL_NS::force_not_newline
  948. : this->flags() & regbase::mod_s ?
  949. BOOST_REGEX_DETAIL_NS::force_newline : BOOST_REGEX_DETAIL_NS::dont_care);
  950. return true;
  951. }
  952. template <class charT, class traits>
  953. bool basic_regex_parser<charT, traits>::parse_repeat(std::size_t low, std::size_t high)
  954. {
  955. bool greedy = true;
  956. bool possessive = false;
  957. std::size_t insert_point;
  958. //
  959. // when we get to here we may have a non-greedy ? mark still to come:
  960. //
  961. if((m_position != m_end)
  962. && (
  963. (0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
  964. || ((regbase::basic_syntax_group|regbase::emacs_ex) == (this->flags() & (regbase::main_option_type | regbase::emacs_ex)))
  965. )
  966. )
  967. {
  968. // OK we have a perl or emacs regex, check for a '?':
  969. if ((this->flags() & (regbase::main_option_type | regbase::mod_x | regbase::no_perl_ex)) == regbase::mod_x)
  970. {
  971. // whitespace skip:
  972. while ((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
  973. ++m_position;
  974. }
  975. if((m_position != m_end) && (this->m_traits.syntax_type(*m_position) == regex_constants::syntax_question))
  976. {
  977. greedy = false;
  978. ++m_position;
  979. }
  980. // for perl regexes only check for possessive ++ repeats.
  981. if((m_position != m_end)
  982. && (0 == (this->flags() & regbase::main_option_type))
  983. && (this->m_traits.syntax_type(*m_position) == regex_constants::syntax_plus))
  984. {
  985. possessive = true;
  986. ++m_position;
  987. }
  988. }
  989. if(0 == this->m_last_state)
  990. {
  991. fail(regex_constants::error_badrepeat, ::boost::BOOST_REGEX_DETAIL_NS::distance(m_base, m_position), "Nothing to repeat.");
  992. return false;
  993. }
  994. if(this->m_last_state->type == syntax_element_endmark)
  995. {
  996. // insert a repeat before the '(' matching the last ')':
  997. insert_point = this->m_paren_start;
  998. }
  999. else if((this->m_last_state->type == syntax_element_literal) && (static_cast<re_literal*>(this->m_last_state)->length > 1))
  1000. {
  1001. // the last state was a literal with more than one character, split it in two:
  1002. re_literal* lit = static_cast<re_literal*>(this->m_last_state);
  1003. charT c = (static_cast<charT*>(static_cast<void*>(lit+1)))[lit->length - 1];
  1004. lit->length -= 1;
  1005. // now append new state:
  1006. lit = static_cast<re_literal*>(this->append_state(syntax_element_literal, sizeof(re_literal) + sizeof(charT)));
  1007. lit->length = 1;
  1008. (static_cast<charT*>(static_cast<void*>(lit+1)))[0] = c;
  1009. insert_point = this->getoffset(this->m_last_state);
  1010. }
  1011. else
  1012. {
  1013. // repeat the last state whatever it was, need to add some error checking here:
  1014. switch(this->m_last_state->type)
  1015. {
  1016. case syntax_element_start_line:
  1017. case syntax_element_end_line:
  1018. case syntax_element_word_boundary:
  1019. case syntax_element_within_word:
  1020. case syntax_element_word_start:
  1021. case syntax_element_word_end:
  1022. case syntax_element_buffer_start:
  1023. case syntax_element_buffer_end:
  1024. case syntax_element_alt:
  1025. case syntax_element_soft_buffer_end:
  1026. case syntax_element_restart_continue:
  1027. case syntax_element_jump:
  1028. case syntax_element_startmark:
  1029. case syntax_element_backstep:
  1030. // can't legally repeat any of the above:
  1031. fail(regex_constants::error_badrepeat, m_position - m_base);
  1032. return false;
  1033. default:
  1034. // do nothing...
  1035. break;
  1036. }
  1037. insert_point = this->getoffset(this->m_last_state);
  1038. }
  1039. //
  1040. // OK we now know what to repeat, so insert the repeat around it:
  1041. //
  1042. re_repeat* rep = static_cast<re_repeat*>(this->insert_state(insert_point, syntax_element_rep, re_repeater_size));
  1043. rep->min = low;
  1044. rep->max = high;
  1045. rep->greedy = greedy;
  1046. rep->leading = false;
  1047. // store our repeater position for later:
  1048. std::ptrdiff_t rep_off = this->getoffset(rep);
  1049. // and append a back jump to the repeat:
  1050. re_jump* jmp = static_cast<re_jump*>(this->append_state(syntax_element_jump, sizeof(re_jump)));
  1051. jmp->alt.i = rep_off - this->getoffset(jmp);
  1052. this->m_pdata->m_data.align();
  1053. // now fill in the alt jump for the repeat:
  1054. rep = static_cast<re_repeat*>(this->getaddress(rep_off));
  1055. rep->alt.i = this->m_pdata->m_data.size() - rep_off;
  1056. //
  1057. // If the repeat is possessive then bracket the repeat with a (?>...)
  1058. // independent sub-expression construct:
  1059. //
  1060. if(possessive)
  1061. {
  1062. if(m_position != m_end)
  1063. {
  1064. //
  1065. // Check for illegal following quantifier, we have to do this here, because
  1066. // the extra states we insert below circumvents our usual error checking :-(
  1067. //
  1068. bool contin = false;
  1069. do
  1070. {
  1071. if ((this->flags() & (regbase::main_option_type | regbase::mod_x | regbase::no_perl_ex)) == regbase::mod_x)
  1072. {
  1073. // whitespace skip:
  1074. while ((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
  1075. ++m_position;
  1076. }
  1077. if (m_position != m_end)
  1078. {
  1079. switch (this->m_traits.syntax_type(*m_position))
  1080. {
  1081. case regex_constants::syntax_star:
  1082. case regex_constants::syntax_plus:
  1083. case regex_constants::syntax_question:
  1084. case regex_constants::syntax_open_brace:
  1085. fail(regex_constants::error_badrepeat, m_position - m_base);
  1086. return false;
  1087. case regex_constants::syntax_open_mark:
  1088. // Do we have a comment? If so we need to skip it here...
  1089. if ((m_position + 2 < m_end) && this->m_traits.syntax_type(*(m_position + 1)) == regex_constants::syntax_question
  1090. && this->m_traits.syntax_type(*(m_position + 2)) == regex_constants::syntax_hash)
  1091. {
  1092. while ((m_position != m_end)
  1093. && (this->m_traits.syntax_type(*m_position++) != regex_constants::syntax_close_mark)) {
  1094. }
  1095. contin = true;
  1096. }
  1097. else
  1098. contin = false;
  1099. }
  1100. }
  1101. else
  1102. contin = false;
  1103. } while (contin);
  1104. }
  1105. re_brace* pb = static_cast<re_brace*>(this->insert_state(insert_point, syntax_element_startmark, sizeof(re_brace)));
  1106. pb->index = -3;
  1107. pb->icase = this->flags() & regbase::icase;
  1108. jmp = static_cast<re_jump*>(this->insert_state(insert_point + sizeof(re_brace), syntax_element_jump, sizeof(re_jump)));
  1109. this->m_pdata->m_data.align();
  1110. jmp->alt.i = this->m_pdata->m_data.size() - this->getoffset(jmp);
  1111. pb = static_cast<re_brace*>(this->append_state(syntax_element_endmark, sizeof(re_brace)));
  1112. pb->index = -3;
  1113. pb->icase = this->flags() & regbase::icase;
  1114. }
  1115. return true;
  1116. }
  1117. template <class charT, class traits>
  1118. bool basic_regex_parser<charT, traits>::parse_repeat_range(bool isbasic)
  1119. {
  1120. static const char incomplete_message[] = "Missing } in quantified repetition.";
  1121. //
  1122. // parse a repeat-range:
  1123. //
  1124. std::size_t min, max;
  1125. boost::intmax_t v;
  1126. // skip whitespace:
  1127. while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
  1128. ++m_position;
  1129. if(this->m_position == this->m_end)
  1130. {
  1131. if(this->flags() & (regbase::main_option_type | regbase::no_perl_ex))
  1132. {
  1133. fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
  1134. return false;
  1135. }
  1136. // Treat the opening '{' as a literal character, rewind to start of error:
  1137. --m_position;
  1138. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_brace) --m_position;
  1139. return parse_literal();
  1140. }
  1141. // get min:
  1142. v = this->m_traits.toi(m_position, m_end, 10);
  1143. // skip whitespace:
  1144. if((v < 0) || (v > umax()))
  1145. {
  1146. if(this->flags() & (regbase::main_option_type | regbase::no_perl_ex))
  1147. {
  1148. fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
  1149. return false;
  1150. }
  1151. // Treat the opening '{' as a literal character, rewind to start of error:
  1152. --m_position;
  1153. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_brace) --m_position;
  1154. return parse_literal();
  1155. }
  1156. while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
  1157. ++m_position;
  1158. if(this->m_position == this->m_end)
  1159. {
  1160. if(this->flags() & (regbase::main_option_type | regbase::no_perl_ex))
  1161. {
  1162. fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
  1163. return false;
  1164. }
  1165. // Treat the opening '{' as a literal character, rewind to start of error:
  1166. --m_position;
  1167. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_brace) --m_position;
  1168. return parse_literal();
  1169. }
  1170. min = static_cast<std::size_t>(v);
  1171. // see if we have a comma:
  1172. if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_comma)
  1173. {
  1174. // move on and error check:
  1175. ++m_position;
  1176. // skip whitespace:
  1177. while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
  1178. ++m_position;
  1179. if(this->m_position == this->m_end)
  1180. {
  1181. if(this->flags() & (regbase::main_option_type | regbase::no_perl_ex))
  1182. {
  1183. fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
  1184. return false;
  1185. }
  1186. // Treat the opening '{' as a literal character, rewind to start of error:
  1187. --m_position;
  1188. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_brace) --m_position;
  1189. return parse_literal();
  1190. }
  1191. // get the value if any:
  1192. v = this->m_traits.toi(m_position, m_end, 10);
  1193. max = ((v >= 0) && (v < umax())) ? (std::size_t)v : (std::numeric_limits<std::size_t>::max)();
  1194. }
  1195. else
  1196. {
  1197. // no comma, max = min:
  1198. max = min;
  1199. }
  1200. // skip whitespace:
  1201. while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
  1202. ++m_position;
  1203. // OK now check trailing }:
  1204. if(this->m_position == this->m_end)
  1205. {
  1206. if(this->flags() & (regbase::main_option_type | regbase::no_perl_ex))
  1207. {
  1208. fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
  1209. return false;
  1210. }
  1211. // Treat the opening '{' as a literal character, rewind to start of error:
  1212. --m_position;
  1213. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_brace) --m_position;
  1214. return parse_literal();
  1215. }
  1216. if(isbasic)
  1217. {
  1218. if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_escape)
  1219. {
  1220. ++m_position;
  1221. if(this->m_position == this->m_end)
  1222. {
  1223. fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
  1224. return false;
  1225. }
  1226. }
  1227. else
  1228. {
  1229. fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
  1230. return false;
  1231. }
  1232. }
  1233. if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_brace)
  1234. ++m_position;
  1235. else
  1236. {
  1237. // Treat the opening '{' as a literal character, rewind to start of error:
  1238. --m_position;
  1239. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_brace) --m_position;
  1240. return parse_literal();
  1241. }
  1242. //
  1243. // finally go and add the repeat, unless error:
  1244. //
  1245. if(min > max)
  1246. {
  1247. // Backtrack to error location:
  1248. m_position -= 2;
  1249. while(this->m_traits.isctype(*m_position, this->m_word_mask)) --m_position;
  1250. ++m_position;
  1251. fail(regex_constants::error_badbrace, m_position - m_base);
  1252. return false;
  1253. }
  1254. return parse_repeat(min, max);
  1255. }
  1256. template <class charT, class traits>
  1257. bool basic_regex_parser<charT, traits>::parse_alt()
  1258. {
  1259. //
  1260. // error check: if there have been no previous states,
  1261. // or if the last state was a '(' then error:
  1262. //
  1263. if(
  1264. ((this->m_last_state == 0) || (this->m_last_state->type == syntax_element_startmark))
  1265. &&
  1266. !(
  1267. ((this->flags() & regbase::main_option_type) == regbase::perl_syntax_group)
  1268. &&
  1269. ((this->flags() & regbase::no_empty_expressions) == 0)
  1270. )
  1271. )
  1272. {
  1273. fail(regex_constants::error_empty, this->m_position - this->m_base, "A regular expression cannot start with the alternation operator |.");
  1274. return false;
  1275. }
  1276. //
  1277. // Reset mark count if required:
  1278. //
  1279. if(m_max_mark < m_mark_count)
  1280. m_max_mark = m_mark_count;
  1281. if(m_mark_reset >= 0)
  1282. m_mark_count = m_mark_reset;
  1283. ++m_position;
  1284. //
  1285. // we need to append a trailing jump:
  1286. //
  1287. re_syntax_base* pj = this->append_state(BOOST_REGEX_DETAIL_NS::syntax_element_jump, sizeof(re_jump));
  1288. std::ptrdiff_t jump_offset = this->getoffset(pj);
  1289. //
  1290. // now insert the alternative:
  1291. //
  1292. re_alt* palt = static_cast<re_alt*>(this->insert_state(this->m_alt_insert_point, syntax_element_alt, re_alt_size));
  1293. jump_offset += re_alt_size;
  1294. this->m_pdata->m_data.align();
  1295. palt->alt.i = this->m_pdata->m_data.size() - this->getoffset(palt);
  1296. //
  1297. // update m_alt_insert_point so that the next alternate gets
  1298. // inserted at the start of the second of the two we've just created:
  1299. //
  1300. this->m_alt_insert_point = this->m_pdata->m_data.size();
  1301. //
  1302. // the start of this alternative must have a case changes state
  1303. // if the current block has messed around with case changes:
  1304. //
  1305. if(m_has_case_change)
  1306. {
  1307. static_cast<re_case*>(
  1308. this->append_state(syntax_element_toggle_case, sizeof(re_case))
  1309. )->icase = this->m_icase;
  1310. }
  1311. //
  1312. // push the alternative onto our stack, a recursive
  1313. // implementation here is easier to understand (and faster
  1314. // as it happens), but causes all kinds of stack overflow problems
  1315. // on programs with small stacks (COM+).
  1316. //
  1317. m_alt_jumps.push_back(jump_offset);
  1318. return true;
  1319. }
  1320. template <class charT, class traits>
  1321. bool basic_regex_parser<charT, traits>::parse_set()
  1322. {
  1323. static const char incomplete_message[] = "Character set declaration starting with [ terminated prematurely - either no ] was found or the set had no content.";
  1324. ++m_position;
  1325. if(m_position == m_end)
  1326. {
  1327. fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
  1328. return false;
  1329. }
  1330. basic_char_set<charT, traits> char_set;
  1331. const charT* base = m_position; // where the '[' was
  1332. const charT* item_base = m_position; // where the '[' or '^' was
  1333. while(m_position != m_end)
  1334. {
  1335. switch(this->m_traits.syntax_type(*m_position))
  1336. {
  1337. case regex_constants::syntax_caret:
  1338. if(m_position == base)
  1339. {
  1340. char_set.negate();
  1341. ++m_position;
  1342. item_base = m_position;
  1343. }
  1344. else
  1345. parse_set_literal(char_set);
  1346. break;
  1347. case regex_constants::syntax_close_set:
  1348. if(m_position == item_base)
  1349. {
  1350. parse_set_literal(char_set);
  1351. break;
  1352. }
  1353. else
  1354. {
  1355. ++m_position;
  1356. if(0 == this->append_set(char_set))
  1357. {
  1358. fail(regex_constants::error_ctype, m_position - m_base);
  1359. return false;
  1360. }
  1361. }
  1362. return true;
  1363. case regex_constants::syntax_open_set:
  1364. if(parse_inner_set(char_set))
  1365. break;
  1366. return true;
  1367. case regex_constants::syntax_escape:
  1368. {
  1369. //
  1370. // look ahead and see if this is a character class shortcut
  1371. // \d \w \s etc...
  1372. //
  1373. ++m_position;
  1374. if(this->m_traits.escape_syntax_type(*m_position)
  1375. == regex_constants::escape_type_class)
  1376. {
  1377. char_class_type m = this->m_traits.lookup_classname(m_position, m_position+1);
  1378. if(m != 0)
  1379. {
  1380. char_set.add_class(m);
  1381. ++m_position;
  1382. break;
  1383. }
  1384. }
  1385. else if(this->m_traits.escape_syntax_type(*m_position)
  1386. == regex_constants::escape_type_not_class)
  1387. {
  1388. // negated character class:
  1389. char_class_type m = this->m_traits.lookup_classname(m_position, m_position+1);
  1390. if(m != 0)
  1391. {
  1392. char_set.add_negated_class(m);
  1393. ++m_position;
  1394. break;
  1395. }
  1396. }
  1397. // not a character class, just a regular escape:
  1398. --m_position;
  1399. parse_set_literal(char_set);
  1400. break;
  1401. }
  1402. default:
  1403. parse_set_literal(char_set);
  1404. break;
  1405. }
  1406. }
  1407. return m_position != m_end;
  1408. }
  1409. template <class charT, class traits>
  1410. bool basic_regex_parser<charT, traits>::parse_inner_set(basic_char_set<charT, traits>& char_set)
  1411. {
  1412. static const char incomplete_message[] = "Character class declaration starting with [ terminated prematurely - either no ] was found or the set had no content.";
  1413. //
  1414. // we have either a character class [:name:]
  1415. // a collating element [.name.]
  1416. // or an equivalence class [=name=]
  1417. //
  1418. if(m_end == ++m_position)
  1419. {
  1420. fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
  1421. return false;
  1422. }
  1423. switch(this->m_traits.syntax_type(*m_position))
  1424. {
  1425. case regex_constants::syntax_dot:
  1426. //
  1427. // a collating element is treated as a literal:
  1428. //
  1429. --m_position;
  1430. parse_set_literal(char_set);
  1431. return true;
  1432. case regex_constants::syntax_colon:
  1433. {
  1434. // check that character classes are actually enabled:
  1435. if((this->flags() & (regbase::main_option_type | regbase::no_char_classes))
  1436. == (regbase::basic_syntax_group | regbase::no_char_classes))
  1437. {
  1438. --m_position;
  1439. parse_set_literal(char_set);
  1440. return true;
  1441. }
  1442. // skip the ':'
  1443. if(m_end == ++m_position)
  1444. {
  1445. fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
  1446. return false;
  1447. }
  1448. const charT* name_first = m_position;
  1449. // skip at least one character, then find the matching ':]'
  1450. if(m_end == ++m_position)
  1451. {
  1452. fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
  1453. return false;
  1454. }
  1455. while((m_position != m_end)
  1456. && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_colon))
  1457. ++m_position;
  1458. const charT* name_last = m_position;
  1459. if(m_end == m_position)
  1460. {
  1461. fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
  1462. return false;
  1463. }
  1464. if((m_end == ++m_position)
  1465. || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
  1466. {
  1467. fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
  1468. return false;
  1469. }
  1470. //
  1471. // check for negated class:
  1472. //
  1473. bool negated = false;
  1474. if(this->m_traits.syntax_type(*name_first) == regex_constants::syntax_caret)
  1475. {
  1476. ++name_first;
  1477. negated = true;
  1478. }
  1479. typedef typename traits::char_class_type m_type;
  1480. m_type m = this->m_traits.lookup_classname(name_first, name_last);
  1481. if(m == 0)
  1482. {
  1483. if(char_set.empty() && (name_last - name_first == 1))
  1484. {
  1485. // maybe a special case:
  1486. ++m_position;
  1487. if( (m_position != m_end)
  1488. && (this->m_traits.syntax_type(*m_position)
  1489. == regex_constants::syntax_close_set))
  1490. {
  1491. if(this->m_traits.escape_syntax_type(*name_first)
  1492. == regex_constants::escape_type_left_word)
  1493. {
  1494. ++m_position;
  1495. this->append_state(syntax_element_word_start);
  1496. return false;
  1497. }
  1498. if(this->m_traits.escape_syntax_type(*name_first)
  1499. == regex_constants::escape_type_right_word)
  1500. {
  1501. ++m_position;
  1502. this->append_state(syntax_element_word_end);
  1503. return false;
  1504. }
  1505. }
  1506. }
  1507. fail(regex_constants::error_ctype, name_first - m_base);
  1508. return false;
  1509. }
  1510. if(!negated)
  1511. char_set.add_class(m);
  1512. else
  1513. char_set.add_negated_class(m);
  1514. ++m_position;
  1515. break;
  1516. }
  1517. case regex_constants::syntax_equal:
  1518. {
  1519. // skip the '='
  1520. if(m_end == ++m_position)
  1521. {
  1522. fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
  1523. return false;
  1524. }
  1525. const charT* name_first = m_position;
  1526. // skip at least one character, then find the matching '=]'
  1527. if(m_end == ++m_position)
  1528. {
  1529. fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
  1530. return false;
  1531. }
  1532. while((m_position != m_end)
  1533. && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_equal))
  1534. ++m_position;
  1535. const charT* name_last = m_position;
  1536. if(m_end == m_position)
  1537. {
  1538. fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
  1539. return false;
  1540. }
  1541. if((m_end == ++m_position)
  1542. || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
  1543. {
  1544. fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
  1545. return false;
  1546. }
  1547. string_type m = this->m_traits.lookup_collatename(name_first, name_last);
  1548. if(m.empty() || (m.size() > 2))
  1549. {
  1550. fail(regex_constants::error_collate, name_first - m_base);
  1551. return false;
  1552. }
  1553. digraph<charT> d;
  1554. d.first = m[0];
  1555. if(m.size() > 1)
  1556. d.second = m[1];
  1557. else
  1558. d.second = 0;
  1559. char_set.add_equivalent(d);
  1560. ++m_position;
  1561. break;
  1562. }
  1563. default:
  1564. --m_position;
  1565. parse_set_literal(char_set);
  1566. break;
  1567. }
  1568. return true;
  1569. }
  1570. template <class charT, class traits>
  1571. void basic_regex_parser<charT, traits>::parse_set_literal(basic_char_set<charT, traits>& char_set)
  1572. {
  1573. digraph<charT> start_range(get_next_set_literal(char_set));
  1574. if(m_end == m_position)
  1575. {
  1576. fail(regex_constants::error_brack, m_position - m_base);
  1577. return;
  1578. }
  1579. if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_dash)
  1580. {
  1581. // we have a range:
  1582. if(m_end == ++m_position)
  1583. {
  1584. fail(regex_constants::error_brack, m_position - m_base);
  1585. return;
  1586. }
  1587. if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set)
  1588. {
  1589. digraph<charT> end_range = get_next_set_literal(char_set);
  1590. char_set.add_range(start_range, end_range);
  1591. if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_dash)
  1592. {
  1593. if(m_end == ++m_position)
  1594. {
  1595. fail(regex_constants::error_brack, m_position - m_base);
  1596. return;
  1597. }
  1598. if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_set)
  1599. {
  1600. // trailing - :
  1601. --m_position;
  1602. return;
  1603. }
  1604. fail(regex_constants::error_range, m_position - m_base);
  1605. return;
  1606. }
  1607. return;
  1608. }
  1609. --m_position;
  1610. }
  1611. char_set.add_single(start_range);
  1612. }
  1613. template <class charT, class traits>
  1614. digraph<charT> basic_regex_parser<charT, traits>::get_next_set_literal(basic_char_set<charT, traits>& char_set)
  1615. {
  1616. digraph<charT> result;
  1617. switch(this->m_traits.syntax_type(*m_position))
  1618. {
  1619. case regex_constants::syntax_dash:
  1620. if(!char_set.empty())
  1621. {
  1622. // see if we are at the end of the set:
  1623. if((++m_position == m_end) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
  1624. {
  1625. fail(regex_constants::error_range, m_position - m_base);
  1626. return result;
  1627. }
  1628. --m_position;
  1629. }
  1630. result.first = *m_position++;
  1631. return result;
  1632. case regex_constants::syntax_escape:
  1633. // check to see if escapes are supported first:
  1634. if(this->flags() & regex_constants::no_escape_in_lists)
  1635. {
  1636. result = *m_position++;
  1637. break;
  1638. }
  1639. ++m_position;
  1640. result = unescape_character();
  1641. break;
  1642. case regex_constants::syntax_open_set:
  1643. {
  1644. if(m_end == ++m_position)
  1645. {
  1646. fail(regex_constants::error_collate, m_position - m_base);
  1647. return result;
  1648. }
  1649. if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_dot)
  1650. {
  1651. --m_position;
  1652. result.first = *m_position;
  1653. ++m_position;
  1654. return result;
  1655. }
  1656. if(m_end == ++m_position)
  1657. {
  1658. fail(regex_constants::error_collate, m_position - m_base);
  1659. return result;
  1660. }
  1661. const charT* name_first = m_position;
  1662. // skip at least one character, then find the matching ':]'
  1663. if(m_end == ++m_position)
  1664. {
  1665. fail(regex_constants::error_collate, name_first - m_base);
  1666. return result;
  1667. }
  1668. while((m_position != m_end)
  1669. && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_dot))
  1670. ++m_position;
  1671. const charT* name_last = m_position;
  1672. if(m_end == m_position)
  1673. {
  1674. fail(regex_constants::error_collate, name_first - m_base);
  1675. return result;
  1676. }
  1677. if((m_end == ++m_position)
  1678. || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
  1679. {
  1680. fail(regex_constants::error_collate, name_first - m_base);
  1681. return result;
  1682. }
  1683. ++m_position;
  1684. string_type s = this->m_traits.lookup_collatename(name_first, name_last);
  1685. if(s.empty() || (s.size() > 2))
  1686. {
  1687. fail(regex_constants::error_collate, name_first - m_base);
  1688. return result;
  1689. }
  1690. result.first = s[0];
  1691. if(s.size() > 1)
  1692. result.second = s[1];
  1693. else
  1694. result.second = 0;
  1695. return result;
  1696. }
  1697. default:
  1698. result = *m_position++;
  1699. }
  1700. return result;
  1701. }
  1702. //
  1703. // does a value fit in the specified charT type?
  1704. //
  1705. template <class charT>
  1706. bool valid_value(charT, boost::intmax_t v, const mpl::true_&)
  1707. {
  1708. return (v >> (sizeof(charT) * CHAR_BIT)) == 0;
  1709. }
  1710. template <class charT>
  1711. bool valid_value(charT, boost::intmax_t, const mpl::false_&)
  1712. {
  1713. return true; // v will alsways fit in a charT
  1714. }
  1715. template <class charT>
  1716. bool valid_value(charT c, boost::intmax_t v)
  1717. {
  1718. return valid_value(c, v, mpl::bool_<(sizeof(charT) < sizeof(boost::intmax_t))>());
  1719. }
  1720. template <class charT, class traits>
  1721. charT basic_regex_parser<charT, traits>::unescape_character()
  1722. {
  1723. #ifdef BOOST_MSVC
  1724. #pragma warning(push)
  1725. #pragma warning(disable:4127)
  1726. #endif
  1727. charT result(0);
  1728. if(m_position == m_end)
  1729. {
  1730. fail(regex_constants::error_escape, m_position - m_base, "Escape sequence terminated prematurely.");
  1731. return false;
  1732. }
  1733. switch(this->m_traits.escape_syntax_type(*m_position))
  1734. {
  1735. case regex_constants::escape_type_control_a:
  1736. result = charT('\a');
  1737. break;
  1738. case regex_constants::escape_type_e:
  1739. result = charT(27);
  1740. break;
  1741. case regex_constants::escape_type_control_f:
  1742. result = charT('\f');
  1743. break;
  1744. case regex_constants::escape_type_control_n:
  1745. result = charT('\n');
  1746. break;
  1747. case regex_constants::escape_type_control_r:
  1748. result = charT('\r');
  1749. break;
  1750. case regex_constants::escape_type_control_t:
  1751. result = charT('\t');
  1752. break;
  1753. case regex_constants::escape_type_control_v:
  1754. result = charT('\v');
  1755. break;
  1756. case regex_constants::escape_type_word_assert:
  1757. result = charT('\b');
  1758. break;
  1759. case regex_constants::escape_type_ascii_control:
  1760. ++m_position;
  1761. if(m_position == m_end)
  1762. {
  1763. // Rewind to start of escape:
  1764. --m_position;
  1765. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
  1766. fail(regex_constants::error_escape, m_position - m_base, "ASCII escape sequence terminated prematurely.");
  1767. return result;
  1768. }
  1769. result = static_cast<charT>(*m_position % 32);
  1770. break;
  1771. case regex_constants::escape_type_hex:
  1772. ++m_position;
  1773. if(m_position == m_end)
  1774. {
  1775. // Rewind to start of escape:
  1776. --m_position;
  1777. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
  1778. fail(regex_constants::error_escape, m_position - m_base, "Hexadecimal escape sequence terminated prematurely.");
  1779. return result;
  1780. }
  1781. // maybe have \x{ddd}
  1782. if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_open_brace)
  1783. {
  1784. ++m_position;
  1785. if(m_position == m_end)
  1786. {
  1787. // Rewind to start of escape:
  1788. --m_position;
  1789. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
  1790. fail(regex_constants::error_escape, m_position - m_base, "Missing } in hexadecimal escape sequence.");
  1791. return result;
  1792. }
  1793. boost::intmax_t i = this->m_traits.toi(m_position, m_end, 16);
  1794. if((m_position == m_end)
  1795. || (i < 0)
  1796. || ((std::numeric_limits<charT>::is_specialized) && (i > (boost::intmax_t)(std::numeric_limits<charT>::max)()))
  1797. || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_brace))
  1798. {
  1799. // Rewind to start of escape:
  1800. --m_position;
  1801. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
  1802. fail(regex_constants::error_badbrace, m_position - m_base, "Hexadecimal escape sequence was invalid.");
  1803. return result;
  1804. }
  1805. ++m_position;
  1806. result = charT(i);
  1807. }
  1808. else
  1809. {
  1810. std::ptrdiff_t len = (std::min)(static_cast<std::ptrdiff_t>(2), static_cast<std::ptrdiff_t>(m_end - m_position));
  1811. boost::intmax_t i = this->m_traits.toi(m_position, m_position + len, 16);
  1812. if((i < 0)
  1813. || !valid_value(charT(0), i))
  1814. {
  1815. // Rewind to start of escape:
  1816. --m_position;
  1817. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
  1818. fail(regex_constants::error_escape, m_position - m_base, "Escape sequence did not encode a valid character.");
  1819. return result;
  1820. }
  1821. result = charT(i);
  1822. }
  1823. return result;
  1824. case regex_constants::syntax_digit:
  1825. {
  1826. // an octal escape sequence, the first character must be a zero
  1827. // followed by up to 3 octal digits:
  1828. std::ptrdiff_t len = (std::min)(::boost::BOOST_REGEX_DETAIL_NS::distance(m_position, m_end), static_cast<std::ptrdiff_t>(4));
  1829. const charT* bp = m_position;
  1830. boost::intmax_t val = this->m_traits.toi(bp, bp + 1, 8);
  1831. if(val != 0)
  1832. {
  1833. // Rewind to start of escape:
  1834. --m_position;
  1835. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
  1836. // Oops not an octal escape after all:
  1837. fail(regex_constants::error_escape, m_position - m_base, "Invalid octal escape sequence.");
  1838. return result;
  1839. }
  1840. val = this->m_traits.toi(m_position, m_position + len, 8);
  1841. if((val < 0) || (val > (boost::intmax_t)(std::numeric_limits<charT>::max)()))
  1842. {
  1843. // Rewind to start of escape:
  1844. --m_position;
  1845. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
  1846. fail(regex_constants::error_escape, m_position - m_base, "Octal escape sequence is invalid.");
  1847. return result;
  1848. }
  1849. return static_cast<charT>(val);
  1850. }
  1851. case regex_constants::escape_type_named_char:
  1852. {
  1853. ++m_position;
  1854. if(m_position == m_end)
  1855. {
  1856. // Rewind to start of escape:
  1857. --m_position;
  1858. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
  1859. fail(regex_constants::error_escape, m_position - m_base);
  1860. return false;
  1861. }
  1862. // maybe have \N{name}
  1863. if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_open_brace)
  1864. {
  1865. const charT* base = m_position;
  1866. // skip forward until we find enclosing brace:
  1867. while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_brace))
  1868. ++m_position;
  1869. if(m_position == m_end)
  1870. {
  1871. // Rewind to start of escape:
  1872. --m_position;
  1873. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
  1874. fail(regex_constants::error_escape, m_position - m_base);
  1875. return false;
  1876. }
  1877. string_type s = this->m_traits.lookup_collatename(++base, m_position++);
  1878. if(s.empty())
  1879. {
  1880. // Rewind to start of escape:
  1881. --m_position;
  1882. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
  1883. fail(regex_constants::error_collate, m_position - m_base);
  1884. return false;
  1885. }
  1886. if(s.size() == 1)
  1887. {
  1888. return s[0];
  1889. }
  1890. }
  1891. // fall through is a failure:
  1892. // Rewind to start of escape:
  1893. --m_position;
  1894. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
  1895. fail(regex_constants::error_escape, m_position - m_base);
  1896. return false;
  1897. }
  1898. default:
  1899. result = *m_position;
  1900. break;
  1901. }
  1902. ++m_position;
  1903. return result;
  1904. #ifdef BOOST_MSVC
  1905. #pragma warning(pop)
  1906. #endif
  1907. }
  1908. template <class charT, class traits>
  1909. bool basic_regex_parser<charT, traits>::parse_backref()
  1910. {
  1911. BOOST_REGEX_ASSERT(m_position != m_end);
  1912. const charT* pc = m_position;
  1913. boost::intmax_t i = this->m_traits.toi(pc, pc + 1, 10);
  1914. if((i == 0) || (((this->flags() & regbase::main_option_type) == regbase::perl_syntax_group) && (this->flags() & regbase::no_bk_refs)))
  1915. {
  1916. // not a backref at all but an octal escape sequence:
  1917. charT c = unescape_character();
  1918. this->append_literal(c);
  1919. }
  1920. else if((i > 0) && (this->m_backrefs.test(i)))
  1921. {
  1922. m_position = pc;
  1923. re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_backref, sizeof(re_brace)));
  1924. pb->index = i;
  1925. pb->icase = this->flags() & regbase::icase;
  1926. }
  1927. else
  1928. {
  1929. // Rewind to start of escape:
  1930. --m_position;
  1931. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
  1932. fail(regex_constants::error_backref, m_position - m_base);
  1933. return false;
  1934. }
  1935. return true;
  1936. }
  1937. template <class charT, class traits>
  1938. bool basic_regex_parser<charT, traits>::parse_QE()
  1939. {
  1940. #ifdef BOOST_MSVC
  1941. #pragma warning(push)
  1942. #pragma warning(disable:4127)
  1943. #endif
  1944. //
  1945. // parse a \Q...\E sequence:
  1946. //
  1947. ++m_position; // skip the Q
  1948. const charT* start = m_position;
  1949. const charT* end;
  1950. do
  1951. {
  1952. while((m_position != m_end)
  1953. && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape))
  1954. ++m_position;
  1955. if(m_position == m_end)
  1956. {
  1957. // a \Q...\E sequence may terminate with the end of the expression:
  1958. end = m_position;
  1959. break;
  1960. }
  1961. if(++m_position == m_end) // skip the escape
  1962. {
  1963. fail(regex_constants::error_escape, m_position - m_base, "Unterminated \\Q...\\E sequence.");
  1964. return false;
  1965. }
  1966. // check to see if it's a \E:
  1967. if(this->m_traits.escape_syntax_type(*m_position) == regex_constants::escape_type_E)
  1968. {
  1969. ++m_position;
  1970. end = m_position - 2;
  1971. break;
  1972. }
  1973. // otherwise go round again:
  1974. }while(true);
  1975. //
  1976. // now add all the character between the two escapes as literals:
  1977. //
  1978. while(start != end)
  1979. {
  1980. this->append_literal(*start);
  1981. ++start;
  1982. }
  1983. return true;
  1984. #ifdef BOOST_MSVC
  1985. #pragma warning(pop)
  1986. #endif
  1987. }
  1988. template <class charT, class traits>
  1989. bool basic_regex_parser<charT, traits>::parse_perl_extension()
  1990. {
  1991. if(++m_position == m_end)
  1992. {
  1993. // Rewind to start of (? sequence:
  1994. --m_position;
  1995. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  1996. fail(regex_constants::error_perl_extension, m_position - m_base);
  1997. return false;
  1998. }
  1999. //
  2000. // treat comments as a special case, as these
  2001. // are the only ones that don't start with a leading
  2002. // startmark state:
  2003. //
  2004. if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_hash)
  2005. {
  2006. while((m_position != m_end)
  2007. && (this->m_traits.syntax_type(*m_position++) != regex_constants::syntax_close_mark))
  2008. {}
  2009. return true;
  2010. }
  2011. //
  2012. // backup some state, and prepare the way:
  2013. //
  2014. int markid = 0;
  2015. std::ptrdiff_t jump_offset = 0;
  2016. re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_startmark, sizeof(re_brace)));
  2017. pb->icase = this->flags() & regbase::icase;
  2018. std::ptrdiff_t last_paren_start = this->getoffset(pb);
  2019. // back up insertion point for alternations, and set new point:
  2020. std::ptrdiff_t last_alt_point = m_alt_insert_point;
  2021. this->m_pdata->m_data.align();
  2022. m_alt_insert_point = this->m_pdata->m_data.size();
  2023. std::ptrdiff_t expected_alt_point = m_alt_insert_point;
  2024. bool restore_flags = true;
  2025. regex_constants::syntax_option_type old_flags = this->flags();
  2026. bool old_case_change = m_has_case_change;
  2027. m_has_case_change = false;
  2028. charT name_delim;
  2029. int mark_reset = m_mark_reset;
  2030. int max_mark = m_max_mark;
  2031. m_mark_reset = -1;
  2032. m_max_mark = m_mark_count;
  2033. boost::intmax_t v;
  2034. //
  2035. // select the actual extension used:
  2036. //
  2037. switch(this->m_traits.syntax_type(*m_position))
  2038. {
  2039. case regex_constants::syntax_or:
  2040. m_mark_reset = m_mark_count;
  2041. BOOST_FALLTHROUGH;
  2042. case regex_constants::syntax_colon:
  2043. //
  2044. // a non-capturing mark:
  2045. //
  2046. pb->index = markid = 0;
  2047. ++m_position;
  2048. break;
  2049. case regex_constants::syntax_digit:
  2050. {
  2051. //
  2052. // a recursive subexpression:
  2053. //
  2054. v = this->m_traits.toi(m_position, m_end, 10);
  2055. if((v < 0) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
  2056. {
  2057. // Rewind to start of (? sequence:
  2058. --m_position;
  2059. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2060. fail(regex_constants::error_perl_extension, m_position - m_base, "The recursive sub-expression refers to an invalid marking group, or is unterminated.");
  2061. return false;
  2062. }
  2063. insert_recursion:
  2064. pb->index = markid = 0;
  2065. re_recurse* pr = static_cast<re_recurse*>(this->append_state(syntax_element_recurse, sizeof(re_recurse)));
  2066. pr->alt.i = v;
  2067. pr->state_id = 0;
  2068. static_cast<re_case*>(
  2069. this->append_state(syntax_element_toggle_case, sizeof(re_case))
  2070. )->icase = this->flags() & regbase::icase;
  2071. break;
  2072. }
  2073. case regex_constants::syntax_plus:
  2074. //
  2075. // A forward-relative recursive subexpression:
  2076. //
  2077. ++m_position;
  2078. v = this->m_traits.toi(m_position, m_end, 10);
  2079. if((v <= 0) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
  2080. {
  2081. // Rewind to start of (? sequence:
  2082. --m_position;
  2083. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2084. fail(regex_constants::error_perl_extension, m_position - m_base, "An invalid or unterminated recursive sub-expression.");
  2085. return false;
  2086. }
  2087. if ((std::numeric_limits<boost::intmax_t>::max)() - m_mark_count < v)
  2088. {
  2089. fail(regex_constants::error_perl_extension, m_position - m_base, "An invalid or unterminated recursive sub-expression.");
  2090. return false;
  2091. }
  2092. v += m_mark_count;
  2093. goto insert_recursion;
  2094. case regex_constants::syntax_dash:
  2095. //
  2096. // Possibly a backward-relative recursive subexpression:
  2097. //
  2098. ++m_position;
  2099. v = this->m_traits.toi(m_position, m_end, 10);
  2100. if(v <= 0)
  2101. {
  2102. --m_position;
  2103. // Oops not a relative recursion at all, but a (?-imsx) group:
  2104. goto option_group_jump;
  2105. }
  2106. v = static_cast<boost::intmax_t>(m_mark_count) + 1 - v;
  2107. if(v <= 0)
  2108. {
  2109. // Rewind to start of (? sequence:
  2110. --m_position;
  2111. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2112. fail(regex_constants::error_perl_extension, m_position - m_base, "An invalid or unterminated recursive sub-expression.");
  2113. return false;
  2114. }
  2115. goto insert_recursion;
  2116. case regex_constants::syntax_equal:
  2117. pb->index = markid = -1;
  2118. ++m_position;
  2119. jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
  2120. this->m_pdata->m_data.align();
  2121. m_alt_insert_point = this->m_pdata->m_data.size();
  2122. break;
  2123. case regex_constants::syntax_not:
  2124. pb->index = markid = -2;
  2125. ++m_position;
  2126. jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
  2127. this->m_pdata->m_data.align();
  2128. m_alt_insert_point = this->m_pdata->m_data.size();
  2129. break;
  2130. case regex_constants::escape_type_left_word:
  2131. {
  2132. // a lookbehind assertion:
  2133. if(++m_position == m_end)
  2134. {
  2135. // Rewind to start of (? sequence:
  2136. --m_position;
  2137. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2138. fail(regex_constants::error_perl_extension, m_position - m_base);
  2139. return false;
  2140. }
  2141. regex_constants::syntax_type t = this->m_traits.syntax_type(*m_position);
  2142. if(t == regex_constants::syntax_not)
  2143. pb->index = markid = -2;
  2144. else if(t == regex_constants::syntax_equal)
  2145. pb->index = markid = -1;
  2146. else
  2147. {
  2148. // Probably a named capture which also starts (?< :
  2149. name_delim = '>';
  2150. --m_position;
  2151. goto named_capture_jump;
  2152. }
  2153. ++m_position;
  2154. jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
  2155. this->append_state(syntax_element_backstep, sizeof(re_brace));
  2156. this->m_pdata->m_data.align();
  2157. m_alt_insert_point = this->m_pdata->m_data.size();
  2158. break;
  2159. }
  2160. case regex_constants::escape_type_right_word:
  2161. //
  2162. // an independent sub-expression:
  2163. //
  2164. pb->index = markid = -3;
  2165. ++m_position;
  2166. jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
  2167. this->m_pdata->m_data.align();
  2168. m_alt_insert_point = this->m_pdata->m_data.size();
  2169. break;
  2170. case regex_constants::syntax_open_mark:
  2171. {
  2172. // a conditional expression:
  2173. pb->index = markid = -4;
  2174. if(++m_position == m_end)
  2175. {
  2176. // Rewind to start of (? sequence:
  2177. --m_position;
  2178. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2179. fail(regex_constants::error_perl_extension, m_position - m_base);
  2180. return false;
  2181. }
  2182. v = this->m_traits.toi(m_position, m_end, 10);
  2183. if(m_position == m_end)
  2184. {
  2185. // Rewind to start of (? sequence:
  2186. --m_position;
  2187. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2188. fail(regex_constants::error_perl_extension, m_position - m_base);
  2189. return false;
  2190. }
  2191. if(*m_position == charT('R'))
  2192. {
  2193. if(++m_position == m_end)
  2194. {
  2195. // Rewind to start of (? sequence:
  2196. --m_position;
  2197. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2198. fail(regex_constants::error_perl_extension, m_position - m_base);
  2199. return false;
  2200. }
  2201. if(*m_position == charT('&'))
  2202. {
  2203. const charT* base = ++m_position;
  2204. while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
  2205. ++m_position;
  2206. if(m_position == m_end)
  2207. {
  2208. // Rewind to start of (? sequence:
  2209. --m_position;
  2210. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2211. fail(regex_constants::error_perl_extension, m_position - m_base);
  2212. return false;
  2213. }
  2214. v = -static_cast<int>(hash_value_from_capture_name(base, m_position));
  2215. }
  2216. else
  2217. {
  2218. v = -this->m_traits.toi(m_position, m_end, 10);
  2219. }
  2220. re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
  2221. br->index = v < 0 ? (v - 1) : 0;
  2222. if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
  2223. {
  2224. // Rewind to start of (? sequence:
  2225. --m_position;
  2226. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2227. fail(regex_constants::error_perl_extension, m_position - m_base);
  2228. return false;
  2229. }
  2230. if(++m_position == m_end)
  2231. {
  2232. // Rewind to start of (? sequence:
  2233. --m_position;
  2234. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2235. fail(regex_constants::error_perl_extension, m_position - m_base);
  2236. return false;
  2237. }
  2238. }
  2239. else if((*m_position == charT('\'')) || (*m_position == charT('<')))
  2240. {
  2241. const charT* base = ++m_position;
  2242. while((m_position != m_end) && (*m_position != charT('>')) && (*m_position != charT('\'')))
  2243. ++m_position;
  2244. if(m_position == m_end)
  2245. {
  2246. // Rewind to start of (? sequence:
  2247. --m_position;
  2248. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2249. fail(regex_constants::error_perl_extension, m_position - m_base);
  2250. return false;
  2251. }
  2252. v = static_cast<int>(hash_value_from_capture_name(base, m_position));
  2253. re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
  2254. br->index = v;
  2255. if(((*m_position != charT('>')) && (*m_position != charT('\''))) || (++m_position == m_end))
  2256. {
  2257. // Rewind to start of (? sequence:
  2258. --m_position;
  2259. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2260. fail(regex_constants::error_perl_extension, m_position - m_base, "Unterminated named capture.");
  2261. return false;
  2262. }
  2263. if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
  2264. {
  2265. // Rewind to start of (? sequence:
  2266. --m_position;
  2267. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2268. fail(regex_constants::error_perl_extension, m_position - m_base);
  2269. return false;
  2270. }
  2271. if(++m_position == m_end)
  2272. {
  2273. // Rewind to start of (? sequence:
  2274. --m_position;
  2275. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2276. fail(regex_constants::error_perl_extension, m_position - m_base);
  2277. return false;
  2278. }
  2279. }
  2280. else if(*m_position == charT('D'))
  2281. {
  2282. const char* def = "DEFINE";
  2283. while(*def && (m_position != m_end) && (*m_position == charT(*def)))
  2284. ++m_position, ++def;
  2285. if((m_position == m_end) || *def)
  2286. {
  2287. // Rewind to start of (? sequence:
  2288. --m_position;
  2289. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2290. fail(regex_constants::error_perl_extension, m_position - m_base);
  2291. return false;
  2292. }
  2293. re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
  2294. br->index = 9999; // special magic value!
  2295. if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
  2296. {
  2297. // Rewind to start of (? sequence:
  2298. --m_position;
  2299. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2300. fail(regex_constants::error_perl_extension, m_position - m_base);
  2301. return false;
  2302. }
  2303. if(++m_position == m_end)
  2304. {
  2305. // Rewind to start of (? sequence:
  2306. --m_position;
  2307. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2308. fail(regex_constants::error_perl_extension, m_position - m_base);
  2309. return false;
  2310. }
  2311. }
  2312. else if(v > 0)
  2313. {
  2314. re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
  2315. br->index = v;
  2316. if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
  2317. {
  2318. // Rewind to start of (? sequence:
  2319. --m_position;
  2320. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2321. fail(regex_constants::error_perl_extension, m_position - m_base);
  2322. return false;
  2323. }
  2324. if(++m_position == m_end)
  2325. {
  2326. // Rewind to start of (? sequence:
  2327. --m_position;
  2328. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2329. fail(regex_constants::error_perl_extension, m_position - m_base);
  2330. return false;
  2331. }
  2332. }
  2333. else
  2334. {
  2335. // verify that we have a lookahead or lookbehind assert:
  2336. if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_question)
  2337. {
  2338. // Rewind to start of (? sequence:
  2339. --m_position;
  2340. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2341. fail(regex_constants::error_perl_extension, m_position - m_base);
  2342. return false;
  2343. }
  2344. if(++m_position == m_end)
  2345. {
  2346. // Rewind to start of (? sequence:
  2347. --m_position;
  2348. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2349. fail(regex_constants::error_perl_extension, m_position - m_base);
  2350. return false;
  2351. }
  2352. if(this->m_traits.syntax_type(*m_position) == regex_constants::escape_type_left_word)
  2353. {
  2354. if(++m_position == m_end)
  2355. {
  2356. // Rewind to start of (? sequence:
  2357. --m_position;
  2358. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2359. fail(regex_constants::error_perl_extension, m_position - m_base);
  2360. return false;
  2361. }
  2362. if((this->m_traits.syntax_type(*m_position) != regex_constants::syntax_equal)
  2363. && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_not))
  2364. {
  2365. // Rewind to start of (? sequence:
  2366. --m_position;
  2367. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2368. fail(regex_constants::error_perl_extension, m_position - m_base);
  2369. return false;
  2370. }
  2371. m_position -= 3;
  2372. }
  2373. else
  2374. {
  2375. if((this->m_traits.syntax_type(*m_position) != regex_constants::syntax_equal)
  2376. && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_not))
  2377. {
  2378. // Rewind to start of (? sequence:
  2379. --m_position;
  2380. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2381. fail(regex_constants::error_perl_extension, m_position - m_base);
  2382. return false;
  2383. }
  2384. m_position -= 2;
  2385. }
  2386. }
  2387. break;
  2388. }
  2389. case regex_constants::syntax_close_mark:
  2390. // Rewind to start of (? sequence:
  2391. --m_position;
  2392. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2393. fail(regex_constants::error_perl_extension, m_position - m_base);
  2394. return false;
  2395. case regex_constants::escape_type_end_buffer:
  2396. {
  2397. name_delim = *m_position;
  2398. named_capture_jump:
  2399. markid = 0;
  2400. if(0 == (this->flags() & regbase::nosubs))
  2401. {
  2402. markid = ++m_mark_count;
  2403. #ifndef BOOST_NO_STD_DISTANCE
  2404. if(this->flags() & regbase::save_subexpression_location)
  2405. this->m_pdata->m_subs.push_back(std::pair<std::size_t, std::size_t>(std::distance(m_base, m_position) - 2, 0));
  2406. #else
  2407. if(this->flags() & regbase::save_subexpression_location)
  2408. this->m_pdata->m_subs.push_back(std::pair<std::size_t, std::size_t>((m_position - m_base) - 2, 0));
  2409. #endif
  2410. }
  2411. pb->index = markid;
  2412. const charT* base = ++m_position;
  2413. if(m_position == m_end)
  2414. {
  2415. // Rewind to start of (? sequence:
  2416. --m_position;
  2417. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2418. fail(regex_constants::error_perl_extension, m_position - m_base);
  2419. return false;
  2420. }
  2421. while((m_position != m_end) && (*m_position != name_delim))
  2422. ++m_position;
  2423. if(m_position == m_end)
  2424. {
  2425. // Rewind to start of (? sequence:
  2426. --m_position;
  2427. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2428. fail(regex_constants::error_perl_extension, m_position - m_base);
  2429. return false;
  2430. }
  2431. this->m_pdata->set_name(base, m_position, markid);
  2432. ++m_position;
  2433. break;
  2434. }
  2435. default:
  2436. if(*m_position == charT('R'))
  2437. {
  2438. ++m_position;
  2439. v = 0;
  2440. if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
  2441. {
  2442. // Rewind to start of (? sequence:
  2443. --m_position;
  2444. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2445. fail(regex_constants::error_perl_extension, m_position - m_base);
  2446. return false;
  2447. }
  2448. goto insert_recursion;
  2449. }
  2450. if(*m_position == charT('&'))
  2451. {
  2452. ++m_position;
  2453. const charT* base = m_position;
  2454. while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
  2455. ++m_position;
  2456. if(m_position == m_end)
  2457. {
  2458. // Rewind to start of (? sequence:
  2459. --m_position;
  2460. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2461. fail(regex_constants::error_perl_extension, m_position - m_base);
  2462. return false;
  2463. }
  2464. v = static_cast<int>(hash_value_from_capture_name(base, m_position));
  2465. goto insert_recursion;
  2466. }
  2467. if(*m_position == charT('P'))
  2468. {
  2469. ++m_position;
  2470. if(m_position == m_end)
  2471. {
  2472. // Rewind to start of (? sequence:
  2473. --m_position;
  2474. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2475. fail(regex_constants::error_perl_extension, m_position - m_base);
  2476. return false;
  2477. }
  2478. if(*m_position == charT('>'))
  2479. {
  2480. ++m_position;
  2481. const charT* base = m_position;
  2482. while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
  2483. ++m_position;
  2484. if(m_position == m_end)
  2485. {
  2486. // Rewind to start of (? sequence:
  2487. --m_position;
  2488. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2489. fail(regex_constants::error_perl_extension, m_position - m_base);
  2490. return false;
  2491. }
  2492. v = static_cast<int>(hash_value_from_capture_name(base, m_position));
  2493. goto insert_recursion;
  2494. }
  2495. }
  2496. //
  2497. // lets assume that we have a (?imsx) group and try and parse it:
  2498. //
  2499. option_group_jump:
  2500. regex_constants::syntax_option_type opts = parse_options();
  2501. if(m_position == m_end)
  2502. {
  2503. // Rewind to start of (? sequence:
  2504. --m_position;
  2505. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2506. fail(regex_constants::error_perl_extension, m_position - m_base);
  2507. return false;
  2508. }
  2509. // make a note of whether we have a case change:
  2510. m_has_case_change = ((opts & regbase::icase) != (this->flags() & regbase::icase));
  2511. pb->index = markid = 0;
  2512. if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_mark)
  2513. {
  2514. // update flags and carry on as normal:
  2515. this->flags(opts);
  2516. restore_flags = false;
  2517. old_case_change |= m_has_case_change; // defer end of scope by one ')'
  2518. }
  2519. else if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_colon)
  2520. {
  2521. // update flags and carry on until the matching ')' is found:
  2522. this->flags(opts);
  2523. ++m_position;
  2524. }
  2525. else
  2526. {
  2527. // Rewind to start of (? sequence:
  2528. --m_position;
  2529. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2530. fail(regex_constants::error_perl_extension, m_position - m_base);
  2531. return false;
  2532. }
  2533. // finally append a case change state if we need it:
  2534. if(m_has_case_change)
  2535. {
  2536. static_cast<re_case*>(
  2537. this->append_state(syntax_element_toggle_case, sizeof(re_case))
  2538. )->icase = opts & regbase::icase;
  2539. }
  2540. }
  2541. //
  2542. // now recursively add more states, this will terminate when we get to a
  2543. // matching ')' :
  2544. //
  2545. parse_all();
  2546. //
  2547. // Unwind alternatives:
  2548. //
  2549. if(0 == unwind_alts(last_paren_start))
  2550. {
  2551. // Rewind to start of (? sequence:
  2552. --m_position;
  2553. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2554. fail(regex_constants::error_perl_extension, m_position - m_base, "Invalid alternation operators within (?...) block.");
  2555. return false;
  2556. }
  2557. //
  2558. // we either have a ')' or we have run out of characters prematurely:
  2559. //
  2560. if(m_position == m_end)
  2561. {
  2562. // Rewind to start of (? sequence:
  2563. --m_position;
  2564. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2565. this->fail(regex_constants::error_paren, ::boost::BOOST_REGEX_DETAIL_NS::distance(m_base, m_end));
  2566. return false;
  2567. }
  2568. BOOST_REGEX_ASSERT(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_mark);
  2569. ++m_position;
  2570. //
  2571. // restore the flags:
  2572. //
  2573. if(restore_flags)
  2574. {
  2575. // append a case change state if we need it:
  2576. if(m_has_case_change)
  2577. {
  2578. static_cast<re_case*>(
  2579. this->append_state(syntax_element_toggle_case, sizeof(re_case))
  2580. )->icase = old_flags & regbase::icase;
  2581. }
  2582. this->flags(old_flags);
  2583. }
  2584. //
  2585. // set up the jump pointer if we have one:
  2586. //
  2587. if(jump_offset)
  2588. {
  2589. this->m_pdata->m_data.align();
  2590. re_jump* jmp = static_cast<re_jump*>(this->getaddress(jump_offset));
  2591. jmp->alt.i = this->m_pdata->m_data.size() - this->getoffset(jmp);
  2592. if((this->m_last_state == jmp) && (markid != -2))
  2593. {
  2594. // Oops... we didn't have anything inside the assertion.
  2595. // Note we don't get here for negated forward lookahead as (?!)
  2596. // does have some uses.
  2597. // Rewind to start of (? sequence:
  2598. --m_position;
  2599. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2600. fail(regex_constants::error_perl_extension, m_position - m_base, "Invalid or empty zero width assertion.");
  2601. return false;
  2602. }
  2603. }
  2604. //
  2605. // verify that if this is conditional expression, that we do have
  2606. // an alternative, if not add one:
  2607. //
  2608. if(markid == -4)
  2609. {
  2610. re_syntax_base* b = this->getaddress(expected_alt_point);
  2611. // Make sure we have exactly one alternative following this state:
  2612. if(b->type != syntax_element_alt)
  2613. {
  2614. re_alt* alt = static_cast<re_alt*>(this->insert_state(expected_alt_point, syntax_element_alt, sizeof(re_alt)));
  2615. alt->alt.i = this->m_pdata->m_data.size() - this->getoffset(alt);
  2616. }
  2617. else if(((std::ptrdiff_t)this->m_pdata->m_data.size() > (static_cast<re_alt*>(b)->alt.i + this->getoffset(b))) && (static_cast<re_alt*>(b)->alt.i > 0) && this->getaddress(static_cast<re_alt*>(b)->alt.i, b)->type == syntax_element_alt)
  2618. {
  2619. // Can't have seen more than one alternative:
  2620. // Rewind to start of (? sequence:
  2621. --m_position;
  2622. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2623. fail(regex_constants::error_bad_pattern, m_position - m_base, "More than one alternation operator | was encountered inside a conditional expression.");
  2624. return false;
  2625. }
  2626. else
  2627. {
  2628. // We must *not* have seen an alternative inside a (DEFINE) block:
  2629. b = this->getaddress(b->next.i, b);
  2630. if((b->type == syntax_element_assert_backref) && (static_cast<re_brace*>(b)->index == 9999))
  2631. {
  2632. // Rewind to start of (? sequence:
  2633. --m_position;
  2634. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2635. fail(regex_constants::error_bad_pattern, m_position - m_base, "Alternation operators are not allowed inside a DEFINE block.");
  2636. return false;
  2637. }
  2638. }
  2639. // check for invalid repetition of next state:
  2640. b = this->getaddress(expected_alt_point);
  2641. b = this->getaddress(static_cast<re_alt*>(b)->next.i, b);
  2642. if((b->type != syntax_element_assert_backref)
  2643. && (b->type != syntax_element_startmark))
  2644. {
  2645. // Rewind to start of (? sequence:
  2646. --m_position;
  2647. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2648. fail(regex_constants::error_badrepeat, m_position - m_base, "A repetition operator cannot be applied to a zero-width assertion.");
  2649. return false;
  2650. }
  2651. }
  2652. //
  2653. // append closing parenthesis state:
  2654. //
  2655. pb = static_cast<re_brace*>(this->append_state(syntax_element_endmark, sizeof(re_brace)));
  2656. pb->index = markid;
  2657. pb->icase = this->flags() & regbase::icase;
  2658. this->m_paren_start = last_paren_start;
  2659. //
  2660. // restore the alternate insertion point:
  2661. //
  2662. this->m_alt_insert_point = last_alt_point;
  2663. //
  2664. // and the case change data:
  2665. //
  2666. m_has_case_change = old_case_change;
  2667. //
  2668. // And the mark_reset data:
  2669. //
  2670. if(m_max_mark > m_mark_count)
  2671. {
  2672. m_mark_count = m_max_mark;
  2673. }
  2674. m_mark_reset = mark_reset;
  2675. m_max_mark = max_mark;
  2676. if(markid > 0)
  2677. {
  2678. #ifndef BOOST_NO_STD_DISTANCE
  2679. if(this->flags() & regbase::save_subexpression_location)
  2680. this->m_pdata->m_subs.at((std::size_t)markid - 1).second = std::distance(m_base, m_position) - 1;
  2681. #else
  2682. if(this->flags() & regbase::save_subexpression_location)
  2683. this->m_pdata->m_subs.at(markid - 1).second = (m_position - m_base) - 1;
  2684. #endif
  2685. //
  2686. // allow backrefs to this mark:
  2687. //
  2688. this->m_backrefs.set(markid);
  2689. }
  2690. return true;
  2691. }
  2692. template <class charT, class traits>
  2693. bool basic_regex_parser<charT, traits>::match_verb(const char* verb)
  2694. {
  2695. while(*verb)
  2696. {
  2697. if(static_cast<charT>(*verb) != *m_position)
  2698. {
  2699. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2700. fail(regex_constants::error_perl_extension, m_position - m_base);
  2701. return false;
  2702. }
  2703. if(++m_position == m_end)
  2704. {
  2705. --m_position;
  2706. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2707. fail(regex_constants::error_perl_extension, m_position - m_base);
  2708. return false;
  2709. }
  2710. ++verb;
  2711. }
  2712. return true;
  2713. }
  2714. #ifdef BOOST_MSVC
  2715. # pragma warning(push)
  2716. #if BOOST_MSVC >= 1800
  2717. #pragma warning(disable:26812)
  2718. #endif
  2719. #endif
  2720. template <class charT, class traits>
  2721. bool basic_regex_parser<charT, traits>::parse_perl_verb()
  2722. {
  2723. if(++m_position == m_end)
  2724. {
  2725. // Rewind to start of (* sequence:
  2726. --m_position;
  2727. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2728. fail(regex_constants::error_perl_extension, m_position - m_base);
  2729. return false;
  2730. }
  2731. switch(*m_position)
  2732. {
  2733. case 'F':
  2734. if(++m_position == m_end)
  2735. {
  2736. // Rewind to start of (* sequence:
  2737. --m_position;
  2738. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2739. fail(regex_constants::error_perl_extension, m_position - m_base);
  2740. return false;
  2741. }
  2742. if((this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_mark) || match_verb("AIL"))
  2743. {
  2744. if((m_position == m_end) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
  2745. {
  2746. // Rewind to start of (* sequence:
  2747. --m_position;
  2748. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2749. fail(regex_constants::error_perl_extension, m_position - m_base);
  2750. return false;
  2751. }
  2752. ++m_position;
  2753. this->append_state(syntax_element_fail);
  2754. return true;
  2755. }
  2756. break;
  2757. case 'A':
  2758. if(++m_position == m_end)
  2759. {
  2760. // Rewind to start of (* sequence:
  2761. --m_position;
  2762. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2763. fail(regex_constants::error_perl_extension, m_position - m_base);
  2764. return false;
  2765. }
  2766. if(match_verb("CCEPT"))
  2767. {
  2768. if((m_position == m_end) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
  2769. {
  2770. // Rewind to start of (* sequence:
  2771. --m_position;
  2772. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2773. fail(regex_constants::error_perl_extension, m_position - m_base);
  2774. return false;
  2775. }
  2776. ++m_position;
  2777. this->append_state(syntax_element_accept);
  2778. return true;
  2779. }
  2780. break;
  2781. case 'C':
  2782. if(++m_position == m_end)
  2783. {
  2784. // Rewind to start of (* sequence:
  2785. --m_position;
  2786. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2787. fail(regex_constants::error_perl_extension, m_position - m_base);
  2788. return false;
  2789. }
  2790. if(match_verb("OMMIT"))
  2791. {
  2792. if((m_position == m_end) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
  2793. {
  2794. // Rewind to start of (* sequence:
  2795. --m_position;
  2796. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2797. fail(regex_constants::error_perl_extension, m_position - m_base);
  2798. return false;
  2799. }
  2800. ++m_position;
  2801. static_cast<re_commit*>(this->append_state(syntax_element_commit, sizeof(re_commit)))->action = commit_commit;
  2802. this->m_pdata->m_disable_match_any = true;
  2803. return true;
  2804. }
  2805. break;
  2806. case 'P':
  2807. if(++m_position == m_end)
  2808. {
  2809. // Rewind to start of (* sequence:
  2810. --m_position;
  2811. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2812. fail(regex_constants::error_perl_extension, m_position - m_base);
  2813. return false;
  2814. }
  2815. if(match_verb("RUNE"))
  2816. {
  2817. if((m_position == m_end) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
  2818. {
  2819. // Rewind to start of (* sequence:
  2820. --m_position;
  2821. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2822. fail(regex_constants::error_perl_extension, m_position - m_base);
  2823. return false;
  2824. }
  2825. ++m_position;
  2826. static_cast<re_commit*>(this->append_state(syntax_element_commit, sizeof(re_commit)))->action = commit_prune;
  2827. this->m_pdata->m_disable_match_any = true;
  2828. return true;
  2829. }
  2830. break;
  2831. case 'S':
  2832. if(++m_position == m_end)
  2833. {
  2834. // Rewind to start of (* sequence:
  2835. --m_position;
  2836. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2837. fail(regex_constants::error_perl_extension, m_position - m_base);
  2838. return false;
  2839. }
  2840. if(match_verb("KIP"))
  2841. {
  2842. if((m_position == m_end) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
  2843. {
  2844. // Rewind to start of (* sequence:
  2845. --m_position;
  2846. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2847. fail(regex_constants::error_perl_extension, m_position - m_base);
  2848. return false;
  2849. }
  2850. ++m_position;
  2851. static_cast<re_commit*>(this->append_state(syntax_element_commit, sizeof(re_commit)))->action = commit_skip;
  2852. this->m_pdata->m_disable_match_any = true;
  2853. return true;
  2854. }
  2855. break;
  2856. case 'T':
  2857. if(++m_position == m_end)
  2858. {
  2859. // Rewind to start of (* sequence:
  2860. --m_position;
  2861. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2862. fail(regex_constants::error_perl_extension, m_position - m_base);
  2863. return false;
  2864. }
  2865. if(match_verb("HEN"))
  2866. {
  2867. if((m_position == m_end) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
  2868. {
  2869. // Rewind to start of (* sequence:
  2870. --m_position;
  2871. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2872. fail(regex_constants::error_perl_extension, m_position - m_base);
  2873. return false;
  2874. }
  2875. ++m_position;
  2876. this->append_state(syntax_element_then);
  2877. this->m_pdata->m_disable_match_any = true;
  2878. return true;
  2879. }
  2880. break;
  2881. }
  2882. // Rewind to start of (* sequence:
  2883. --m_position;
  2884. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  2885. fail(regex_constants::error_perl_extension, m_position - m_base);
  2886. return false;
  2887. }
  2888. #ifdef BOOST_MSVC
  2889. # pragma warning(pop)
  2890. #endif
  2891. template <class charT, class traits>
  2892. bool basic_regex_parser<charT, traits>::add_emacs_code(bool negate)
  2893. {
  2894. //
  2895. // parses an emacs style \sx or \Sx construct.
  2896. //
  2897. if(++m_position == m_end)
  2898. {
  2899. // Rewind to start of sequence:
  2900. --m_position;
  2901. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
  2902. fail(regex_constants::error_escape, m_position - m_base);
  2903. return false;
  2904. }
  2905. basic_char_set<charT, traits> char_set;
  2906. if(negate)
  2907. char_set.negate();
  2908. static const charT s_punct[5] = { 'p', 'u', 'n', 'c', 't', };
  2909. switch(*m_position)
  2910. {
  2911. case 's':
  2912. case ' ':
  2913. char_set.add_class(this->m_mask_space);
  2914. break;
  2915. case 'w':
  2916. char_set.add_class(this->m_word_mask);
  2917. break;
  2918. case '_':
  2919. char_set.add_single(digraph<charT>(charT('$')));
  2920. char_set.add_single(digraph<charT>(charT('&')));
  2921. char_set.add_single(digraph<charT>(charT('*')));
  2922. char_set.add_single(digraph<charT>(charT('+')));
  2923. char_set.add_single(digraph<charT>(charT('-')));
  2924. char_set.add_single(digraph<charT>(charT('_')));
  2925. char_set.add_single(digraph<charT>(charT('<')));
  2926. char_set.add_single(digraph<charT>(charT('>')));
  2927. break;
  2928. case '.':
  2929. char_set.add_class(this->m_traits.lookup_classname(s_punct, s_punct+5));
  2930. break;
  2931. case '(':
  2932. char_set.add_single(digraph<charT>(charT('(')));
  2933. char_set.add_single(digraph<charT>(charT('[')));
  2934. char_set.add_single(digraph<charT>(charT('{')));
  2935. break;
  2936. case ')':
  2937. char_set.add_single(digraph<charT>(charT(')')));
  2938. char_set.add_single(digraph<charT>(charT(']')));
  2939. char_set.add_single(digraph<charT>(charT('}')));
  2940. break;
  2941. case '"':
  2942. char_set.add_single(digraph<charT>(charT('"')));
  2943. char_set.add_single(digraph<charT>(charT('\'')));
  2944. char_set.add_single(digraph<charT>(charT('`')));
  2945. break;
  2946. case '\'':
  2947. char_set.add_single(digraph<charT>(charT('\'')));
  2948. char_set.add_single(digraph<charT>(charT(',')));
  2949. char_set.add_single(digraph<charT>(charT('#')));
  2950. break;
  2951. case '<':
  2952. char_set.add_single(digraph<charT>(charT(';')));
  2953. break;
  2954. case '>':
  2955. char_set.add_single(digraph<charT>(charT('\n')));
  2956. char_set.add_single(digraph<charT>(charT('\f')));
  2957. break;
  2958. default:
  2959. fail(regex_constants::error_ctype, m_position - m_base);
  2960. return false;
  2961. }
  2962. if(0 == this->append_set(char_set))
  2963. {
  2964. fail(regex_constants::error_ctype, m_position - m_base);
  2965. return false;
  2966. }
  2967. ++m_position;
  2968. return true;
  2969. }
  2970. template <class charT, class traits>
  2971. regex_constants::syntax_option_type basic_regex_parser<charT, traits>::parse_options()
  2972. {
  2973. // we have a (?imsx-imsx) group, convert it into a set of flags:
  2974. regex_constants::syntax_option_type f = this->flags();
  2975. bool breakout = false;
  2976. do
  2977. {
  2978. switch(*m_position)
  2979. {
  2980. case 's':
  2981. f |= regex_constants::mod_s;
  2982. f &= ~regex_constants::no_mod_s;
  2983. break;
  2984. case 'm':
  2985. f &= ~regex_constants::no_mod_m;
  2986. break;
  2987. case 'i':
  2988. f |= regex_constants::icase;
  2989. break;
  2990. case 'x':
  2991. f |= regex_constants::mod_x;
  2992. break;
  2993. default:
  2994. breakout = true;
  2995. continue;
  2996. }
  2997. if(++m_position == m_end)
  2998. {
  2999. // Rewind to start of (? sequence:
  3000. --m_position;
  3001. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  3002. fail(regex_constants::error_paren, m_position - m_base);
  3003. return false;
  3004. }
  3005. }
  3006. while(!breakout);
  3007. breakout = false;
  3008. if(*m_position == static_cast<charT>('-'))
  3009. {
  3010. if(++m_position == m_end)
  3011. {
  3012. // Rewind to start of (? sequence:
  3013. --m_position;
  3014. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  3015. fail(regex_constants::error_paren, m_position - m_base);
  3016. return false;
  3017. }
  3018. do
  3019. {
  3020. switch(*m_position)
  3021. {
  3022. case 's':
  3023. f &= ~regex_constants::mod_s;
  3024. f |= regex_constants::no_mod_s;
  3025. break;
  3026. case 'm':
  3027. f |= regex_constants::no_mod_m;
  3028. break;
  3029. case 'i':
  3030. f &= ~regex_constants::icase;
  3031. break;
  3032. case 'x':
  3033. f &= ~regex_constants::mod_x;
  3034. break;
  3035. default:
  3036. breakout = true;
  3037. continue;
  3038. }
  3039. if(++m_position == m_end)
  3040. {
  3041. // Rewind to start of (? sequence:
  3042. --m_position;
  3043. while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
  3044. fail(regex_constants::error_paren, m_position - m_base);
  3045. return false;
  3046. }
  3047. }
  3048. while(!breakout);
  3049. }
  3050. return f;
  3051. }
  3052. template <class charT, class traits>
  3053. bool basic_regex_parser<charT, traits>::unwind_alts(std::ptrdiff_t last_paren_start)
  3054. {
  3055. //
  3056. // If we didn't actually add any states after the last
  3057. // alternative then that's an error:
  3058. //
  3059. if((this->m_alt_insert_point == static_cast<std::ptrdiff_t>(this->m_pdata->m_data.size()))
  3060. && (!m_alt_jumps.empty()) && (m_alt_jumps.back() > last_paren_start)
  3061. &&
  3062. !(
  3063. ((this->flags() & regbase::main_option_type) == regbase::perl_syntax_group)
  3064. &&
  3065. ((this->flags() & regbase::no_empty_expressions) == 0)
  3066. )
  3067. )
  3068. {
  3069. fail(regex_constants::error_empty, this->m_position - this->m_base, "Can't terminate a sub-expression with an alternation operator |.");
  3070. return false;
  3071. }
  3072. //
  3073. // Fix up our alternatives:
  3074. //
  3075. while((!m_alt_jumps.empty()) && (m_alt_jumps.back() > last_paren_start))
  3076. {
  3077. //
  3078. // fix up the jump to point to the end of the states
  3079. // that we've just added:
  3080. //
  3081. std::ptrdiff_t jump_offset = m_alt_jumps.back();
  3082. m_alt_jumps.pop_back();
  3083. this->m_pdata->m_data.align();
  3084. re_jump* jmp = static_cast<re_jump*>(this->getaddress(jump_offset));
  3085. BOOST_REGEX_ASSERT(jmp->type == syntax_element_jump);
  3086. jmp->alt.i = this->m_pdata->m_data.size() - jump_offset;
  3087. }
  3088. return true;
  3089. }
  3090. #ifdef BOOST_MSVC
  3091. #pragma warning(pop)
  3092. #endif
  3093. } // namespace BOOST_REGEX_DETAIL_NS
  3094. } // namespace boost
  3095. #ifdef BOOST_MSVC
  3096. #pragma warning(push)
  3097. #pragma warning(disable: 4103)
  3098. #endif
  3099. #ifdef BOOST_HAS_ABI_HEADERS
  3100. # include BOOST_ABI_SUFFIX
  3101. #endif
  3102. #ifdef BOOST_MSVC
  3103. #pragma warning(pop)
  3104. #endif
  3105. #endif