RegexParser.cpp 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358
  1. /*
  2. * Copyright (c) 2020, Emanuel Sprung <emanuel.sprung@gmail.com>
  3. * Copyright (c) 2020-2021, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "RegexParser.h"
  8. #include "RegexDebug.h"
  9. #include <AK/AnyOf.h>
  10. #include <AK/CharacterTypes.h>
  11. #include <AK/GenericLexer.h>
  12. #include <AK/ScopeGuard.h>
  13. #include <AK/String.h>
  14. #include <AK/StringBuilder.h>
  15. #include <AK/StringUtils.h>
  16. #include <AK/TemporaryChange.h>
  17. #include <AK/Utf16View.h>
  18. #include <LibUnicode/CharacterTypes.h>
  19. namespace regex {
  20. static constexpr size_t s_maximum_repetition_count = 1024 * 1024;
  21. static constexpr u64 s_ecma262_maximum_repetition_count = (1ull << 53) - 1;
  22. static constexpr auto s_alphabetic_characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"sv;
  23. static constexpr auto s_decimal_characters = "0123456789"sv;
  24. static constexpr StringView identity_escape_characters(bool unicode, bool browser_extended)
  25. {
  26. if (unicode)
  27. return "^$\\.*+?()[]{}|/"sv;
  28. if (browser_extended)
  29. return "^$\\.*+?()[|"sv;
  30. return "^$\\.*+?()[]{}|"sv;
  31. }
  32. ALWAYS_INLINE bool Parser::set_error(Error error)
  33. {
  34. if (m_parser_state.error == Error::NoError) {
  35. m_parser_state.error = error;
  36. m_parser_state.error_token = m_parser_state.current_token;
  37. }
  38. return false; // always return false, that eases the API usage (return set_error(...)) :^)
  39. }
  40. ALWAYS_INLINE bool Parser::done() const
  41. {
  42. return match(TokenType::Eof);
  43. }
  44. ALWAYS_INLINE bool Parser::match(TokenType type) const
  45. {
  46. return m_parser_state.current_token.type() == type;
  47. }
  48. ALWAYS_INLINE bool Parser::match(char ch) const
  49. {
  50. return m_parser_state.current_token.type() == TokenType::Char && m_parser_state.current_token.value().length() == 1 && m_parser_state.current_token.value()[0] == ch;
  51. }
  52. ALWAYS_INLINE Token Parser::consume()
  53. {
  54. auto old_token = m_parser_state.current_token;
  55. m_parser_state.current_token = m_parser_state.lexer.next();
  56. return old_token;
  57. }
  58. ALWAYS_INLINE Token Parser::consume(TokenType type, Error error)
  59. {
  60. if (m_parser_state.current_token.type() != type) {
  61. set_error(error);
  62. dbgln_if(REGEX_DEBUG, "[PARSER] Error: Unexpected token {}. Expected: {}", m_parser_state.current_token.name(), Token::name(type));
  63. }
  64. return consume();
  65. }
  66. ALWAYS_INLINE bool Parser::consume(String const& str)
  67. {
  68. size_t potentially_go_back { 1 };
  69. for (auto ch : str) {
  70. if (match(TokenType::Char)) {
  71. if (m_parser_state.current_token.value()[0] != ch) {
  72. m_parser_state.lexer.back(potentially_go_back);
  73. m_parser_state.current_token = m_parser_state.lexer.next();
  74. return false;
  75. }
  76. } else {
  77. m_parser_state.lexer.back(potentially_go_back);
  78. m_parser_state.current_token = m_parser_state.lexer.next();
  79. return false;
  80. }
  81. consume(TokenType::Char, Error::NoError);
  82. ++potentially_go_back;
  83. }
  84. return true;
  85. }
  86. ALWAYS_INLINE Optional<u32> Parser::consume_escaped_code_point(bool unicode)
  87. {
  88. if (match(TokenType::LeftCurly) && !unicode) {
  89. // In non-Unicode mode, this should be parsed as a repetition symbol (repeating the 'u').
  90. return static_cast<u32>('u');
  91. }
  92. m_parser_state.lexer.retreat(2 + !done()); // Go back to just before '\u' (+1 char, because we will have consumed an extra character)
  93. if (auto code_point_or_error = m_parser_state.lexer.consume_escaped_code_point(unicode); !code_point_or_error.is_error()) {
  94. m_parser_state.current_token = m_parser_state.lexer.next();
  95. return code_point_or_error.value();
  96. }
  97. if (!unicode) {
  98. // '\u' is allowed in non-unicode mode, just matches 'u'.
  99. return static_cast<u32>('u');
  100. }
  101. set_error(Error::InvalidPattern);
  102. return {};
  103. }
  104. ALWAYS_INLINE bool Parser::try_skip(StringView str)
  105. {
  106. if (str.starts_with(m_parser_state.current_token.value()))
  107. str = str.substring_view(m_parser_state.current_token.value().length(), str.length() - m_parser_state.current_token.value().length());
  108. else
  109. return false;
  110. size_t potentially_go_back { 0 };
  111. for (auto ch : str) {
  112. if (!m_parser_state.lexer.consume_specific(ch)) {
  113. m_parser_state.lexer.back(potentially_go_back);
  114. return false;
  115. }
  116. ++potentially_go_back;
  117. }
  118. m_parser_state.current_token = m_parser_state.lexer.next();
  119. return true;
  120. }
  121. ALWAYS_INLINE bool Parser::lookahead_any(StringView str)
  122. {
  123. return AK::any_of(str, [this](auto ch) { return match(ch); });
  124. }
  125. ALWAYS_INLINE unsigned char Parser::skip()
  126. {
  127. unsigned char ch;
  128. if (m_parser_state.current_token.value().length() == 1) {
  129. ch = m_parser_state.current_token.value()[0];
  130. } else {
  131. m_parser_state.lexer.back(m_parser_state.current_token.value().length());
  132. ch = m_parser_state.lexer.consume();
  133. }
  134. m_parser_state.current_token = m_parser_state.lexer.next();
  135. return ch;
  136. }
  137. ALWAYS_INLINE void Parser::back(size_t count)
  138. {
  139. m_parser_state.lexer.back(count);
  140. m_parser_state.current_token = m_parser_state.lexer.next();
  141. }
  142. ALWAYS_INLINE void Parser::reset()
  143. {
  144. m_parser_state.bytecode.clear();
  145. m_parser_state.lexer.reset();
  146. m_parser_state.current_token = m_parser_state.lexer.next();
  147. m_parser_state.error = Error::NoError;
  148. m_parser_state.error_token = { TokenType::Eof, 0, {} };
  149. m_parser_state.capture_group_minimum_lengths.clear();
  150. m_parser_state.capture_groups_count = 0;
  151. m_parser_state.named_capture_groups_count = 0;
  152. m_parser_state.named_capture_groups.clear();
  153. }
  154. Parser::Result Parser::parse(Optional<AllOptions> regex_options)
  155. {
  156. reset();
  157. if (regex_options.has_value())
  158. m_parser_state.regex_options = regex_options.value();
  159. if (parse_internal(m_parser_state.bytecode, m_parser_state.match_length_minimum))
  160. consume(TokenType::Eof, Error::InvalidPattern);
  161. else
  162. set_error(Error::InvalidPattern);
  163. dbgln_if(REGEX_DEBUG, "[PARSER] Produced bytecode with {} entries (opcodes + arguments)", m_parser_state.bytecode.size());
  164. return {
  165. move(m_parser_state.bytecode),
  166. move(m_parser_state.capture_groups_count),
  167. move(m_parser_state.named_capture_groups_count),
  168. move(m_parser_state.match_length_minimum),
  169. move(m_parser_state.error),
  170. move(m_parser_state.error_token),
  171. m_parser_state.named_capture_groups.keys(),
  172. m_parser_state.regex_options,
  173. };
  174. }
  175. ALWAYS_INLINE bool Parser::match_ordinary_characters()
  176. {
  177. // NOTE: This method must not be called during bracket and repetition parsing!
  178. // FIXME: Add assertion for that?
  179. auto type = m_parser_state.current_token.type();
  180. return (type == TokenType::Char
  181. || type == TokenType::Comma
  182. || type == TokenType::Slash
  183. || type == TokenType::EqualSign
  184. || type == TokenType::HyphenMinus
  185. || type == TokenType::Colon);
  186. }
  187. // =============================
  188. // Abstract Posix Parser
  189. // =============================
  190. ALWAYS_INLINE bool AbstractPosixParser::parse_bracket_expression(Vector<CompareTypeAndValuePair>& values, size_t& match_length_minimum)
  191. {
  192. for (; !done();) {
  193. if (match(TokenType::HyphenMinus)) {
  194. consume();
  195. if (values.is_empty() || (values.size() == 1 && values.last().type == CharacterCompareType::Inverse)) {
  196. // first in the bracket expression
  197. values.append({ CharacterCompareType::Char, (ByteCodeValueType)'-' });
  198. } else if (match(TokenType::RightBracket)) {
  199. // Last in the bracket expression
  200. values.append({ CharacterCompareType::Char, (ByteCodeValueType)'-' });
  201. } else if (values.last().type == CharacterCompareType::Char) {
  202. values.append({ CharacterCompareType::RangeExpressionDummy, 0 });
  203. if (done())
  204. return set_error(Error::MismatchingBracket);
  205. if (match(TokenType::HyphenMinus)) {
  206. consume();
  207. // Valid range, add ordinary character
  208. values.append({ CharacterCompareType::Char, (ByteCodeValueType)'-' });
  209. }
  210. } else {
  211. return set_error(Error::InvalidRange);
  212. }
  213. } else if (match(TokenType::Circumflex)) {
  214. auto t = consume();
  215. if (values.is_empty())
  216. values.append({ CharacterCompareType::Inverse, 0 });
  217. else
  218. values.append({ CharacterCompareType::Char, (ByteCodeValueType)*t.value().characters_without_null_termination() });
  219. } else if (match(TokenType::LeftBracket)) {
  220. consume();
  221. if (match(TokenType::Period)) {
  222. consume();
  223. // FIXME: Parse collating element, this is needed when we have locale support
  224. // This could have impact on length parameter, I guess.
  225. set_error(Error::InvalidCollationElement);
  226. consume(TokenType::Period, Error::InvalidCollationElement);
  227. consume(TokenType::RightBracket, Error::MismatchingBracket);
  228. } else if (match(TokenType::EqualSign)) {
  229. consume();
  230. // FIXME: Parse collating element, this is needed when we have locale support
  231. // This could have impact on length parameter, I guess.
  232. set_error(Error::InvalidCollationElement);
  233. consume(TokenType::EqualSign, Error::InvalidCollationElement);
  234. consume(TokenType::RightBracket, Error::MismatchingBracket);
  235. } else if (match(TokenType::Colon)) {
  236. consume();
  237. CharClass ch_class;
  238. // parse character class
  239. if (match(TokenType::Char)) {
  240. if (consume("alnum"))
  241. ch_class = CharClass::Alnum;
  242. else if (consume("alpha"))
  243. ch_class = CharClass::Alpha;
  244. else if (consume("blank"))
  245. ch_class = CharClass::Blank;
  246. else if (consume("cntrl"))
  247. ch_class = CharClass::Cntrl;
  248. else if (consume("digit"))
  249. ch_class = CharClass::Digit;
  250. else if (consume("graph"))
  251. ch_class = CharClass::Graph;
  252. else if (consume("lower"))
  253. ch_class = CharClass::Lower;
  254. else if (consume("print"))
  255. ch_class = CharClass::Print;
  256. else if (consume("punct"))
  257. ch_class = CharClass::Punct;
  258. else if (consume("space"))
  259. ch_class = CharClass::Space;
  260. else if (consume("upper"))
  261. ch_class = CharClass::Upper;
  262. else if (consume("xdigit"))
  263. ch_class = CharClass::Xdigit;
  264. else
  265. return set_error(Error::InvalidCharacterClass);
  266. values.append({ CharacterCompareType::CharClass, (ByteCodeValueType)ch_class });
  267. } else
  268. return set_error(Error::InvalidCharacterClass);
  269. // FIXME: we do not support locale specific character classes until locales are implemented
  270. consume(TokenType::Colon, Error::InvalidCharacterClass);
  271. consume(TokenType::RightBracket, Error::MismatchingBracket);
  272. } else {
  273. return set_error(Error::MismatchingBracket);
  274. }
  275. } else if (match(TokenType::RightBracket)) {
  276. if (values.is_empty() || (values.size() == 1 && values.last().type == CharacterCompareType::Inverse)) {
  277. // handle bracket as ordinary character
  278. values.append({ CharacterCompareType::Char, (ByteCodeValueType)*consume().value().characters_without_null_termination() });
  279. } else {
  280. // closing bracket expression
  281. break;
  282. }
  283. } else {
  284. values.append({ CharacterCompareType::Char, (ByteCodeValueType)skip() });
  285. }
  286. // check if range expression has to be completed...
  287. if (values.size() >= 3 && values.at(values.size() - 2).type == CharacterCompareType::RangeExpressionDummy) {
  288. if (values.last().type != CharacterCompareType::Char)
  289. return set_error(Error::InvalidRange);
  290. auto value2 = values.take_last();
  291. values.take_last(); // RangeExpressionDummy
  292. auto value1 = values.take_last();
  293. values.append({ CharacterCompareType::CharRange, static_cast<ByteCodeValueType>(CharRange { (u32)value1.value, (u32)value2.value }) });
  294. }
  295. }
  296. if (!values.is_empty()) {
  297. match_length_minimum = 1;
  298. if (values.first().type == CharacterCompareType::Inverse)
  299. match_length_minimum = 0;
  300. }
  301. return true;
  302. }
  303. // =============================
  304. // PosixBasic Parser
  305. // =============================
  306. bool PosixBasicParser::parse_internal(ByteCode& stack, size_t& match_length_minimum)
  307. {
  308. return parse_root(stack, match_length_minimum);
  309. }
  310. bool PosixBasicParser::parse_root(ByteCode& bytecode, size_t& match_length_minimum)
  311. {
  312. // basic_reg_exp : L_ANCHOR? RE_expression R_ANCHOR?
  313. if (match(TokenType::Circumflex)) {
  314. consume();
  315. bytecode.empend((ByteCodeValueType)OpCodeId::CheckBegin);
  316. }
  317. if (!parse_re_expression(bytecode, match_length_minimum))
  318. return false;
  319. if (match(TokenType::Dollar)) {
  320. consume();
  321. bytecode.empend((ByteCodeValueType)OpCodeId::CheckEnd);
  322. }
  323. return !has_error();
  324. }
  325. bool PosixBasicParser::parse_re_expression(ByteCode& bytecode, size_t& match_length_minimum)
  326. {
  327. // RE_expression : RE_expression? simple_RE
  328. while (!done()) {
  329. if (!parse_simple_re(bytecode, match_length_minimum))
  330. break;
  331. }
  332. return !has_error();
  333. }
  334. bool PosixBasicParser::parse_simple_re(ByteCode& bytecode, size_t& match_length_minimum)
  335. {
  336. // simple_RE : nondupl_RE RE_dupl_symbol?
  337. ByteCode simple_re_bytecode;
  338. size_t re_match_length_minimum = 0;
  339. if (!parse_nonduplicating_re(simple_re_bytecode, re_match_length_minimum))
  340. return false;
  341. // RE_dupl_symbol : '*' | Back_open_brace DUP_COUNT (',' DUP_COUNT?)? Back_close_brace
  342. if (match(TokenType::Asterisk)) {
  343. consume();
  344. ByteCode::transform_bytecode_repetition_any(simple_re_bytecode, true);
  345. } else if (try_skip("\\{"sv)) {
  346. auto read_number = [&]() -> Optional<size_t> {
  347. if (!match(TokenType::Char))
  348. return {};
  349. size_t value = 0;
  350. while (match(TokenType::Char)) {
  351. auto c = m_parser_state.current_token.value().substring_view(0, 1);
  352. auto c_value = c.to_uint();
  353. if (!c_value.has_value())
  354. break;
  355. value *= 10;
  356. value += *c_value;
  357. consume();
  358. }
  359. return value;
  360. };
  361. size_t min_limit;
  362. Optional<size_t> max_limit;
  363. if (auto limit = read_number(); !limit.has_value())
  364. return set_error(Error::InvalidRepetitionMarker);
  365. else
  366. min_limit = *limit;
  367. if (match(TokenType::Comma)) {
  368. consume();
  369. max_limit = read_number();
  370. }
  371. if (!try_skip("\\}"sv))
  372. return set_error(Error::MismatchingBrace);
  373. if (max_limit.value_or(min_limit) < min_limit)
  374. return set_error(Error::InvalidBraceContent);
  375. if (min_limit > s_maximum_repetition_count || (max_limit.has_value() && *max_limit > s_maximum_repetition_count))
  376. return set_error(Error::InvalidBraceContent);
  377. auto min_repetition_mark_id = m_parser_state.repetition_mark_count++;
  378. auto max_repetition_mark_id = m_parser_state.repetition_mark_count++;
  379. ByteCode::transform_bytecode_repetition_min_max(simple_re_bytecode, min_limit, max_limit, min_repetition_mark_id, max_repetition_mark_id, true);
  380. match_length_minimum += re_match_length_minimum * min_limit;
  381. } else {
  382. match_length_minimum += re_match_length_minimum;
  383. }
  384. bytecode.extend(move(simple_re_bytecode));
  385. return true;
  386. }
  387. bool PosixBasicParser::parse_nonduplicating_re(ByteCode& bytecode, size_t& match_length_minimum)
  388. {
  389. // nondupl_RE : one_char_or_coll_elem_RE | Back_open_paren RE_expression Back_close_paren | BACKREF
  390. if (try_skip("\\("sv)) {
  391. TemporaryChange change { m_current_capture_group_depth, m_current_capture_group_depth + 1 };
  392. // Max number of addressable capture groups is 10, let's just be lenient
  393. // and accept 20; anything past that is probably a silly pattern anyway.
  394. if (m_current_capture_group_depth > 20)
  395. return set_error(Error::InvalidPattern);
  396. ByteCode capture_bytecode;
  397. size_t capture_length_minimum = 0;
  398. auto capture_group_index = ++m_parser_state.capture_groups_count;
  399. if (!parse_re_expression(capture_bytecode, capture_length_minimum))
  400. return false;
  401. if (!try_skip("\\)"sv))
  402. return set_error(Error::MismatchingParen);
  403. match_length_minimum += capture_length_minimum;
  404. if (capture_group_index <= number_of_addressable_capture_groups) {
  405. m_capture_group_minimum_lengths[capture_group_index - 1] = capture_length_minimum;
  406. m_capture_group_seen[capture_group_index - 1] = true;
  407. bytecode.insert_bytecode_group_capture_left(capture_group_index);
  408. }
  409. bytecode.extend(capture_bytecode);
  410. if (capture_group_index <= number_of_addressable_capture_groups)
  411. bytecode.insert_bytecode_group_capture_right(capture_group_index);
  412. return true;
  413. }
  414. for (size_t i = 1; i < 10; ++i) {
  415. char backref_name[2] { '\\', '0' };
  416. backref_name[1] += i;
  417. if (try_skip({ backref_name, 2 })) {
  418. if (!m_capture_group_seen[i - 1])
  419. return set_error(Error::InvalidNumber);
  420. match_length_minimum += m_capture_group_minimum_lengths[i - 1];
  421. bytecode.insert_bytecode_compare_values({ { CharacterCompareType::Reference, (ByteCodeValueType)i } });
  422. return true;
  423. }
  424. }
  425. return parse_one_char_or_collation_element(bytecode, match_length_minimum);
  426. }
  427. bool PosixBasicParser::parse_one_char_or_collation_element(ByteCode& bytecode, size_t& match_length_minimum)
  428. {
  429. // one_char_or_coll_elem_RE : ORD_CHAR | QUOTED_CHAR | '.' | bracket_expression
  430. if (match(TokenType::Period)) {
  431. consume();
  432. bytecode.insert_bytecode_compare_values({ { CharacterCompareType::AnyChar, 0 } });
  433. match_length_minimum += 1;
  434. return true;
  435. }
  436. // Dollars are special if at the end of a pattern.
  437. if (match(TokenType::Dollar)) {
  438. consume();
  439. // If we are at the end of a pattern, emit an end check instruction.
  440. if (match(TokenType::Eof)) {
  441. bytecode.empend((ByteCodeValueType)OpCodeId::CheckEnd);
  442. return true;
  443. }
  444. // We are not at the end of the string, so we should roll back and continue as normal.
  445. back(2);
  446. }
  447. // None of these are special in BRE.
  448. if (match(TokenType::Char) || match(TokenType::Questionmark) || match(TokenType::RightParen) || match(TokenType::HyphenMinus)
  449. || match(TokenType::Circumflex) || match(TokenType::RightCurly) || match(TokenType::Comma) || match(TokenType::Colon)
  450. || match(TokenType::Dollar) || match(TokenType::EqualSign) || match(TokenType::LeftCurly) || match(TokenType::LeftParen)
  451. || match(TokenType::Pipe) || match(TokenType::Slash) || match(TokenType::RightBracket) || match(TokenType::RightParen)) {
  452. auto ch = consume().value()[0];
  453. bytecode.insert_bytecode_compare_values({ { CharacterCompareType::Char, (ByteCodeValueType)ch } });
  454. match_length_minimum += 1;
  455. return true;
  456. }
  457. if (match(TokenType::EscapeSequence)) {
  458. if (m_parser_state.current_token.value().is_one_of("\\)"sv, "\\}"sv, "\\("sv, "\\{"sv))
  459. return false;
  460. auto ch = consume().value()[1];
  461. bytecode.insert_bytecode_compare_values({ { CharacterCompareType::Char, (ByteCodeValueType)ch } });
  462. match_length_minimum += 1;
  463. return true;
  464. }
  465. Vector<CompareTypeAndValuePair> values;
  466. size_t bracket_minimum_length = 0;
  467. if (match(TokenType::LeftBracket)) {
  468. consume();
  469. if (!AbstractPosixParser::parse_bracket_expression(values, bracket_minimum_length))
  470. return false;
  471. consume(TokenType::RightBracket, Error::MismatchingBracket);
  472. if (!has_error())
  473. bytecode.insert_bytecode_compare_values(move(values));
  474. match_length_minimum += bracket_minimum_length;
  475. return !has_error();
  476. }
  477. return set_error(Error::InvalidPattern);
  478. }
  479. // =============================
  480. // PosixExtended Parser
  481. // =============================
  482. bool PosixExtendedParser::parse_internal(ByteCode& stack, size_t& match_length_minimum)
  483. {
  484. return parse_root(stack, match_length_minimum);
  485. }
  486. ALWAYS_INLINE bool PosixExtendedParser::match_repetition_symbol()
  487. {
  488. auto type = m_parser_state.current_token.type();
  489. return (type == TokenType::Asterisk
  490. || type == TokenType::Plus
  491. || type == TokenType::Questionmark
  492. || type == TokenType::LeftCurly);
  493. }
  494. ALWAYS_INLINE bool PosixExtendedParser::parse_repetition_symbol(ByteCode& bytecode_to_repeat, size_t& match_length_minimum)
  495. {
  496. if (match(TokenType::LeftCurly)) {
  497. consume();
  498. StringBuilder number_builder;
  499. while (match(TokenType::Char)) {
  500. number_builder.append(consume().value());
  501. }
  502. auto maybe_minimum = number_builder.build().to_uint();
  503. if (!maybe_minimum.has_value())
  504. return set_error(Error::InvalidBraceContent);
  505. auto minimum = maybe_minimum.value();
  506. match_length_minimum *= minimum;
  507. if (minimum > s_maximum_repetition_count)
  508. return set_error(Error::InvalidBraceContent);
  509. if (match(TokenType::Comma)) {
  510. consume();
  511. } else {
  512. auto repetition_mark_id = m_parser_state.repetition_mark_count++;
  513. ByteCode bytecode;
  514. bytecode.insert_bytecode_repetition_n(bytecode_to_repeat, minimum, repetition_mark_id);
  515. bytecode_to_repeat = move(bytecode);
  516. consume(TokenType::RightCurly, Error::MismatchingBrace);
  517. return !has_error();
  518. }
  519. Optional<u32> maybe_maximum {};
  520. number_builder.clear();
  521. while (match(TokenType::Char)) {
  522. number_builder.append(consume().value());
  523. }
  524. if (!number_builder.is_empty()) {
  525. auto value = number_builder.build().to_uint();
  526. if (!value.has_value() || minimum > value.value() || *value > s_maximum_repetition_count)
  527. return set_error(Error::InvalidBraceContent);
  528. maybe_maximum = value.value();
  529. }
  530. auto min_repetition_mark_id = m_parser_state.repetition_mark_count++;
  531. auto max_repetition_mark_id = m_parser_state.repetition_mark_count++;
  532. ByteCode::transform_bytecode_repetition_min_max(bytecode_to_repeat, minimum, maybe_maximum, min_repetition_mark_id, max_repetition_mark_id);
  533. consume(TokenType::RightCurly, Error::MismatchingBrace);
  534. return !has_error();
  535. }
  536. if (match(TokenType::Plus)) {
  537. consume();
  538. bool nongreedy = match(TokenType::Questionmark);
  539. if (nongreedy)
  540. consume();
  541. // Note: don't touch match_length_minimum, it's already correct
  542. ByteCode::transform_bytecode_repetition_min_one(bytecode_to_repeat, !nongreedy);
  543. return !has_error();
  544. }
  545. if (match(TokenType::Asterisk)) {
  546. consume();
  547. match_length_minimum = 0;
  548. bool nongreedy = match(TokenType::Questionmark);
  549. if (nongreedy)
  550. consume();
  551. ByteCode::transform_bytecode_repetition_any(bytecode_to_repeat, !nongreedy);
  552. return !has_error();
  553. }
  554. if (match(TokenType::Questionmark)) {
  555. consume();
  556. match_length_minimum = 0;
  557. bool nongreedy = match(TokenType::Questionmark);
  558. if (nongreedy)
  559. consume();
  560. ByteCode::transform_bytecode_repetition_zero_or_one(bytecode_to_repeat, !nongreedy);
  561. return !has_error();
  562. }
  563. return false;
  564. }
  565. ALWAYS_INLINE bool PosixExtendedParser::parse_bracket_expression(ByteCode& stack, size_t& match_length_minimum)
  566. {
  567. Vector<CompareTypeAndValuePair> values;
  568. if (!AbstractPosixParser::parse_bracket_expression(values, match_length_minimum))
  569. return false;
  570. if (!has_error())
  571. stack.insert_bytecode_compare_values(move(values));
  572. return !has_error();
  573. }
  574. ALWAYS_INLINE bool PosixExtendedParser::parse_sub_expression(ByteCode& stack, size_t& match_length_minimum)
  575. {
  576. ByteCode bytecode;
  577. size_t length = 0;
  578. bool should_parse_repetition_symbol { false };
  579. for (;;) {
  580. if (match_ordinary_characters()) {
  581. Token start_token = m_parser_state.current_token;
  582. Token last_token = m_parser_state.current_token;
  583. for (;;) {
  584. if (!match_ordinary_characters())
  585. break;
  586. ++length;
  587. last_token = consume();
  588. }
  589. if (length > 1) {
  590. // last character is inserted into 'bytecode' for duplication symbol handling
  591. auto new_length = length - ((match_repetition_symbol() && length > 1) ? 1 : 0);
  592. stack.insert_bytecode_compare_string({ start_token.value().characters_without_null_termination(), new_length });
  593. }
  594. if ((match_repetition_symbol() && length > 1) || length == 1) // Create own compare opcode for last character before duplication symbol
  595. bytecode.insert_bytecode_compare_values({ { CharacterCompareType::Char, (ByteCodeValueType)last_token.value().characters_without_null_termination()[0] } });
  596. should_parse_repetition_symbol = true;
  597. break;
  598. }
  599. if (match_repetition_symbol())
  600. return set_error(Error::InvalidRepetitionMarker);
  601. if (match(TokenType::Period)) {
  602. length = 1;
  603. consume();
  604. bytecode.insert_bytecode_compare_values({ { CharacterCompareType::AnyChar, 0 } });
  605. should_parse_repetition_symbol = true;
  606. break;
  607. }
  608. if (match(TokenType::EscapeSequence)) {
  609. length = 1;
  610. Token t = consume();
  611. dbgln_if(REGEX_DEBUG, "[PARSER] EscapeSequence with substring {}", t.value());
  612. bytecode.insert_bytecode_compare_values({ { CharacterCompareType::Char, (u32)t.value().characters_without_null_termination()[1] } });
  613. should_parse_repetition_symbol = true;
  614. break;
  615. }
  616. if (match(TokenType::LeftBracket)) {
  617. consume();
  618. ByteCode sub_ops;
  619. if (!parse_bracket_expression(sub_ops, length) || !sub_ops.size())
  620. return set_error(Error::InvalidBracketContent);
  621. bytecode.extend(move(sub_ops));
  622. consume(TokenType::RightBracket, Error::MismatchingBracket);
  623. should_parse_repetition_symbol = true;
  624. break;
  625. }
  626. if (match(TokenType::RightBracket)) {
  627. return set_error(Error::MismatchingBracket);
  628. }
  629. if (match(TokenType::RightCurly)) {
  630. return set_error(Error::MismatchingBrace);
  631. }
  632. if (match(TokenType::Circumflex)) {
  633. consume();
  634. bytecode.empend((ByteCodeValueType)OpCodeId::CheckBegin);
  635. break;
  636. }
  637. if (match(TokenType::Dollar)) {
  638. consume();
  639. bytecode.empend((ByteCodeValueType)OpCodeId::CheckEnd);
  640. break;
  641. }
  642. if (match(TokenType::RightParen))
  643. return false;
  644. if (match(TokenType::LeftParen)) {
  645. enum GroupMode {
  646. Normal,
  647. Lookahead,
  648. NegativeLookahead,
  649. Lookbehind,
  650. NegativeLookbehind,
  651. } group_mode { Normal };
  652. consume();
  653. Optional<StringView> capture_group_name;
  654. bool prevent_capture_group = false;
  655. if (match(TokenType::Questionmark)) {
  656. consume();
  657. if (match(TokenType::Colon)) {
  658. consume();
  659. prevent_capture_group = true;
  660. } else if (consume("<")) { // named capturing group
  661. Token start_token = m_parser_state.current_token;
  662. Token last_token = m_parser_state.current_token;
  663. size_t capture_group_name_length = 0;
  664. for (;;) {
  665. if (!match_ordinary_characters())
  666. return set_error(Error::InvalidNameForCaptureGroup);
  667. if (match(TokenType::Char) && m_parser_state.current_token.value()[0] == '>') {
  668. consume();
  669. break;
  670. }
  671. ++capture_group_name_length;
  672. last_token = consume();
  673. }
  674. capture_group_name = StringView(start_token.value().characters_without_null_termination(), capture_group_name_length);
  675. ++m_parser_state.named_capture_groups_count;
  676. } else if (match(TokenType::EqualSign)) { // positive lookahead
  677. consume();
  678. group_mode = Lookahead;
  679. } else if (consume("!")) { // negative lookahead
  680. group_mode = NegativeLookahead;
  681. } else if (consume("<")) {
  682. if (match(TokenType::EqualSign)) { // positive lookbehind
  683. consume();
  684. group_mode = Lookbehind;
  685. }
  686. if (consume("!")) // negative lookbehind
  687. group_mode = NegativeLookbehind;
  688. } else {
  689. return set_error(Error::InvalidRepetitionMarker);
  690. }
  691. }
  692. auto current_capture_group = m_parser_state.capture_groups_count;
  693. if (!(m_parser_state.regex_options & AllFlags::SkipSubExprResults || prevent_capture_group)) {
  694. bytecode.insert_bytecode_group_capture_left(current_capture_group);
  695. m_parser_state.capture_groups_count++;
  696. }
  697. ByteCode capture_group_bytecode;
  698. if (!parse_root(capture_group_bytecode, length))
  699. return set_error(Error::InvalidPattern);
  700. switch (group_mode) {
  701. case Normal:
  702. bytecode.extend(move(capture_group_bytecode));
  703. break;
  704. case Lookahead:
  705. bytecode.insert_bytecode_lookaround(move(capture_group_bytecode), ByteCode::LookAroundType::LookAhead, length);
  706. break;
  707. case NegativeLookahead:
  708. bytecode.insert_bytecode_lookaround(move(capture_group_bytecode), ByteCode::LookAroundType::NegatedLookAhead, length);
  709. break;
  710. case Lookbehind:
  711. bytecode.insert_bytecode_lookaround(move(capture_group_bytecode), ByteCode::LookAroundType::LookBehind, length);
  712. break;
  713. case NegativeLookbehind:
  714. bytecode.insert_bytecode_lookaround(move(capture_group_bytecode), ByteCode::LookAroundType::NegatedLookBehind, length);
  715. break;
  716. }
  717. consume(TokenType::RightParen, Error::MismatchingParen);
  718. if (!(m_parser_state.regex_options & AllFlags::SkipSubExprResults || prevent_capture_group)) {
  719. if (capture_group_name.has_value())
  720. bytecode.insert_bytecode_group_capture_right(current_capture_group, capture_group_name.value());
  721. else
  722. bytecode.insert_bytecode_group_capture_right(current_capture_group);
  723. }
  724. should_parse_repetition_symbol = true;
  725. break;
  726. }
  727. return false;
  728. }
  729. if (match_repetition_symbol()) {
  730. if (should_parse_repetition_symbol)
  731. parse_repetition_symbol(bytecode, length);
  732. else
  733. return set_error(Error::InvalidRepetitionMarker);
  734. }
  735. stack.extend(move(bytecode));
  736. match_length_minimum += length;
  737. return true;
  738. }
  739. bool PosixExtendedParser::parse_root(ByteCode& stack, size_t& match_length_minimum)
  740. {
  741. ByteCode bytecode_left;
  742. size_t match_length_minimum_left { 0 };
  743. if (match_repetition_symbol())
  744. return set_error(Error::InvalidRepetitionMarker);
  745. for (;;) {
  746. if (!parse_sub_expression(bytecode_left, match_length_minimum_left))
  747. break;
  748. if (match(TokenType::Pipe)) {
  749. consume();
  750. ByteCode bytecode_right;
  751. size_t match_length_minimum_right { 0 };
  752. if (!parse_root(bytecode_right, match_length_minimum_right) || bytecode_right.is_empty())
  753. return set_error(Error::InvalidPattern);
  754. ByteCode new_bytecode;
  755. new_bytecode.insert_bytecode_alternation(move(bytecode_left), move(bytecode_right));
  756. bytecode_left = move(new_bytecode);
  757. match_length_minimum_left = min(match_length_minimum_right, match_length_minimum_left);
  758. }
  759. }
  760. if (bytecode_left.is_empty())
  761. set_error(Error::EmptySubExpression);
  762. stack.extend(move(bytecode_left));
  763. match_length_minimum = match_length_minimum_left;
  764. return !has_error();
  765. }
  766. // =============================
  767. // ECMA262 Parser
  768. // =============================
  769. bool ECMA262Parser::parse_internal(ByteCode& stack, size_t& match_length_minimum)
  770. {
  771. if (m_parser_state.regex_options.has_flag_set(AllFlags::Unicode)) {
  772. return parse_pattern(stack, match_length_minimum, true, true);
  773. }
  774. ByteCode new_stack;
  775. size_t new_match_length = 0;
  776. auto res = parse_pattern(new_stack, new_match_length, false, false);
  777. if (m_parser_state.named_capture_groups_count > 0) {
  778. reset();
  779. return parse_pattern(stack, match_length_minimum, false, true);
  780. }
  781. if (!res)
  782. return false;
  783. stack.extend(new_stack);
  784. match_length_minimum = new_match_length;
  785. return res;
  786. }
  787. bool ECMA262Parser::parse_pattern(ByteCode& stack, size_t& match_length_minimum, bool unicode, bool named)
  788. {
  789. return parse_disjunction(stack, match_length_minimum, unicode, named);
  790. }
  791. bool ECMA262Parser::parse_disjunction(ByteCode& stack, size_t& match_length_minimum, bool unicode, bool named)
  792. {
  793. size_t total_match_length_minimum = NumericLimits<size_t>::max();
  794. Vector<ByteCode> alternatives;
  795. while (true) {
  796. ByteCode alternative_stack;
  797. size_t alternative_minimum_length = 0;
  798. auto alt_ok = parse_alternative(alternative_stack, alternative_minimum_length, unicode, named);
  799. if (!alt_ok)
  800. return false;
  801. alternatives.append(move(alternative_stack));
  802. total_match_length_minimum = min(alternative_minimum_length, total_match_length_minimum);
  803. if (!match(TokenType::Pipe))
  804. break;
  805. consume();
  806. }
  807. Optimizer::append_alternation(stack, alternatives.span());
  808. match_length_minimum = total_match_length_minimum;
  809. return true;
  810. }
  811. bool ECMA262Parser::parse_alternative(ByteCode& stack, size_t& match_length_minimum, bool unicode, bool named)
  812. {
  813. for (;;) {
  814. if (match(TokenType::Eof))
  815. return true;
  816. if (parse_term(stack, match_length_minimum, unicode, named))
  817. continue;
  818. return !has_error();
  819. }
  820. }
  821. bool ECMA262Parser::parse_term(ByteCode& stack, size_t& match_length_minimum, bool unicode, bool named)
  822. {
  823. if (parse_assertion(stack, match_length_minimum, unicode, named))
  824. return true;
  825. ByteCode atom_stack;
  826. size_t minimum_atom_length = 0;
  827. auto parse_with_quantifier = [&] {
  828. bool did_parse_one = false;
  829. if (m_should_use_browser_extended_grammar)
  830. did_parse_one = parse_extended_atom(atom_stack, minimum_atom_length, named);
  831. if (!did_parse_one)
  832. did_parse_one = parse_atom(atom_stack, minimum_atom_length, unicode, named);
  833. if (!did_parse_one)
  834. return false;
  835. VERIFY(did_parse_one);
  836. return parse_quantifier(atom_stack, minimum_atom_length, unicode, named);
  837. };
  838. if (!parse_with_quantifier())
  839. return false;
  840. stack.extend(move(atom_stack));
  841. match_length_minimum += minimum_atom_length;
  842. return true;
  843. }
  844. bool ECMA262Parser::parse_assertion(ByteCode& stack, [[maybe_unused]] size_t& match_length_minimum, bool unicode, bool named)
  845. {
  846. if (match(TokenType::Circumflex)) {
  847. consume();
  848. stack.empend((ByteCodeValueType)OpCodeId::CheckBegin);
  849. return true;
  850. }
  851. if (match(TokenType::Dollar)) {
  852. consume();
  853. stack.empend((ByteCodeValueType)OpCodeId::CheckEnd);
  854. return true;
  855. }
  856. if (try_skip("\\b"sv)) {
  857. stack.insert_bytecode_check_boundary(BoundaryCheckType::Word);
  858. return true;
  859. }
  860. if (try_skip("\\B"sv)) {
  861. stack.insert_bytecode_check_boundary(BoundaryCheckType::NonWord);
  862. return true;
  863. }
  864. if (match(TokenType::LeftParen)) {
  865. if (!try_skip("(?"sv))
  866. return false;
  867. if (done()) {
  868. set_error(Error::InvalidCaptureGroup);
  869. return false;
  870. }
  871. ByteCode assertion_stack;
  872. size_t length_dummy = 0;
  873. bool should_parse_forward_assertion = m_should_use_browser_extended_grammar ? unicode : true;
  874. if (should_parse_forward_assertion && try_skip("="sv)) {
  875. if (!parse_inner_disjunction(assertion_stack, length_dummy, unicode, named))
  876. return false;
  877. stack.insert_bytecode_lookaround(move(assertion_stack), ByteCode::LookAroundType::LookAhead);
  878. return true;
  879. }
  880. if (should_parse_forward_assertion && try_skip("!"sv)) {
  881. enter_capture_group_scope();
  882. ScopeGuard quit_scope {
  883. [this] {
  884. exit_capture_group_scope();
  885. }
  886. };
  887. if (!parse_inner_disjunction(assertion_stack, length_dummy, unicode, named))
  888. return false;
  889. stack.insert_bytecode_lookaround(move(assertion_stack), ByteCode::LookAroundType::NegatedLookAhead);
  890. clear_all_capture_groups_in_scope(stack);
  891. return true;
  892. }
  893. if (m_should_use_browser_extended_grammar) {
  894. if (!unicode) {
  895. if (parse_quantifiable_assertion(assertion_stack, match_length_minimum, named)) {
  896. if (!parse_quantifier(assertion_stack, match_length_minimum, unicode, named))
  897. return false;
  898. stack.extend(move(assertion_stack));
  899. return true;
  900. }
  901. }
  902. }
  903. if (try_skip("<="sv)) {
  904. if (!parse_inner_disjunction(assertion_stack, length_dummy, unicode, named))
  905. return false;
  906. // FIXME: Somehow ensure that this assertion regexp has a fixed length.
  907. stack.insert_bytecode_lookaround(move(assertion_stack), ByteCode::LookAroundType::LookBehind, length_dummy);
  908. return true;
  909. }
  910. if (try_skip("<!"sv)) {
  911. enter_capture_group_scope();
  912. ScopeGuard quit_scope {
  913. [this] {
  914. exit_capture_group_scope();
  915. }
  916. };
  917. if (!parse_inner_disjunction(assertion_stack, length_dummy, unicode, named))
  918. return false;
  919. stack.insert_bytecode_lookaround(move(assertion_stack), ByteCode::LookAroundType::NegatedLookBehind, length_dummy);
  920. clear_all_capture_groups_in_scope(stack);
  921. return true;
  922. }
  923. // If none of these matched, put the '(?' back.
  924. m_parser_state.lexer.back(3);
  925. m_parser_state.current_token = m_parser_state.lexer.next();
  926. return false;
  927. }
  928. return false;
  929. }
  930. bool ECMA262Parser::parse_inner_disjunction(ByteCode& bytecode_stack, size_t& length, bool unicode, bool named)
  931. {
  932. auto disjunction_ok = parse_disjunction(bytecode_stack, length, unicode, named);
  933. if (!disjunction_ok)
  934. return false;
  935. consume(TokenType::RightParen, Error::MismatchingParen);
  936. return true;
  937. }
  938. bool ECMA262Parser::parse_quantifiable_assertion(ByteCode& stack, size_t&, bool named)
  939. {
  940. VERIFY(m_should_use_browser_extended_grammar);
  941. ByteCode assertion_stack;
  942. size_t match_length_minimum = 0;
  943. if (try_skip("="sv)) {
  944. if (!parse_inner_disjunction(assertion_stack, match_length_minimum, false, named))
  945. return false;
  946. stack.insert_bytecode_lookaround(move(assertion_stack), ByteCode::LookAroundType::LookAhead);
  947. return true;
  948. }
  949. if (try_skip("!"sv)) {
  950. enter_capture_group_scope();
  951. ScopeGuard quit_scope {
  952. [this] {
  953. exit_capture_group_scope();
  954. }
  955. };
  956. if (!parse_inner_disjunction(assertion_stack, match_length_minimum, false, named))
  957. return false;
  958. stack.insert_bytecode_lookaround(move(assertion_stack), ByteCode::LookAroundType::NegatedLookAhead);
  959. clear_all_capture_groups_in_scope(stack);
  960. return true;
  961. }
  962. return false;
  963. }
  964. StringView ECMA262Parser::read_digits_as_string(ReadDigitsInitialZeroState initial_zero, bool hex, int max_count, int min_count)
  965. {
  966. if (!match(TokenType::Char))
  967. return {};
  968. if (initial_zero == ReadDigitsInitialZeroState::Disallow && m_parser_state.current_token.value() == "0")
  969. return {};
  970. int count = 0;
  971. size_t offset = 0;
  972. auto start_token = m_parser_state.current_token;
  973. while (match(TokenType::Char)) {
  974. auto const c = m_parser_state.current_token.value();
  975. if (max_count > 0 && count >= max_count)
  976. break;
  977. if (hex && !AK::StringUtils::convert_to_uint_from_hex(c).has_value())
  978. break;
  979. if (!hex && !c.to_uint().has_value())
  980. break;
  981. offset += consume().value().length();
  982. ++count;
  983. }
  984. if (count < min_count)
  985. return {};
  986. return StringView { start_token.value().characters_without_null_termination(), offset };
  987. }
  988. Optional<unsigned> ECMA262Parser::read_digits(ECMA262Parser::ReadDigitsInitialZeroState initial_zero, bool hex, int max_count, int min_count)
  989. {
  990. auto str = read_digits_as_string(initial_zero, hex, max_count, min_count);
  991. if (str.is_empty())
  992. return {};
  993. if (hex)
  994. return AK::StringUtils::convert_to_uint_from_hex(str);
  995. return str.to_uint();
  996. }
  997. bool ECMA262Parser::parse_quantifier(ByteCode& stack, size_t& match_length_minimum, bool unicode, bool)
  998. {
  999. enum class Repetition {
  1000. OneOrMore,
  1001. ZeroOrMore,
  1002. Optional,
  1003. Explicit,
  1004. None,
  1005. } repetition_mark { Repetition::None };
  1006. bool ungreedy = false;
  1007. Optional<u64> repeat_min, repeat_max;
  1008. if (match(TokenType::Asterisk)) {
  1009. consume();
  1010. repetition_mark = Repetition::ZeroOrMore;
  1011. } else if (match(TokenType::Plus)) {
  1012. consume();
  1013. repetition_mark = Repetition::OneOrMore;
  1014. } else if (match(TokenType::Questionmark)) {
  1015. consume();
  1016. repetition_mark = Repetition::Optional;
  1017. } else if (match(TokenType::LeftCurly)) {
  1018. repetition_mark = Repetition::Explicit;
  1019. if (!parse_interval_quantifier(repeat_min, repeat_max)) {
  1020. if (unicode) {
  1021. // Invalid interval quantifiers are disallowed in Unicode mod - they must be escaped with '\{'.
  1022. set_error(Error::InvalidPattern);
  1023. }
  1024. return !has_error();
  1025. }
  1026. } else {
  1027. return true;
  1028. }
  1029. if (match(TokenType::Questionmark)) {
  1030. consume();
  1031. ungreedy = true;
  1032. }
  1033. switch (repetition_mark) {
  1034. case Repetition::OneOrMore:
  1035. ByteCode::transform_bytecode_repetition_min_one(stack, !ungreedy);
  1036. break;
  1037. case Repetition::ZeroOrMore:
  1038. ByteCode::transform_bytecode_repetition_any(stack, !ungreedy);
  1039. match_length_minimum = 0;
  1040. break;
  1041. case Repetition::Optional:
  1042. ByteCode::transform_bytecode_repetition_zero_or_one(stack, !ungreedy);
  1043. match_length_minimum = 0;
  1044. break;
  1045. case Repetition::Explicit: {
  1046. auto min_repetition_mark_id = m_parser_state.repetition_mark_count++;
  1047. auto max_repetition_mark_id = m_parser_state.repetition_mark_count++;
  1048. ByteCode::transform_bytecode_repetition_min_max(stack, repeat_min.value(), repeat_max, min_repetition_mark_id, max_repetition_mark_id, !ungreedy);
  1049. match_length_minimum *= repeat_min.value();
  1050. break;
  1051. }
  1052. case Repetition::None:
  1053. VERIFY_NOT_REACHED();
  1054. }
  1055. return true;
  1056. }
  1057. bool ECMA262Parser::parse_interval_quantifier(Optional<u64>& repeat_min, Optional<u64>& repeat_max)
  1058. {
  1059. VERIFY(match(TokenType::LeftCurly));
  1060. consume();
  1061. auto chars_consumed = 1;
  1062. auto low_bound_string = read_digits_as_string();
  1063. chars_consumed += low_bound_string.length();
  1064. auto low_bound = low_bound_string.to_uint<u64>();
  1065. if (!low_bound.has_value()) {
  1066. if (!m_should_use_browser_extended_grammar && done())
  1067. return set_error(Error::MismatchingBrace);
  1068. back(chars_consumed + !done());
  1069. return false;
  1070. }
  1071. repeat_min = low_bound.value();
  1072. if (match(TokenType::Comma)) {
  1073. consume();
  1074. ++chars_consumed;
  1075. auto high_bound_string = read_digits_as_string();
  1076. auto high_bound = high_bound_string.to_uint<u64>();
  1077. if (high_bound.has_value()) {
  1078. repeat_max = high_bound.value();
  1079. chars_consumed += high_bound_string.length();
  1080. }
  1081. } else {
  1082. repeat_max = repeat_min;
  1083. }
  1084. if (!match(TokenType::RightCurly)) {
  1085. if (!m_should_use_browser_extended_grammar && done())
  1086. return set_error(Error::MismatchingBrace);
  1087. back(chars_consumed + !done());
  1088. return false;
  1089. }
  1090. consume();
  1091. ++chars_consumed;
  1092. if (repeat_max.has_value()) {
  1093. if (repeat_min.value() > repeat_max.value())
  1094. set_error(Error::InvalidBraceContent);
  1095. }
  1096. if ((*repeat_min > s_ecma262_maximum_repetition_count) || (repeat_max.has_value() && (*repeat_max > s_ecma262_maximum_repetition_count)))
  1097. return set_error(Error::InvalidBraceContent);
  1098. return true;
  1099. }
  1100. bool ECMA262Parser::parse_atom(ByteCode& stack, size_t& match_length_minimum, bool unicode, bool named)
  1101. {
  1102. if (match(TokenType::EscapeSequence)) {
  1103. // Also part of AtomEscape.
  1104. auto token = consume();
  1105. match_length_minimum += 1;
  1106. stack.insert_bytecode_compare_values({ { CharacterCompareType::Char, (u8)token.value()[1] } });
  1107. return true;
  1108. }
  1109. if (try_skip("\\"sv)) {
  1110. // AtomEscape.
  1111. return parse_atom_escape(stack, match_length_minimum, unicode, named);
  1112. }
  1113. if (match(TokenType::LeftBracket)) {
  1114. // Character class.
  1115. return parse_character_class(stack, match_length_minimum, unicode, named);
  1116. }
  1117. if (match(TokenType::LeftParen)) {
  1118. // Non-capturing group, or a capture group.
  1119. return parse_capture_group(stack, match_length_minimum, unicode, named);
  1120. }
  1121. if (match(TokenType::Period)) {
  1122. consume();
  1123. match_length_minimum += 1;
  1124. stack.insert_bytecode_compare_values({ { CharacterCompareType::AnyChar, 0 } });
  1125. return true;
  1126. }
  1127. if (match(TokenType::Circumflex) || match(TokenType::Dollar) || match(TokenType::RightParen)
  1128. || match(TokenType::Pipe) || match(TokenType::Plus) || match(TokenType::Asterisk)
  1129. || match(TokenType::Questionmark)) {
  1130. return false;
  1131. }
  1132. if (match(TokenType::RightBracket) || match(TokenType::RightCurly) || match(TokenType::LeftCurly)) {
  1133. if (unicode)
  1134. return set_error(Error::InvalidPattern);
  1135. if (m_should_use_browser_extended_grammar) {
  1136. auto token = consume();
  1137. match_length_minimum += 1;
  1138. stack.insert_bytecode_compare_values({ { CharacterCompareType::Char, (u8)token.value()[0] } });
  1139. return true;
  1140. }
  1141. return false;
  1142. }
  1143. if (match_ordinary_characters()) {
  1144. auto token = consume().value();
  1145. match_length_minimum += 1;
  1146. stack.insert_bytecode_compare_values({ { CharacterCompareType::Char, (u8)token[0] } });
  1147. return true;
  1148. }
  1149. set_error(Error::InvalidPattern);
  1150. return false;
  1151. }
  1152. bool ECMA262Parser::parse_extended_atom(ByteCode&, size_t&, bool)
  1153. {
  1154. // Note: This includes only rules *not* present in parse_atom()
  1155. VERIFY(m_should_use_browser_extended_grammar);
  1156. return parse_invalid_braced_quantifier(); // FAIL FAIL FAIL
  1157. }
  1158. bool ECMA262Parser::parse_invalid_braced_quantifier()
  1159. {
  1160. if (!match(TokenType::LeftCurly))
  1161. return false;
  1162. consume();
  1163. size_t chars_consumed = 1;
  1164. auto low_bound = read_digits_as_string();
  1165. StringView high_bound;
  1166. if (low_bound.is_empty()) {
  1167. back(chars_consumed + !done());
  1168. return false;
  1169. }
  1170. chars_consumed += low_bound.length();
  1171. if (match(TokenType::Comma)) {
  1172. consume();
  1173. ++chars_consumed;
  1174. high_bound = read_digits_as_string();
  1175. chars_consumed += high_bound.length();
  1176. }
  1177. if (!match(TokenType::RightCurly)) {
  1178. back(chars_consumed + !done());
  1179. return false;
  1180. }
  1181. consume();
  1182. set_error(Error::InvalidPattern);
  1183. return true;
  1184. }
  1185. bool ECMA262Parser::parse_atom_escape(ByteCode& stack, size_t& match_length_minimum, bool unicode, bool named)
  1186. {
  1187. if (auto escape_str = read_digits_as_string(ReadDigitsInitialZeroState::Disallow); !escape_str.is_empty()) {
  1188. if (auto escape = escape_str.to_uint(); escape.has_value()) {
  1189. // See if this is a "back"-reference (we've already parsed the group it refers to)
  1190. auto maybe_length = m_parser_state.capture_group_minimum_lengths.get(escape.value());
  1191. if (maybe_length.has_value()) {
  1192. match_length_minimum += maybe_length.value();
  1193. stack.insert_bytecode_compare_values({ { CharacterCompareType::Reference, (ByteCodeValueType)escape.value() } });
  1194. return true;
  1195. }
  1196. // It's not a pattern seen before, so we have to see if it's a valid reference to a future group.
  1197. if (escape.value() <= ensure_total_number_of_capturing_parenthesis()) {
  1198. // This refers to a future group, and it will _always_ be matching an empty string
  1199. // So just match nothing and move on.
  1200. return true;
  1201. }
  1202. if (!m_should_use_browser_extended_grammar) {
  1203. set_error(Error::InvalidNumber);
  1204. return false;
  1205. }
  1206. }
  1207. // If not, put the characters back.
  1208. back(escape_str.length());
  1209. }
  1210. // CharacterEscape > ControlEscape
  1211. if (try_skip("f"sv)) {
  1212. match_length_minimum += 1;
  1213. stack.insert_bytecode_compare_values({ { CharacterCompareType::Char, (ByteCodeValueType)'\f' } });
  1214. return true;
  1215. }
  1216. if (try_skip("n"sv)) {
  1217. match_length_minimum += 1;
  1218. stack.insert_bytecode_compare_values({ { CharacterCompareType::Char, (ByteCodeValueType)'\n' } });
  1219. return true;
  1220. }
  1221. if (try_skip("r"sv)) {
  1222. match_length_minimum += 1;
  1223. stack.insert_bytecode_compare_values({ { CharacterCompareType::Char, (ByteCodeValueType)'\r' } });
  1224. return true;
  1225. }
  1226. if (try_skip("t"sv)) {
  1227. match_length_minimum += 1;
  1228. stack.insert_bytecode_compare_values({ { CharacterCompareType::Char, (ByteCodeValueType)'\t' } });
  1229. return true;
  1230. }
  1231. if (try_skip("v"sv)) {
  1232. match_length_minimum += 1;
  1233. stack.insert_bytecode_compare_values({ { CharacterCompareType::Char, (ByteCodeValueType)'\v' } });
  1234. return true;
  1235. }
  1236. // CharacterEscape > ControlLetter
  1237. if (try_skip("c"sv)) {
  1238. for (auto c : s_alphabetic_characters) {
  1239. if (try_skip({ &c, 1 })) {
  1240. match_length_minimum += 1;
  1241. stack.insert_bytecode_compare_values({ { CharacterCompareType::Char, (ByteCodeValueType)(c % 32) } });
  1242. return true;
  1243. }
  1244. }
  1245. if (unicode) {
  1246. set_error(Error::InvalidPattern);
  1247. return false;
  1248. }
  1249. if (m_should_use_browser_extended_grammar) {
  1250. back(1 + !done());
  1251. stack.insert_bytecode_compare_values({ { CharacterCompareType::Char, (ByteCodeValueType)'\\' } });
  1252. match_length_minimum += 1;
  1253. return true;
  1254. }
  1255. // Allow '\c' in non-unicode mode, just matches 'c'.
  1256. match_length_minimum += 1;
  1257. stack.insert_bytecode_compare_values({ { CharacterCompareType::Char, (ByteCodeValueType)'c' } });
  1258. return true;
  1259. }
  1260. // '\0'
  1261. if (try_skip("0"sv)) {
  1262. if (!lookahead_any(s_decimal_characters)) {
  1263. match_length_minimum += 1;
  1264. stack.insert_bytecode_compare_values({ { CharacterCompareType::Char, (ByteCodeValueType)0 } });
  1265. return true;
  1266. }
  1267. back();
  1268. }
  1269. // LegacyOctalEscapeSequence
  1270. if (m_should_use_browser_extended_grammar) {
  1271. if (!unicode) {
  1272. if (auto escape = parse_legacy_octal_escape(); escape.has_value()) {
  1273. stack.insert_bytecode_compare_values({ { CharacterCompareType::Char, (ByteCodeValueType)escape.value() } });
  1274. match_length_minimum += 1;
  1275. return true;
  1276. }
  1277. }
  1278. }
  1279. // HexEscape
  1280. if (try_skip("x"sv)) {
  1281. if (auto hex_escape = read_digits(ReadDigitsInitialZeroState::Allow, true, 2, 2); hex_escape.has_value()) {
  1282. match_length_minimum += 1;
  1283. stack.insert_bytecode_compare_values({ { CharacterCompareType::Char, (ByteCodeValueType)hex_escape.value() } });
  1284. return true;
  1285. }
  1286. if (!unicode) {
  1287. // '\x' is allowed in non-unicode mode, just matches 'x'.
  1288. match_length_minimum += 1;
  1289. stack.insert_bytecode_compare_values({ { CharacterCompareType::Char, (ByteCodeValueType)'x' } });
  1290. return true;
  1291. }
  1292. set_error(Error::InvalidPattern);
  1293. return false;
  1294. }
  1295. if (try_skip("u"sv)) {
  1296. if (auto code_point = consume_escaped_code_point(unicode); code_point.has_value()) {
  1297. match_length_minimum += 1;
  1298. stack.insert_bytecode_compare_values({ { CharacterCompareType::Char, (ByteCodeValueType)code_point.value() } });
  1299. return true;
  1300. }
  1301. return false;
  1302. }
  1303. // IdentityEscape
  1304. for (auto ch : identity_escape_characters(unicode, m_should_use_browser_extended_grammar)) {
  1305. if (try_skip({ &ch, 1 })) {
  1306. match_length_minimum += 1;
  1307. stack.insert_bytecode_compare_values({ { CharacterCompareType::Char, (ByteCodeValueType)ch } });
  1308. return true;
  1309. }
  1310. }
  1311. if (unicode) {
  1312. if (try_skip("/"sv)) {
  1313. match_length_minimum += 1;
  1314. stack.insert_bytecode_compare_values({ { CharacterCompareType::Char, (ByteCodeValueType)'/' } });
  1315. return true;
  1316. }
  1317. }
  1318. if (named && try_skip("k"sv)) {
  1319. auto name = read_capture_group_specifier(true);
  1320. if (name.is_empty()) {
  1321. set_error(Error::InvalidNameForCaptureGroup);
  1322. return false;
  1323. }
  1324. auto maybe_capture_group = m_parser_state.named_capture_groups.get(name);
  1325. if (!maybe_capture_group.has_value()) {
  1326. set_error(Error::InvalidNameForCaptureGroup);
  1327. return false;
  1328. }
  1329. match_length_minimum += maybe_capture_group->minimum_length;
  1330. stack.insert_bytecode_compare_values({ { CharacterCompareType::Reference, (ByteCodeValueType)maybe_capture_group->group_index } });
  1331. return true;
  1332. }
  1333. if (unicode) {
  1334. PropertyEscape property {};
  1335. bool negated = false;
  1336. if (parse_unicode_property_escape(property, negated)) {
  1337. Vector<CompareTypeAndValuePair> compares;
  1338. if (negated)
  1339. compares.empend(CompareTypeAndValuePair { CharacterCompareType::Inverse, 0 });
  1340. property.visit(
  1341. [&](Unicode::Property property) {
  1342. compares.empend(CompareTypeAndValuePair { CharacterCompareType::Property, (ByteCodeValueType)property });
  1343. },
  1344. [&](Unicode::GeneralCategory general_category) {
  1345. compares.empend(CompareTypeAndValuePair { CharacterCompareType::GeneralCategory, (ByteCodeValueType)general_category });
  1346. },
  1347. [&](Script script) {
  1348. if (script.is_extension)
  1349. compares.empend(CompareTypeAndValuePair { CharacterCompareType::ScriptExtension, (ByteCodeValueType)script.script });
  1350. else
  1351. compares.empend(CompareTypeAndValuePair { CharacterCompareType::Script, (ByteCodeValueType)script.script });
  1352. },
  1353. [](Empty&) { VERIFY_NOT_REACHED(); });
  1354. stack.insert_bytecode_compare_values(move(compares));
  1355. match_length_minimum += 1;
  1356. return true;
  1357. }
  1358. }
  1359. if (done())
  1360. return set_error(Error::InvalidTrailingEscape);
  1361. bool negate = false;
  1362. auto ch = parse_character_class_escape(negate);
  1363. if (!ch.has_value()) {
  1364. if (!unicode) {
  1365. // Allow all SourceCharacter's as escapes here.
  1366. auto token = consume();
  1367. match_length_minimum += 1;
  1368. stack.insert_bytecode_compare_values({ { CharacterCompareType::Char, (u8)token.value()[0] } });
  1369. return true;
  1370. }
  1371. set_error(Error::InvalidCharacterClass);
  1372. return false;
  1373. }
  1374. Vector<CompareTypeAndValuePair> compares;
  1375. if (negate)
  1376. compares.empend(CompareTypeAndValuePair { CharacterCompareType::Inverse, 0 });
  1377. compares.empend(CompareTypeAndValuePair { CharacterCompareType::CharClass, (ByteCodeValueType)ch.value() });
  1378. match_length_minimum += 1;
  1379. stack.insert_bytecode_compare_values(move(compares));
  1380. return true;
  1381. }
  1382. Optional<u8> ECMA262Parser::parse_legacy_octal_escape()
  1383. {
  1384. constexpr auto all_octal_digits = "01234567"sv;
  1385. auto read_octal_digit = [&](auto start, auto end, bool should_ensure_no_following_octal_digit) -> Optional<u8> {
  1386. for (char c = '0' + start; c <= '0' + end; ++c) {
  1387. if (try_skip({ &c, 1 })) {
  1388. if (!should_ensure_no_following_octal_digit || !lookahead_any(all_octal_digits))
  1389. return c - '0';
  1390. back(2);
  1391. return {};
  1392. }
  1393. }
  1394. return {};
  1395. };
  1396. // OctalDigit(1)
  1397. if (auto digit = read_octal_digit(0, 7, true); digit.has_value()) {
  1398. return digit.value();
  1399. }
  1400. // OctalDigit(2)
  1401. if (auto left_digit = read_octal_digit(0, 3, false); left_digit.has_value()) {
  1402. if (auto right_digit = read_octal_digit(0, 7, true); right_digit.has_value()) {
  1403. return left_digit.value() * 8 + right_digit.value();
  1404. }
  1405. back(2);
  1406. }
  1407. // OctalDigit(2)
  1408. if (auto left_digit = read_octal_digit(4, 7, false); left_digit.has_value()) {
  1409. if (auto right_digit = read_octal_digit(0, 7, false); right_digit.has_value()) {
  1410. return left_digit.value() * 8 + right_digit.value();
  1411. }
  1412. back(2);
  1413. }
  1414. // OctalDigit(3)
  1415. if (auto left_digit = read_octal_digit(0, 3, false); left_digit.has_value()) {
  1416. size_t chars_consumed = 1;
  1417. if (auto mid_digit = read_octal_digit(0, 7, false); mid_digit.has_value()) {
  1418. ++chars_consumed;
  1419. if (auto right_digit = read_octal_digit(0, 7, false); right_digit.has_value()) {
  1420. return left_digit.value() * 64 + mid_digit.value() * 8 + right_digit.value();
  1421. }
  1422. }
  1423. back(chars_consumed);
  1424. }
  1425. return {};
  1426. }
  1427. Optional<CharClass> ECMA262Parser::parse_character_class_escape(bool& negate, bool expect_backslash)
  1428. {
  1429. if (expect_backslash && !try_skip("\\"sv))
  1430. return {};
  1431. // CharacterClassEscape
  1432. CharClass ch_class;
  1433. if (try_skip("d"sv)) {
  1434. ch_class = CharClass::Digit;
  1435. } else if (try_skip("D"sv)) {
  1436. ch_class = CharClass::Digit;
  1437. negate = true;
  1438. } else if (try_skip("s"sv)) {
  1439. ch_class = CharClass::Space;
  1440. } else if (try_skip("S"sv)) {
  1441. ch_class = CharClass::Space;
  1442. negate = true;
  1443. } else if (try_skip("w"sv)) {
  1444. ch_class = CharClass::Word;
  1445. } else if (try_skip("W"sv)) {
  1446. ch_class = CharClass::Word;
  1447. negate = true;
  1448. } else {
  1449. return {};
  1450. }
  1451. return ch_class;
  1452. }
  1453. bool ECMA262Parser::parse_character_class(ByteCode& stack, size_t& match_length_minimum, bool unicode, bool)
  1454. {
  1455. consume(TokenType::LeftBracket, Error::InvalidPattern);
  1456. Vector<CompareTypeAndValuePair> compares;
  1457. if (match(TokenType::Circumflex)) {
  1458. // Negated charclass
  1459. consume();
  1460. compares.empend(CompareTypeAndValuePair { CharacterCompareType::Inverse, 0 });
  1461. }
  1462. if (match(TokenType::RightBracket)) {
  1463. consume();
  1464. // Should only have at most an 'Inverse'
  1465. VERIFY(compares.size() <= 1);
  1466. stack.insert_bytecode_compare_values(move(compares));
  1467. return true;
  1468. }
  1469. if (!parse_nonempty_class_ranges(compares, unicode))
  1470. return false;
  1471. match_length_minimum += 1;
  1472. stack.insert_bytecode_compare_values(move(compares));
  1473. return true;
  1474. }
  1475. struct CharClassRangeElement {
  1476. union {
  1477. CharClass character_class;
  1478. u32 code_point { 0 };
  1479. Unicode::Property property;
  1480. Unicode::GeneralCategory general_category;
  1481. Unicode::Script script;
  1482. };
  1483. bool is_negated { false };
  1484. bool is_character_class { false };
  1485. bool is_property { false };
  1486. bool is_general_category { false };
  1487. bool is_script { false };
  1488. bool is_script_extension { false };
  1489. };
  1490. bool ECMA262Parser::parse_nonempty_class_ranges(Vector<CompareTypeAndValuePair>& ranges, bool unicode)
  1491. {
  1492. auto read_class_atom_no_dash = [&]() -> Optional<CharClassRangeElement> {
  1493. if (match(TokenType::EscapeSequence)) {
  1494. auto token = consume().value();
  1495. return { CharClassRangeElement { .code_point = (u32)token[1], .is_character_class = false } };
  1496. }
  1497. if (try_skip("\\"sv)) {
  1498. if (done()) {
  1499. set_error(Error::InvalidTrailingEscape);
  1500. return {};
  1501. }
  1502. if (try_skip("f"sv))
  1503. return { CharClassRangeElement { .code_point = '\f', .is_character_class = false } };
  1504. if (try_skip("n"sv))
  1505. return { CharClassRangeElement { .code_point = '\n', .is_character_class = false } };
  1506. if (try_skip("r"sv))
  1507. return { CharClassRangeElement { .code_point = '\r', .is_character_class = false } };
  1508. if (try_skip("t"sv))
  1509. return { CharClassRangeElement { .code_point = '\t', .is_character_class = false } };
  1510. if (try_skip("v"sv))
  1511. return { CharClassRangeElement { .code_point = '\v', .is_character_class = false } };
  1512. if (try_skip("b"sv))
  1513. return { CharClassRangeElement { .code_point = '\b', .is_character_class = false } };
  1514. if (try_skip("/"sv))
  1515. return { CharClassRangeElement { .code_point = '/', .is_character_class = false } };
  1516. // CharacterEscape > ControlLetter
  1517. if (try_skip("c"sv)) {
  1518. for (auto c : s_alphabetic_characters) {
  1519. if (try_skip({ &c, 1 })) {
  1520. return { CharClassRangeElement { .code_point = (u32)(c % 32), .is_character_class = false } };
  1521. }
  1522. }
  1523. if (unicode) {
  1524. set_error(Error::InvalidPattern);
  1525. return {};
  1526. }
  1527. if (m_should_use_browser_extended_grammar) {
  1528. for (auto c = '0'; c <= '9'; ++c) {
  1529. if (try_skip({ &c, 1 }))
  1530. return { CharClassRangeElement { .code_point = (u32)(c % 32), .is_character_class = false } };
  1531. }
  1532. if (try_skip("_"sv))
  1533. return { CharClassRangeElement { .code_point = (u32)('_' % 32), .is_character_class = false } };
  1534. back(1 + !done());
  1535. return { CharClassRangeElement { .code_point = '\\', .is_character_class = false } };
  1536. }
  1537. }
  1538. // '\0'
  1539. if (try_skip("0"sv)) {
  1540. if (!lookahead_any(s_decimal_characters))
  1541. return { CharClassRangeElement { .code_point = 0, .is_character_class = false } };
  1542. back();
  1543. }
  1544. // LegacyOctalEscapeSequence
  1545. if (m_should_use_browser_extended_grammar && !unicode) {
  1546. if (auto escape = parse_legacy_octal_escape(); escape.has_value())
  1547. return { CharClassRangeElement { .code_point = escape.value(), .is_character_class = false } };
  1548. }
  1549. // HexEscape
  1550. if (try_skip("x"sv)) {
  1551. if (auto hex_escape = read_digits(ReadDigitsInitialZeroState::Allow, true, 2, 2); hex_escape.has_value()) {
  1552. return { CharClassRangeElement { .code_point = hex_escape.value(), .is_character_class = false } };
  1553. } else if (!unicode) {
  1554. // '\x' is allowed in non-unicode mode, just matches 'x'.
  1555. return { CharClassRangeElement { .code_point = 'x', .is_character_class = false } };
  1556. } else {
  1557. set_error(Error::InvalidPattern);
  1558. return {};
  1559. }
  1560. }
  1561. if (try_skip("u"sv)) {
  1562. if (auto code_point = consume_escaped_code_point(unicode); code_point.has_value()) {
  1563. // FIXME: While code point ranges are supported, code point matches as "Char" are not!
  1564. return { CharClassRangeElement { .code_point = code_point.value(), .is_character_class = false } };
  1565. }
  1566. return {};
  1567. }
  1568. // IdentityEscape
  1569. for (auto ch : identity_escape_characters(unicode, m_should_use_browser_extended_grammar)) {
  1570. if (try_skip({ &ch, 1 }))
  1571. return { CharClassRangeElement { .code_point = (u32)ch, .is_character_class = false } };
  1572. }
  1573. if (unicode) {
  1574. if (try_skip("-"sv))
  1575. return { CharClassRangeElement { .code_point = '-', .is_character_class = false } };
  1576. PropertyEscape property {};
  1577. bool negated = false;
  1578. if (parse_unicode_property_escape(property, negated)) {
  1579. return property.visit(
  1580. [&](Unicode::Property property) {
  1581. return CharClassRangeElement { .property = property, .is_negated = negated, .is_character_class = true, .is_property = true };
  1582. },
  1583. [&](Unicode::GeneralCategory general_category) {
  1584. return CharClassRangeElement { .general_category = general_category, .is_negated = negated, .is_character_class = true, .is_general_category = true };
  1585. },
  1586. [&](Script script) {
  1587. if (script.is_extension)
  1588. return CharClassRangeElement { .script = script.script, .is_negated = negated, .is_character_class = true, .is_script_extension = true };
  1589. return CharClassRangeElement { .script = script.script, .is_negated = negated, .is_character_class = true, .is_script = true };
  1590. },
  1591. [](Empty&) -> CharClassRangeElement { VERIFY_NOT_REACHED(); });
  1592. }
  1593. }
  1594. if (try_skip("d"sv))
  1595. return { CharClassRangeElement { .character_class = CharClass::Digit, .is_character_class = true } };
  1596. if (try_skip("s"sv))
  1597. return { CharClassRangeElement { .character_class = CharClass::Space, .is_character_class = true } };
  1598. if (try_skip("w"sv))
  1599. return { CharClassRangeElement { .character_class = CharClass::Word, .is_character_class = true } };
  1600. if (try_skip("D"sv))
  1601. return { CharClassRangeElement { .character_class = CharClass::Digit, .is_negated = true, .is_character_class = true } };
  1602. if (try_skip("S"sv))
  1603. return { CharClassRangeElement { .character_class = CharClass::Space, .is_negated = true, .is_character_class = true } };
  1604. if (try_skip("W"sv))
  1605. return { CharClassRangeElement { .character_class = CharClass::Word, .is_negated = true, .is_character_class = true } };
  1606. if (!unicode) {
  1607. // Any unrecognised escape is allowed in non-unicode mode.
  1608. return { CharClassRangeElement { .code_point = (u32)skip(), .is_character_class = false } };
  1609. }
  1610. set_error(Error::InvalidPattern);
  1611. return {};
  1612. }
  1613. if (match(TokenType::RightBracket) || match(TokenType::HyphenMinus))
  1614. return {};
  1615. // Allow any (other) SourceCharacter.
  1616. return { CharClassRangeElement { .code_point = (u32)skip(), .is_character_class = false } };
  1617. };
  1618. auto read_class_atom = [&]() -> Optional<CharClassRangeElement> {
  1619. if (match(TokenType::HyphenMinus)) {
  1620. consume();
  1621. return { CharClassRangeElement { .code_point = '-', .is_character_class = false } };
  1622. }
  1623. return read_class_atom_no_dash();
  1624. };
  1625. auto empend_atom = [&](auto& atom) {
  1626. if (atom.is_character_class) {
  1627. if (atom.is_negated)
  1628. ranges.empend(CompareTypeAndValuePair { CharacterCompareType::TemporaryInverse, 0 });
  1629. if (atom.is_property)
  1630. ranges.empend(CompareTypeAndValuePair { CharacterCompareType::Property, (ByteCodeValueType)(atom.property) });
  1631. else if (atom.is_general_category)
  1632. ranges.empend(CompareTypeAndValuePair { CharacterCompareType::GeneralCategory, (ByteCodeValueType)(atom.general_category) });
  1633. else if (atom.is_script)
  1634. ranges.empend(CompareTypeAndValuePair { CharacterCompareType::Script, (ByteCodeValueType)(atom.script) });
  1635. else if (atom.is_script_extension)
  1636. ranges.empend(CompareTypeAndValuePair { CharacterCompareType::ScriptExtension, (ByteCodeValueType)(atom.script) });
  1637. else
  1638. ranges.empend(CompareTypeAndValuePair { CharacterCompareType::CharClass, (ByteCodeValueType)atom.character_class });
  1639. } else {
  1640. VERIFY(!atom.is_negated);
  1641. ranges.empend(CompareTypeAndValuePair { CharacterCompareType::Char, atom.code_point });
  1642. }
  1643. };
  1644. while (!match(TokenType::RightBracket)) {
  1645. if (match(TokenType::Eof)) {
  1646. set_error(Error::MismatchingBracket);
  1647. return false;
  1648. }
  1649. auto first_atom = read_class_atom();
  1650. if (!first_atom.has_value())
  1651. return false;
  1652. if (match(TokenType::HyphenMinus)) {
  1653. consume();
  1654. if (match(TokenType::RightBracket)) {
  1655. // Allow '-' as the last element in a charclass, even after an atom.
  1656. m_parser_state.lexer.back(2); // -]
  1657. m_parser_state.current_token = m_parser_state.lexer.next();
  1658. goto read_as_single_atom;
  1659. }
  1660. auto second_atom = read_class_atom();
  1661. if (!second_atom.has_value())
  1662. return false;
  1663. if (first_atom.value().is_character_class || second_atom.value().is_character_class) {
  1664. if (m_should_use_browser_extended_grammar) {
  1665. if (unicode) {
  1666. set_error(Error::InvalidRange);
  1667. return false;
  1668. }
  1669. // CharacterRangeOrUnion > !Unicode > CharClass
  1670. empend_atom(*first_atom);
  1671. ranges.empend(CompareTypeAndValuePair { CharacterCompareType::Char, (ByteCodeValueType)'-' });
  1672. empend_atom(*second_atom);
  1673. continue;
  1674. }
  1675. set_error(Error::InvalidRange);
  1676. return false;
  1677. }
  1678. if (first_atom.value().code_point > second_atom.value().code_point) {
  1679. set_error(Error::InvalidRange);
  1680. return false;
  1681. }
  1682. VERIFY(!first_atom.value().is_negated);
  1683. VERIFY(!second_atom.value().is_negated);
  1684. ranges.empend(CompareTypeAndValuePair { CharacterCompareType::CharRange, CharRange { first_atom.value().code_point, second_atom.value().code_point } });
  1685. continue;
  1686. }
  1687. read_as_single_atom:;
  1688. auto atom = first_atom.value();
  1689. empend_atom(atom);
  1690. }
  1691. consume(TokenType::RightBracket, Error::MismatchingBracket);
  1692. return true;
  1693. }
  1694. bool ECMA262Parser::parse_unicode_property_escape(PropertyEscape& property, bool& negated)
  1695. {
  1696. negated = false;
  1697. if (try_skip("p"sv))
  1698. negated = false;
  1699. else if (try_skip("P"sv))
  1700. negated = true;
  1701. else
  1702. return false;
  1703. auto parsed_property = read_unicode_property_escape();
  1704. if (!parsed_property.has_value()) {
  1705. set_error(Error::InvalidNameForProperty);
  1706. return false;
  1707. }
  1708. property = move(*parsed_property);
  1709. return property.visit(
  1710. [this](Unicode::Property property) {
  1711. if (!Unicode::is_ecma262_property(property)) {
  1712. set_error(Error::InvalidNameForProperty);
  1713. return false;
  1714. }
  1715. return true;
  1716. },
  1717. [](Unicode::GeneralCategory) { return true; },
  1718. [](Script) { return true; },
  1719. [](Empty&) -> bool { VERIFY_NOT_REACHED(); });
  1720. }
  1721. FlyString ECMA262Parser::read_capture_group_specifier(bool take_starting_angle_bracket)
  1722. {
  1723. static auto id_start_category = Unicode::property_from_string("ID_Start"sv);
  1724. static auto id_continue_category = Unicode::property_from_string("ID_Continue"sv);
  1725. static constexpr const u32 REPLACEMENT_CHARACTER = 0xFFFD;
  1726. constexpr const u32 ZERO_WIDTH_NON_JOINER { 0x200C };
  1727. constexpr const u32 ZERO_WIDTH_JOINER { 0x200D };
  1728. if (take_starting_angle_bracket && !consume("<"))
  1729. return {};
  1730. StringBuilder builder;
  1731. auto consume_code_point = [&] {
  1732. Utf8View utf_8_view { m_parser_state.lexer.source().substring_view(m_parser_state.lexer.tell() - 1) };
  1733. if (utf_8_view.is_empty())
  1734. return REPLACEMENT_CHARACTER;
  1735. u32 code_point = *utf_8_view.begin();
  1736. auto characters = utf_8_view.byte_offset_of(1);
  1737. while (characters-- > 0)
  1738. consume();
  1739. return code_point;
  1740. };
  1741. {
  1742. // The first character is limited to: https://tc39.es/ecma262/#prod-RegExpIdentifierStart
  1743. // RegExpIdentifierStart[UnicodeMode] ::
  1744. // IdentifierStartChar
  1745. // \ RegExpUnicodeEscapeSequence[+UnicodeMode]
  1746. // [~UnicodeMode] UnicodeLeadSurrogate UnicodeTrailSurrogate
  1747. auto code_point = consume_code_point();
  1748. if (code_point == '\\' && match('u')) {
  1749. consume();
  1750. if (auto maybe_code_point = consume_escaped_code_point(true); maybe_code_point.has_value()) {
  1751. code_point = *maybe_code_point;
  1752. } else {
  1753. set_error(Error::InvalidNameForCaptureGroup);
  1754. return {};
  1755. }
  1756. }
  1757. if (is_ascii(code_point)) {
  1758. // The only valid ID_Start unicode characters in ascii are the letters.
  1759. if (!is_ascii_alpha(code_point) && code_point != '$' && code_point != '_') {
  1760. set_error(Error::InvalidNameForCaptureGroup);
  1761. return {};
  1762. }
  1763. } else if (id_start_category.has_value() && !Unicode::code_point_has_property(code_point, *id_start_category)) {
  1764. set_error(Error::InvalidNameForCaptureGroup);
  1765. return {};
  1766. }
  1767. builder.append_code_point(code_point);
  1768. }
  1769. bool hit_end = false;
  1770. // Any following characters are limited to:
  1771. // RegExpIdentifierPart[UnicodeMode] ::
  1772. // IdentifierPartChar
  1773. // \ RegExpUnicodeEscapeSequence[+UnicodeMode]
  1774. // [~UnicodeMode] UnicodeLeadSurrogate UnicodeTrailSurrogate
  1775. while (match(TokenType::Char) || match(TokenType::Dollar) || match(TokenType::LeftCurly) || match(TokenType::RightCurly)) {
  1776. auto code_point = consume_code_point();
  1777. if (code_point == '>') {
  1778. hit_end = true;
  1779. break;
  1780. }
  1781. if (code_point == '\\') {
  1782. if (!try_skip("u"sv)) {
  1783. set_error(Error::InvalidNameForCaptureGroup);
  1784. return {};
  1785. }
  1786. if (auto maybe_code_point = consume_escaped_code_point(true); maybe_code_point.has_value()) {
  1787. code_point = *maybe_code_point;
  1788. } else {
  1789. set_error(Error::InvalidNameForCaptureGroup);
  1790. return {};
  1791. }
  1792. }
  1793. if (is_ascii(code_point)) {
  1794. // The only valid ID_Continue unicode characters in ascii are the letters and numbers.
  1795. if (!is_ascii_alphanumeric(code_point) && code_point != '$' && code_point != '_') {
  1796. set_error(Error::InvalidNameForCaptureGroup);
  1797. return {};
  1798. }
  1799. } else if (code_point != ZERO_WIDTH_JOINER && code_point != ZERO_WIDTH_NON_JOINER) {
  1800. if (id_continue_category.has_value() && !Unicode::code_point_has_property(code_point, *id_continue_category)) {
  1801. set_error(Error::InvalidNameForCaptureGroup);
  1802. return {};
  1803. }
  1804. }
  1805. builder.append_code_point(code_point);
  1806. }
  1807. FlyString name = builder.build();
  1808. if (!hit_end || name.is_empty())
  1809. set_error(Error::InvalidNameForCaptureGroup);
  1810. return name;
  1811. }
  1812. Optional<ECMA262Parser::PropertyEscape> ECMA262Parser::read_unicode_property_escape()
  1813. {
  1814. consume(TokenType::LeftCurly, Error::InvalidPattern);
  1815. // Note: clang-format is disabled here because it doesn't handle templated lambdas yet.
  1816. // clang-format off
  1817. auto read_until = [&]<typename... Ts>(Ts&&... terminators) {
  1818. auto start_token = m_parser_state.current_token;
  1819. size_t offset = 0;
  1820. while (match(TokenType::Char)) {
  1821. if (m_parser_state.current_token.value().is_one_of(forward<Ts>(terminators)...))
  1822. break;
  1823. offset += consume().value().length();
  1824. }
  1825. return StringView { start_token.value().characters_without_null_termination(), offset };
  1826. };
  1827. // clang-format on
  1828. StringView property_type;
  1829. StringView property_name = read_until("="sv, "}"sv);
  1830. if (try_skip("="sv)) {
  1831. if (property_name.is_empty())
  1832. return {};
  1833. property_type = property_name;
  1834. property_name = read_until("}"sv);
  1835. }
  1836. consume(TokenType::RightCurly, Error::InvalidPattern);
  1837. if (property_type.is_empty()) {
  1838. if (auto property = Unicode::property_from_string(property_name); property.has_value())
  1839. return { *property };
  1840. if (auto general_category = Unicode::general_category_from_string(property_name); general_category.has_value())
  1841. return { *general_category };
  1842. } else if ((property_type == "General_Category"sv) || (property_type == "gc"sv)) {
  1843. if (auto general_category = Unicode::general_category_from_string(property_name); general_category.has_value())
  1844. return { *general_category };
  1845. } else if ((property_type == "Script"sv) || (property_type == "sc"sv)) {
  1846. if (auto script = Unicode::script_from_string(property_name); script.has_value())
  1847. return Script { *script, false };
  1848. } else if ((property_type == "Script_Extensions"sv) || (property_type == "scx"sv)) {
  1849. if (auto script = Unicode::script_from_string(property_name); script.has_value())
  1850. return Script { *script, true };
  1851. }
  1852. return {};
  1853. }
  1854. bool ECMA262Parser::parse_capture_group(ByteCode& stack, size_t& match_length_minimum, bool unicode, bool named)
  1855. {
  1856. consume(TokenType::LeftParen, Error::InvalidPattern);
  1857. auto register_capture_group_in_current_scope = [&](auto identifier) {
  1858. m_capture_groups_in_scope.last().empend(identifier);
  1859. };
  1860. if (match(TokenType::Questionmark)) {
  1861. // Non-capturing group or group with specifier.
  1862. consume();
  1863. if (match(TokenType::Colon)) {
  1864. consume();
  1865. ByteCode noncapture_group_bytecode;
  1866. size_t length = 0;
  1867. enter_capture_group_scope();
  1868. if (!parse_disjunction(noncapture_group_bytecode, length, unicode, named))
  1869. return set_error(Error::InvalidPattern);
  1870. clear_all_capture_groups_in_scope(stack);
  1871. exit_capture_group_scope();
  1872. consume(TokenType::RightParen, Error::MismatchingParen);
  1873. stack.extend(move(noncapture_group_bytecode));
  1874. match_length_minimum += length;
  1875. return true;
  1876. }
  1877. if (consume("<")) {
  1878. ++m_parser_state.named_capture_groups_count;
  1879. auto group_index = ++m_parser_state.capture_groups_count; // Named capture groups count as normal capture groups too.
  1880. auto name = read_capture_group_specifier();
  1881. if (name.is_empty()) {
  1882. set_error(Error::InvalidNameForCaptureGroup);
  1883. return false;
  1884. }
  1885. if (m_parser_state.named_capture_groups.contains(name)) {
  1886. set_error(Error::DuplicateNamedCapture);
  1887. return false;
  1888. }
  1889. ByteCode capture_group_bytecode;
  1890. size_t length = 0;
  1891. enter_capture_group_scope();
  1892. if (!parse_disjunction(capture_group_bytecode, length, unicode, named))
  1893. return set_error(Error::InvalidPattern);
  1894. clear_all_capture_groups_in_scope(stack);
  1895. exit_capture_group_scope();
  1896. register_capture_group_in_current_scope(group_index);
  1897. consume(TokenType::RightParen, Error::MismatchingParen);
  1898. stack.insert_bytecode_group_capture_left(group_index);
  1899. stack.extend(move(capture_group_bytecode));
  1900. stack.insert_bytecode_group_capture_right(group_index, name.view());
  1901. match_length_minimum += length;
  1902. m_parser_state.capture_group_minimum_lengths.set(group_index, length);
  1903. m_parser_state.named_capture_groups.set(name, { group_index, length });
  1904. return true;
  1905. }
  1906. set_error(Error::InvalidCaptureGroup);
  1907. return false;
  1908. }
  1909. auto group_index = ++m_parser_state.capture_groups_count;
  1910. enter_capture_group_scope();
  1911. ByteCode capture_group_bytecode;
  1912. size_t length = 0;
  1913. if (!parse_disjunction(capture_group_bytecode, length, unicode, named))
  1914. return set_error(Error::InvalidPattern);
  1915. clear_all_capture_groups_in_scope(stack);
  1916. exit_capture_group_scope();
  1917. register_capture_group_in_current_scope(group_index);
  1918. stack.insert_bytecode_group_capture_left(group_index);
  1919. stack.extend(move(capture_group_bytecode));
  1920. m_parser_state.capture_group_minimum_lengths.set(group_index, length);
  1921. consume(TokenType::RightParen, Error::MismatchingParen);
  1922. stack.insert_bytecode_group_capture_right(group_index);
  1923. match_length_minimum += length;
  1924. return true;
  1925. }
  1926. size_t ECMA262Parser::ensure_total_number_of_capturing_parenthesis()
  1927. {
  1928. if (m_total_number_of_capturing_parenthesis.has_value())
  1929. return m_total_number_of_capturing_parenthesis.value();
  1930. GenericLexer lexer { m_parser_state.lexer.source() };
  1931. size_t count = 0;
  1932. while (!lexer.is_eof()) {
  1933. switch (lexer.peek()) {
  1934. case '\\':
  1935. lexer.consume(2);
  1936. continue;
  1937. case '[':
  1938. while (!lexer.is_eof()) {
  1939. if (lexer.consume_specific('\\'))
  1940. lexer.consume();
  1941. else if (lexer.consume_specific(']'))
  1942. break;
  1943. lexer.consume();
  1944. }
  1945. break;
  1946. case '(':
  1947. lexer.consume();
  1948. if (lexer.consume_specific('?')) {
  1949. // non-capturing group '(?:', lookaround '(?<='/'(?<!', or named capture '(?<'
  1950. if (!lexer.consume_specific('<'))
  1951. break;
  1952. if (lexer.next_is(is_any_of("=!"sv)))
  1953. break;
  1954. ++count;
  1955. } else {
  1956. ++count;
  1957. }
  1958. break;
  1959. default:
  1960. lexer.consume();
  1961. break;
  1962. }
  1963. }
  1964. m_total_number_of_capturing_parenthesis = count;
  1965. return count;
  1966. }
  1967. }