basic_regex_parser.hpp 110 KB

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