12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786 |
- /*
- * Copyright (c) 2020, Emanuel Sprung <emanuel.sprung@gmail.com>
- * Copyright (c) 2020-2021, the SerenityOS developers.
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
- #include "RegexParser.h"
- #include "RegexDebug.h"
- #include <AK/AnyOf.h>
- #include <AK/ByteString.h>
- #include <AK/CharacterTypes.h>
- #include <AK/Debug.h>
- #include <AK/GenericLexer.h>
- #include <AK/ScopeGuard.h>
- #include <AK/StringBuilder.h>
- #include <AK/StringUtils.h>
- #include <AK/TemporaryChange.h>
- #include <AK/Utf16View.h>
- #include <LibUnicode/CharacterTypes.h>
- namespace regex {
- static constexpr size_t s_maximum_repetition_count = 1024 * 1024;
- static constexpr u64 s_ecma262_maximum_repetition_count = (1ull << 53) - 1;
- static constexpr auto s_alphabetic_characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"sv;
- static constexpr auto s_decimal_characters = "0123456789"sv;
- static constexpr StringView identity_escape_characters(bool unicode, bool browser_extended)
- {
- if (unicode)
- return "^$\\.*+?()[]{}|/"sv;
- if (browser_extended)
- return "^$\\.*+?()[|"sv;
- return "^$\\.*+?()[]{}|"sv;
- }
- ALWAYS_INLINE bool Parser::set_error(Error error)
- {
- if (m_parser_state.error == Error::NoError) {
- m_parser_state.error = error;
- m_parser_state.error_token = m_parser_state.current_token;
- }
- return false; // always return false, that eases the API usage (return set_error(...)) :^)
- }
- ALWAYS_INLINE bool Parser::done() const
- {
- return match(TokenType::Eof);
- }
- ALWAYS_INLINE bool Parser::match(TokenType type) const
- {
- return m_parser_state.current_token.type() == type;
- }
- ALWAYS_INLINE bool Parser::match(char ch) const
- {
- 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;
- }
- ALWAYS_INLINE Token Parser::consume()
- {
- auto old_token = m_parser_state.current_token;
- m_parser_state.current_token = m_parser_state.lexer.next();
- return old_token;
- }
- ALWAYS_INLINE Token Parser::consume(TokenType type, Error error)
- {
- if (m_parser_state.current_token.type() != type) {
- set_error(error);
- dbgln_if(REGEX_DEBUG, "[PARSER] Error: Unexpected token {}. Expected: {}", m_parser_state.current_token.name(), Token::name(type));
- }
- return consume();
- }
- ALWAYS_INLINE bool Parser::consume(ByteString const& str)
- {
- size_t potentially_go_back { 1 };
- for (auto ch : str) {
- if (match(TokenType::Char)) {
- if (m_parser_state.current_token.value()[0] != ch) {
- m_parser_state.lexer.back(potentially_go_back);
- m_parser_state.current_token = m_parser_state.lexer.next();
- return false;
- }
- } else {
- m_parser_state.lexer.back(potentially_go_back);
- m_parser_state.current_token = m_parser_state.lexer.next();
- return false;
- }
- consume(TokenType::Char, Error::NoError);
- ++potentially_go_back;
- }
- return true;
- }
- ALWAYS_INLINE Optional<u32> Parser::consume_escaped_code_point(bool unicode)
- {
- if (match(TokenType::LeftCurly) && !unicode) {
- // In non-Unicode mode, this should be parsed as a repetition symbol (repeating the 'u').
- return static_cast<u32>('u');
- }
- m_parser_state.lexer.retreat(2 + !done()); // Go back to just before '\u' (+1 char, because we will have consumed an extra character)
- if (auto code_point_or_error = m_parser_state.lexer.consume_escaped_code_point(unicode); !code_point_or_error.is_error()) {
- m_parser_state.current_token = m_parser_state.lexer.next();
- return code_point_or_error.value();
- }
- if (!unicode) {
- // '\u' is allowed in non-unicode mode, just matches 'u'.
- return static_cast<u32>('u');
- }
- set_error(Error::InvalidPattern);
- return {};
- }
- ALWAYS_INLINE bool Parser::try_skip(StringView str)
- {
- if (str.starts_with(m_parser_state.current_token.value()))
- str = str.substring_view(m_parser_state.current_token.value().length(), str.length() - m_parser_state.current_token.value().length());
- else
- return false;
- size_t potentially_go_back { 0 };
- for (auto ch : str) {
- if (!m_parser_state.lexer.consume_specific(ch)) {
- m_parser_state.lexer.back(potentially_go_back);
- return false;
- }
- ++potentially_go_back;
- }
- m_parser_state.current_token = m_parser_state.lexer.next();
- return true;
- }
- ALWAYS_INLINE bool Parser::lookahead_any(StringView str)
- {
- return AK::any_of(str, [this](auto ch) { return match(ch); });
- }
- ALWAYS_INLINE unsigned char Parser::skip()
- {
- unsigned char ch;
- if (m_parser_state.current_token.value().length() == 1) {
- ch = m_parser_state.current_token.value()[0];
- } else {
- m_parser_state.lexer.back(m_parser_state.current_token.value().length());
- ch = m_parser_state.lexer.consume();
- }
- m_parser_state.current_token = m_parser_state.lexer.next();
- return ch;
- }
- ALWAYS_INLINE void Parser::back(size_t count)
- {
- m_parser_state.lexer.back(count);
- m_parser_state.current_token = m_parser_state.lexer.next();
- }
- ALWAYS_INLINE void Parser::reset()
- {
- m_parser_state.bytecode.clear();
- m_parser_state.lexer.reset();
- m_parser_state.current_token = m_parser_state.lexer.next();
- m_parser_state.error = Error::NoError;
- m_parser_state.error_token = { TokenType::Eof, 0, {} };
- m_parser_state.capture_group_minimum_lengths.clear();
- m_parser_state.capture_groups_count = 0;
- m_parser_state.named_capture_groups_count = 0;
- m_parser_state.named_capture_groups.clear();
- }
- Parser::Result Parser::parse(Optional<AllOptions> regex_options)
- {
- ByteCode::reset_checkpoint_serial_id();
- reset();
- if (regex_options.has_value())
- m_parser_state.regex_options = regex_options.value();
- if (parse_internal(m_parser_state.bytecode, m_parser_state.match_length_minimum))
- consume(TokenType::Eof, Error::InvalidPattern);
- else
- set_error(Error::InvalidPattern);
- dbgln_if(REGEX_DEBUG, "[PARSER] Produced bytecode with {} entries (opcodes + arguments)", m_parser_state.bytecode.size());
- return {
- move(m_parser_state.bytecode),
- move(m_parser_state.capture_groups_count),
- move(m_parser_state.named_capture_groups_count),
- move(m_parser_state.match_length_minimum),
- move(m_parser_state.error),
- move(m_parser_state.error_token),
- m_parser_state.named_capture_groups.keys(),
- m_parser_state.regex_options,
- };
- }
- ALWAYS_INLINE bool Parser::match_ordinary_characters()
- {
- // NOTE: This method must not be called during bracket and repetition parsing!
- // FIXME: Add assertion for that?
- auto type = m_parser_state.current_token.type();
- return ((type == TokenType::Char && m_parser_state.current_token.value() != "\\"sv) // NOTE: Backslash will only be matched as 'char' if it does not form a valid escape.
- || type == TokenType::Comma
- || type == TokenType::Slash
- || type == TokenType::EqualSign
- || type == TokenType::HyphenMinus
- || type == TokenType::Colon);
- }
- // =============================
- // Abstract Posix Parser
- // =============================
- ALWAYS_INLINE bool AbstractPosixParser::parse_bracket_expression(Vector<CompareTypeAndValuePair>& values, size_t& match_length_minimum)
- {
- for (; !done();) {
- if (match(TokenType::HyphenMinus)) {
- consume();
- if (values.is_empty() || (values.size() == 1 && values.last().type == CharacterCompareType::Inverse)) {
- // first in the bracket expression
- values.append({ CharacterCompareType::Char, (ByteCodeValueType)'-' });
- } else if (match(TokenType::RightBracket)) {
- // Last in the bracket expression
- values.append({ CharacterCompareType::Char, (ByteCodeValueType)'-' });
- } else if (values.last().type == CharacterCompareType::Char) {
- values.append({ CharacterCompareType::RangeExpressionDummy, 0 });
- if (done())
- return set_error(Error::MismatchingBracket);
- if (match(TokenType::HyphenMinus)) {
- consume();
- // Valid range, add ordinary character
- values.append({ CharacterCompareType::Char, (ByteCodeValueType)'-' });
- }
- } else {
- return set_error(Error::InvalidRange);
- }
- } else if (match(TokenType::Circumflex)) {
- auto t = consume();
- if (values.is_empty())
- values.append({ CharacterCompareType::Inverse, 0 });
- else
- values.append({ CharacterCompareType::Char, (ByteCodeValueType)*t.value().characters_without_null_termination() });
- } else if (match(TokenType::LeftBracket)) {
- consume();
- if (match(TokenType::Period)) {
- consume();
- // FIXME: Parse collating element, this is needed when we have locale support
- // This could have impact on length parameter, I guess.
- set_error(Error::InvalidCollationElement);
- consume(TokenType::Period, Error::InvalidCollationElement);
- consume(TokenType::RightBracket, Error::MismatchingBracket);
- } else if (match(TokenType::EqualSign)) {
- consume();
- // FIXME: Parse collating element, this is needed when we have locale support
- // This could have impact on length parameter, I guess.
- set_error(Error::InvalidCollationElement);
- consume(TokenType::EqualSign, Error::InvalidCollationElement);
- consume(TokenType::RightBracket, Error::MismatchingBracket);
- } else if (match(TokenType::Colon)) {
- consume();
- CharClass ch_class;
- // parse character class
- if (match(TokenType::Char)) {
- if (consume("alnum"))
- ch_class = CharClass::Alnum;
- else if (consume("alpha"))
- ch_class = CharClass::Alpha;
- else if (consume("blank"))
- ch_class = CharClass::Blank;
- else if (consume("cntrl"))
- ch_class = CharClass::Cntrl;
- else if (consume("digit"))
- ch_class = CharClass::Digit;
- else if (consume("graph"))
- ch_class = CharClass::Graph;
- else if (consume("lower"))
- ch_class = CharClass::Lower;
- else if (consume("print"))
- ch_class = CharClass::Print;
- else if (consume("punct"))
- ch_class = CharClass::Punct;
- else if (consume("space"))
- ch_class = CharClass::Space;
- else if (consume("upper"))
- ch_class = CharClass::Upper;
- else if (consume("xdigit"))
- ch_class = CharClass::Xdigit;
- else
- return set_error(Error::InvalidCharacterClass);
- values.append({ CharacterCompareType::CharClass, (ByteCodeValueType)ch_class });
- } else
- return set_error(Error::InvalidCharacterClass);
- // FIXME: we do not support locale specific character classes until locales are implemented
- consume(TokenType::Colon, Error::InvalidCharacterClass);
- consume(TokenType::RightBracket, Error::MismatchingBracket);
- } else {
- return set_error(Error::MismatchingBracket);
- }
- } else if (match(TokenType::RightBracket)) {
- if (values.is_empty() || (values.size() == 1 && values.last().type == CharacterCompareType::Inverse)) {
- // handle bracket as ordinary character
- values.append({ CharacterCompareType::Char, (ByteCodeValueType)*consume().value().characters_without_null_termination() });
- } else {
- // closing bracket expression
- break;
- }
- } else {
- values.append({ CharacterCompareType::Char, (ByteCodeValueType)skip() });
- }
- // check if range expression has to be completed...
- if (values.size() >= 3 && values.at(values.size() - 2).type == CharacterCompareType::RangeExpressionDummy) {
- if (values.last().type != CharacterCompareType::Char)
- return set_error(Error::InvalidRange);
- auto value2 = values.take_last();
- values.take_last(); // RangeExpressionDummy
- auto value1 = values.take_last();
- values.append({ CharacterCompareType::CharRange, static_cast<ByteCodeValueType>(CharRange { (u32)value1.value, (u32)value2.value }) });
- }
- }
- if (!values.is_empty()) {
- match_length_minimum = 1;
- if (values.first().type == CharacterCompareType::Inverse)
- match_length_minimum = 0;
- }
- return true;
- }
- // =============================
- // PosixBasic Parser
- // =============================
- bool PosixBasicParser::parse_internal(ByteCode& stack, size_t& match_length_minimum)
- {
- return parse_root(stack, match_length_minimum);
- }
- bool PosixBasicParser::parse_root(ByteCode& bytecode, size_t& match_length_minimum)
- {
- // basic_reg_exp : L_ANCHOR? RE_expression R_ANCHOR?
- if (match(TokenType::Circumflex)) {
- consume();
- bytecode.empend((ByteCodeValueType)OpCodeId::CheckBegin);
- }
- if (!parse_re_expression(bytecode, match_length_minimum))
- return false;
- if (match(TokenType::Dollar)) {
- consume();
- bytecode.empend((ByteCodeValueType)OpCodeId::CheckEnd);
- }
- return !has_error();
- }
- bool PosixBasicParser::parse_re_expression(ByteCode& bytecode, size_t& match_length_minimum)
- {
- // RE_expression : RE_expression? simple_RE
- while (!done()) {
- if (!parse_simple_re(bytecode, match_length_minimum))
- break;
- }
- return !has_error();
- }
- bool PosixBasicParser::parse_simple_re(ByteCode& bytecode, size_t& match_length_minimum)
- {
- // simple_RE : nondupl_RE RE_dupl_symbol?
- ByteCode simple_re_bytecode;
- size_t re_match_length_minimum = 0;
- if (!parse_nonduplicating_re(simple_re_bytecode, re_match_length_minimum))
- return false;
- // RE_dupl_symbol : '*' | Back_open_brace DUP_COUNT (',' DUP_COUNT?)? Back_close_brace
- if (match(TokenType::Asterisk)) {
- consume();
- ByteCode::transform_bytecode_repetition_any(simple_re_bytecode, true);
- } else if (try_skip("\\{"sv)) {
- auto read_number = [&]() -> Optional<size_t> {
- if (!match(TokenType::Char))
- return {};
- size_t value = 0;
- while (match(TokenType::Char)) {
- auto c = m_parser_state.current_token.value().substring_view(0, 1);
- auto c_value = c.to_number<unsigned>();
- if (!c_value.has_value())
- break;
- value *= 10;
- value += *c_value;
- consume();
- }
- return value;
- };
- size_t min_limit;
- Optional<size_t> max_limit;
- if (auto limit = read_number(); !limit.has_value())
- return set_error(Error::InvalidRepetitionMarker);
- else
- min_limit = *limit;
- if (match(TokenType::Comma)) {
- consume();
- max_limit = read_number();
- }
- if (!try_skip("\\}"sv))
- return set_error(Error::MismatchingBrace);
- if (max_limit.value_or(min_limit) < min_limit)
- return set_error(Error::InvalidBraceContent);
- if (min_limit > s_maximum_repetition_count || (max_limit.has_value() && *max_limit > s_maximum_repetition_count))
- return set_error(Error::InvalidBraceContent);
- auto min_repetition_mark_id = m_parser_state.repetition_mark_count++;
- auto max_repetition_mark_id = m_parser_state.repetition_mark_count++;
- ByteCode::transform_bytecode_repetition_min_max(simple_re_bytecode, min_limit, max_limit, min_repetition_mark_id, max_repetition_mark_id, true);
- match_length_minimum += re_match_length_minimum * min_limit;
- } else {
- match_length_minimum += re_match_length_minimum;
- }
- bytecode.extend(move(simple_re_bytecode));
- return true;
- }
- bool PosixBasicParser::parse_nonduplicating_re(ByteCode& bytecode, size_t& match_length_minimum)
- {
- // nondupl_RE : one_char_or_coll_elem_RE | Back_open_paren RE_expression Back_close_paren | BACKREF
- if (try_skip("\\("sv)) {
- TemporaryChange change { m_current_capture_group_depth, m_current_capture_group_depth + 1 };
- // Max number of addressable capture groups is 10, let's just be lenient
- // and accept 20; anything past that is probably a silly pattern anyway.
- if (m_current_capture_group_depth > 20)
- return set_error(Error::InvalidPattern);
- ByteCode capture_bytecode;
- size_t capture_length_minimum = 0;
- auto capture_group_index = ++m_parser_state.capture_groups_count;
- if (!parse_re_expression(capture_bytecode, capture_length_minimum))
- return false;
- if (!try_skip("\\)"sv))
- return set_error(Error::MismatchingParen);
- match_length_minimum += capture_length_minimum;
- if (capture_group_index <= number_of_addressable_capture_groups) {
- m_capture_group_minimum_lengths[capture_group_index - 1] = capture_length_minimum;
- m_capture_group_seen[capture_group_index - 1] = true;
- bytecode.insert_bytecode_group_capture_left(capture_group_index);
- }
- bytecode.extend(capture_bytecode);
- if (capture_group_index <= number_of_addressable_capture_groups)
- bytecode.insert_bytecode_group_capture_right(capture_group_index);
- return true;
- }
- for (size_t i = 1; i < 10; ++i) {
- char backref_name[2] { '\\', '0' };
- backref_name[1] += i;
- if (try_skip({ backref_name, 2 })) {
- if (!m_capture_group_seen[i - 1])
- return set_error(Error::InvalidNumber);
- match_length_minimum += m_capture_group_minimum_lengths[i - 1];
- bytecode.insert_bytecode_compare_values({ { CharacterCompareType::Reference, (ByteCodeValueType)i } });
- return true;
- }
- }
- return parse_one_char_or_collation_element(bytecode, match_length_minimum);
- }
- bool PosixBasicParser::parse_one_char_or_collation_element(ByteCode& bytecode, size_t& match_length_minimum)
- {
- // one_char_or_coll_elem_RE : ORD_CHAR | QUOTED_CHAR | '.' | bracket_expression
- if (match(TokenType::Period)) {
- consume();
- bytecode.insert_bytecode_compare_values({ { CharacterCompareType::AnyChar, 0 } });
- match_length_minimum += 1;
- return true;
- }
- // Dollars are special if at the end of a pattern.
- if (match(TokenType::Dollar)) {
- consume();
- // If we are at the end of a pattern, emit an end check instruction.
- if (match(TokenType::Eof)) {
- bytecode.empend((ByteCodeValueType)OpCodeId::CheckEnd);
- return true;
- }
- // We are not at the end of the string, so we should roll back and continue as normal.
- back(2);
- }
- if (match(TokenType::Char)) {
- auto ch = consume().value()[0];
- if (ch == '\\') {
- if (m_parser_state.regex_options.has_flag_set(AllFlags::Extra))
- return set_error(Error::InvalidPattern);
- // This was \<ORD_CHAR>, the spec does not define any behaviour for this but glibc regex ignores it - and so do we.
- return true;
- }
- bytecode.insert_bytecode_compare_values({ { CharacterCompareType::Char, (ByteCodeValueType)ch } });
- match_length_minimum += 1;
- return true;
- }
- // None of these are special in BRE.
- if (match(TokenType::Questionmark) || match(TokenType::RightParen) || match(TokenType::HyphenMinus)
- || match(TokenType::Circumflex) || match(TokenType::RightCurly) || match(TokenType::Comma) || match(TokenType::Colon)
- || match(TokenType::Dollar) || match(TokenType::EqualSign) || match(TokenType::LeftCurly) || match(TokenType::LeftParen)
- || match(TokenType::Pipe) || match(TokenType::Slash) || match(TokenType::RightBracket) || match(TokenType::RightParen)) {
- auto ch = consume().value()[0];
- bytecode.insert_bytecode_compare_values({ { CharacterCompareType::Char, (ByteCodeValueType)ch } });
- match_length_minimum += 1;
- return true;
- }
- if (match(TokenType::EscapeSequence)) {
- if (m_parser_state.current_token.value().is_one_of("\\)"sv, "\\}"sv, "\\("sv, "\\{"sv))
- return false;
- auto ch = consume().value()[1];
- bytecode.insert_bytecode_compare_values({ { CharacterCompareType::Char, (ByteCodeValueType)ch } });
- match_length_minimum += 1;
- return true;
- }
- Vector<CompareTypeAndValuePair> values;
- size_t bracket_minimum_length = 0;
- if (match(TokenType::LeftBracket)) {
- consume();
- if (!AbstractPosixParser::parse_bracket_expression(values, bracket_minimum_length))
- return false;
- consume(TokenType::RightBracket, Error::MismatchingBracket);
- if (!has_error())
- bytecode.insert_bytecode_compare_values(move(values));
- match_length_minimum += bracket_minimum_length;
- return !has_error();
- }
- return set_error(Error::InvalidPattern);
- }
- // =============================
- // PosixExtended Parser
- // =============================
- bool PosixExtendedParser::parse_internal(ByteCode& stack, size_t& match_length_minimum)
- {
- return parse_root(stack, match_length_minimum);
- }
- ALWAYS_INLINE bool PosixExtendedParser::match_repetition_symbol()
- {
- auto type = m_parser_state.current_token.type();
- return (type == TokenType::Asterisk
- || type == TokenType::Plus
- || type == TokenType::Questionmark
- || type == TokenType::LeftCurly);
- }
- ALWAYS_INLINE bool PosixExtendedParser::parse_repetition_symbol(ByteCode& bytecode_to_repeat, size_t& match_length_minimum)
- {
- if (match(TokenType::LeftCurly)) {
- consume();
- StringBuilder number_builder;
- while (match(TokenType::Char)) {
- number_builder.append(consume().value());
- }
- auto maybe_minimum = number_builder.to_byte_string().to_number<unsigned>();
- if (!maybe_minimum.has_value())
- return set_error(Error::InvalidBraceContent);
- auto minimum = maybe_minimum.value();
- match_length_minimum *= minimum;
- if (minimum > s_maximum_repetition_count)
- return set_error(Error::InvalidBraceContent);
- if (match(TokenType::Comma)) {
- consume();
- } else {
- auto repetition_mark_id = m_parser_state.repetition_mark_count++;
- ByteCode bytecode;
- bytecode.insert_bytecode_repetition_n(bytecode_to_repeat, minimum, repetition_mark_id);
- bytecode_to_repeat = move(bytecode);
- consume(TokenType::RightCurly, Error::MismatchingBrace);
- return !has_error();
- }
- Optional<u32> maybe_maximum {};
- number_builder.clear();
- while (match(TokenType::Char)) {
- number_builder.append(consume().value());
- }
- if (!number_builder.is_empty()) {
- auto value = number_builder.to_byte_string().to_number<unsigned>();
- if (!value.has_value() || minimum > value.value() || *value > s_maximum_repetition_count)
- return set_error(Error::InvalidBraceContent);
- maybe_maximum = value.value();
- }
- auto min_repetition_mark_id = m_parser_state.repetition_mark_count++;
- auto max_repetition_mark_id = m_parser_state.repetition_mark_count++;
- ByteCode::transform_bytecode_repetition_min_max(bytecode_to_repeat, minimum, maybe_maximum, min_repetition_mark_id, max_repetition_mark_id);
- consume(TokenType::RightCurly, Error::MismatchingBrace);
- return !has_error();
- }
- if (match(TokenType::Plus)) {
- consume();
- bool nongreedy = match(TokenType::Questionmark);
- if (nongreedy)
- consume();
- // Note: don't touch match_length_minimum, it's already correct
- ByteCode::transform_bytecode_repetition_min_one(bytecode_to_repeat, !nongreedy);
- return !has_error();
- }
- if (match(TokenType::Asterisk)) {
- consume();
- match_length_minimum = 0;
- bool nongreedy = match(TokenType::Questionmark);
- if (nongreedy)
- consume();
- ByteCode::transform_bytecode_repetition_any(bytecode_to_repeat, !nongreedy);
- return !has_error();
- }
- if (match(TokenType::Questionmark)) {
- consume();
- match_length_minimum = 0;
- bool nongreedy = match(TokenType::Questionmark);
- if (nongreedy)
- consume();
- ByteCode::transform_bytecode_repetition_zero_or_one(bytecode_to_repeat, !nongreedy);
- return !has_error();
- }
- return false;
- }
- ALWAYS_INLINE bool PosixExtendedParser::parse_bracket_expression(ByteCode& stack, size_t& match_length_minimum)
- {
- Vector<CompareTypeAndValuePair> values;
- if (!AbstractPosixParser::parse_bracket_expression(values, match_length_minimum))
- return false;
- if (!has_error())
- stack.insert_bytecode_compare_values(move(values));
- return !has_error();
- }
- ALWAYS_INLINE bool PosixExtendedParser::parse_sub_expression(ByteCode& stack, size_t& match_length_minimum)
- {
- ByteCode bytecode;
- size_t length = 0;
- bool should_parse_repetition_symbol { false };
- for (;;) {
- if (match_ordinary_characters()) {
- Token start_token = m_parser_state.current_token;
- Token last_token = m_parser_state.current_token;
- for (;;) {
- if (!match_ordinary_characters())
- break;
- ++length;
- last_token = consume();
- }
- if (length > 1) {
- // last character is inserted into 'bytecode' for duplication symbol handling
- auto new_length = length - ((match_repetition_symbol() && length > 1) ? 1 : 0);
- stack.insert_bytecode_compare_string({ start_token.value().characters_without_null_termination(), new_length });
- }
- if ((match_repetition_symbol() && length > 1) || length == 1) // Create own compare opcode for last character before duplication symbol
- bytecode.insert_bytecode_compare_values({ { CharacterCompareType::Char, (ByteCodeValueType)last_token.value().characters_without_null_termination()[0] } });
- should_parse_repetition_symbol = true;
- break;
- }
- if (m_parser_state.current_token.value() == "\\"sv) {
- if (m_parser_state.regex_options.has_flag_set(AllFlags::Extra))
- return set_error(Error::InvalidPattern);
- consume();
- continue;
- }
- if (match_repetition_symbol())
- return set_error(Error::InvalidRepetitionMarker);
- if (match(TokenType::Period)) {
- length = 1;
- consume();
- bytecode.insert_bytecode_compare_values({ { CharacterCompareType::AnyChar, 0 } });
- should_parse_repetition_symbol = true;
- break;
- }
- if (match(TokenType::EscapeSequence)) {
- length = 1;
- Token t = consume();
- dbgln_if(REGEX_DEBUG, "[PARSER] EscapeSequence with substring {}", t.value());
- bytecode.insert_bytecode_compare_values({ { CharacterCompareType::Char, (u32)t.value().characters_without_null_termination()[1] } });
- should_parse_repetition_symbol = true;
- break;
- }
- if (match(TokenType::LeftBracket)) {
- consume();
- ByteCode sub_ops;
- if (!parse_bracket_expression(sub_ops, length) || !sub_ops.size())
- return set_error(Error::InvalidBracketContent);
- bytecode.extend(move(sub_ops));
- consume(TokenType::RightBracket, Error::MismatchingBracket);
- should_parse_repetition_symbol = true;
- break;
- }
- if (match(TokenType::RightBracket)) {
- return set_error(Error::MismatchingBracket);
- }
- if (match(TokenType::RightCurly)) {
- return set_error(Error::MismatchingBrace);
- }
- if (match(TokenType::Circumflex)) {
- consume();
- bytecode.empend((ByteCodeValueType)OpCodeId::CheckBegin);
- break;
- }
- if (match(TokenType::Dollar)) {
- consume();
- bytecode.empend((ByteCodeValueType)OpCodeId::CheckEnd);
- break;
- }
- if (match(TokenType::RightParen))
- return false;
- if (match(TokenType::LeftParen)) {
- enum GroupMode {
- Normal,
- Lookahead,
- NegativeLookahead,
- Lookbehind,
- NegativeLookbehind,
- } group_mode { Normal };
- consume();
- Optional<StringView> capture_group_name;
- bool prevent_capture_group = false;
- if (match(TokenType::Questionmark)) {
- consume();
- if (match(TokenType::Colon)) {
- consume();
- prevent_capture_group = true;
- } else if (consume("<")) { // named capturing group
- Token start_token = m_parser_state.current_token;
- Token last_token = m_parser_state.current_token;
- size_t capture_group_name_length = 0;
- for (;;) {
- if (!match_ordinary_characters())
- return set_error(Error::InvalidNameForCaptureGroup);
- if (match(TokenType::Char) && m_parser_state.current_token.value()[0] == '>') {
- consume();
- break;
- }
- ++capture_group_name_length;
- last_token = consume();
- }
- capture_group_name = StringView(start_token.value().characters_without_null_termination(), capture_group_name_length);
- ++m_parser_state.named_capture_groups_count;
- } else if (match(TokenType::EqualSign)) { // positive lookahead
- consume();
- group_mode = Lookahead;
- } else if (consume("!")) { // negative lookahead
- group_mode = NegativeLookahead;
- } else if (consume("<")) {
- if (match(TokenType::EqualSign)) { // positive lookbehind
- consume();
- group_mode = Lookbehind;
- }
- if (consume("!")) // negative lookbehind
- group_mode = NegativeLookbehind;
- } else {
- return set_error(Error::InvalidRepetitionMarker);
- }
- }
- auto current_capture_group = m_parser_state.capture_groups_count;
- if (!(m_parser_state.regex_options & AllFlags::SkipSubExprResults || prevent_capture_group)) {
- bytecode.insert_bytecode_group_capture_left(current_capture_group);
- m_parser_state.capture_groups_count++;
- }
- ByteCode capture_group_bytecode;
- if (!parse_root(capture_group_bytecode, length))
- return set_error(Error::InvalidPattern);
- switch (group_mode) {
- case Normal:
- bytecode.extend(move(capture_group_bytecode));
- break;
- case Lookahead:
- bytecode.insert_bytecode_lookaround(move(capture_group_bytecode), ByteCode::LookAroundType::LookAhead, length);
- break;
- case NegativeLookahead:
- bytecode.insert_bytecode_lookaround(move(capture_group_bytecode), ByteCode::LookAroundType::NegatedLookAhead, length);
- break;
- case Lookbehind:
- bytecode.insert_bytecode_lookaround(move(capture_group_bytecode), ByteCode::LookAroundType::LookBehind, length);
- break;
- case NegativeLookbehind:
- bytecode.insert_bytecode_lookaround(move(capture_group_bytecode), ByteCode::LookAroundType::NegatedLookBehind, length);
- break;
- }
- consume(TokenType::RightParen, Error::MismatchingParen);
- if (!(m_parser_state.regex_options & AllFlags::SkipSubExprResults || prevent_capture_group)) {
- if (capture_group_name.has_value())
- bytecode.insert_bytecode_group_capture_right(current_capture_group, capture_group_name.value());
- else
- bytecode.insert_bytecode_group_capture_right(current_capture_group);
- }
- should_parse_repetition_symbol = true;
- break;
- }
- return false;
- }
- if (match_repetition_symbol()) {
- if (should_parse_repetition_symbol)
- parse_repetition_symbol(bytecode, length);
- else
- return set_error(Error::InvalidRepetitionMarker);
- }
- stack.extend(move(bytecode));
- match_length_minimum += length;
- return true;
- }
- bool PosixExtendedParser::parse_root(ByteCode& stack, size_t& match_length_minimum)
- {
- ByteCode bytecode_left;
- size_t match_length_minimum_left { 0 };
- if (match_repetition_symbol())
- return set_error(Error::InvalidRepetitionMarker);
- for (;;) {
- if (!parse_sub_expression(bytecode_left, match_length_minimum_left))
- break;
- if (match(TokenType::Pipe)) {
- consume();
- ByteCode bytecode_right;
- size_t match_length_minimum_right { 0 };
- if (!parse_root(bytecode_right, match_length_minimum_right) || bytecode_right.is_empty())
- return set_error(Error::InvalidPattern);
- ByteCode new_bytecode;
- new_bytecode.insert_bytecode_alternation(move(bytecode_left), move(bytecode_right));
- bytecode_left = move(new_bytecode);
- match_length_minimum_left = min(match_length_minimum_right, match_length_minimum_left);
- }
- }
- if (bytecode_left.is_empty())
- set_error(Error::EmptySubExpression);
- stack.extend(move(bytecode_left));
- match_length_minimum = match_length_minimum_left;
- return !has_error();
- }
- // =============================
- // ECMA262 Parser
- // =============================
- bool ECMA262Parser::parse_internal(ByteCode& stack, size_t& match_length_minimum)
- {
- auto unicode = m_parser_state.regex_options.has_flag_set(AllFlags::Unicode);
- auto unicode_sets = m_parser_state.regex_options.has_flag_set(AllFlags::UnicodeSets);
- if (unicode || unicode_sets) {
- return parse_pattern(stack, match_length_minimum, { .unicode = true, .named = true, .unicode_sets = unicode_sets });
- }
- ByteCode new_stack;
- size_t new_match_length = 0;
- auto res = parse_pattern(new_stack, new_match_length, { .unicode = false, .named = false, .unicode_sets = false });
- if (m_parser_state.named_capture_groups_count > 0) {
- reset();
- return parse_pattern(stack, match_length_minimum, { .unicode = false, .named = true, .unicode_sets = false });
- }
- if (!res)
- return false;
- stack.extend(new_stack);
- match_length_minimum = new_match_length;
- return res;
- }
- bool ECMA262Parser::parse_pattern(ByteCode& stack, size_t& match_length_minimum, ParseFlags flags)
- {
- return parse_disjunction(stack, match_length_minimum, flags);
- }
- bool ECMA262Parser::parse_disjunction(ByteCode& stack, size_t& match_length_minimum, ParseFlags flags)
- {
- size_t total_match_length_minimum = NumericLimits<size_t>::max();
- Vector<ByteCode> alternatives;
- while (true) {
- ByteCode alternative_stack;
- size_t alternative_minimum_length = 0;
- auto alt_ok = parse_alternative(alternative_stack, alternative_minimum_length, flags);
- if (!alt_ok)
- return false;
- alternatives.append(move(alternative_stack));
- total_match_length_minimum = min(alternative_minimum_length, total_match_length_minimum);
- if (!match(TokenType::Pipe))
- break;
- consume();
- }
- Optimizer::append_alternation(stack, alternatives.span());
- match_length_minimum = total_match_length_minimum;
- return true;
- }
- bool ECMA262Parser::parse_alternative(ByteCode& stack, size_t& match_length_minimum, ParseFlags flags)
- {
- for (;;) {
- if (match(TokenType::Eof))
- return true;
- if (parse_term(stack, match_length_minimum, flags))
- continue;
- return !has_error();
- }
- }
- bool ECMA262Parser::parse_term(ByteCode& stack, size_t& match_length_minimum, ParseFlags flags)
- {
- if (parse_assertion(stack, match_length_minimum, flags))
- return true;
- ByteCode atom_stack;
- size_t minimum_atom_length = 0;
- auto parse_with_quantifier = [&] {
- bool did_parse_one = false;
- if (m_should_use_browser_extended_grammar)
- did_parse_one = parse_extended_atom(atom_stack, minimum_atom_length, flags);
- if (!did_parse_one)
- did_parse_one = parse_atom(atom_stack, minimum_atom_length, flags);
- if (!did_parse_one)
- return false;
- VERIFY(did_parse_one);
- return parse_quantifier(atom_stack, minimum_atom_length, flags);
- };
- if (!parse_with_quantifier())
- return false;
- stack.extend(move(atom_stack));
- match_length_minimum += minimum_atom_length;
- return true;
- }
- bool ECMA262Parser::parse_assertion(ByteCode& stack, [[maybe_unused]] size_t& match_length_minimum, ParseFlags flags)
- {
- if (match(TokenType::Circumflex)) {
- consume();
- stack.empend((ByteCodeValueType)OpCodeId::CheckBegin);
- return true;
- }
- if (match(TokenType::Dollar)) {
- consume();
- stack.empend((ByteCodeValueType)OpCodeId::CheckEnd);
- return true;
- }
- if (try_skip("\\b"sv)) {
- stack.insert_bytecode_check_boundary(BoundaryCheckType::Word);
- return true;
- }
- if (try_skip("\\B"sv)) {
- stack.insert_bytecode_check_boundary(BoundaryCheckType::NonWord);
- return true;
- }
- if (match(TokenType::LeftParen)) {
- if (!try_skip("(?"sv))
- return false;
- if (done()) {
- set_error(Error::InvalidCaptureGroup);
- return false;
- }
- ByteCode assertion_stack;
- size_t length_dummy = 0;
- bool should_parse_forward_assertion = !m_should_use_browser_extended_grammar || flags.unicode;
- if (should_parse_forward_assertion && try_skip("="sv)) {
- if (!parse_inner_disjunction(assertion_stack, length_dummy, flags))
- return false;
- stack.insert_bytecode_lookaround(move(assertion_stack), ByteCode::LookAroundType::LookAhead);
- return true;
- }
- if (should_parse_forward_assertion && try_skip("!"sv)) {
- enter_capture_group_scope();
- ScopeGuard quit_scope {
- [this] {
- exit_capture_group_scope();
- }
- };
- if (!parse_inner_disjunction(assertion_stack, length_dummy, flags))
- return false;
- stack.insert_bytecode_lookaround(move(assertion_stack), ByteCode::LookAroundType::NegatedLookAhead);
- clear_all_capture_groups_in_scope(stack);
- return true;
- }
- if (m_should_use_browser_extended_grammar) {
- if (!flags.unicode) {
- if (parse_quantifiable_assertion(assertion_stack, match_length_minimum, flags)) {
- if (!parse_quantifier(assertion_stack, match_length_minimum, flags))
- return false;
- stack.extend(move(assertion_stack));
- return true;
- }
- }
- }
- if (try_skip("<="sv)) {
- if (!parse_inner_disjunction(assertion_stack, length_dummy, flags))
- return false;
- // FIXME: Somehow ensure that this assertion regexp has a fixed length.
- stack.insert_bytecode_lookaround(move(assertion_stack), ByteCode::LookAroundType::LookBehind, length_dummy);
- return true;
- }
- if (try_skip("<!"sv)) {
- enter_capture_group_scope();
- ScopeGuard quit_scope {
- [this] {
- exit_capture_group_scope();
- }
- };
- if (!parse_inner_disjunction(assertion_stack, length_dummy, flags))
- return false;
- stack.insert_bytecode_lookaround(move(assertion_stack), ByteCode::LookAroundType::NegatedLookBehind, length_dummy);
- clear_all_capture_groups_in_scope(stack);
- return true;
- }
- // If none of these matched, put the '(?' back.
- m_parser_state.lexer.back(3);
- m_parser_state.current_token = m_parser_state.lexer.next();
- return false;
- }
- return false;
- }
- bool ECMA262Parser::parse_inner_disjunction(ByteCode& bytecode_stack, size_t& length, ParseFlags flags)
- {
- auto disjunction_ok = parse_disjunction(bytecode_stack, length, flags);
- if (!disjunction_ok)
- return false;
- consume(TokenType::RightParen, Error::MismatchingParen);
- return true;
- }
- bool ECMA262Parser::parse_quantifiable_assertion(ByteCode& stack, size_t&, ParseFlags flags)
- {
- VERIFY(m_should_use_browser_extended_grammar);
- ByteCode assertion_stack;
- size_t match_length_minimum = 0;
- if (try_skip("="sv)) {
- if (!parse_inner_disjunction(assertion_stack, match_length_minimum, { .unicode = false, .named = flags.named, .unicode_sets = false }))
- return false;
- stack.insert_bytecode_lookaround(move(assertion_stack), ByteCode::LookAroundType::LookAhead);
- return true;
- }
- if (try_skip("!"sv)) {
- enter_capture_group_scope();
- ScopeGuard quit_scope {
- [this] {
- exit_capture_group_scope();
- }
- };
- if (!parse_inner_disjunction(assertion_stack, match_length_minimum, { .unicode = false, .named = flags.named, .unicode_sets = false }))
- return false;
- stack.insert_bytecode_lookaround(move(assertion_stack), ByteCode::LookAroundType::NegatedLookAhead);
- clear_all_capture_groups_in_scope(stack);
- return true;
- }
- return false;
- }
- StringView ECMA262Parser::read_digits_as_string(ReadDigitsInitialZeroState initial_zero, bool hex, int max_count, int min_count)
- {
- if (!match(TokenType::Char))
- return {};
- if (initial_zero == ReadDigitsInitialZeroState::Disallow && m_parser_state.current_token.value() == "0")
- return {};
- int count = 0;
- size_t offset = 0;
- auto start_token = m_parser_state.current_token;
- while (match(TokenType::Char)) {
- auto const c = m_parser_state.current_token.value();
- if (max_count > 0 && count >= max_count)
- break;
- if (hex && !AK::StringUtils::convert_to_uint_from_hex(c).has_value())
- break;
- if (!hex && !c.to_number<unsigned>().has_value())
- break;
- offset += consume().value().length();
- ++count;
- }
- if (count < min_count)
- return {};
- return StringView { start_token.value().characters_without_null_termination(), offset };
- }
- Optional<unsigned> ECMA262Parser::read_digits(ECMA262Parser::ReadDigitsInitialZeroState initial_zero, bool hex, int max_count, int min_count)
- {
- auto str = read_digits_as_string(initial_zero, hex, max_count, min_count);
- if (str.is_empty())
- return {};
- if (hex)
- return AK::StringUtils::convert_to_uint_from_hex(str);
- return str.to_number<unsigned>();
- }
- bool ECMA262Parser::parse_quantifier(ByteCode& stack, size_t& match_length_minimum, ParseFlags flags)
- {
- enum class Repetition {
- OneOrMore,
- ZeroOrMore,
- Optional,
- Explicit,
- None,
- } repetition_mark { Repetition::None };
- bool ungreedy = false;
- Optional<u64> repeat_min, repeat_max;
- if (match(TokenType::Asterisk)) {
- consume();
- repetition_mark = Repetition::ZeroOrMore;
- } else if (match(TokenType::Plus)) {
- consume();
- repetition_mark = Repetition::OneOrMore;
- } else if (match(TokenType::Questionmark)) {
- consume();
- repetition_mark = Repetition::Optional;
- } else if (match(TokenType::LeftCurly)) {
- repetition_mark = Repetition::Explicit;
- if (!parse_interval_quantifier(repeat_min, repeat_max)) {
- if (flags.unicode) {
- // Invalid interval quantifiers are disallowed in Unicode mod - they must be escaped with '\{'.
- set_error(Error::InvalidPattern);
- }
- return !has_error();
- }
- } else {
- return true;
- }
- if (match(TokenType::Questionmark)) {
- consume();
- ungreedy = true;
- }
- switch (repetition_mark) {
- case Repetition::OneOrMore:
- ByteCode::transform_bytecode_repetition_min_one(stack, !ungreedy);
- break;
- case Repetition::ZeroOrMore:
- ByteCode::transform_bytecode_repetition_any(stack, !ungreedy);
- match_length_minimum = 0;
- break;
- case Repetition::Optional:
- ByteCode::transform_bytecode_repetition_zero_or_one(stack, !ungreedy);
- match_length_minimum = 0;
- break;
- case Repetition::Explicit: {
- auto min_repetition_mark_id = m_parser_state.repetition_mark_count++;
- auto max_repetition_mark_id = m_parser_state.repetition_mark_count++;
- ByteCode::transform_bytecode_repetition_min_max(stack, repeat_min.value(), repeat_max, min_repetition_mark_id, max_repetition_mark_id, !ungreedy);
- match_length_minimum *= repeat_min.value();
- break;
- }
- case Repetition::None:
- VERIFY_NOT_REACHED();
- }
- return true;
- }
- bool ECMA262Parser::parse_interval_quantifier(Optional<u64>& repeat_min, Optional<u64>& repeat_max)
- {
- VERIFY(match(TokenType::LeftCurly));
- consume();
- auto chars_consumed = 1;
- auto low_bound_string = read_digits_as_string();
- chars_consumed += low_bound_string.length();
- auto low_bound = low_bound_string.to_number<u64>();
- if (!low_bound.has_value()) {
- if (!m_should_use_browser_extended_grammar && done())
- return set_error(Error::MismatchingBrace);
- back(chars_consumed + !done());
- return false;
- }
- repeat_min = low_bound.value();
- if (match(TokenType::Comma)) {
- consume();
- ++chars_consumed;
- auto high_bound_string = read_digits_as_string();
- auto high_bound = high_bound_string.to_number<u64>();
- if (high_bound.has_value()) {
- repeat_max = high_bound.value();
- chars_consumed += high_bound_string.length();
- }
- } else {
- repeat_max = repeat_min;
- }
- if (!match(TokenType::RightCurly)) {
- if (!m_should_use_browser_extended_grammar && done())
- return set_error(Error::MismatchingBrace);
- back(chars_consumed + !done());
- return false;
- }
- consume();
- ++chars_consumed;
- if (repeat_max.has_value()) {
- if (repeat_min.value() > repeat_max.value())
- set_error(Error::InvalidBraceContent);
- }
- if ((*repeat_min > s_ecma262_maximum_repetition_count) || (repeat_max.has_value() && (*repeat_max > s_ecma262_maximum_repetition_count)))
- return set_error(Error::InvalidBraceContent);
- return true;
- }
- bool ECMA262Parser::parse_atom(ByteCode& stack, size_t& match_length_minimum, ParseFlags flags)
- {
- if (match(TokenType::EscapeSequence)) {
- // Also part of AtomEscape.
- auto token = consume();
- match_length_minimum += 1;
- stack.insert_bytecode_compare_values({ { CharacterCompareType::Char, (u8)token.value()[1] } });
- return true;
- }
- if (try_skip("\\"sv)) {
- // AtomEscape.
- return parse_atom_escape(stack, match_length_minimum, flags);
- }
- if (match(TokenType::LeftBracket)) {
- // Character class.
- return parse_character_class(stack, match_length_minimum, flags);
- }
- if (match(TokenType::LeftParen)) {
- // Non-capturing group, or a capture group.
- return parse_capture_group(stack, match_length_minimum, flags);
- }
- if (match(TokenType::Period)) {
- consume();
- match_length_minimum += 1;
- stack.insert_bytecode_compare_values({ { CharacterCompareType::AnyChar, 0 } });
- return true;
- }
- if (match(TokenType::Circumflex) || match(TokenType::Dollar) || match(TokenType::RightParen)
- || match(TokenType::Pipe) || match(TokenType::Plus) || match(TokenType::Asterisk)
- || match(TokenType::Questionmark)) {
- return false;
- }
- if (match(TokenType::RightBracket) || match(TokenType::RightCurly) || match(TokenType::LeftCurly)) {
- if (flags.unicode)
- return set_error(Error::InvalidPattern);
- if (m_should_use_browser_extended_grammar) {
- auto token = consume();
- match_length_minimum += 1;
- stack.insert_bytecode_compare_values({ { CharacterCompareType::Char, (u8)token.value()[0] } });
- return true;
- }
- return false;
- }
- if (match_ordinary_characters()) {
- auto token = consume().value();
- match_length_minimum += 1;
- stack.insert_bytecode_compare_values({ { CharacterCompareType::Char, (u8)token[0] } });
- return true;
- }
- set_error(Error::InvalidPattern);
- return false;
- }
- bool ECMA262Parser::parse_extended_atom(ByteCode&, size_t&, ParseFlags)
- {
- // Note: This includes only rules *not* present in parse_atom()
- VERIFY(m_should_use_browser_extended_grammar);
- return parse_invalid_braced_quantifier(); // FAIL FAIL FAIL
- }
- bool ECMA262Parser::parse_invalid_braced_quantifier()
- {
- if (!match(TokenType::LeftCurly))
- return false;
- consume();
- size_t chars_consumed = 1;
- auto low_bound = read_digits_as_string();
- StringView high_bound;
- if (low_bound.is_empty()) {
- back(chars_consumed + !done());
- return false;
- }
- chars_consumed += low_bound.length();
- if (match(TokenType::Comma)) {
- consume();
- ++chars_consumed;
- high_bound = read_digits_as_string();
- chars_consumed += high_bound.length();
- }
- if (!match(TokenType::RightCurly)) {
- back(chars_consumed + !done());
- return false;
- }
- consume();
- set_error(Error::InvalidPattern);
- return true;
- }
- bool ECMA262Parser::parse_character_escape(Vector<CompareTypeAndValuePair>& compares, size_t& match_length_minimum, ParseFlags flags)
- {
- // CharacterEscape > ControlEscape
- if (try_skip("f"sv)) {
- match_length_minimum += 1;
- compares.append({ CharacterCompareType::Char, (ByteCodeValueType)'\f' });
- return true;
- }
- if (try_skip("n"sv)) {
- match_length_minimum += 1;
- compares.append({ CharacterCompareType::Char, (ByteCodeValueType)'\n' });
- return true;
- }
- if (try_skip("r"sv)) {
- match_length_minimum += 1;
- compares.append({ CharacterCompareType::Char, (ByteCodeValueType)'\r' });
- return true;
- }
- if (try_skip("t"sv)) {
- match_length_minimum += 1;
- compares.append({ CharacterCompareType::Char, (ByteCodeValueType)'\t' });
- return true;
- }
- if (try_skip("v"sv)) {
- match_length_minimum += 1;
- compares.append({ CharacterCompareType::Char, (ByteCodeValueType)'\v' });
- return true;
- }
- // CharacterEscape > ControlLetter
- if (try_skip("c"sv)) {
- for (auto c : s_alphabetic_characters) {
- if (try_skip({ &c, 1 })) {
- match_length_minimum += 1;
- compares.append({ CharacterCompareType::Char, (ByteCodeValueType)(c % 32) });
- return true;
- }
- }
- if (flags.unicode) {
- set_error(Error::InvalidPattern);
- return false;
- }
- if (m_should_use_browser_extended_grammar) {
- back(1 + (done() ? 0 : 1));
- compares.append({ CharacterCompareType::Char, (ByteCodeValueType)'\\' });
- match_length_minimum += 1;
- return true;
- }
- // Allow '\c' in non-unicode mode, just matches 'c'.
- match_length_minimum += 1;
- compares.append({ CharacterCompareType::Char, (ByteCodeValueType)'c' });
- return true;
- }
- // '\0'
- if (try_skip("0"sv)) {
- if (!lookahead_any(s_decimal_characters)) {
- match_length_minimum += 1;
- compares.append({ CharacterCompareType::Char, (ByteCodeValueType)0 });
- return true;
- }
- back();
- }
- // LegacyOctalEscapeSequence
- if (m_should_use_browser_extended_grammar) {
- if (!flags.unicode) {
- if (auto escape = parse_legacy_octal_escape(); escape.has_value()) {
- compares.append({ CharacterCompareType::Char, (ByteCodeValueType)escape.value() });
- match_length_minimum += 1;
- return true;
- }
- }
- }
- // HexEscape
- if (try_skip("x"sv)) {
- if (auto hex_escape = read_digits(ReadDigitsInitialZeroState::Allow, true, 2, 2); hex_escape.has_value()) {
- match_length_minimum += 1;
- compares.append({ CharacterCompareType::Char, (ByteCodeValueType)hex_escape.value() });
- return true;
- }
- if (!flags.unicode) {
- // '\x' is allowed in non-unicode mode, just matches 'x'.
- match_length_minimum += 1;
- compares.append({ CharacterCompareType::Char, (ByteCodeValueType)'x' });
- return true;
- }
- set_error(Error::InvalidPattern);
- return false;
- }
- if (try_skip("u"sv)) {
- if (auto code_point = consume_escaped_code_point(flags.unicode); code_point.has_value()) {
- match_length_minimum += 1;
- compares.append({ CharacterCompareType::Char, (ByteCodeValueType)code_point.value() });
- return true;
- }
- return false;
- }
- // IdentityEscape
- for (auto ch : identity_escape_characters(flags.unicode, m_should_use_browser_extended_grammar)) {
- if (try_skip({ &ch, 1 })) {
- match_length_minimum += 1;
- compares.append({ CharacterCompareType::Char, (ByteCodeValueType)ch });
- return true;
- }
- }
- if (flags.unicode) {
- if (try_skip("/"sv)) {
- match_length_minimum += 1;
- compares.append({ CharacterCompareType::Char, (ByteCodeValueType)'/' });
- return true;
- }
- }
- return false;
- }
- bool ECMA262Parser::parse_atom_escape(ByteCode& stack, size_t& match_length_minimum, ParseFlags flags)
- {
- if (auto escape_str = read_digits_as_string(ReadDigitsInitialZeroState::Disallow); !escape_str.is_empty()) {
- if (auto escape = escape_str.to_number<unsigned>(); escape.has_value()) {
- // See if this is a "back"-reference (we've already parsed the group it refers to)
- auto maybe_length = m_parser_state.capture_group_minimum_lengths.get(escape.value());
- if (maybe_length.has_value()) {
- match_length_minimum += maybe_length.value();
- stack.insert_bytecode_compare_values({ { CharacterCompareType::Reference, (ByteCodeValueType)escape.value() } });
- return true;
- }
- // It's not a pattern seen before, so we have to see if it's a valid reference to a future group.
- if (escape.value() <= ensure_total_number_of_capturing_parenthesis()) {
- // This refers to a future group, and it will _always_ be matching an empty string
- // So just match nothing and move on.
- return true;
- }
- if (!m_should_use_browser_extended_grammar) {
- set_error(Error::InvalidNumber);
- return false;
- }
- }
- // If not, put the characters back.
- back(escape_str.length() + (done() ? 0 : 1));
- }
- Vector<CompareTypeAndValuePair> escape_compares;
- if (parse_character_escape(escape_compares, match_length_minimum, flags)) {
- stack.insert_bytecode_compare_values(move(escape_compares));
- return true;
- }
- if (flags.named && try_skip("k"sv)) {
- auto name = read_capture_group_specifier(true);
- if (name.is_empty()) {
- set_error(Error::InvalidNameForCaptureGroup);
- return false;
- }
- auto maybe_capture_group = m_parser_state.named_capture_groups.get(name);
- if (!maybe_capture_group.has_value()) {
- set_error(Error::InvalidNameForCaptureGroup);
- return false;
- }
- match_length_minimum += maybe_capture_group->minimum_length;
- stack.insert_bytecode_compare_values({ { CharacterCompareType::Reference, (ByteCodeValueType)maybe_capture_group->group_index } });
- return true;
- }
- if (flags.unicode) {
- PropertyEscape property {};
- bool negated = false;
- if (parse_unicode_property_escape(property, negated)) {
- Vector<CompareTypeAndValuePair> compares;
- if (negated)
- compares.empend(CompareTypeAndValuePair { CharacterCompareType::Inverse, 0 });
- property.visit(
- [&](Unicode::Property property) {
- compares.empend(CompareTypeAndValuePair { CharacterCompareType::Property, (ByteCodeValueType)property.value() });
- },
- [&](Unicode::GeneralCategory general_category) {
- compares.empend(CompareTypeAndValuePair { CharacterCompareType::GeneralCategory, (ByteCodeValueType)general_category.value() });
- },
- [&](Script script) {
- if (script.is_extension)
- compares.empend(CompareTypeAndValuePair { CharacterCompareType::ScriptExtension, (ByteCodeValueType)script.script.value() });
- else
- compares.empend(CompareTypeAndValuePair { CharacterCompareType::Script, (ByteCodeValueType)script.script.value() });
- },
- [](Empty&) { VERIFY_NOT_REACHED(); });
- stack.insert_bytecode_compare_values(move(compares));
- match_length_minimum += 1;
- return true;
- }
- }
- if (done())
- return set_error(Error::InvalidTrailingEscape);
- bool negate = false;
- auto ch = parse_character_class_escape(negate);
- if (!ch.has_value()) {
- if (!flags.unicode) {
- // Allow all SourceCharacter's as escapes here.
- auto token = consume();
- match_length_minimum += 1;
- stack.insert_bytecode_compare_values({ { CharacterCompareType::Char, (u8)token.value()[0] } });
- return true;
- }
- set_error(Error::InvalidCharacterClass);
- return false;
- }
- Vector<CompareTypeAndValuePair> compares;
- if (negate)
- compares.empend(CompareTypeAndValuePair { CharacterCompareType::Inverse, 0 });
- compares.empend(CompareTypeAndValuePair { CharacterCompareType::CharClass, (ByteCodeValueType)ch.value() });
- match_length_minimum += 1;
- stack.insert_bytecode_compare_values(move(compares));
- return true;
- }
- Optional<u8> ECMA262Parser::parse_legacy_octal_escape()
- {
- constexpr auto all_octal_digits = "01234567"sv;
- auto read_octal_digit = [&](auto start, auto end, bool should_ensure_no_following_octal_digit) -> Optional<u8> {
- for (char c = '0' + start; c <= '0' + end; ++c) {
- if (try_skip({ &c, 1 })) {
- if (!should_ensure_no_following_octal_digit || !lookahead_any(all_octal_digits))
- return c - '0';
- back(2);
- return {};
- }
- }
- return {};
- };
- // OctalDigit(1)
- if (auto digit = read_octal_digit(0, 7, true); digit.has_value()) {
- return digit.value();
- }
- // OctalDigit(2)
- if (auto left_digit = read_octal_digit(0, 3, false); left_digit.has_value()) {
- if (auto right_digit = read_octal_digit(0, 7, true); right_digit.has_value()) {
- return left_digit.value() * 8 + right_digit.value();
- }
- back(2);
- }
- // OctalDigit(2)
- if (auto left_digit = read_octal_digit(4, 7, false); left_digit.has_value()) {
- if (auto right_digit = read_octal_digit(0, 7, false); right_digit.has_value()) {
- return left_digit.value() * 8 + right_digit.value();
- }
- back(2);
- }
- // OctalDigit(3)
- if (auto left_digit = read_octal_digit(0, 3, false); left_digit.has_value()) {
- size_t chars_consumed = 1;
- if (auto mid_digit = read_octal_digit(0, 7, false); mid_digit.has_value()) {
- ++chars_consumed;
- if (auto right_digit = read_octal_digit(0, 7, false); right_digit.has_value()) {
- return left_digit.value() * 64 + mid_digit.value() * 8 + right_digit.value();
- }
- }
- back(chars_consumed);
- }
- return {};
- }
- Optional<CharClass> ECMA262Parser::parse_character_class_escape(bool& negate, bool expect_backslash)
- {
- if (expect_backslash && !try_skip("\\"sv))
- return {};
- // CharacterClassEscape
- CharClass ch_class;
- if (try_skip("d"sv)) {
- ch_class = CharClass::Digit;
- } else if (try_skip("D"sv)) {
- ch_class = CharClass::Digit;
- negate = true;
- } else if (try_skip("s"sv)) {
- ch_class = CharClass::Space;
- } else if (try_skip("S"sv)) {
- ch_class = CharClass::Space;
- negate = true;
- } else if (try_skip("w"sv)) {
- ch_class = CharClass::Word;
- } else if (try_skip("W"sv)) {
- ch_class = CharClass::Word;
- negate = true;
- } else {
- return {};
- }
- return ch_class;
- }
- bool ECMA262Parser::parse_character_class(ByteCode& stack, size_t& match_length_minimum, ParseFlags flags)
- {
- consume(TokenType::LeftBracket, Error::InvalidPattern);
- Vector<CompareTypeAndValuePair> compares;
- auto uses_explicit_or_semantics = false;
- if (match(TokenType::Circumflex)) {
- // Negated charclass
- consume();
- compares.empend(CompareTypeAndValuePair { CharacterCompareType::Inverse, 0 });
- uses_explicit_or_semantics = true;
- }
- // ClassContents :: [empty]
- if (match(TokenType::RightBracket)) {
- consume();
- // Should only have at most an 'Inverse'
- VERIFY(compares.size() <= 1);
- stack.insert_bytecode_compare_values(move(compares));
- return true;
- }
- // ClassContents :: [~UnicodeSetsMode] NonemptyClassRanges[?UnicodeMode]
- if (!flags.unicode_sets && !parse_nonempty_class_ranges(compares, flags))
- return false;
- // ClassContents :: [+UnicodeSetsMode] ClassSetExpression
- if (flags.unicode_sets && !parse_class_set_expression(compares))
- return false;
- if (uses_explicit_or_semantics && compares.size() > 2) {
- compares.insert(1, CompareTypeAndValuePair { CharacterCompareType::Or, 0 });
- compares.empend(CompareTypeAndValuePair { CharacterCompareType::EndAndOr, 0 });
- }
- match_length_minimum += 1;
- stack.insert_bytecode_compare_values(move(compares));
- return true;
- }
- struct CharClassRangeElement {
- union {
- CharClass character_class;
- u32 code_point { 0 };
- Unicode::Property property;
- Unicode::GeneralCategory general_category;
- Unicode::Script script;
- };
- bool is_negated { false };
- bool is_character_class { false };
- bool is_property { false };
- bool is_general_category { false };
- bool is_script { false };
- bool is_script_extension { false };
- };
- bool ECMA262Parser::parse_nonempty_class_ranges(Vector<CompareTypeAndValuePair>& ranges, ParseFlags flags)
- {
- auto read_class_atom_no_dash = [&]() -> Optional<CharClassRangeElement> {
- if (match(TokenType::EscapeSequence)) {
- auto token = consume().value();
- return { CharClassRangeElement { .code_point = (u32)token[1], .is_character_class = false } };
- }
- if (try_skip("\\"sv)) {
- if (done()) {
- set_error(Error::InvalidTrailingEscape);
- return {};
- }
- if (try_skip("f"sv))
- return { CharClassRangeElement { .code_point = '\f', .is_character_class = false } };
- if (try_skip("n"sv))
- return { CharClassRangeElement { .code_point = '\n', .is_character_class = false } };
- if (try_skip("r"sv))
- return { CharClassRangeElement { .code_point = '\r', .is_character_class = false } };
- if (try_skip("t"sv))
- return { CharClassRangeElement { .code_point = '\t', .is_character_class = false } };
- if (try_skip("v"sv))
- return { CharClassRangeElement { .code_point = '\v', .is_character_class = false } };
- if (try_skip("b"sv))
- return { CharClassRangeElement { .code_point = '\b', .is_character_class = false } };
- if (try_skip("/"sv))
- return { CharClassRangeElement { .code_point = '/', .is_character_class = false } };
- // CharacterEscape > ControlLetter
- if (try_skip("c"sv)) {
- for (auto c : s_alphabetic_characters) {
- if (try_skip({ &c, 1 })) {
- return { CharClassRangeElement { .code_point = (u32)(c % 32), .is_character_class = false } };
- }
- }
- if (flags.unicode) {
- set_error(Error::InvalidPattern);
- return {};
- }
- if (m_should_use_browser_extended_grammar) {
- for (auto c = '0'; c <= '9'; ++c) {
- if (try_skip({ &c, 1 }))
- return { CharClassRangeElement { .code_point = (u32)(c % 32), .is_character_class = false } };
- }
- if (try_skip("_"sv))
- return { CharClassRangeElement { .code_point = (u32)('_' % 32), .is_character_class = false } };
- back(1 + !done());
- return { CharClassRangeElement { .code_point = '\\', .is_character_class = false } };
- }
- }
- // '\0'
- if (try_skip("0"sv)) {
- if (!lookahead_any(s_decimal_characters))
- return { CharClassRangeElement { .code_point = 0, .is_character_class = false } };
- back();
- }
- // LegacyOctalEscapeSequence
- if (m_should_use_browser_extended_grammar && !flags.unicode) {
- if (auto escape = parse_legacy_octal_escape(); escape.has_value())
- return { CharClassRangeElement { .code_point = escape.value(), .is_character_class = false } };
- }
- // HexEscape
- if (try_skip("x"sv)) {
- if (auto hex_escape = read_digits(ReadDigitsInitialZeroState::Allow, true, 2, 2); hex_escape.has_value()) {
- return { CharClassRangeElement { .code_point = hex_escape.value(), .is_character_class = false } };
- } else if (!flags.unicode) {
- // '\x' is allowed in non-unicode mode, just matches 'x'.
- return { CharClassRangeElement { .code_point = 'x', .is_character_class = false } };
- } else {
- set_error(Error::InvalidPattern);
- return {};
- }
- }
- if (try_skip("u"sv)) {
- if (auto code_point = consume_escaped_code_point(flags.unicode); code_point.has_value()) {
- // FIXME: While code point ranges are supported, code point matches as "Char" are not!
- return { CharClassRangeElement { .code_point = code_point.value(), .is_character_class = false } };
- }
- return {};
- }
- // IdentityEscape
- for (auto ch : identity_escape_characters(flags.unicode, m_should_use_browser_extended_grammar)) {
- if (try_skip({ &ch, 1 }))
- return { CharClassRangeElement { .code_point = (u32)ch, .is_character_class = false } };
- }
- if (flags.unicode) {
- if (try_skip("-"sv))
- return { CharClassRangeElement { .code_point = '-', .is_character_class = false } };
- PropertyEscape property {};
- bool negated = false;
- if (parse_unicode_property_escape(property, negated)) {
- return property.visit(
- [&](Unicode::Property property) {
- return CharClassRangeElement { .property = property, .is_negated = negated, .is_character_class = true, .is_property = true };
- },
- [&](Unicode::GeneralCategory general_category) {
- return CharClassRangeElement { .general_category = general_category, .is_negated = negated, .is_character_class = true, .is_general_category = true };
- },
- [&](Script script) {
- if (script.is_extension)
- return CharClassRangeElement { .script = script.script, .is_negated = negated, .is_character_class = true, .is_script_extension = true };
- return CharClassRangeElement { .script = script.script, .is_negated = negated, .is_character_class = true, .is_script = true };
- },
- [](Empty&) -> CharClassRangeElement { VERIFY_NOT_REACHED(); });
- }
- }
- if (try_skip("d"sv))
- return { CharClassRangeElement { .character_class = CharClass::Digit, .is_character_class = true } };
- if (try_skip("s"sv))
- return { CharClassRangeElement { .character_class = CharClass::Space, .is_character_class = true } };
- if (try_skip("w"sv))
- return { CharClassRangeElement { .character_class = CharClass::Word, .is_character_class = true } };
- if (try_skip("D"sv))
- return { CharClassRangeElement { .character_class = CharClass::Digit, .is_negated = true, .is_character_class = true } };
- if (try_skip("S"sv))
- return { CharClassRangeElement { .character_class = CharClass::Space, .is_negated = true, .is_character_class = true } };
- if (try_skip("W"sv))
- return { CharClassRangeElement { .character_class = CharClass::Word, .is_negated = true, .is_character_class = true } };
- if (!flags.unicode) {
- // Any unrecognised escape is allowed in non-unicode mode.
- return { CharClassRangeElement { .code_point = (u32)skip(), .is_character_class = false } };
- }
- set_error(Error::InvalidPattern);
- return {};
- }
- if (match(TokenType::Eof)) {
- set_error(Error::MismatchingBracket);
- return {};
- }
- if (match(TokenType::RightBracket) || match(TokenType::HyphenMinus))
- return {};
- // Allow any (other) SourceCharacter.
- return { CharClassRangeElement { .code_point = (u32)skip(), .is_character_class = false } };
- };
- auto read_class_atom = [&]() -> Optional<CharClassRangeElement> {
- if (match(TokenType::HyphenMinus)) {
- consume();
- return { CharClassRangeElement { .code_point = '-', .is_character_class = false } };
- }
- return read_class_atom_no_dash();
- };
- auto empend_atom = [&](auto& atom) {
- if (atom.is_character_class) {
- if (atom.is_negated)
- ranges.empend(CompareTypeAndValuePair { CharacterCompareType::TemporaryInverse, 0 });
- if (atom.is_property)
- ranges.empend(CompareTypeAndValuePair { CharacterCompareType::Property, (ByteCodeValueType)(atom.property.value()) });
- else if (atom.is_general_category)
- ranges.empend(CompareTypeAndValuePair { CharacterCompareType::GeneralCategory, (ByteCodeValueType)(atom.general_category.value()) });
- else if (atom.is_script)
- ranges.empend(CompareTypeAndValuePair { CharacterCompareType::Script, (ByteCodeValueType)(atom.script.value()) });
- else if (atom.is_script_extension)
- ranges.empend(CompareTypeAndValuePair { CharacterCompareType::ScriptExtension, (ByteCodeValueType)(atom.script.value()) });
- else
- ranges.empend(CompareTypeAndValuePair { CharacterCompareType::CharClass, (ByteCodeValueType)atom.character_class });
- } else {
- VERIFY(!atom.is_negated);
- ranges.empend(CompareTypeAndValuePair { CharacterCompareType::Char, atom.code_point });
- }
- };
- while (!match(TokenType::RightBracket)) {
- if (match(TokenType::Eof)) {
- set_error(Error::MismatchingBracket);
- return false;
- }
- auto first_atom = read_class_atom();
- if (!first_atom.has_value())
- return false;
- if (match(TokenType::HyphenMinus)) {
- consume();
- if (match(TokenType::RightBracket)) {
- // Allow '-' as the last element in a charclass, even after an atom.
- m_parser_state.lexer.back(2); // -]
- m_parser_state.current_token = m_parser_state.lexer.next();
- goto read_as_single_atom;
- }
- auto second_atom = read_class_atom();
- if (!second_atom.has_value())
- return false;
- if (first_atom.value().is_character_class || second_atom.value().is_character_class) {
- if (m_should_use_browser_extended_grammar) {
- if (flags.unicode) {
- set_error(Error::InvalidRange);
- return false;
- }
- // CharacterRangeOrUnion > !Unicode > CharClass
- empend_atom(*first_atom);
- ranges.empend(CompareTypeAndValuePair { CharacterCompareType::Char, (ByteCodeValueType)'-' });
- empend_atom(*second_atom);
- continue;
- }
- set_error(Error::InvalidRange);
- return false;
- }
- if (first_atom.value().code_point > second_atom.value().code_point) {
- set_error(Error::InvalidRange);
- return false;
- }
- VERIFY(!first_atom.value().is_negated);
- VERIFY(!second_atom.value().is_negated);
- ranges.empend(CompareTypeAndValuePair { CharacterCompareType::CharRange, CharRange { first_atom.value().code_point, second_atom.value().code_point } });
- continue;
- }
- read_as_single_atom:;
- auto atom = first_atom.value();
- empend_atom(atom);
- }
- consume(TokenType::RightBracket, Error::MismatchingBracket);
- return true;
- }
- bool ECMA262Parser::parse_class_set_expression(Vector<CompareTypeAndValuePair>& compares)
- {
- auto start_position = tell();
- // ClassSetExpression :: ClassUnion | ClassIntersection | ClassSubtraction
- if (parse_class_subtraction(compares)) {
- consume(TokenType::RightBracket, Error::MismatchingBracket);
- return true;
- }
- if (has_error())
- return false;
- back(tell() - start_position + 1);
- if (parse_class_intersection(compares)) {
- consume(TokenType::RightBracket, Error::MismatchingBracket);
- return true;
- }
- if (has_error())
- return false;
- back(tell() - start_position + 1);
- if (parse_class_union(compares)) {
- consume(TokenType::RightBracket, Error::MismatchingBracket);
- return true;
- }
- return false;
- }
- bool ECMA262Parser::parse_class_union(Vector<regex::CompareTypeAndValuePair>& compares)
- {
- auto start_position = tell();
- ArmedScopeGuard restore_position { [&] { back(tell() - start_position + 1); } };
- auto first = true;
- // ClassUnion :: ClassSetRange ClassUnion[opt] | ClassSetOperand ClassUnion[opt]
- for (;;) {
- if (!parse_class_set_range(compares)) {
- if (has_error() || match(TokenType::RightBracket))
- break;
- if (!parse_class_set_operand(compares)) {
- if (first || has_error())
- return false;
- break;
- }
- }
- first = false;
- }
- restore_position.disarm();
- return !has_error();
- }
- bool ECMA262Parser::parse_class_intersection(Vector<CompareTypeAndValuePair>& compares)
- {
- // ClassIntersection :: ClassSetOperand "&&" [lookahead != "&"] ClassSetOperand
- // | ClassIntersection "&&" [lookahead != "&"] ClassSetOperand
- Vector<CompareTypeAndValuePair> lhs;
- Vector<CompareTypeAndValuePair> rhs;
- auto start_position = tell();
- ArmedScopeGuard restore_position { [&] { back(tell() - start_position + 1); } };
- if (!parse_class_set_operand(lhs))
- return false;
- if (!try_skip("&&"sv))
- return false;
- compares.append({ CharacterCompareType::And, 0 });
- compares.extend(move(lhs));
- do {
- rhs.clear_with_capacity();
- if (!parse_class_set_operand(rhs))
- return false;
- compares.extend(rhs);
- if (try_skip("&&&"sv))
- return false;
- } while (!has_error() && try_skip("&&"sv));
- compares.append({ CharacterCompareType::EndAndOr, 0 });
- restore_position.disarm();
- return true;
- }
- bool ECMA262Parser::parse_class_subtraction(Vector<CompareTypeAndValuePair>& compares)
- {
- // ClassSubtraction :: ClassSetOperand "--" ClassSetOperand | ClassSubtraction "--" ClassSetOperand
- Vector<CompareTypeAndValuePair> lhs;
- Vector<CompareTypeAndValuePair> rhs;
- auto start_position = tell();
- ArmedScopeGuard restore_position { [&] { back(tell() - start_position + 1); } };
- if (!parse_class_set_operand(lhs))
- return false;
- if (!try_skip("--"sv))
- return false;
- compares.append({ CharacterCompareType::And, 0 });
- compares.extend(move(lhs));
- do {
- rhs.clear_with_capacity();
- if (!parse_class_set_operand(rhs))
- return false;
- compares.append({ CharacterCompareType::TemporaryInverse, 0 });
- compares.extend(rhs);
- } while (!has_error() && try_skip("--"sv));
- compares.append({ CharacterCompareType::EndAndOr, 0 });
- restore_position.disarm();
- return true;
- }
- bool ECMA262Parser::parse_class_set_range(Vector<CompareTypeAndValuePair>& compares)
- {
- // ClassSetRange :: ClassSetCharacter "-" ClassSetCharacter
- auto start_position = tell();
- ArmedScopeGuard restore_position { [&] { back(tell() - start_position + 1); } };
- auto lhs = parse_class_set_character();
- if (!lhs.has_value())
- return false;
- if (!match(TokenType::HyphenMinus))
- return false;
- consume();
- auto rhs = parse_class_set_character();
- if (!rhs.has_value())
- return false;
- compares.append({
- CharacterCompareType::CharRange,
- CharRange { lhs.value(), rhs.value() },
- });
- restore_position.disarm();
- return true;
- }
- Optional<u32> ECMA262Parser::parse_class_set_character()
- {
- // ClassSetCharacter :: [lookahead ∉ ClassSetReservedDoublePunctuator] SourceCharacter but not ClassSetSyntaxCharacter
- // | "\" CharacterEscape[+UnicodeMode]
- // | "\" ClassSetReservedPunctuator
- // | "\" b
- // ClassSetReservedDoublePunctuator :: one of "&&" "!!" "##" "$$" "%%" "**" "++" ",," ".." "::" ";;" "<<" "==" ">>" "??" "@@" "^^" "``" "~~"
- // ClassSetSyntaxCharacter :: one of "(" ")" "{" "}" "[" "]" "/" "-" "\" "|"
- // ClassSetReservedPunctuator :: one of "&" "-" "!" "#" "%" "," ":" ";" "<" "=" ">" "@" "`" "~"
- constexpr auto class_set_reserved_double_punctuator = Array {
- "&&"sv, "!!"sv, "##"sv, "$$"sv, "%%"sv, "**"sv, "++"sv, ",,"sv, ".."sv, "::"sv, ";;"sv, "<<"sv, "=="sv, ">>"sv, "??"sv, "@@"sv, "^^"sv, "``"sv, "~~"sv
- };
- if (done()) {
- set_error(Error::InvalidPattern);
- return {};
- }
- auto start_position = tell();
- ArmedScopeGuard restore { [&] { back(tell() - start_position + 1); } };
- if (try_skip("\\"sv)) {
- if (done()) {
- set_error(Error::InvalidTrailingEscape);
- return {};
- }
- // "\" ClassSetReservedPunctuator
- for (auto const& reserved : class_set_reserved_double_punctuator) {
- if (try_skip(reserved)) {
- // "\" ClassSetReservedPunctuator (ClassSetReservedPunctuator)
- back();
- restore.disarm();
- return reserved[0];
- }
- }
- // "\" b
- if (try_skip("b"sv)) {
- restore.disarm();
- return '\b';
- }
- // "\" CharacterEscape[+UnicodeMode]
- Vector<CompareTypeAndValuePair> compares;
- size_t minimum_length = 0;
- if (parse_character_escape(compares, minimum_length, { .unicode = true })) {
- VERIFY(compares.size() == 1);
- auto& compare = compares.first();
- VERIFY(compare.type == CharacterCompareType::Char);
- restore.disarm();
- return compare.value;
- }
- return {};
- }
- // [lookahead ∉ ClassSetReservedDoublePunctuator] SourceCharacter but not ClassSetSyntaxCharacter
- auto lookahead_matches = any_of(class_set_reserved_double_punctuator, [this](auto& reserved) {
- return try_skip(reserved);
- });
- if (lookahead_matches)
- return {};
- for (auto character : { "("sv, ")"sv, "{"sv, "}"sv, "["sv, "]"sv, "/"sv, "-"sv, "\\"sv, "|"sv }) {
- if (try_skip(character))
- return {};
- }
- restore.disarm();
- return skip();
- }
- bool ECMA262Parser::parse_class_set_operand(Vector<regex::CompareTypeAndValuePair>& compares)
- {
- auto start_position = tell();
- // ClassSetOperand :: ClassSetCharacter | ClassStringDisjunction | NestedClass
- if (auto character = parse_class_set_character(); character.has_value()) {
- compares.append({ CharacterCompareType::Char, character.value() });
- return true;
- }
- // NestedClass :: "[" [lookahead != "^"] ClassContents[+UnicodeMode +UnicodeSetsMode] "]"
- // | "[" "^" ClassContents[+UnicodeMode +UnicodeSetsMode] "]"
- // | "\" CharacterClassEscape[+UnicodeMode]
- if (parse_nested_class(compares))
- return true;
- if (has_error())
- return false;
- auto negated = false;
- if (auto ch = parse_character_class_escape(negated, true); ch.has_value()) {
- if (negated)
- compares.append({ CharacterCompareType::TemporaryInverse, 1 });
- compares.append({ CharacterCompareType::CharClass, (ByteCodeValueType)ch.value() });
- return true;
- }
- PropertyEscape property {};
- if (parse_unicode_property_escape(property, negated)) {
- if (negated)
- compares.empend(CompareTypeAndValuePair { CharacterCompareType::Inverse, 0 });
- property.visit(
- [&](Unicode::Property property) {
- compares.empend(CompareTypeAndValuePair { CharacterCompareType::Property, (ByteCodeValueType)property.value() });
- },
- [&](Unicode::GeneralCategory general_category) {
- compares.empend(CompareTypeAndValuePair { CharacterCompareType::GeneralCategory, (ByteCodeValueType)general_category.value() });
- },
- [&](Script script) {
- if (script.is_extension)
- compares.empend(CompareTypeAndValuePair { CharacterCompareType::ScriptExtension, (ByteCodeValueType)script.script.value() });
- else
- compares.empend(CompareTypeAndValuePair { CharacterCompareType::Script, (ByteCodeValueType)script.script.value() });
- },
- [](Empty&) { VERIFY_NOT_REACHED(); });
- return true;
- }
- if (has_error())
- return false;
- // ClassStringDisjunction :: "\q{" ClassStringDisjunctionContents "}"
- // ClassStringDisjunctionContents :: ClassString | ClassString "|" ClassStringDisjunctionContents
- // ClassString :: [empty] | NonEmptyClassString
- // NonEmptyClassString :: ClassCharacter NonEmptyClassString[opt]
- if (try_skip("\\q{"sv)) {
- // FIXME: Implement this :P
- return set_error(Error::InvalidCharacterClass);
- }
- back(tell() - start_position + 1);
- return false;
- }
- bool ECMA262Parser::parse_nested_class(Vector<regex::CompareTypeAndValuePair>& compares)
- {
- auto start_position = tell();
- // NestedClass :: "[" [lookahead ≠ ^ ] ClassContents [+UnicodeMode, +UnicodeSetsMode] "]"
- // | "[" "^" ClassContents[+UnicodeMode, +UnicodeSetsMode] "]"
- // | "\" CharacterClassEscape[+UnicodeMode]
- if (match(TokenType::LeftBracket)) {
- consume();
- compares.append(CompareTypeAndValuePair { CharacterCompareType::Or, 0 });
- if (match(TokenType::Circumflex)) {
- // Negated charclass
- consume();
- compares.empend(CompareTypeAndValuePair { CharacterCompareType::Inverse, 0 });
- }
- // ClassContents :: [empty]
- if (match(TokenType::RightBracket)) {
- consume();
- // Should only have at most an 'Inverse' (after an 'Or')
- if (m_parser_state.regex_options.has_flag_set(regex::AllFlags::UnicodeSets)) {
- // In unicode sets mode, we can have an additional 'And'/'Or' before the 'Inverse'.
- VERIFY(compares.size() <= 3);
- } else {
- VERIFY(compares.size() <= 2);
- }
- compares.append(CompareTypeAndValuePair { CharacterCompareType::EndAndOr, 0 });
- return true;
- }
- // ClassContents :: [+UnicodeSetsMode] ClassSetExpression
- if (!parse_class_set_expression(compares))
- return false;
- compares.append(CompareTypeAndValuePair { CharacterCompareType::EndAndOr, 0 });
- return true;
- }
- if (try_skip("\\"sv)) {
- auto negated = false;
- if (auto char_class = parse_character_class_escape(negated); char_class.has_value()) {
- if (negated)
- compares.append({ CharacterCompareType::TemporaryInverse, 1 });
- compares.append({ CharacterCompareType::CharClass, (ByteCodeValueType)char_class.value() });
- return true;
- }
- PropertyEscape property {};
- if (parse_unicode_property_escape(property, negated)) {
- if (negated)
- compares.empend(CompareTypeAndValuePair { CharacterCompareType::Inverse, 0 });
- property.visit(
- [&](Unicode::Property property) {
- compares.empend(CompareTypeAndValuePair { CharacterCompareType::Property, (ByteCodeValueType)property.value() });
- },
- [&](Unicode::GeneralCategory general_category) {
- compares.empend(CompareTypeAndValuePair { CharacterCompareType::GeneralCategory, (ByteCodeValueType)general_category.value() });
- },
- [&](Script script) {
- if (script.is_extension)
- compares.empend(CompareTypeAndValuePair { CharacterCompareType::ScriptExtension, (ByteCodeValueType)script.script.value() });
- else
- compares.empend(CompareTypeAndValuePair { CharacterCompareType::Script, (ByteCodeValueType)script.script.value() });
- },
- [](Empty&) { VERIFY_NOT_REACHED(); });
- return true;
- }
- if (has_error())
- return false;
- }
- back(tell() - start_position + 1);
- return false;
- }
- bool ECMA262Parser::parse_unicode_property_escape(PropertyEscape& property, bool& negated)
- {
- negated = false;
- if (try_skip("p"sv))
- negated = false;
- else if (try_skip("P"sv))
- negated = true;
- else
- return false;
- auto parsed_property = read_unicode_property_escape();
- if (!parsed_property.has_value()) {
- set_error(Error::InvalidNameForProperty);
- return false;
- }
- property = move(*parsed_property);
- return property.visit(
- [this](Unicode::Property property) {
- if (!Unicode::is_ecma262_property(property)) {
- set_error(Error::InvalidNameForProperty);
- return false;
- }
- return true;
- },
- [](Unicode::GeneralCategory) { return true; },
- [](Script) { return true; },
- [](Empty&) -> bool { VERIFY_NOT_REACHED(); });
- }
- DeprecatedFlyString ECMA262Parser::read_capture_group_specifier(bool take_starting_angle_bracket)
- {
- static constexpr u32 const REPLACEMENT_CHARACTER = 0xFFFD;
- constexpr u32 const ZERO_WIDTH_NON_JOINER { 0x200C };
- constexpr u32 const ZERO_WIDTH_JOINER { 0x200D };
- if (take_starting_angle_bracket && !consume("<"))
- return {};
- StringBuilder builder;
- auto consume_code_point = [&] {
- Utf8View utf_8_view { m_parser_state.lexer.source().substring_view(m_parser_state.lexer.tell() - 1) };
- if (utf_8_view.is_empty())
- return REPLACEMENT_CHARACTER;
- u32 code_point = *utf_8_view.begin();
- auto characters = utf_8_view.byte_offset_of(1);
- while (characters-- > 0)
- consume();
- return code_point;
- };
- {
- // The first character is limited to: https://tc39.es/ecma262/#prod-RegExpIdentifierStart
- // RegExpIdentifierStart[UnicodeMode] ::
- // IdentifierStartChar
- // \ RegExpUnicodeEscapeSequence[+UnicodeMode]
- // [~UnicodeMode] UnicodeLeadSurrogate UnicodeTrailSurrogate
- auto code_point = consume_code_point();
- if (code_point == '\\' && match('u')) {
- consume();
- if (auto maybe_code_point = consume_escaped_code_point(true); maybe_code_point.has_value()) {
- code_point = *maybe_code_point;
- } else {
- set_error(Error::InvalidNameForCaptureGroup);
- return {};
- }
- }
- if (is_ascii(code_point)) {
- // The only valid ID_Start unicode characters in ascii are the letters.
- if (!is_ascii_alpha(code_point) && code_point != '$' && code_point != '_') {
- set_error(Error::InvalidNameForCaptureGroup);
- return {};
- }
- } else if (!Unicode::code_point_has_identifier_start_property(code_point)) {
- set_error(Error::InvalidNameForCaptureGroup);
- return {};
- }
- builder.append_code_point(code_point);
- }
- bool hit_end = false;
- // Any following characters are limited to:
- // RegExpIdentifierPart[UnicodeMode] ::
- // IdentifierPartChar
- // \ RegExpUnicodeEscapeSequence[+UnicodeMode]
- // [~UnicodeMode] UnicodeLeadSurrogate UnicodeTrailSurrogate
- while (match(TokenType::Char) || match(TokenType::Dollar) || match(TokenType::LeftCurly) || match(TokenType::RightCurly)) {
- auto code_point = consume_code_point();
- if (code_point == '>') {
- hit_end = true;
- break;
- }
- if (code_point == '\\') {
- if (!try_skip("u"sv)) {
- set_error(Error::InvalidNameForCaptureGroup);
- return {};
- }
- if (auto maybe_code_point = consume_escaped_code_point(true); maybe_code_point.has_value()) {
- code_point = *maybe_code_point;
- } else {
- set_error(Error::InvalidNameForCaptureGroup);
- return {};
- }
- }
- if (is_ascii(code_point)) {
- // The only valid ID_Continue unicode characters in ascii are the letters and numbers.
- if (!is_ascii_alphanumeric(code_point) && code_point != '$' && code_point != '_') {
- set_error(Error::InvalidNameForCaptureGroup);
- return {};
- }
- } else if (code_point != ZERO_WIDTH_JOINER && code_point != ZERO_WIDTH_NON_JOINER) {
- if (!Unicode::code_point_has_identifier_continue_property(code_point)) {
- set_error(Error::InvalidNameForCaptureGroup);
- return {};
- }
- }
- builder.append_code_point(code_point);
- }
- DeprecatedFlyString name = builder.to_byte_string();
- if (!hit_end || name.is_empty())
- set_error(Error::InvalidNameForCaptureGroup);
- return name;
- }
- Optional<ECMA262Parser::PropertyEscape> ECMA262Parser::read_unicode_property_escape()
- {
- consume(TokenType::LeftCurly, Error::InvalidPattern);
- auto read_until = [&]<typename... Ts>(Ts&&... terminators) {
- auto start_token = m_parser_state.current_token;
- size_t offset = 0;
- while (match(TokenType::Char)) {
- if (m_parser_state.current_token.value().is_one_of(forward<Ts>(terminators)...))
- break;
- offset += consume().value().length();
- }
- return StringView { start_token.value().characters_without_null_termination(), offset };
- };
- StringView property_type;
- StringView property_name = read_until("="sv, "}"sv);
- if (try_skip("="sv)) {
- if (property_name.is_empty())
- return {};
- property_type = property_name;
- property_name = read_until("}"sv);
- }
- consume(TokenType::RightCurly, Error::InvalidPattern);
- if (property_type.is_empty()) {
- if (auto property = Unicode::property_from_string(property_name); property.has_value())
- return { *property };
- if (auto general_category = Unicode::general_category_from_string(property_name); general_category.has_value())
- return { *general_category };
- } else if ((property_type == "General_Category"sv) || (property_type == "gc"sv)) {
- if (auto general_category = Unicode::general_category_from_string(property_name); general_category.has_value())
- return { *general_category };
- } else if ((property_type == "Script"sv) || (property_type == "sc"sv)) {
- if (auto script = Unicode::script_from_string(property_name); script.has_value())
- return Script { *script, false };
- } else if ((property_type == "Script_Extensions"sv) || (property_type == "scx"sv)) {
- if (auto script = Unicode::script_from_string(property_name); script.has_value())
- return Script { *script, true };
- }
- return {};
- }
- bool ECMA262Parser::parse_capture_group(ByteCode& stack, size_t& match_length_minimum, ParseFlags flags)
- {
- consume(TokenType::LeftParen, Error::InvalidPattern);
- auto register_capture_group_in_current_scope = [&](auto identifier) {
- m_capture_groups_in_scope.last().empend(identifier);
- };
- if (match(TokenType::Questionmark)) {
- // Non-capturing group or group with specifier.
- consume();
- if (match(TokenType::Colon)) {
- consume();
- ByteCode noncapture_group_bytecode;
- size_t length = 0;
- enter_capture_group_scope();
- if (!parse_disjunction(noncapture_group_bytecode, length, flags))
- return set_error(Error::InvalidPattern);
- clear_all_capture_groups_in_scope(stack);
- exit_capture_group_scope();
- consume(TokenType::RightParen, Error::MismatchingParen);
- stack.extend(move(noncapture_group_bytecode));
- match_length_minimum += length;
- return true;
- }
- if (consume("<")) {
- ++m_parser_state.named_capture_groups_count;
- auto group_index = ++m_parser_state.capture_groups_count; // Named capture groups count as normal capture groups too.
- auto name = read_capture_group_specifier();
- if (name.is_empty()) {
- set_error(Error::InvalidNameForCaptureGroup);
- return false;
- }
- if (m_parser_state.named_capture_groups.contains(name)) {
- set_error(Error::DuplicateNamedCapture);
- return false;
- }
- ByteCode capture_group_bytecode;
- size_t length = 0;
- enter_capture_group_scope();
- if (!parse_disjunction(capture_group_bytecode, length, flags))
- return set_error(Error::InvalidPattern);
- clear_all_capture_groups_in_scope(stack);
- exit_capture_group_scope();
- register_capture_group_in_current_scope(group_index);
- consume(TokenType::RightParen, Error::MismatchingParen);
- stack.insert_bytecode_group_capture_left(group_index);
- stack.extend(move(capture_group_bytecode));
- stack.insert_bytecode_group_capture_right(group_index, name.view());
- match_length_minimum += length;
- m_parser_state.capture_group_minimum_lengths.set(group_index, length);
- m_parser_state.named_capture_groups.set(name, { group_index, length });
- return true;
- }
- set_error(Error::InvalidCaptureGroup);
- return false;
- }
- auto group_index = ++m_parser_state.capture_groups_count;
- enter_capture_group_scope();
- ByteCode capture_group_bytecode;
- size_t length = 0;
- if (!parse_disjunction(capture_group_bytecode, length, flags))
- return set_error(Error::InvalidPattern);
- clear_all_capture_groups_in_scope(stack);
- exit_capture_group_scope();
- register_capture_group_in_current_scope(group_index);
- stack.insert_bytecode_group_capture_left(group_index);
- stack.extend(move(capture_group_bytecode));
- m_parser_state.capture_group_minimum_lengths.set(group_index, length);
- consume(TokenType::RightParen, Error::MismatchingParen);
- stack.insert_bytecode_group_capture_right(group_index);
- match_length_minimum += length;
- return true;
- }
- size_t ECMA262Parser::ensure_total_number_of_capturing_parenthesis()
- {
- if (m_total_number_of_capturing_parenthesis.has_value())
- return m_total_number_of_capturing_parenthesis.value();
- GenericLexer lexer { m_parser_state.lexer.source() };
- size_t count = 0;
- while (!lexer.is_eof()) {
- switch (lexer.peek()) {
- case '\\':
- lexer.consume(min(lexer.tell_remaining(), 2));
- continue;
- case '[':
- while (!lexer.is_eof()) {
- if (lexer.consume_specific('\\')) {
- if (lexer.is_eof())
- break;
- lexer.consume();
- continue;
- }
- if (lexer.consume_specific(']')) {
- break;
- }
- if (lexer.is_eof())
- break;
- lexer.consume();
- }
- break;
- case '(':
- lexer.consume();
- if (lexer.consume_specific('?')) {
- // non-capturing group '(?:', lookaround '(?<='/'(?<!', or named capture '(?<'
- if (!lexer.consume_specific('<'))
- break;
- if (lexer.next_is(is_any_of("=!"sv)))
- break;
- ++count;
- } else {
- ++count;
- }
- break;
- default:
- lexer.consume();
- break;
- }
- }
- m_total_number_of_capturing_parenthesis = count;
- return count;
- }
- }
|