RegexParser.cpp 84 KB

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