RegexParser.cpp 80 KB

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