RegexParser.cpp 81 KB

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