12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875 |
- /*
- * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org>
- * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
- * Copyright (c) 2021, David Tuin <davidot@serenityos.org>
- * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
- #include "Parser.h"
- #include <AK/Array.h>
- #include <AK/CharacterTypes.h>
- #include <AK/HashTable.h>
- #include <AK/ScopeGuard.h>
- #include <AK/StdLibExtras.h>
- #include <AK/TemporaryChange.h>
- #include <LibJS/Runtime/RegExpObject.h>
- #include <LibRegex/Regex.h>
- namespace JS {
- class ScopePusher {
- private:
- ScopePusher(Parser& parser, ScopeNode* node, bool is_top_level)
- : m_parser(parser)
- , m_is_top_level(is_top_level)
- {
- m_parent_scope = exchange(m_parser.m_state.current_scope_pusher, this);
- VERIFY(node || (m_parent_scope && !is_top_level));
- if (!node)
- m_node = m_parent_scope->m_node;
- else
- m_node = node;
- VERIFY(m_node);
- if (!is_top_level)
- m_top_level_scope = m_parent_scope->m_top_level_scope;
- else
- m_top_level_scope = this;
- }
- public:
- static ScopePusher function_scope(Parser& parser, FunctionBody& function_body, Vector<FunctionDeclaration::Parameter> const& parameters)
- {
- ScopePusher scope_pusher(parser, &function_body, true);
- scope_pusher.m_function_parameters = parameters;
- for (auto& parameter : parameters) {
- parameter.binding.visit(
- [&](FlyString const& name) {
- scope_pusher.m_forbidden_lexical_names.set(name);
- },
- [&](NonnullRefPtr<BindingPattern> const& binding_pattern) {
- binding_pattern->for_each_bound_name([&](auto const& name) {
- scope_pusher.m_forbidden_lexical_names.set(name);
- });
- });
- }
- return scope_pusher;
- }
- static ScopePusher program_scope(Parser& parser, Program& program)
- {
- return ScopePusher(parser, &program, true);
- }
- static ScopePusher block_scope(Parser& parser, ScopeNode& node)
- {
- return ScopePusher(parser, &node, false);
- }
- static ScopePusher for_loop_scope(Parser& parser, RefPtr<ASTNode> const& init)
- {
- ScopePusher scope_pusher(parser, nullptr, false);
- if (init && is<VariableDeclaration>(*init)) {
- auto& variable_declaration = static_cast<VariableDeclaration const&>(*init);
- if (variable_declaration.declaration_kind() != DeclarationKind::Var) {
- variable_declaration.for_each_bound_name([&](auto const& name) {
- scope_pusher.m_forbidden_var_names.set(name);
- });
- }
- }
- return scope_pusher;
- }
- static ScopePusher catch_scope(Parser& parser, RefPtr<BindingPattern> const& pattern, FlyString const& parameter)
- {
- ScopePusher scope_pusher(parser, nullptr, false);
- if (pattern) {
- pattern->for_each_bound_name([&](auto const& name) {
- scope_pusher.m_forbidden_var_names.set(name);
- });
- } else if (!parameter.is_empty()) {
- scope_pusher.m_var_names.set(parameter);
- }
- return scope_pusher;
- }
- static ScopePusher static_init_block_scope(Parser& parser, ScopeNode& node)
- {
- return ScopePusher(parser, &node, true);
- }
- static ScopePusher class_field_scope(Parser& parser)
- {
- return ScopePusher(parser, nullptr, false);
- }
- void add_declaration(NonnullRefPtr<Declaration> declaration)
- {
- if (declaration->is_lexical_declaration()) {
- declaration->for_each_bound_name([&](auto const& name) {
- if (m_var_names.contains(name) || m_forbidden_lexical_names.contains(name) || m_function_names.contains(name))
- throw_identifier_declared(name, declaration);
- if (m_lexical_names.set(name) != AK::HashSetResult::InsertedNewEntry)
- throw_identifier_declared(name, declaration);
- });
- m_node->add_lexical_declaration(move(declaration));
- } else if (!declaration->is_function_declaration()) {
- declaration->for_each_bound_name([&](auto const& name) {
- ScopePusher* pusher = this;
- while (true) {
- if (pusher->m_lexical_names.contains(name)
- || pusher->m_function_names.contains(name)
- || pusher->m_forbidden_var_names.contains(name))
- throw_identifier_declared(name, declaration);
- pusher->m_var_names.set(name);
- if (pusher->m_is_top_level)
- break;
- VERIFY(pusher->m_parent_scope != nullptr);
- pusher = pusher->m_parent_scope;
- }
- VERIFY(pusher->m_is_top_level && pusher->m_node);
- pusher->m_node->add_var_scoped_declaration(declaration);
- });
- VERIFY(m_top_level_scope);
- m_top_level_scope->m_node->add_var_scoped_declaration(move(declaration));
- } else {
- if (m_is_top_level) {
- declaration->for_each_bound_name([&](auto const& name) {
- m_var_names.set(name);
- });
- m_node->add_var_scoped_declaration(move(declaration));
- } else {
- VERIFY(is<FunctionDeclaration>(*declaration));
- auto& function_declaration = static_cast<FunctionDeclaration const&>(*declaration);
- auto& function_name = function_declaration.name();
- if (m_var_names.contains(function_name) || m_lexical_names.contains(function_name))
- throw_identifier_declared(function_name, declaration);
- if (function_declaration.kind() != FunctionKind::Regular || m_parser.m_state.strict_mode) {
- if (m_function_names.contains(function_name))
- throw_identifier_declared(function_name, declaration);
- m_lexical_names.set(function_name);
- m_node->add_lexical_declaration(move(declaration));
- return;
- }
- m_function_names.set(function_name);
- if (!m_lexical_names.contains(function_name))
- m_functions_to_hoist.append(static_ptr_cast<FunctionDeclaration>(declaration));
- m_node->add_lexical_declaration(move(declaration));
- }
- }
- }
- ScopePusher const* last_function_scope() const
- {
- for (auto scope_ptr = this; scope_ptr; scope_ptr = scope_ptr->m_parent_scope) {
- if (scope_ptr->m_function_parameters.has_value())
- return scope_ptr;
- }
- return nullptr;
- }
- Vector<FunctionDeclaration::Parameter> const& function_parameters() const
- {
- return *m_function_parameters;
- }
- ScopePusher* parent_scope() { return m_parent_scope; }
- ScopePusher const* parent_scope() const { return m_parent_scope; }
- [[nodiscard]] bool has_declaration(StringView name) const
- {
- return m_lexical_names.contains(name) || m_var_names.contains(name) || !m_functions_to_hoist.find_if([&name](auto& function) { return function->name() == name; }).is_end();
- }
- bool contains_direct_call_to_eval() const { return m_contains_direct_call_to_eval; }
- bool contains_access_to_arguments_object() const { return m_contains_access_to_arguments_object; }
- void set_contains_direct_call_to_eval() { m_contains_direct_call_to_eval = true; }
- void set_contains_access_to_arguments_object() { m_contains_access_to_arguments_object = true; }
- ~ScopePusher()
- {
- VERIFY(m_is_top_level || m_parent_scope);
- if (!m_contains_access_to_arguments_object) {
- for (auto& it : m_identifier_and_argument_index_associations) {
- for (auto& identifier : it.value) {
- if (!has_declaration(identifier.string()))
- identifier.set_lexically_bound_function_argument_index(it.key);
- }
- }
- }
- for (size_t i = 0; i < m_functions_to_hoist.size(); i++) {
- auto const& function_declaration = m_functions_to_hoist[i];
- if (m_lexical_names.contains(function_declaration.name()) || m_forbidden_var_names.contains(function_declaration.name()))
- continue;
- if (m_is_top_level)
- m_node->add_hoisted_function(move(m_functions_to_hoist[i]));
- else
- m_parent_scope->m_functions_to_hoist.append(move(m_functions_to_hoist[i]));
- }
- if (m_parent_scope && !m_function_parameters.has_value()) {
- m_parent_scope->m_contains_access_to_arguments_object |= m_contains_access_to_arguments_object;
- m_parent_scope->m_contains_direct_call_to_eval |= m_contains_direct_call_to_eval;
- }
- VERIFY(m_parser.m_state.current_scope_pusher == this);
- m_parser.m_state.current_scope_pusher = m_parent_scope;
- }
- void associate_identifier_with_argument_index(NonnullRefPtr<Identifier> identifier, size_t index)
- {
- m_identifier_and_argument_index_associations.ensure(index).append(move(identifier));
- }
- private:
- void throw_identifier_declared(FlyString const& name, NonnullRefPtr<Declaration> const& declaration)
- {
- m_parser.syntax_error(String::formatted("Identifier '{}' already declared", name), declaration->source_range().start);
- }
- Parser& m_parser;
- ScopeNode* m_node { nullptr };
- bool m_is_top_level { false };
- ScopePusher* m_parent_scope { nullptr };
- ScopePusher* m_top_level_scope { nullptr };
- HashTable<FlyString> m_lexical_names;
- HashTable<FlyString> m_var_names;
- HashTable<FlyString> m_function_names;
- HashTable<FlyString> m_forbidden_lexical_names;
- HashTable<FlyString> m_forbidden_var_names;
- NonnullRefPtrVector<FunctionDeclaration> m_functions_to_hoist;
- Optional<Vector<FunctionDeclaration::Parameter>> m_function_parameters;
- HashMap<size_t, NonnullRefPtrVector<Identifier>> m_identifier_and_argument_index_associations;
- bool m_contains_access_to_arguments_object { false };
- bool m_contains_direct_call_to_eval { false };
- };
- class OperatorPrecedenceTable {
- public:
- constexpr OperatorPrecedenceTable()
- : m_token_precedence()
- {
- for (size_t i = 0; i < array_size(m_operator_precedence); ++i) {
- auto& op = m_operator_precedence[i];
- m_token_precedence[static_cast<size_t>(op.token)] = op.precedence;
- }
- }
- constexpr int get(TokenType token) const
- {
- int p = m_token_precedence[static_cast<size_t>(token)];
- if (p == 0) {
- warnln("Internal Error: No precedence for operator {}", Token::name(token));
- VERIFY_NOT_REACHED();
- return -1;
- }
- return p;
- }
- private:
- int m_token_precedence[cs_num_of_js_tokens];
- struct OperatorPrecedence {
- TokenType token;
- int precedence;
- };
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
- static constexpr const OperatorPrecedence m_operator_precedence[] = {
- { TokenType::Period, 20 },
- { TokenType::BracketOpen, 20 },
- { TokenType::ParenOpen, 20 },
- { TokenType::QuestionMarkPeriod, 20 },
- { TokenType::New, 19 },
- { TokenType::PlusPlus, 18 },
- { TokenType::MinusMinus, 18 },
- { TokenType::ExclamationMark, 17 },
- { TokenType::Tilde, 17 },
- { TokenType::Typeof, 17 },
- { TokenType::Void, 17 },
- { TokenType::Delete, 17 },
- { TokenType::Await, 17 },
- { TokenType::DoubleAsterisk, 16 },
- { TokenType::Asterisk, 15 },
- { TokenType::Slash, 15 },
- { TokenType::Percent, 15 },
- { TokenType::Plus, 14 },
- { TokenType::Minus, 14 },
- { TokenType::ShiftLeft, 13 },
- { TokenType::ShiftRight, 13 },
- { TokenType::UnsignedShiftRight, 13 },
- { TokenType::LessThan, 12 },
- { TokenType::LessThanEquals, 12 },
- { TokenType::GreaterThan, 12 },
- { TokenType::GreaterThanEquals, 12 },
- { TokenType::In, 12 },
- { TokenType::Instanceof, 12 },
- { TokenType::EqualsEquals, 11 },
- { TokenType::ExclamationMarkEquals, 11 },
- { TokenType::EqualsEqualsEquals, 11 },
- { TokenType::ExclamationMarkEqualsEquals, 11 },
- { TokenType::Ampersand, 10 },
- { TokenType::Caret, 9 },
- { TokenType::Pipe, 8 },
- { TokenType::DoubleQuestionMark, 7 },
- { TokenType::DoubleAmpersand, 6 },
- { TokenType::DoublePipe, 5 },
- { TokenType::QuestionMark, 4 },
- { TokenType::Equals, 3 },
- { TokenType::PlusEquals, 3 },
- { TokenType::MinusEquals, 3 },
- { TokenType::DoubleAsteriskEquals, 3 },
- { TokenType::AsteriskEquals, 3 },
- { TokenType::SlashEquals, 3 },
- { TokenType::PercentEquals, 3 },
- { TokenType::ShiftLeftEquals, 3 },
- { TokenType::ShiftRightEquals, 3 },
- { TokenType::UnsignedShiftRightEquals, 3 },
- { TokenType::AmpersandEquals, 3 },
- { TokenType::CaretEquals, 3 },
- { TokenType::PipeEquals, 3 },
- { TokenType::DoubleAmpersandEquals, 3 },
- { TokenType::DoublePipeEquals, 3 },
- { TokenType::DoubleQuestionMarkEquals, 3 },
- { TokenType::Yield, 2 },
- { TokenType::Comma, 1 },
- };
- };
- constexpr OperatorPrecedenceTable g_operator_precedence;
- Parser::ParserState::ParserState(Lexer l, Program::Type program_type)
- : lexer(move(l))
- {
- if (program_type == Program::Type::Module)
- lexer.disallow_html_comments();
- current_token = lexer.next();
- }
- Parser::Parser(Lexer lexer, Program::Type program_type)
- : m_state(move(lexer), program_type)
- , m_program_type(program_type)
- {
- }
- Associativity Parser::operator_associativity(TokenType type) const
- {
- switch (type) {
- case TokenType::Period:
- case TokenType::BracketOpen:
- case TokenType::ParenOpen:
- case TokenType::QuestionMarkPeriod:
- case TokenType::Asterisk:
- case TokenType::Slash:
- case TokenType::Percent:
- case TokenType::Plus:
- case TokenType::Minus:
- case TokenType::ShiftLeft:
- case TokenType::ShiftRight:
- case TokenType::UnsignedShiftRight:
- case TokenType::LessThan:
- case TokenType::LessThanEquals:
- case TokenType::GreaterThan:
- case TokenType::GreaterThanEquals:
- case TokenType::In:
- case TokenType::Instanceof:
- case TokenType::EqualsEquals:
- case TokenType::ExclamationMarkEquals:
- case TokenType::EqualsEqualsEquals:
- case TokenType::ExclamationMarkEqualsEquals:
- case TokenType::Typeof:
- case TokenType::Void:
- case TokenType::Delete:
- case TokenType::Ampersand:
- case TokenType::Caret:
- case TokenType::Pipe:
- case TokenType::DoubleQuestionMark:
- case TokenType::DoubleAmpersand:
- case TokenType::DoublePipe:
- case TokenType::Comma:
- return Associativity::Left;
- default:
- return Associativity::Right;
- }
- }
- bool Parser::parse_directive(ScopeNode& body)
- {
- bool found_use_strict = false;
- while (!done() && match(TokenType::StringLiteral)) {
- // It cannot be a labelled function since we hit a string literal.
- auto statement = parse_statement(AllowLabelledFunction::No);
- body.append(statement);
- VERIFY(is<ExpressionStatement>(*statement));
- auto& expression = static_cast<ExpressionStatement const&>(*statement).expression();
- if (!is<StringLiteral>(expression))
- break;
- auto& string_literal = static_cast<StringLiteral const&>(expression);
- if (string_literal.is_use_strict_directive()) {
- found_use_strict = true;
- if (m_state.string_legacy_octal_escape_sequence_in_scope)
- syntax_error("Octal escape sequence in string literal not allowed in strict mode");
- break;
- }
- }
- m_state.string_legacy_octal_escape_sequence_in_scope = false;
- return found_use_strict;
- }
- NonnullRefPtr<Program> Parser::parse_program(bool starts_in_strict_mode)
- {
- auto rule_start = push_start();
- auto program = adopt_ref(*new Program({ m_filename, rule_start.position(), position() }, m_program_type));
- ScopePusher program_scope = ScopePusher::program_scope(*this, *program);
- if (starts_in_strict_mode || m_program_type == Program::Type::Module)
- m_state.strict_mode = true;
- bool has_use_strict = parse_directive(program);
- if (m_state.strict_mode || has_use_strict) {
- program->set_strict_mode();
- m_state.strict_mode = true;
- }
- while (!done()) {
- parse_statement_list(program, AllowLabelledFunction::Yes);
- if (done())
- break;
- if (match_export_or_import()) {
- VERIFY(m_state.current_token.type() == TokenType::Export || m_state.current_token.type() == TokenType::Import);
- if (m_state.current_token.type() == TokenType::Export)
- program->append_export(parse_export_statement(*program));
- else
- program->append_import(parse_import_statement(*program));
- } else {
- expected("statement or declaration");
- consume();
- }
- }
- program->source_range().end = position();
- return program;
- }
- NonnullRefPtr<Declaration> Parser::parse_declaration()
- {
- auto rule_start = push_start();
- switch (m_state.current_token.type()) {
- case TokenType::Class:
- return parse_class_declaration();
- case TokenType::Function:
- return parse_function_node<FunctionDeclaration>();
- case TokenType::Let:
- case TokenType::Const:
- return parse_variable_declaration();
- default:
- expected("declaration");
- consume();
- return create_ast_node<ErrorDeclaration>({ m_state.current_token.filename(), rule_start.position(), position() });
- }
- }
- NonnullRefPtr<Statement> Parser::parse_statement(AllowLabelledFunction allow_labelled_function)
- {
- auto rule_start = push_start();
- auto type = m_state.current_token.type();
- switch (type) {
- case TokenType::CurlyOpen:
- return parse_block_statement();
- case TokenType::Return:
- return parse_return_statement();
- case TokenType::Var: {
- auto declaration = parse_variable_declaration();
- m_state.current_scope_pusher->add_declaration(declaration);
- return declaration;
- }
- case TokenType::For:
- return parse_for_statement();
- case TokenType::If:
- return parse_if_statement();
- case TokenType::Throw:
- return parse_throw_statement();
- case TokenType::Try:
- return parse_try_statement();
- case TokenType::Break:
- return parse_break_statement();
- case TokenType::Continue:
- return parse_continue_statement();
- case TokenType::Switch:
- return parse_switch_statement();
- case TokenType::Do:
- return parse_do_while_statement();
- case TokenType::While:
- return parse_while_statement();
- case TokenType::With:
- if (m_state.strict_mode)
- syntax_error("'with' statement not allowed in strict mode");
- return parse_with_statement();
- case TokenType::Debugger:
- return parse_debugger_statement();
- case TokenType::Semicolon:
- consume();
- return create_ast_node<EmptyStatement>({ m_state.current_token.filename(), rule_start.position(), position() });
- case TokenType::Slash:
- case TokenType::SlashEquals:
- m_state.current_token = m_state.lexer.force_slash_as_regex();
- [[fallthrough]];
- default:
- if (match_invalid_escaped_keyword())
- syntax_error("Keyword must not contain escaped characters");
- if (match_identifier_name()) {
- auto result = try_parse_labelled_statement(allow_labelled_function);
- if (!result.is_null())
- return result.release_nonnull();
- }
- if (match_expression()) {
- if (match(TokenType::Function) || match(TokenType::Class))
- syntax_error(String::formatted("{} declaration not allowed in single-statement context", m_state.current_token.name()));
- if (match(TokenType::Let) && next_token().type() == TokenType::BracketOpen)
- syntax_error(String::formatted("let followed by [ is not allowed in single-statement context"));
- auto expr = parse_expression(0);
- consume_or_insert_semicolon();
- return create_ast_node<ExpressionStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(expr));
- }
- expected("statement");
- consume();
- return create_ast_node<ErrorStatement>({ m_state.current_token.filename(), rule_start.position(), position() });
- }
- }
- bool Parser::match_invalid_escaped_keyword() const
- {
- if (m_state.current_token.type() != TokenType::EscapedKeyword)
- return false;
- auto token_value = m_state.current_token.value();
- if (token_value == "await"sv) {
- return m_program_type == Program::Type::Module;
- }
- if (m_state.strict_mode) {
- return true;
- }
- return token_value != "yield"sv && token_value != "let"sv;
- }
- static constexpr AK::Array<StringView, 9> strict_reserved_words = { "implements", "interface", "let", "package", "private", "protected", "public", "static", "yield" };
- static bool is_strict_reserved_word(StringView str)
- {
- return any_of(strict_reserved_words, [&str](StringView const& word) {
- return word == str;
- });
- }
- static bool is_simple_parameter_list(Vector<FunctionNode::Parameter> const& parameters)
- {
- return all_of(parameters, [](FunctionNode::Parameter const& parameter) {
- return !parameter.is_rest && parameter.default_value.is_null() && parameter.binding.has<FlyString>();
- });
- }
- RefPtr<FunctionExpression> Parser::try_parse_arrow_function_expression(bool expect_parens)
- {
- if (!expect_parens) {
- // NOTE: This is a fast path where we try to fail early in case this can't possibly
- // be a match. The idea is to avoid the expensive parser state save/load mechanism.
- // The logic is duplicated below in the "real" !expect_parens branch.
- if (!match_identifier() && !match(TokenType::Yield) && !match(TokenType::Await))
- return nullptr;
- auto token = next_token();
- if (token.trivia_contains_line_terminator())
- return nullptr;
- if (token.type() != TokenType::Arrow)
- return nullptr;
- }
- save_state();
- auto rule_start = push_start();
- ArmedScopeGuard state_rollback_guard = [&] {
- load_state();
- };
- Vector<FunctionNode::Parameter> parameters;
- i32 function_length = -1;
- if (expect_parens) {
- // We have parens around the function parameters and can re-use the same parsing
- // logic used for regular functions: multiple parameters, default values, rest
- // parameter, maybe a trailing comma. If we have a new syntax error afterwards we
- // check if it's about a wrong token (something like duplicate parameter name must
- // not abort), know parsing failed and rollback the parser state.
- auto previous_syntax_errors = m_state.errors.size();
- parameters = parse_formal_parameters(function_length, FunctionNodeParseOptions::IsArrowFunction);
- if (m_state.errors.size() > previous_syntax_errors && m_state.errors[previous_syntax_errors].message.starts_with("Unexpected token"))
- return nullptr;
- if (!match(TokenType::ParenClose))
- return nullptr;
- consume();
- } else {
- // No parens - this must be an identifier followed by arrow. That's it.
- if (!match_identifier() && !match(TokenType::Yield) && !match(TokenType::Await))
- return nullptr;
- auto token = consume_identifier_reference();
- if (m_state.strict_mode && token.value().is_one_of("arguments"sv, "eval"sv))
- syntax_error("BindingIdentifier may not be 'arguments' or 'eval' in strict mode");
- parameters.append({ FlyString { token.value() }, {} });
- }
- // If there's a newline between the closing paren and arrow it's not a valid arrow function,
- // ASI should kick in instead (it'll then fail with "Unexpected token Arrow")
- if (m_state.current_token.trivia_contains_line_terminator())
- return nullptr;
- if (!match(TokenType::Arrow))
- return nullptr;
- consume();
- if (function_length == -1)
- function_length = parameters.size();
- auto old_labels_in_scope = move(m_state.labels_in_scope);
- ScopeGuard guard([&]() {
- m_state.labels_in_scope = move(old_labels_in_scope);
- });
- bool contains_direct_call_to_eval = false;
- auto function_body_result = [&]() -> RefPtr<FunctionBody> {
- TemporaryChange change(m_state.in_arrow_function_context, true);
- if (match(TokenType::CurlyOpen)) {
- // Parse a function body with statements
- return parse_function_body(parameters, FunctionKind::Regular, contains_direct_call_to_eval);
- }
- if (match_expression()) {
- // Parse a function body which returns a single expression
- // FIXME: We synthesize a block with a return statement
- // for arrow function bodies which are a single expression.
- // Esprima generates a single "ArrowFunctionExpression"
- // with a "body" property.
- auto return_block = create_ast_node<FunctionBody>({ m_state.current_token.filename(), rule_start.position(), position() });
- ScopePusher function_scope = ScopePusher::function_scope(*this, return_block, parameters);
- auto return_expression = parse_expression(2);
- return_block->append<ReturnStatement>({ m_filename, rule_start.position(), position() }, move(return_expression));
- if (m_state.strict_mode)
- return_block->set_strict_mode();
- contains_direct_call_to_eval = function_scope.contains_direct_call_to_eval();
- return return_block;
- }
- // Invalid arrow function body
- return nullptr;
- }();
- if (function_body_result.is_null())
- return nullptr;
- state_rollback_guard.disarm();
- discard_saved_state();
- auto body = function_body_result.release_nonnull();
- if (body->in_strict_mode()) {
- for (auto& parameter : parameters) {
- parameter.binding.visit(
- [&](FlyString const& name) {
- check_identifier_name_for_assignment_validity(name, true);
- },
- [&](auto const&) {});
- }
- }
- return create_ast_node<FunctionExpression>(
- { m_state.current_token.filename(), rule_start.position(), position() }, "", move(body),
- move(parameters), function_length, FunctionKind::Regular, body->in_strict_mode(),
- /* might_need_arguments_object */ false, contains_direct_call_to_eval, /* is_arrow_function */ true);
- }
- RefPtr<Statement> Parser::try_parse_labelled_statement(AllowLabelledFunction allow_function)
- {
- {
- // NOTE: This is a fast path where we try to fail early to avoid the expensive save_state+load_state.
- if (next_token().type() != TokenType::Colon)
- return {};
- }
- save_state();
- auto rule_start = push_start();
- ArmedScopeGuard state_rollback_guard = [&] {
- load_state();
- };
- if (m_state.current_token.value() == "yield"sv && (m_state.strict_mode || m_state.in_generator_function_context)) {
- return {};
- }
- if (m_state.current_token.value() == "await"sv && m_program_type == Program::Type::Module) {
- return {};
- }
- auto identifier = [&] {
- if (m_state.current_token.value() == "await"sv) {
- return consume().value();
- }
- return consume_identifier_reference().value();
- }();
- if (!match(TokenType::Colon))
- return {};
- consume(TokenType::Colon);
- if (!match_statement())
- return {};
- state_rollback_guard.disarm();
- discard_saved_state();
- if (m_state.strict_mode && identifier == "let"sv) {
- syntax_error("Strict mode reserved word 'let' is not allowed in label", rule_start.position());
- return {};
- }
- if (match(TokenType::Function) && (allow_function == AllowLabelledFunction::No || m_state.strict_mode)) {
- syntax_error("Not allowed to declare a function here");
- return {};
- }
- if (m_state.labels_in_scope.contains(identifier))
- syntax_error(String::formatted("Label '{}' has already been declared", identifier));
- RefPtr<Statement> labelled_statement;
- auto is_iteration_statement = false;
- if (match(TokenType::Function)) {
- m_state.labels_in_scope.set(identifier, {});
- auto function_declaration = parse_function_node<FunctionDeclaration>();
- VERIFY(m_state.current_scope_pusher);
- m_state.current_scope_pusher->add_declaration(function_declaration);
- if (function_declaration->kind() == FunctionKind::Generator)
- syntax_error("Generator functions cannot be defined in labelled statements");
- labelled_statement = move(function_declaration);
- } else {
- m_state.labels_in_scope.set(identifier, {});
- labelled_statement = parse_statement(allow_function);
- if (is<IterationStatement>(*labelled_statement)) {
- is_iteration_statement = true;
- static_cast<IterationStatement&>(*labelled_statement).add_label(identifier);
- } else if (is<LabelableStatement>(*labelled_statement)) {
- static_cast<LabelableStatement&>(*labelled_statement).add_label(identifier);
- }
- }
- if (!is_iteration_statement) {
- if (auto entry = m_state.labels_in_scope.find(identifier); entry != m_state.labels_in_scope.end() && entry->value.has_value())
- syntax_error("labelled continue statement cannot use non iterating statement", m_state.labels_in_scope.get(identifier).value());
- }
- m_state.labels_in_scope.remove(identifier);
- return labelled_statement.release_nonnull();
- }
- RefPtr<MetaProperty> Parser::try_parse_new_target_expression()
- {
- save_state();
- auto rule_start = push_start();
- ArmedScopeGuard state_rollback_guard = [&] {
- load_state();
- };
- consume(TokenType::New);
- if (!match(TokenType::Period))
- return {};
- consume();
- if (!match(TokenType::Identifier))
- return {};
- // The string 'target' cannot have escapes so we check original value.
- if (consume().original_value() != "target"sv)
- return {};
- state_rollback_guard.disarm();
- discard_saved_state();
- return create_ast_node<MetaProperty>({ m_state.current_token.filename(), rule_start.position(), position() }, MetaProperty::Type::NewTarget);
- }
- NonnullRefPtr<ClassDeclaration> Parser::parse_class_declaration()
- {
- auto rule_start = push_start();
- return create_ast_node<ClassDeclaration>({ m_state.current_token.filename(), rule_start.position(), position() }, parse_class_expression(true));
- }
- NonnullRefPtr<ClassExpression> Parser::parse_class_expression(bool expect_class_name)
- {
- auto rule_start = push_start();
- // Classes are always in strict mode.
- TemporaryChange strict_mode_rollback(m_state.strict_mode, true);
- consume(TokenType::Class);
- NonnullRefPtrVector<ClassElement> elements;
- RefPtr<Expression> super_class;
- RefPtr<FunctionExpression> constructor;
- HashTable<FlyString> found_private_names;
- String class_name = expect_class_name || match_identifier() || match(TokenType::Yield) || match(TokenType::Await)
- ? consume_identifier_reference().value().to_string()
- : "";
- check_identifier_name_for_assignment_validity(class_name, true);
- if (match(TokenType::Extends)) {
- consume();
- auto [expression, should_continue_parsing] = parse_primary_expression();
- // Basically a (much) simplified parse_secondary_expression().
- for (;;) {
- if (match(TokenType::TemplateLiteralStart)) {
- auto template_literal = parse_template_literal(true);
- expression = create_ast_node<TaggedTemplateLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, move(expression), move(template_literal));
- continue;
- }
- if (match(TokenType::BracketOpen) || match(TokenType::Period) || match(TokenType::ParenOpen)) {
- auto precedence = g_operator_precedence.get(m_state.current_token.type());
- expression = parse_secondary_expression(move(expression), precedence);
- continue;
- }
- break;
- }
- super_class = move(expression);
- (void)should_continue_parsing;
- }
- consume(TokenType::CurlyOpen);
- HashTable<StringView> referenced_private_names;
- HashTable<StringView>* outer_referenced_private_names = m_state.referenced_private_names;
- m_state.referenced_private_names = &referenced_private_names;
- ScopeGuard restore_private_name_table = [&] {
- m_state.referenced_private_names = outer_referenced_private_names;
- };
- while (!done() && !match(TokenType::CurlyClose)) {
- RefPtr<Expression> property_key;
- bool is_static = false;
- bool is_constructor = false;
- bool is_generator = false;
- auto method_kind = ClassMethod::Kind::Method;
- if (match(TokenType::Semicolon)) {
- consume();
- continue;
- }
- if (match(TokenType::Asterisk)) {
- consume();
- is_generator = true;
- }
- StringView name;
- if (match_property_key() || match(TokenType::PrivateIdentifier)) {
- if (!is_generator && m_state.current_token.original_value() == "static"sv) {
- if (match(TokenType::Identifier)) {
- consume();
- is_static = true;
- if (match(TokenType::Asterisk)) {
- consume();
- is_generator = true;
- }
- }
- }
- if (match(TokenType::Identifier)) {
- auto identifier_name = m_state.current_token.original_value();
- if (identifier_name == "get"sv) {
- method_kind = ClassMethod::Kind::Getter;
- consume();
- } else if (identifier_name == "set"sv) {
- method_kind = ClassMethod::Kind::Setter;
- consume();
- }
- }
- if (match_property_key() || match(TokenType::PrivateIdentifier)) {
- switch (m_state.current_token.type()) {
- case TokenType::Identifier:
- name = consume().value();
- property_key = create_ast_node<StringLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, name);
- break;
- case TokenType::PrivateIdentifier:
- name = consume().value();
- if (name == "#constructor")
- syntax_error("Private property with name '#constructor' is not allowed");
- if (method_kind != ClassMethod::Kind::Method) {
- // It is a Syntax Error if PrivateBoundIdentifiers of ClassElementList contains any duplicate entries,
- // unless the name is used once for a getter and once for a setter and in no other entries,
- // and the getter and setter are either both static or both non-static.
- for (auto& element : elements) {
- auto private_name = element.private_bound_identifier();
- if (!private_name.has_value() || private_name.value() != name)
- continue;
- if (element.class_element_kind() != ClassElement::ElementKind::Method
- || element.is_static() != is_static) {
- syntax_error(String::formatted("Duplicate private field or method named '{}'", name));
- break;
- }
- VERIFY(is<ClassMethod>(element));
- auto& class_method_element = static_cast<ClassMethod const&>(element);
- if (class_method_element.kind() == ClassMethod::Kind::Method || class_method_element.kind() == method_kind) {
- syntax_error(String::formatted("Duplicate private field or method named '{}'", name));
- break;
- }
- }
- found_private_names.set(name);
- } else if (found_private_names.set(name) != AK::HashSetResult::InsertedNewEntry) {
- syntax_error(String::formatted("Duplicate private field or method named '{}'", name));
- }
- property_key = create_ast_node<PrivateIdentifier>({ m_state.current_token.filename(), rule_start.position(), position() }, name);
- break;
- case TokenType::StringLiteral: {
- auto string_literal = parse_string_literal(consume());
- name = string_literal->value();
- property_key = move(string_literal);
- break;
- }
- default:
- property_key = parse_property_key();
- break;
- }
- // https://tc39.es/ecma262/#sec-class-definitions-static-semantics-early-errors
- // ClassElement : static MethodDefinition
- // It is a Syntax Error if PropName of MethodDefinition is "prototype".
- if (is_static && name == "prototype"sv)
- syntax_error("Classes may not have a static property named 'prototype'");
- } else if ((match(TokenType::ParenOpen) || match(TokenType::Equals)) && (is_static || method_kind != ClassMethod::Kind::Method)) {
- switch (method_kind) {
- case ClassMethod::Kind::Method:
- VERIFY(is_static);
- name = "static";
- is_static = false;
- break;
- case ClassMethod::Kind::Getter:
- name = "get";
- method_kind = ClassMethod::Kind::Method;
- break;
- case ClassMethod::Kind::Setter:
- name = "set";
- method_kind = ClassMethod::Kind::Method;
- break;
- }
- property_key = create_ast_node<StringLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, name);
- } else if (match(TokenType::CurlyOpen) && is_static) {
- auto static_start = push_start();
- consume(TokenType::CurlyOpen);
- auto static_init_block = create_ast_node<FunctionBody>({ m_state.current_token.filename(), rule_start.position(), position() });
- TemporaryChange break_context_rollback(m_state.in_break_context, false);
- TemporaryChange continue_context_rollback(m_state.in_continue_context, false);
- TemporaryChange function_context_rollback(m_state.in_function_context, false);
- TemporaryChange generator_function_context_rollback(m_state.in_generator_function_context, false);
- TemporaryChange in_class_field_initializer_rollback(m_state.in_class_field_initializer, true);
- ScopePusher static_init_scope = ScopePusher::static_init_block_scope(*this, *static_init_block);
- parse_statement_list(static_init_block);
- consume(TokenType::CurlyClose);
- elements.append(create_ast_node<StaticInitializer>({ m_state.current_token.filename(), static_start.position(), position() }, move(static_init_block), static_init_scope.contains_direct_call_to_eval()));
- continue;
- } else {
- expected("property key");
- }
- // Constructor may be a StringLiteral or an Identifier.
- if (!is_static && name == "constructor"sv) {
- if (method_kind != ClassMethod::Kind::Method)
- syntax_error("Class constructor may not be an accessor");
- if (!constructor.is_null())
- syntax_error("Classes may not have more than one constructor");
- if (is_generator)
- syntax_error("Class constructor may not be a generator");
- is_constructor = true;
- }
- }
- if (match(TokenType::ParenOpen)) {
- u8 parse_options = FunctionNodeParseOptions::AllowSuperPropertyLookup;
- if (!super_class.is_null() && !is_static && is_constructor)
- parse_options |= FunctionNodeParseOptions::AllowSuperConstructorCall;
- if (method_kind == ClassMethod::Kind::Getter)
- parse_options |= FunctionNodeParseOptions::IsGetterFunction;
- if (method_kind == ClassMethod::Kind::Setter)
- parse_options |= FunctionNodeParseOptions::IsSetterFunction;
- if (is_generator)
- parse_options |= FunctionNodeParseOptions::IsGeneratorFunction;
- auto function = parse_function_node<FunctionExpression>(parse_options);
- if (is_constructor) {
- constructor = move(function);
- } else if (!property_key.is_null()) {
- elements.append(create_ast_node<ClassMethod>({ m_state.current_token.filename(), rule_start.position(), position() }, property_key.release_nonnull(), move(function), method_kind, is_static));
- } else {
- syntax_error("No key for class method");
- }
- } else if (is_generator) {
- expected("ParenOpen");
- consume();
- } else if (property_key.is_null()) {
- expected("property key");
- consume();
- } else {
- if (name == "constructor"sv)
- syntax_error("Class cannot have field named 'constructor'");
- RefPtr<Expression> initializer;
- bool contains_direct_call_to_eval = false;
- if (match(TokenType::Equals)) {
- consume();
- TemporaryChange super_property_access_rollback(m_state.allow_super_property_lookup, true);
- TemporaryChange field_initializer_rollback(m_state.in_class_field_initializer, true);
- auto class_field_scope = ScopePusher::class_field_scope(*this);
- initializer = parse_expression(2);
- contains_direct_call_to_eval = class_field_scope.contains_direct_call_to_eval();
- }
- elements.append(create_ast_node<ClassField>({ m_state.current_token.filename(), rule_start.position(), position() }, property_key.release_nonnull(), move(initializer), contains_direct_call_to_eval, is_static));
- consume_or_insert_semicolon();
- }
- }
- consume(TokenType::CurlyClose);
- if (constructor.is_null()) {
- auto constructor_body = create_ast_node<BlockStatement>({ m_state.current_token.filename(), rule_start.position(), position() });
- if (!super_class.is_null()) {
- // Set constructor to the result of parsing the source text
- // constructor(... args){ super (...args);}
- auto super_call = create_ast_node<SuperCall>(
- { m_state.current_token.filename(), rule_start.position(), position() },
- Vector { CallExpression::Argument { create_ast_node<Identifier>({ m_state.current_token.filename(), rule_start.position(), position() }, "args"), true } });
- constructor_body->append(create_ast_node<ExpressionStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(super_call)));
- constructor = create_ast_node<FunctionExpression>(
- { m_state.current_token.filename(), rule_start.position(), position() }, class_name, move(constructor_body),
- Vector { FunctionNode::Parameter { FlyString { "args" }, nullptr, true } }, 0, FunctionKind::Regular,
- /* is_strict_mode */ true, /* might_need_arguments_object */ false, /* contains_direct_call_to_eval */ false);
- } else {
- constructor = create_ast_node<FunctionExpression>(
- { m_state.current_token.filename(), rule_start.position(), position() }, class_name, move(constructor_body),
- Vector<FunctionNode::Parameter> {}, 0, FunctionKind::Regular,
- /* is_strict_mode */ true, /* might_need_arguments_object */ false, /* contains_direct_call_to_eval */ false);
- }
- }
- // We could be in a subclass defined within the main class so must move all non declared private names to outer.
- for (auto& private_name : referenced_private_names) {
- if (found_private_names.contains(private_name))
- continue;
- if (outer_referenced_private_names)
- outer_referenced_private_names->set(private_name);
- else // FIXME: Make these error appear in the appropriate places.
- syntax_error(String::formatted("Reference to undeclared private field or method '{}'", private_name));
- }
- return create_ast_node<ClassExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, move(class_name), move(constructor), move(super_class), move(elements));
- }
- Parser::PrimaryExpressionParseResult Parser::parse_primary_expression()
- {
- auto rule_start = push_start();
- if (match_unary_prefixed_expression())
- return { parse_unary_prefixed_expression() };
- switch (m_state.current_token.type()) {
- case TokenType::ParenOpen: {
- auto paren_position = position();
- consume(TokenType::ParenOpen);
- if ((match(TokenType::ParenClose) || match_identifier() || match(TokenType::TripleDot) || match(TokenType::CurlyOpen) || match(TokenType::BracketOpen))
- && !try_parse_arrow_function_expression_failed_at_position(paren_position)) {
- auto arrow_function_result = try_parse_arrow_function_expression(true);
- if (!arrow_function_result.is_null())
- return { arrow_function_result.release_nonnull(), false };
- set_try_parse_arrow_function_expression_failed_at_position(paren_position, true);
- }
- auto expression = parse_expression(0);
- consume(TokenType::ParenClose);
- if (is<FunctionExpression>(*expression)) {
- auto& function = static_cast<FunctionExpression&>(*expression);
- if (function.kind() == FunctionKind::Generator && function.name() == "yield"sv)
- syntax_error("function is not allowed to be called 'yield' in this context", function.source_range().start);
- }
- return { move(expression) };
- }
- case TokenType::This:
- consume();
- return { create_ast_node<ThisExpression>({ m_state.current_token.filename(), rule_start.position(), position() }) };
- case TokenType::Class:
- return { parse_class_expression(false) };
- case TokenType::Super:
- consume();
- if (!m_state.allow_super_property_lookup)
- syntax_error("'super' keyword unexpected here");
- return { create_ast_node<SuperExpression>({ m_state.current_token.filename(), rule_start.position(), position() }) };
- case TokenType::EscapedKeyword:
- if (m_state.strict_mode || (m_state.current_token.value() != "let"sv && (m_state.in_generator_function_context || m_state.current_token.value() != "yield"sv)))
- syntax_error("Keyword must not contain escaped characters");
- [[fallthrough]];
- case TokenType::Identifier: {
- read_as_identifier:;
- if (!try_parse_arrow_function_expression_failed_at_position(position())) {
- auto arrow_function_result = try_parse_arrow_function_expression(false);
- if (!arrow_function_result.is_null())
- return { arrow_function_result.release_nonnull(), false };
- set_try_parse_arrow_function_expression_failed_at_position(position(), true);
- }
- auto string = m_state.current_token.value();
- // This could be 'eval' or 'arguments' and thus needs a custom check (`eval[1] = true`)
- if (m_state.strict_mode && (string == "let" || is_strict_reserved_word(string)))
- syntax_error(String::formatted("Identifier must not be a reserved word in strict mode ('{}')", string));
- return { parse_identifier() };
- }
- case TokenType::NumericLiteral:
- return { create_ast_node<NumericLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, consume_and_validate_numeric_literal().double_value()) };
- case TokenType::BigIntLiteral:
- return { create_ast_node<BigIntLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, consume().value()) };
- case TokenType::BoolLiteral:
- return { create_ast_node<BooleanLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, consume().bool_value()) };
- case TokenType::StringLiteral:
- return { parse_string_literal(consume()) };
- case TokenType::NullLiteral:
- consume();
- return { create_ast_node<NullLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }) };
- case TokenType::CurlyOpen:
- return { parse_object_expression() };
- case TokenType::Function:
- return { parse_function_node<FunctionExpression>() };
- case TokenType::BracketOpen:
- return { parse_array_expression() };
- case TokenType::RegexLiteral:
- return { parse_regexp_literal() };
- case TokenType::TemplateLiteralStart:
- return { parse_template_literal(false) };
- case TokenType::New: {
- auto new_start = position();
- auto new_target_result = try_parse_new_target_expression();
- if (!new_target_result.is_null()) {
- if (!m_state.in_function_context)
- syntax_error("'new.target' not allowed outside of a function", new_start);
- return { new_target_result.release_nonnull() };
- }
- return { parse_new_expression() };
- }
- case TokenType::Yield:
- if (!m_state.in_generator_function_context)
- goto read_as_identifier;
- return { parse_yield_expression(), false };
- case TokenType::PrivateIdentifier:
- VERIFY(next_token().type() == TokenType::In);
- if (!is_private_identifier_valid())
- syntax_error(String::formatted("Reference to undeclared private field or method '{}'", m_state.current_token.value()));
- return { create_ast_node<PrivateIdentifier>({ m_state.current_token.filename(), rule_start.position(), position() }, consume().value()) };
- default:
- if (match_identifier_name())
- goto read_as_identifier;
- expected("primary expression");
- consume();
- return { create_ast_node<ErrorExpression>({ m_state.current_token.filename(), rule_start.position(), position() }) };
- }
- }
- NonnullRefPtr<RegExpLiteral> Parser::parse_regexp_literal()
- {
- auto rule_start = push_start();
- auto pattern = consume().value();
- // Remove leading and trailing slash.
- pattern = pattern.substring_view(1, pattern.length() - 2);
- auto flags = String::empty();
- auto parsed_flags = RegExpObject::default_flags;
- if (match(TokenType::RegexFlags)) {
- auto flags_start = position();
- flags = consume().value();
- auto parsed_flags_or_error = regex_flags_from_string(flags);
- if (parsed_flags_or_error.is_error())
- syntax_error(parsed_flags_or_error.release_error(), flags_start);
- else
- parsed_flags = parsed_flags_or_error.release_value();
- }
- auto parsed_pattern = parse_regex_pattern(pattern, parsed_flags.has_flag_set(ECMAScriptFlags::Unicode));
- auto parsed_regex = Regex<ECMA262>::parse_pattern(parsed_pattern, parsed_flags);
- if (parsed_regex.error != regex::Error::NoError)
- syntax_error(String::formatted("RegExp compile error: {}", Regex<ECMA262>(parsed_regex, parsed_pattern, parsed_flags).error_string()), rule_start.position());
- SourceRange range { m_state.current_token.filename(), rule_start.position(), position() };
- return create_ast_node<RegExpLiteral>(move(range), move(parsed_regex), move(parsed_pattern), move(parsed_flags), pattern.to_string(), move(flags));
- }
- NonnullRefPtr<Expression> Parser::parse_unary_prefixed_expression()
- {
- auto rule_start = push_start();
- auto precedence = g_operator_precedence.get(m_state.current_token.type());
- auto associativity = operator_associativity(m_state.current_token.type());
- switch (m_state.current_token.type()) {
- case TokenType::PlusPlus: {
- consume();
- auto rhs_start = position();
- auto rhs = parse_expression(precedence, associativity);
- // FIXME: Apparently for functions this should also not be enforced on a parser level,
- // other engines throw ReferenceError for ++foo()
- if (!is<Identifier>(*rhs) && !is<MemberExpression>(*rhs))
- syntax_error(String::formatted("Right-hand side of prefix increment operator must be identifier or member expression, got {}", rhs->class_name()), rhs_start);
- if (m_state.strict_mode && is<Identifier>(*rhs)) {
- auto& identifier = static_cast<Identifier&>(*rhs);
- auto& name = identifier.string();
- check_identifier_name_for_assignment_validity(name);
- }
- return create_ast_node<UpdateExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, UpdateOp::Increment, move(rhs), true);
- }
- case TokenType::MinusMinus: {
- consume();
- auto rhs_start = position();
- auto rhs = parse_expression(precedence, associativity);
- // FIXME: Apparently for functions this should also not be enforced on a parser level,
- // other engines throw ReferenceError for --foo()
- if (!is<Identifier>(*rhs) && !is<MemberExpression>(*rhs))
- syntax_error(String::formatted("Right-hand side of prefix decrement operator must be identifier or member expression, got {}", rhs->class_name()), rhs_start);
- if (m_state.strict_mode && is<Identifier>(*rhs)) {
- auto& identifier = static_cast<Identifier&>(*rhs);
- auto& name = identifier.string();
- check_identifier_name_for_assignment_validity(name);
- }
- return create_ast_node<UpdateExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, UpdateOp::Decrement, move(rhs), true);
- }
- case TokenType::ExclamationMark:
- consume();
- return create_ast_node<UnaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, UnaryOp::Not, parse_expression(precedence, associativity));
- case TokenType::Tilde:
- consume();
- return create_ast_node<UnaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, UnaryOp::BitwiseNot, parse_expression(precedence, associativity));
- case TokenType::Plus:
- consume();
- return create_ast_node<UnaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, UnaryOp::Plus, parse_expression(precedence, associativity));
- case TokenType::Minus:
- consume();
- return create_ast_node<UnaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, UnaryOp::Minus, parse_expression(precedence, associativity));
- case TokenType::Typeof:
- consume();
- return create_ast_node<UnaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, UnaryOp::Typeof, parse_expression(precedence, associativity));
- case TokenType::Void:
- consume();
- return create_ast_node<UnaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, UnaryOp::Void, parse_expression(precedence, associativity));
- case TokenType::Delete: {
- consume();
- auto rhs_start = position();
- auto rhs = parse_expression(precedence, associativity);
- if (is<Identifier>(*rhs) && m_state.strict_mode) {
- syntax_error("Delete of an unqualified identifier in strict mode.", rhs_start);
- }
- if (is<MemberExpression>(*rhs)) {
- auto& member_expression = static_cast<MemberExpression const&>(*rhs);
- if (member_expression.ends_in_private_name())
- syntax_error("Private fields cannot be deleted");
- }
- return create_ast_node<UnaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, UnaryOp::Delete, move(rhs));
- }
- default:
- expected("primary expression");
- consume();
- return create_ast_node<ErrorExpression>({ m_state.current_token.filename(), rule_start.position(), position() });
- }
- }
- NonnullRefPtr<Expression> Parser::parse_property_key()
- {
- auto rule_start = push_start();
- if (match(TokenType::StringLiteral)) {
- return parse_string_literal(consume());
- } else if (match(TokenType::NumericLiteral)) {
- return create_ast_node<NumericLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, consume().double_value());
- } else if (match(TokenType::BigIntLiteral)) {
- return create_ast_node<BigIntLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, consume().value());
- } else if (match(TokenType::BracketOpen)) {
- consume(TokenType::BracketOpen);
- auto result = parse_expression(2);
- consume(TokenType::BracketClose);
- return result;
- } else {
- if (!match_identifier_name())
- expected("IdentifierName");
- return create_ast_node<StringLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, consume().value());
- }
- }
- NonnullRefPtr<ObjectExpression> Parser::parse_object_expression()
- {
- auto rule_start = push_start();
- consume(TokenType::CurlyOpen);
- NonnullRefPtrVector<ObjectProperty> properties;
- ObjectProperty::Type property_type;
- Optional<SourceRange> invalid_object_literal_property_range;
- auto skip_to_next_property = [&] {
- while (!done() && !match(TokenType::Comma) && !match(TokenType::CurlyOpen))
- consume();
- };
- // It is a Syntax Error if PropertyNameList of PropertyDefinitionList contains any duplicate
- // entries for "__proto__" and at least two of those entries were obtained from productions of
- // the form PropertyDefinition : PropertyName : AssignmentExpression .
- bool has_direct_proto_property = false;
- while (!done() && !match(TokenType::CurlyClose)) {
- property_type = ObjectProperty::Type::KeyValue;
- RefPtr<Expression> property_name;
- RefPtr<Expression> property_value;
- FunctionKind function_kind { FunctionKind::Regular };
- if (match(TokenType::TripleDot)) {
- consume();
- property_name = parse_expression(4);
- properties.append(create_ast_node<ObjectProperty>({ m_state.current_token.filename(), rule_start.position(), position() }, *property_name, nullptr, ObjectProperty::Type::Spread, false));
- if (!match(TokenType::Comma))
- break;
- consume(TokenType::Comma);
- continue;
- }
- auto type = m_state.current_token.type();
- if (match(TokenType::Asterisk)) {
- consume();
- property_type = ObjectProperty::Type::KeyValue;
- property_name = parse_property_key();
- function_kind = FunctionKind ::Generator;
- } else if (match_identifier()) {
- auto identifier = consume();
- if (identifier.original_value() == "get"sv && match_property_key()) {
- property_type = ObjectProperty::Type::Getter;
- property_name = parse_property_key();
- } else if (identifier.original_value() == "set"sv && match_property_key()) {
- property_type = ObjectProperty::Type::Setter;
- property_name = parse_property_key();
- } else {
- property_name = create_ast_node<StringLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, identifier.value());
- property_value = create_ast_node<Identifier>({ m_state.current_token.filename(), rule_start.position(), position() }, identifier.value());
- }
- } else {
- property_name = parse_property_key();
- }
- bool is_proto = (type == TokenType::StringLiteral || type == TokenType::Identifier) && is<StringLiteral>(*property_name) && static_cast<StringLiteral const&>(*property_name).value() == "__proto__";
- if (property_type == ObjectProperty::Type::Getter || property_type == ObjectProperty::Type::Setter) {
- if (!match(TokenType::ParenOpen)) {
- expected("'(' for object getter or setter property");
- skip_to_next_property();
- continue;
- }
- }
- if (match(TokenType::Equals)) {
- // Not a valid object literal, but a valid assignment target
- consume();
- // Parse the expression and throw it away
- auto expression = parse_expression(2);
- if (!invalid_object_literal_property_range.has_value())
- invalid_object_literal_property_range = expression->source_range();
- } else if (match(TokenType::ParenOpen)) {
- VERIFY(property_name);
- u8 parse_options = FunctionNodeParseOptions::AllowSuperPropertyLookup;
- if (property_type == ObjectProperty::Type::Getter)
- parse_options |= FunctionNodeParseOptions::IsGetterFunction;
- if (property_type == ObjectProperty::Type::Setter)
- parse_options |= FunctionNodeParseOptions::IsSetterFunction;
- if (function_kind == FunctionKind::Generator)
- parse_options |= FunctionNodeParseOptions::IsGeneratorFunction;
- auto function = parse_function_node<FunctionExpression>(parse_options);
- properties.append(create_ast_node<ObjectProperty>({ m_state.current_token.filename(), rule_start.position(), position() }, *property_name, function, property_type, true));
- } else if (match(TokenType::Colon)) {
- if (!property_name) {
- expected("a property name");
- skip_to_next_property();
- continue;
- }
- consume();
- if (is_proto) {
- if (has_direct_proto_property)
- syntax_error("Property name '__proto__' must not appear more than once in object literal");
- has_direct_proto_property = true;
- }
- properties.append(create_ast_node<ObjectProperty>({ m_state.current_token.filename(), rule_start.position(), position() }, *property_name, parse_expression(2), property_type, false));
- } else if (property_name && property_value) {
- properties.append(create_ast_node<ObjectProperty>({ m_state.current_token.filename(), rule_start.position(), position() }, *property_name, *property_value, property_type, false));
- } else {
- expected("a property");
- skip_to_next_property();
- continue;
- }
- if (!match(TokenType::Comma))
- break;
- consume(TokenType::Comma);
- }
- consume(TokenType::CurlyClose);
- return create_ast_node<ObjectExpression>(
- { m_state.current_token.filename(), rule_start.position(), position() },
- move(properties),
- move(invalid_object_literal_property_range));
- }
- NonnullRefPtr<ArrayExpression> Parser::parse_array_expression()
- {
- auto rule_start = push_start();
- consume(TokenType::BracketOpen);
- Vector<RefPtr<Expression>> elements;
- while (match_expression() || match(TokenType::TripleDot) || match(TokenType::Comma)) {
- RefPtr<Expression> expression;
- if (match(TokenType::TripleDot)) {
- consume(TokenType::TripleDot);
- expression = create_ast_node<SpreadExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, parse_expression(2));
- } else if (match_expression()) {
- expression = parse_expression(2);
- }
- elements.append(expression);
- if (!match(TokenType::Comma))
- break;
- consume(TokenType::Comma);
- }
- consume(TokenType::BracketClose);
- return create_ast_node<ArrayExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, move(elements));
- }
- NonnullRefPtr<StringLiteral> Parser::parse_string_literal(const Token& token, bool in_template_literal)
- {
- auto rule_start = push_start();
- auto status = Token::StringValueStatus::Ok;
- auto string = token.string_value(status);
- if (status != Token::StringValueStatus::Ok) {
- String message;
- if (status == Token::StringValueStatus::LegacyOctalEscapeSequence) {
- m_state.string_legacy_octal_escape_sequence_in_scope = true;
- if (in_template_literal)
- message = "Octal escape sequence not allowed in template literal";
- else if (m_state.strict_mode)
- message = "Octal escape sequence in string literal not allowed in strict mode";
- } else if (status == Token::StringValueStatus::MalformedHexEscape || status == Token::StringValueStatus::MalformedUnicodeEscape) {
- auto type = status == Token::StringValueStatus::MalformedUnicodeEscape ? "unicode" : "hexadecimal";
- message = String::formatted("Malformed {} escape sequence", type);
- } else if (status == Token::StringValueStatus::UnicodeEscapeOverflow) {
- message = "Unicode code_point must not be greater than 0x10ffff in escape sequence";
- } else {
- VERIFY_NOT_REACHED();
- }
- if (!message.is_empty())
- syntax_error(message, Position { token.line_number(), token.line_column() });
- }
- auto is_use_strict_directive = !in_template_literal && (token.value() == "'use strict'" || token.value() == "\"use strict\"");
- return create_ast_node<StringLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, string, is_use_strict_directive);
- }
- NonnullRefPtr<TemplateLiteral> Parser::parse_template_literal(bool is_tagged)
- {
- auto rule_start = push_start();
- consume(TokenType::TemplateLiteralStart);
- NonnullRefPtrVector<Expression> expressions;
- NonnullRefPtrVector<Expression> raw_strings;
- auto append_empty_string = [this, &rule_start, &expressions, &raw_strings, is_tagged]() {
- auto string_literal = create_ast_node<StringLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, "");
- expressions.append(string_literal);
- if (is_tagged)
- raw_strings.append(string_literal);
- };
- if (!match(TokenType::TemplateLiteralString))
- append_empty_string();
- while (!done() && !match(TokenType::TemplateLiteralEnd) && !match(TokenType::UnterminatedTemplateLiteral)) {
- if (match(TokenType::TemplateLiteralString)) {
- auto token = consume();
- expressions.append(parse_string_literal(token, true));
- if (is_tagged)
- raw_strings.append(create_ast_node<StringLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, token.raw_template_value()));
- } else if (match(TokenType::TemplateLiteralExprStart)) {
- consume(TokenType::TemplateLiteralExprStart);
- if (match(TokenType::TemplateLiteralExprEnd)) {
- syntax_error("Empty template literal expression block");
- return create_ast_node<TemplateLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, expressions);
- }
- expressions.append(parse_expression(0));
- if (match(TokenType::UnterminatedTemplateLiteral)) {
- syntax_error("Unterminated template literal");
- return create_ast_node<TemplateLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, expressions);
- }
- consume(TokenType::TemplateLiteralExprEnd);
- if (!match(TokenType::TemplateLiteralString))
- append_empty_string();
- } else {
- expected("Template literal string or expression");
- break;
- }
- }
- if (match(TokenType::UnterminatedTemplateLiteral)) {
- syntax_error("Unterminated template literal");
- } else {
- consume(TokenType::TemplateLiteralEnd);
- }
- if (is_tagged)
- return create_ast_node<TemplateLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, expressions, raw_strings);
- return create_ast_node<TemplateLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, expressions);
- }
- NonnullRefPtr<Expression> Parser::parse_expression(int min_precedence, Associativity associativity, const Vector<TokenType>& forbidden)
- {
- auto rule_start = push_start();
- auto [expression, should_continue_parsing] = parse_primary_expression();
- auto check_for_invalid_object_property = [&](auto& expression) {
- if (is<ObjectExpression>(*expression)) {
- if (auto range = static_cast<ObjectExpression&>(*expression).invalid_property_range(); range.has_value())
- syntax_error("Invalid property in object literal", range->start);
- }
- };
- if (is<Identifier>(*expression) && m_state.current_scope_pusher) {
- auto identifier_instance = static_ptr_cast<Identifier>(expression);
- auto function_scope = m_state.current_scope_pusher->last_function_scope();
- auto function_parent_scope = function_scope ? function_scope->parent_scope() : nullptr;
- bool has_not_been_declared_as_variable = true;
- for (auto scope = m_state.current_scope_pusher; scope != function_parent_scope; scope = scope->parent_scope()) {
- if (scope->has_declaration(identifier_instance->string())) {
- has_not_been_declared_as_variable = false;
- break;
- }
- }
- if (has_not_been_declared_as_variable) {
- if (identifier_instance->string() == "arguments"sv)
- m_state.current_scope_pusher->set_contains_access_to_arguments_object();
- }
- }
- while (match(TokenType::TemplateLiteralStart)) {
- auto template_literal = parse_template_literal(true);
- expression = create_ast_node<TaggedTemplateLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, move(expression), move(template_literal));
- }
- if (should_continue_parsing) {
- while (match_secondary_expression(forbidden)) {
- int new_precedence = g_operator_precedence.get(m_state.current_token.type());
- if (new_precedence < min_precedence)
- break;
- if (new_precedence == min_precedence && associativity == Associativity::Left)
- break;
- check_for_invalid_object_property(expression);
- Associativity new_associativity = operator_associativity(m_state.current_token.type());
- expression = parse_secondary_expression(move(expression), new_precedence, new_associativity);
- while (match(TokenType::TemplateLiteralStart) && !is<UpdateExpression>(*expression)) {
- auto template_literal = parse_template_literal(true);
- expression = create_ast_node<TaggedTemplateLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, move(expression), move(template_literal));
- }
- }
- }
- if (is<SuperExpression>(*expression))
- syntax_error("'super' keyword unexpected here");
- check_for_invalid_object_property(expression);
- if (is<CallExpression>(*expression) && m_state.current_scope_pusher) {
- auto& callee = static_ptr_cast<CallExpression>(expression)->callee();
- if (is<Identifier>(callee)) {
- auto& identifier_instance = static_cast<Identifier const&>(callee);
- if (identifier_instance.string() == "eval"sv) {
- bool has_not_been_declared_as_variable = true;
- for (auto scope = m_state.current_scope_pusher; scope; scope = scope->parent_scope()) {
- if (scope->has_declaration(identifier_instance.string())) {
- has_not_been_declared_as_variable = false;
- break;
- }
- }
- if (has_not_been_declared_as_variable)
- m_state.current_scope_pusher->set_contains_direct_call_to_eval();
- }
- }
- }
- if (match(TokenType::Comma) && min_precedence <= 1) {
- NonnullRefPtrVector<Expression> expressions;
- expressions.append(expression);
- while (match(TokenType::Comma)) {
- consume();
- expressions.append(parse_expression(2));
- }
- expression = create_ast_node<SequenceExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, move(expressions));
- }
- return expression;
- }
- NonnullRefPtr<Expression> Parser::parse_secondary_expression(NonnullRefPtr<Expression> lhs, int min_precedence, Associativity associativity)
- {
- auto rule_start = push_start();
- switch (m_state.current_token.type()) {
- case TokenType::Plus:
- consume();
- return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::Addition, move(lhs), parse_expression(min_precedence, associativity));
- case TokenType::PlusEquals:
- return parse_assignment_expression(AssignmentOp::AdditionAssignment, move(lhs), min_precedence, associativity);
- case TokenType::Minus:
- consume();
- return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::Subtraction, move(lhs), parse_expression(min_precedence, associativity));
- case TokenType::MinusEquals:
- return parse_assignment_expression(AssignmentOp::SubtractionAssignment, move(lhs), min_precedence, associativity);
- case TokenType::Asterisk:
- consume();
- return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::Multiplication, move(lhs), parse_expression(min_precedence, associativity));
- case TokenType::AsteriskEquals:
- return parse_assignment_expression(AssignmentOp::MultiplicationAssignment, move(lhs), min_precedence, associativity);
- case TokenType::Slash:
- consume();
- return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::Division, move(lhs), parse_expression(min_precedence, associativity));
- case TokenType::SlashEquals:
- return parse_assignment_expression(AssignmentOp::DivisionAssignment, move(lhs), min_precedence, associativity);
- case TokenType::Percent:
- consume();
- return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::Modulo, move(lhs), parse_expression(min_precedence, associativity));
- case TokenType::PercentEquals:
- return parse_assignment_expression(AssignmentOp::ModuloAssignment, move(lhs), min_precedence, associativity);
- case TokenType::DoubleAsterisk:
- consume();
- return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::Exponentiation, move(lhs), parse_expression(min_precedence, associativity));
- case TokenType::DoubleAsteriskEquals:
- return parse_assignment_expression(AssignmentOp::ExponentiationAssignment, move(lhs), min_precedence, associativity);
- case TokenType::GreaterThan:
- consume();
- return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::GreaterThan, move(lhs), parse_expression(min_precedence, associativity));
- case TokenType::GreaterThanEquals:
- consume();
- return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::GreaterThanEquals, move(lhs), parse_expression(min_precedence, associativity));
- case TokenType::LessThan:
- consume();
- return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::LessThan, move(lhs), parse_expression(min_precedence, associativity));
- case TokenType::LessThanEquals:
- consume();
- return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::LessThanEquals, move(lhs), parse_expression(min_precedence, associativity));
- case TokenType::EqualsEqualsEquals:
- consume();
- return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::StrictlyEquals, move(lhs), parse_expression(min_precedence, associativity));
- case TokenType::ExclamationMarkEqualsEquals:
- consume();
- return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::StrictlyInequals, move(lhs), parse_expression(min_precedence, associativity));
- case TokenType::EqualsEquals:
- consume();
- return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::LooselyEquals, move(lhs), parse_expression(min_precedence, associativity));
- case TokenType::ExclamationMarkEquals:
- consume();
- return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::LooselyInequals, move(lhs), parse_expression(min_precedence, associativity));
- case TokenType::In:
- consume();
- return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::In, move(lhs), parse_expression(min_precedence, associativity));
- case TokenType::Instanceof:
- consume();
- return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::InstanceOf, move(lhs), parse_expression(min_precedence, associativity));
- case TokenType::Ampersand:
- consume();
- return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::BitwiseAnd, move(lhs), parse_expression(min_precedence, associativity));
- case TokenType::AmpersandEquals:
- return parse_assignment_expression(AssignmentOp::BitwiseAndAssignment, move(lhs), min_precedence, associativity);
- case TokenType::Pipe:
- consume();
- return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::BitwiseOr, move(lhs), parse_expression(min_precedence, associativity));
- case TokenType::PipeEquals:
- return parse_assignment_expression(AssignmentOp::BitwiseOrAssignment, move(lhs), min_precedence, associativity);
- case TokenType::Caret:
- consume();
- return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::BitwiseXor, move(lhs), parse_expression(min_precedence, associativity));
- case TokenType::CaretEquals:
- return parse_assignment_expression(AssignmentOp::BitwiseXorAssignment, move(lhs), min_precedence, associativity);
- case TokenType::ShiftLeft:
- consume();
- return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::LeftShift, move(lhs), parse_expression(min_precedence, associativity));
- case TokenType::ShiftLeftEquals:
- return parse_assignment_expression(AssignmentOp::LeftShiftAssignment, move(lhs), min_precedence, associativity);
- case TokenType::ShiftRight:
- consume();
- return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::RightShift, move(lhs), parse_expression(min_precedence, associativity));
- case TokenType::ShiftRightEquals:
- return parse_assignment_expression(AssignmentOp::RightShiftAssignment, move(lhs), min_precedence, associativity);
- case TokenType::UnsignedShiftRight:
- consume();
- return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::UnsignedRightShift, move(lhs), parse_expression(min_precedence, associativity));
- case TokenType::UnsignedShiftRightEquals:
- return parse_assignment_expression(AssignmentOp::UnsignedRightShiftAssignment, move(lhs), min_precedence, associativity);
- case TokenType::ParenOpen:
- return parse_call_expression(move(lhs));
- case TokenType::Equals:
- return parse_assignment_expression(AssignmentOp::Assignment, move(lhs), min_precedence, associativity);
- case TokenType::Period:
- consume();
- if (match(TokenType::PrivateIdentifier)) {
- if (!is_private_identifier_valid())
- syntax_error(String::formatted("Reference to undeclared private field or method '{}'", m_state.current_token.value()));
- else if (is<SuperExpression>(*lhs))
- syntax_error(String::formatted("Cannot access private field or method '{}' on super", m_state.current_token.value()));
- return create_ast_node<MemberExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, move(lhs), create_ast_node<PrivateIdentifier>({ m_state.current_token.filename(), rule_start.position(), position() }, consume().value()));
- } else if (!match_identifier_name()) {
- expected("IdentifierName");
- }
- return create_ast_node<MemberExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, move(lhs), create_ast_node<Identifier>({ m_state.current_token.filename(), rule_start.position(), position() }, consume().value()));
- case TokenType::BracketOpen: {
- consume(TokenType::BracketOpen);
- auto expression = create_ast_node<MemberExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, move(lhs), parse_expression(0), true);
- consume(TokenType::BracketClose);
- return expression;
- }
- case TokenType::PlusPlus:
- // FIXME: Apparently for functions this should also not be enforced on a parser level,
- // other engines throw ReferenceError for foo()++
- if (!is<Identifier>(*lhs) && !is<MemberExpression>(*lhs))
- syntax_error(String::formatted("Left-hand side of postfix increment operator must be identifier or member expression, got {}", lhs->class_name()));
- if (m_state.strict_mode && is<Identifier>(*lhs)) {
- auto& identifier = static_cast<Identifier&>(*lhs);
- auto& name = identifier.string();
- check_identifier_name_for_assignment_validity(name);
- }
- consume();
- return create_ast_node<UpdateExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, UpdateOp::Increment, move(lhs));
- case TokenType::MinusMinus:
- // FIXME: Apparently for functions this should also not be enforced on a parser level,
- // other engines throw ReferenceError for foo()--
- if (!is<Identifier>(*lhs) && !is<MemberExpression>(*lhs))
- syntax_error(String::formatted("Left-hand side of postfix increment operator must be identifier or member expression, got {}", lhs->class_name()));
- if (m_state.strict_mode && is<Identifier>(*lhs)) {
- auto& identifier = static_cast<Identifier&>(*lhs);
- auto& name = identifier.string();
- check_identifier_name_for_assignment_validity(name);
- }
- consume();
- return create_ast_node<UpdateExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, UpdateOp::Decrement, move(lhs));
- case TokenType::DoubleAmpersand:
- consume();
- return create_ast_node<LogicalExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, LogicalOp::And, move(lhs), parse_expression(min_precedence, associativity));
- case TokenType::DoubleAmpersandEquals:
- return parse_assignment_expression(AssignmentOp::AndAssignment, move(lhs), min_precedence, associativity);
- case TokenType::DoublePipe:
- consume();
- return create_ast_node<LogicalExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, LogicalOp::Or, move(lhs), parse_expression(min_precedence, associativity));
- case TokenType::DoublePipeEquals:
- return parse_assignment_expression(AssignmentOp::OrAssignment, move(lhs), min_precedence, associativity);
- case TokenType::DoubleQuestionMark:
- consume();
- return create_ast_node<LogicalExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, LogicalOp::NullishCoalescing, move(lhs), parse_expression(min_precedence, associativity));
- case TokenType::DoubleQuestionMarkEquals:
- return parse_assignment_expression(AssignmentOp::NullishAssignment, move(lhs), min_precedence, associativity);
- case TokenType::QuestionMark:
- return parse_conditional_expression(move(lhs));
- case TokenType::QuestionMarkPeriod:
- // FIXME: This should allow `(new Foo)?.bar', but as our parser strips parenthesis,
- // we can't really tell if `lhs' was parenthesized at this point.
- if (is<NewExpression>(lhs.ptr())) {
- syntax_error("'new' cannot be used with optional chaining", position());
- consume();
- return lhs;
- }
- return parse_optional_chain(move(lhs));
- default:
- expected("secondary expression");
- consume();
- return create_ast_node<ErrorExpression>({ m_state.current_token.filename(), rule_start.position(), position() });
- }
- }
- bool Parser::is_private_identifier_valid() const
- {
- VERIFY(match(TokenType::PrivateIdentifier));
- if (!m_state.referenced_private_names)
- return false;
- // We might not have hit the declaration yet so class will check this in the end
- m_state.referenced_private_names->set(m_state.current_token.value());
- return true;
- }
- RefPtr<BindingPattern> Parser::synthesize_binding_pattern(Expression const& expression)
- {
- VERIFY(is<ArrayExpression>(expression) || is<ObjectExpression>(expression));
- // Clear any syntax error that has occurred in the range that 'expression' spans.
- m_state.errors.remove_all_matching([range = expression.source_range()](auto const& error) {
- return error.position.has_value() && range.contains(*error.position);
- });
- // Make a parser and parse the source for this expression as a binding pattern.
- auto source = m_state.lexer.source().substring_view(expression.source_range().start.offset - 2, expression.source_range().end.offset - expression.source_range().start.offset);
- Lexer lexer { source, m_state.lexer.filename(), expression.source_range().start.line, expression.source_range().start.column };
- Parser parser { lexer };
- parser.m_state.strict_mode = m_state.strict_mode;
- parser.m_state.allow_super_property_lookup = m_state.allow_super_property_lookup;
- parser.m_state.allow_super_constructor_call = m_state.allow_super_constructor_call;
- parser.m_state.in_function_context = m_state.in_function_context;
- parser.m_state.in_formal_parameter_context = m_state.in_formal_parameter_context;
- parser.m_state.in_generator_function_context = m_state.in_generator_function_context;
- parser.m_state.in_arrow_function_context = m_state.in_arrow_function_context;
- parser.m_state.in_break_context = m_state.in_break_context;
- parser.m_state.in_continue_context = m_state.in_continue_context;
- parser.m_state.string_legacy_octal_escape_sequence_in_scope = m_state.string_legacy_octal_escape_sequence_in_scope;
- parser.m_state.in_class_field_initializer = m_state.in_class_field_initializer;
- auto result = parser.parse_binding_pattern(AllowDuplicates::Yes, AllowMemberExpressions::Yes);
- if (parser.has_errors())
- m_state.errors.extend(parser.errors());
- return result;
- }
- NonnullRefPtr<AssignmentExpression> Parser::parse_assignment_expression(AssignmentOp assignment_op, NonnullRefPtr<Expression> lhs, int min_precedence, Associativity associativity)
- {
- auto rule_start = push_start();
- VERIFY(match(TokenType::Equals)
- || match(TokenType::PlusEquals)
- || match(TokenType::MinusEquals)
- || match(TokenType::AsteriskEquals)
- || match(TokenType::SlashEquals)
- || match(TokenType::PercentEquals)
- || match(TokenType::DoubleAsteriskEquals)
- || match(TokenType::AmpersandEquals)
- || match(TokenType::PipeEquals)
- || match(TokenType::CaretEquals)
- || match(TokenType::ShiftLeftEquals)
- || match(TokenType::ShiftRightEquals)
- || match(TokenType::UnsignedShiftRightEquals)
- || match(TokenType::DoubleAmpersandEquals)
- || match(TokenType::DoublePipeEquals)
- || match(TokenType::DoubleQuestionMarkEquals));
- consume();
- if (assignment_op == AssignmentOp::Assignment) {
- if (is<ArrayExpression>(*lhs) || is<ObjectExpression>(*lhs)) {
- auto binding_pattern = synthesize_binding_pattern(*lhs);
- if (binding_pattern) {
- auto rhs = parse_expression(min_precedence, associativity);
- return create_ast_node<AssignmentExpression>(
- { m_state.current_token.filename(), rule_start.position(), position() },
- assignment_op,
- binding_pattern.release_nonnull(),
- move(rhs));
- }
- }
- }
- if (!is<Identifier>(*lhs) && !is<MemberExpression>(*lhs) && !is<CallExpression>(*lhs)) {
- syntax_error("Invalid left-hand side in assignment");
- } else if (m_state.strict_mode && is<Identifier>(*lhs)) {
- auto name = static_cast<const Identifier&>(*lhs).string();
- check_identifier_name_for_assignment_validity(name);
- } else if (m_state.strict_mode && is<CallExpression>(*lhs)) {
- syntax_error("Cannot assign to function call");
- }
- auto rhs = parse_expression(min_precedence, associativity);
- return create_ast_node<AssignmentExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, assignment_op, move(lhs), move(rhs));
- }
- NonnullRefPtr<Identifier> Parser::parse_identifier()
- {
- auto identifier_start = position();
- auto token = consume_identifier();
- if (m_state.in_class_field_initializer && token.value() == "arguments"sv)
- syntax_error("'arguments' is not allowed in class field initializer");
- return create_ast_node<Identifier>(
- { m_state.current_token.filename(), identifier_start, position() },
- token.value());
- }
- Vector<CallExpression::Argument> Parser::parse_arguments()
- {
- Vector<CallExpression::Argument> arguments;
- consume(TokenType::ParenOpen);
- while (match_expression() || match(TokenType::TripleDot)) {
- if (match(TokenType::TripleDot)) {
- consume();
- arguments.append({ parse_expression(2), true });
- } else {
- arguments.append({ parse_expression(2), false });
- }
- if (!match(TokenType::Comma))
- break;
- consume();
- }
- consume(TokenType::ParenClose);
- return arguments;
- }
- NonnullRefPtr<Expression> Parser::parse_call_expression(NonnullRefPtr<Expression> lhs)
- {
- auto rule_start = push_start();
- if (!m_state.allow_super_constructor_call && is<SuperExpression>(*lhs))
- syntax_error("'super' keyword unexpected here");
- auto arguments = parse_arguments();
- if (is<SuperExpression>(*lhs))
- return create_ast_node<SuperCall>({ m_state.current_token.filename(), rule_start.position(), position() }, move(arguments));
- return create_ast_node<CallExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, move(lhs), move(arguments));
- }
- NonnullRefPtr<NewExpression> Parser::parse_new_expression()
- {
- auto rule_start = push_start();
- consume(TokenType::New);
- auto callee = parse_expression(g_operator_precedence.get(TokenType::New), Associativity::Right, { TokenType::ParenOpen, TokenType::QuestionMarkPeriod });
- Vector<CallExpression::Argument> arguments;
- if (match(TokenType::ParenOpen)) {
- consume(TokenType::ParenOpen);
- while (match_expression() || match(TokenType::TripleDot)) {
- if (match(TokenType::TripleDot)) {
- consume();
- arguments.append({ parse_expression(2), true });
- } else {
- arguments.append({ parse_expression(2), false });
- }
- if (!match(TokenType::Comma))
- break;
- consume();
- }
- consume(TokenType::ParenClose);
- }
- return create_ast_node<NewExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, move(callee), move(arguments));
- }
- NonnullRefPtr<YieldExpression> Parser::parse_yield_expression()
- {
- auto rule_start = push_start();
- if (m_state.in_formal_parameter_context)
- syntax_error("'Yield' expression is not allowed in formal parameters of generator function");
- consume(TokenType::Yield);
- RefPtr<Expression> argument;
- bool yield_from = false;
- if (!m_state.current_token.trivia_contains_line_terminator()) {
- if (match(TokenType::Asterisk)) {
- consume();
- yield_from = true;
- }
- if (yield_from || match_expression() || match(TokenType::Class))
- argument = parse_expression(0);
- }
- return create_ast_node<YieldExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, move(argument), yield_from);
- }
- NonnullRefPtr<ReturnStatement> Parser::parse_return_statement()
- {
- auto rule_start = push_start();
- if (!m_state.in_function_context && !m_state.in_arrow_function_context)
- syntax_error("'return' not allowed outside of a function");
- consume(TokenType::Return);
- // Automatic semicolon insertion: terminate statement when return is followed by newline
- if (m_state.current_token.trivia_contains_line_terminator())
- return create_ast_node<ReturnStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, nullptr);
- if (match_expression()) {
- auto expression = parse_expression(0);
- consume_or_insert_semicolon();
- return create_ast_node<ReturnStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(expression));
- }
- consume_or_insert_semicolon();
- return create_ast_node<ReturnStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, nullptr);
- }
- void Parser::parse_statement_list(ScopeNode& output_node, AllowLabelledFunction allow_labelled_functions)
- {
- while (!done()) {
- if (match_declaration()) {
- auto declaration = parse_declaration();
- VERIFY(m_state.current_scope_pusher);
- m_state.current_scope_pusher->add_declaration(declaration);
- output_node.append(move(declaration));
- } else if (match_statement()) {
- output_node.append(parse_statement(allow_labelled_functions));
- } else {
- break;
- }
- }
- }
- // FunctionBody, https://tc39.es/ecma262/#prod-FunctionBody
- NonnullRefPtr<FunctionBody> Parser::parse_function_body(Vector<FunctionDeclaration::Parameter> const& parameters, FunctionKind function_kind, bool& contains_direct_call_to_eval)
- {
- auto rule_start = push_start();
- auto function_body = create_ast_node<FunctionBody>({ m_state.current_token.filename(), rule_start.position(), position() });
- ScopePusher function_scope = ScopePusher::function_scope(*this, function_body, parameters); // FIXME <-
- consume(TokenType::CurlyOpen);
- auto has_use_strict = parse_directive(function_body);
- bool previous_strict_mode = m_state.strict_mode;
- if (has_use_strict) {
- m_state.strict_mode = true;
- function_body->set_strict_mode();
- if (!is_simple_parameter_list(parameters))
- syntax_error("Illegal 'use strict' directive in function with non-simple parameter list");
- } else if (previous_strict_mode) {
- function_body->set_strict_mode();
- }
- parse_statement_list(function_body);
- // If the function contains 'use strict' we need to check the parameters (again).
- if (function_body->in_strict_mode() || function_kind != FunctionKind::Regular) {
- Vector<StringView> parameter_names;
- for (auto& parameter : parameters) {
- parameter.binding.visit(
- [&](FlyString const& parameter_name) {
- check_identifier_name_for_assignment_validity(parameter_name, function_body->in_strict_mode());
- if (function_kind == FunctionKind::Generator && parameter_name == "yield"sv)
- syntax_error("Parameter name 'yield' not allowed in this context");
- for (auto& previous_name : parameter_names) {
- if (previous_name == parameter_name) {
- syntax_error(String::formatted("Duplicate parameter '{}' not allowed in strict mode", parameter_name));
- }
- }
- parameter_names.append(parameter_name);
- },
- [&](NonnullRefPtr<BindingPattern> const& binding) {
- binding->for_each_bound_name([&](auto& bound_name) {
- if (function_kind == FunctionKind::Generator && bound_name == "yield"sv)
- syntax_error("Parameter name 'yield' not allowed in this context");
- for (auto& previous_name : parameter_names) {
- if (previous_name == bound_name) {
- syntax_error(String::formatted("Duplicate parameter '{}' not allowed in strict mode", bound_name));
- break;
- }
- }
- parameter_names.append(bound_name);
- });
- });
- }
- }
- consume(TokenType::CurlyClose);
- m_state.strict_mode = previous_strict_mode;
- contains_direct_call_to_eval = function_scope.contains_direct_call_to_eval();
- return function_body;
- }
- NonnullRefPtr<BlockStatement> Parser::parse_block_statement()
- {
- auto rule_start = push_start();
- auto block = create_ast_node<BlockStatement>({ m_state.current_token.filename(), rule_start.position(), position() });
- ScopePusher block_scope = ScopePusher::block_scope(*this, block);
- consume(TokenType::CurlyOpen);
- parse_statement_list(block);
- consume(TokenType::CurlyClose);
- return block;
- }
- template<typename FunctionNodeType>
- NonnullRefPtr<FunctionNodeType> Parser::parse_function_node(u8 parse_options)
- {
- auto rule_start = push_start();
- VERIFY(!(parse_options & FunctionNodeParseOptions::IsGetterFunction && parse_options & FunctionNodeParseOptions::IsSetterFunction));
- TemporaryChange super_property_access_rollback(m_state.allow_super_property_lookup, !!(parse_options & FunctionNodeParseOptions::AllowSuperPropertyLookup));
- TemporaryChange super_constructor_call_rollback(m_state.allow_super_constructor_call, !!(parse_options & FunctionNodeParseOptions::AllowSuperConstructorCall));
- TemporaryChange break_context_rollback(m_state.in_break_context, false);
- TemporaryChange continue_context_rollback(m_state.in_continue_context, false);
- TemporaryChange class_field_initializer_rollback(m_state.in_class_field_initializer, false);
- TemporaryChange might_need_arguments_object_rollback(m_state.function_might_need_arguments_object, false);
- constexpr auto is_function_expression = IsSame<FunctionNodeType, FunctionExpression>;
- auto function_kind = (parse_options & FunctionNodeParseOptions::IsGeneratorFunction) != 0 ? FunctionKind::Generator : FunctionKind::Regular;
- String name;
- if (parse_options & FunctionNodeParseOptions::CheckForFunctionAndName) {
- consume(TokenType::Function);
- if (function_kind == FunctionKind::Regular) {
- function_kind = match(TokenType::Asterisk) ? FunctionKind::Generator : FunctionKind::Regular;
- if (function_kind == FunctionKind::Generator) {
- consume(TokenType::Asterisk);
- parse_options = parse_options | FunctionNodeParseOptions::IsGeneratorFunction;
- }
- }
- if (FunctionNodeType::must_have_name() || match_identifier())
- name = consume_identifier().value();
- else if (is_function_expression && (match(TokenType::Yield) || match(TokenType::Await)))
- name = consume().value();
- check_identifier_name_for_assignment_validity(name);
- }
- TemporaryChange generator_change(m_state.in_generator_function_context, function_kind == FunctionKind::Generator);
- consume(TokenType::ParenOpen);
- i32 function_length = -1;
- auto parameters = parse_formal_parameters(function_length, parse_options);
- consume(TokenType::ParenClose);
- if (function_length == -1)
- function_length = parameters.size();
- TemporaryChange function_context_rollback(m_state.in_function_context, true);
- auto old_labels_in_scope = move(m_state.labels_in_scope);
- ScopeGuard guard([&]() {
- m_state.labels_in_scope = move(old_labels_in_scope);
- });
- bool contains_direct_call_to_eval = false;
- auto body = parse_function_body(parameters, function_kind, contains_direct_call_to_eval);
- auto has_strict_directive = body->in_strict_mode();
- if (has_strict_directive)
- check_identifier_name_for_assignment_validity(name, true);
- return create_ast_node<FunctionNodeType>(
- { m_state.current_token.filename(), rule_start.position(), position() },
- name, move(body), move(parameters), function_length,
- function_kind, has_strict_directive, m_state.function_might_need_arguments_object,
- contains_direct_call_to_eval);
- }
- Vector<FunctionNode::Parameter> Parser::parse_formal_parameters(int& function_length, u8 parse_options)
- {
- auto rule_start = push_start();
- bool has_default_parameter = false;
- bool has_rest_parameter = false;
- TemporaryChange formal_parameter_context_change { m_state.in_formal_parameter_context, true };
- Vector<FunctionNode::Parameter> parameters;
- auto consume_identifier_or_binding_pattern = [&]() -> Variant<FlyString, NonnullRefPtr<BindingPattern>> {
- if (auto pattern = parse_binding_pattern(AllowDuplicates::No, AllowMemberExpressions::Yes))
- return pattern.release_nonnull();
- auto token = consume_identifier();
- auto parameter_name = token.value();
- check_identifier_name_for_assignment_validity(parameter_name);
- for (auto& parameter : parameters) {
- bool has_same_name = parameter.binding.visit(
- [&](FlyString const& name) {
- return name == parameter_name;
- },
- [&](NonnullRefPtr<BindingPattern> const& bindings) {
- bool found_duplicate = false;
- bindings->for_each_bound_name([&](auto& bound_name) {
- if (bound_name == parameter_name)
- found_duplicate = true;
- });
- return found_duplicate;
- });
- if (!has_same_name)
- continue;
- String message;
- if (parse_options & FunctionNodeParseOptions::IsArrowFunction)
- message = String::formatted("Duplicate parameter '{}' not allowed in arrow function", parameter_name);
- else if (m_state.strict_mode)
- message = String::formatted("Duplicate parameter '{}' not allowed in strict mode", parameter_name);
- else if (has_default_parameter || match(TokenType::Equals))
- message = String::formatted("Duplicate parameter '{}' not allowed in function with default parameter", parameter_name);
- else if (has_rest_parameter)
- message = String::formatted("Duplicate parameter '{}' not allowed in function with rest parameter", parameter_name);
- if (!message.is_empty())
- syntax_error(message, Position { token.line_number(), token.line_column() });
- break;
- }
- return FlyString { token.value() };
- };
- while (match(TokenType::CurlyOpen) || match(TokenType::BracketOpen) || match_identifier() || match(TokenType::TripleDot)) {
- if (parse_options & FunctionNodeParseOptions::IsGetterFunction)
- syntax_error("Getter function must have no arguments");
- if (parse_options & FunctionNodeParseOptions::IsSetterFunction && (parameters.size() >= 1 || match(TokenType::TripleDot)))
- syntax_error("Setter function must have one argument");
- auto is_rest = false;
- if (match(TokenType::TripleDot)) {
- consume();
- has_rest_parameter = true;
- function_length = parameters.size();
- is_rest = true;
- }
- auto parameter = consume_identifier_or_binding_pattern();
- RefPtr<Expression> default_value;
- if (match(TokenType::Equals)) {
- consume();
- if (is_rest)
- syntax_error("Rest parameter may not have a default initializer");
- TemporaryChange change(m_state.in_function_context, true);
- has_default_parameter = true;
- function_length = parameters.size();
- default_value = parse_expression(2);
- bool is_generator = parse_options & FunctionNodeParseOptions::IsGeneratorFunction;
- if ((is_generator || m_state.strict_mode) && default_value && default_value->fast_is<Identifier>() && static_cast<Identifier&>(*default_value).string() == "yield"sv)
- syntax_error("Generator function parameter initializer cannot contain a reference to an identifier named \"yield\"");
- }
- parameters.append({ move(parameter), default_value, is_rest });
- if (match(TokenType::ParenClose) || is_rest)
- break;
- consume(TokenType::Comma);
- }
- if (parse_options & FunctionNodeParseOptions::IsSetterFunction && parameters.is_empty())
- syntax_error("Setter function must have one argument");
- return parameters;
- }
- static constexpr AK::Array<StringView, 36> s_reserved_words = { "break", "case", "catch", "class", "const", "continue", "debugger", "default", "delete", "do", "else", "enum", "export", "extends", "false", "finally", "for", "function", "if", "import", "in", "instanceof", "new", "null", "return", "super", "switch", "this", "throw", "true", "try", "typeof", "var", "void", "while", "with" };
- RefPtr<BindingPattern> Parser::parse_binding_pattern(Parser::AllowDuplicates allow_duplicates, Parser::AllowMemberExpressions allow_member_expressions)
- {
- auto rule_start = push_start();
- TokenType closing_token;
- bool is_object = true;
- if (match(TokenType::BracketOpen)) {
- consume();
- closing_token = TokenType::BracketClose;
- is_object = false;
- } else if (match(TokenType::CurlyOpen)) {
- consume();
- closing_token = TokenType::CurlyClose;
- } else {
- return {};
- }
- Vector<BindingPattern::BindingEntry> entries;
- while (!match(closing_token)) {
- if (!is_object && match(TokenType::Comma)) {
- consume();
- entries.append(BindingPattern::BindingEntry {});
- continue;
- }
- auto is_rest = false;
- if (match(TokenType::TripleDot)) {
- consume();
- is_rest = true;
- }
- decltype(BindingPattern::BindingEntry::name) name = Empty {};
- decltype(BindingPattern::BindingEntry::alias) alias = Empty {};
- RefPtr<Expression> initializer = {};
- if (is_object) {
- bool needs_alias = false;
- if (allow_member_expressions == AllowMemberExpressions::Yes && is_rest) {
- auto expression_position = position();
- auto expression = parse_expression(2, Associativity::Right, { TokenType::Equals });
- if (is<MemberExpression>(*expression))
- alias = static_ptr_cast<MemberExpression>(expression);
- else if (is<Identifier>(*expression))
- name = static_ptr_cast<Identifier>(expression);
- else
- syntax_error("Invalid destructuring assignment target", expression_position);
- } else if (match_identifier_name() || match(TokenType::StringLiteral) || match(TokenType::NumericLiteral)) {
- if (match(TokenType::StringLiteral) || match(TokenType::NumericLiteral))
- needs_alias = true;
- if (match(TokenType::StringLiteral)) {
- auto token = consume(TokenType::StringLiteral);
- auto string_literal = parse_string_literal(token);
- name = create_ast_node<Identifier>(
- { m_state.current_token.filename(), rule_start.position(), position() },
- string_literal->value());
- } else {
- name = create_ast_node<Identifier>(
- { m_state.current_token.filename(), rule_start.position(), position() },
- consume().value());
- }
- } else if (match(TokenType::BracketOpen)) {
- consume();
- auto expression = parse_expression(0);
- name = move(expression);
- consume(TokenType::BracketClose);
- } else {
- expected("identifier or computed property name");
- return {};
- }
- if (!is_rest && match(TokenType::Colon)) {
- consume();
- if (allow_member_expressions == AllowMemberExpressions::Yes) {
- auto expression_position = position();
- auto expression = parse_expression(2, Associativity::Right, { TokenType::Equals });
- if (is<ArrayExpression>(*expression) || is<ObjectExpression>(*expression)) {
- if (auto synthesized_binding_pattern = synthesize_binding_pattern(*expression))
- alias = synthesized_binding_pattern.release_nonnull();
- else
- syntax_error("Invalid destructuring assignment target", expression_position);
- } else if (is<MemberExpression>(*expression)) {
- alias = static_ptr_cast<MemberExpression>(expression);
- } else if (is<Identifier>(*expression)) {
- alias = static_ptr_cast<Identifier>(expression);
- } else {
- syntax_error("Invalid destructuring assignment target", expression_position);
- }
- } else if (match(TokenType::CurlyOpen) || match(TokenType::BracketOpen)) {
- auto binding_pattern = parse_binding_pattern(allow_duplicates, allow_member_expressions);
- if (!binding_pattern)
- return {};
- alias = binding_pattern.release_nonnull();
- } else if (match_identifier_name()) {
- alias = create_ast_node<Identifier>(
- { m_state.current_token.filename(), rule_start.position(), position() },
- consume().value());
- } else {
- expected("identifier or binding pattern");
- return {};
- }
- } else if (needs_alias) {
- expected("alias for string or numeric literal name");
- return {};
- }
- } else {
- if (allow_member_expressions == AllowMemberExpressions::Yes) {
- auto expression_position = position();
- auto expression = parse_expression(2, Associativity::Right, { TokenType::Equals });
- if (is<ArrayExpression>(*expression) || is<ObjectExpression>(*expression)) {
- if (auto synthesized_binding_pattern = synthesize_binding_pattern(*expression))
- alias = synthesized_binding_pattern.release_nonnull();
- else
- syntax_error("Invalid destructuring assignment target", expression_position);
- } else if (is<MemberExpression>(*expression)) {
- alias = static_ptr_cast<MemberExpression>(expression);
- } else if (is<Identifier>(*expression)) {
- alias = static_ptr_cast<Identifier>(expression);
- } else {
- syntax_error("Invalid destructuring assignment target", expression_position);
- }
- } else if (match(TokenType::BracketOpen) || match(TokenType::CurlyOpen)) {
- auto pattern = parse_binding_pattern(allow_duplicates, allow_member_expressions);
- if (!pattern) {
- expected("binding pattern");
- return {};
- }
- alias = pattern.release_nonnull();
- } else if (match_identifier_name()) {
- // BindingElement must always have an Empty name field
- auto identifier_name = consume_identifier().value();
- alias = create_ast_node<Identifier>(
- { m_state.current_token.filename(), rule_start.position(), position() },
- identifier_name);
- } else {
- expected("identifier or binding pattern");
- return {};
- }
- }
- if (match(TokenType::Equals)) {
- if (is_rest) {
- syntax_error("Unexpected initializer after rest element");
- return {};
- }
- consume();
- initializer = parse_expression(2);
- if (!initializer) {
- expected("initialization expression");
- return {};
- }
- }
- entries.append(BindingPattern::BindingEntry { move(name), move(alias), move(initializer), is_rest });
- if (match(TokenType::Comma)) {
- if (is_rest) {
- syntax_error("Rest element may not be followed by a comma");
- return {};
- }
- consume();
- }
- }
- while (!is_object && match(TokenType::Comma))
- consume();
- consume(closing_token);
- auto kind = is_object ? BindingPattern::Kind::Object : BindingPattern::Kind::Array;
- auto pattern = adopt_ref(*new BindingPattern);
- pattern->entries = move(entries);
- pattern->kind = kind;
- Vector<StringView> bound_names;
- pattern->for_each_bound_name([&](auto& name) {
- if (allow_duplicates == AllowDuplicates::No) {
- if (bound_names.contains_slow(name))
- syntax_error("Duplicate parameter names in bindings");
- bound_names.append(name);
- }
- check_identifier_name_for_assignment_validity(name);
- });
- return pattern;
- }
- NonnullRefPtr<VariableDeclaration> Parser::parse_variable_declaration(bool for_loop_variable_declaration)
- {
- auto rule_start = push_start();
- DeclarationKind declaration_kind;
- switch (m_state.current_token.type()) {
- case TokenType::Var:
- declaration_kind = DeclarationKind::Var;
- break;
- case TokenType::Let:
- declaration_kind = DeclarationKind::Let;
- break;
- case TokenType::Const:
- declaration_kind = DeclarationKind::Const;
- break;
- default:
- VERIFY_NOT_REACHED();
- }
- consume();
- NonnullRefPtrVector<VariableDeclarator> declarations;
- for (;;) {
- Variant<NonnullRefPtr<Identifier>, NonnullRefPtr<BindingPattern>, Empty> target {};
- if (match_identifier()) {
- auto identifier_start = push_start();
- auto name = consume_identifier().value();
- target = create_ast_node<Identifier>(
- { m_state.current_token.filename(), rule_start.position(), position() },
- name);
- check_identifier_name_for_assignment_validity(name);
- if ((declaration_kind == DeclarationKind::Let || declaration_kind == DeclarationKind::Const) && name == "let"sv)
- syntax_error("Lexical binding may not be called 'let'");
- } else if (auto pattern = parse_binding_pattern(declaration_kind != DeclarationKind::Var ? AllowDuplicates::No : AllowDuplicates::Yes, AllowMemberExpressions::No)) {
- target = pattern.release_nonnull();
- if ((declaration_kind == DeclarationKind::Let || declaration_kind == DeclarationKind::Const)) {
- target.get<NonnullRefPtr<BindingPattern>>()->for_each_bound_name([this](auto& name) {
- if (name == "let"sv)
- syntax_error("Lexical binding may not be called 'let'");
- });
- }
- } else if (!m_state.in_generator_function_context && match(TokenType::Yield)) {
- if (m_state.strict_mode)
- syntax_error("Identifier must not be a reserved word in strict mode ('yield')");
- target = create_ast_node<Identifier>(
- { m_state.current_token.filename(), rule_start.position(), position() },
- consume().value());
- }
- if (target.has<Empty>()) {
- expected("identifier or a binding pattern");
- if (match(TokenType::Comma)) {
- consume();
- continue;
- }
- break;
- }
- RefPtr<Expression> init;
- if (match(TokenType::Equals)) {
- consume();
- // In a for loop 'in' can be ambiguous so we do not allow it
- // 14.7.4 The for Statement, https://tc39.es/ecma262/#prod-ForStatement and 14.7.5 The for-in, for-of, and for-await-of Statements, https://tc39.es/ecma262/#prod-ForInOfStatement
- if (for_loop_variable_declaration)
- init = parse_expression(2, Associativity::Right, { TokenType::In });
- else
- init = parse_expression(2);
- } else if (!for_loop_variable_declaration && declaration_kind == DeclarationKind::Const) {
- syntax_error("Missing initializer in 'const' variable declaration");
- } else if (!for_loop_variable_declaration && target.has<NonnullRefPtr<BindingPattern>>()) {
- syntax_error("Missing initializer in destructuring assignment");
- }
- declarations.append(create_ast_node<VariableDeclarator>(
- { m_state.current_token.filename(), rule_start.position(), position() },
- move(target).downcast<NonnullRefPtr<Identifier>, NonnullRefPtr<BindingPattern>>(),
- move(init)));
- if (match(TokenType::Comma)) {
- consume();
- continue;
- }
- break;
- }
- if (!for_loop_variable_declaration)
- consume_or_insert_semicolon();
- auto declaration = create_ast_node<VariableDeclaration>({ m_state.current_token.filename(), rule_start.position(), position() }, declaration_kind, move(declarations));
- return declaration;
- }
- NonnullRefPtr<ThrowStatement> Parser::parse_throw_statement()
- {
- auto rule_start = push_start();
- consume(TokenType::Throw);
- // Automatic semicolon insertion: terminate statement when throw is followed by newline
- if (m_state.current_token.trivia_contains_line_terminator()) {
- syntax_error("No line break is allowed between 'throw' and its expression");
- return create_ast_node<ThrowStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, create_ast_node<ErrorExpression>({ m_state.current_token.filename(), rule_start.position(), position() }));
- }
- auto expression = parse_expression(0);
- consume_or_insert_semicolon();
- return create_ast_node<ThrowStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(expression));
- }
- NonnullRefPtr<BreakStatement> Parser::parse_break_statement()
- {
- auto rule_start = push_start();
- consume(TokenType::Break);
- FlyString target_label;
- if (match(TokenType::Semicolon)) {
- consume();
- } else {
- if (!m_state.current_token.trivia_contains_line_terminator() && match_identifier()) {
- target_label = consume().value();
- auto label = m_state.labels_in_scope.find(target_label);
- if (label == m_state.labels_in_scope.end())
- syntax_error(String::formatted("Label '{}' not found", target_label));
- }
- consume_or_insert_semicolon();
- }
- if (target_label.is_null() && !m_state.in_break_context)
- syntax_error("Unlabeled 'break' not allowed outside of a loop or switch statement");
- return create_ast_node<BreakStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, target_label);
- }
- NonnullRefPtr<ContinueStatement> Parser::parse_continue_statement()
- {
- auto rule_start = push_start();
- if (!m_state.in_continue_context)
- syntax_error("'continue' not allow outside of a loop");
- consume(TokenType::Continue);
- FlyString target_label;
- if (match(TokenType::Semicolon)) {
- consume();
- return create_ast_node<ContinueStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, target_label);
- }
- if (!m_state.current_token.trivia_contains_line_terminator() && match_identifier()) {
- auto label_position = position();
- target_label = consume().value();
- auto label = m_state.labels_in_scope.find(target_label);
- if (label == m_state.labels_in_scope.end())
- syntax_error(String::formatted("Label '{}' not found or invalid", target_label));
- else
- label->value = label_position;
- }
- consume_or_insert_semicolon();
- return create_ast_node<ContinueStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, target_label);
- }
- NonnullRefPtr<ConditionalExpression> Parser::parse_conditional_expression(NonnullRefPtr<Expression> test)
- {
- auto rule_start = push_start();
- consume(TokenType::QuestionMark);
- auto consequent = parse_expression(2);
- consume(TokenType::Colon);
- auto alternate = parse_expression(2);
- return create_ast_node<ConditionalExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, move(test), move(consequent), move(alternate));
- }
- NonnullRefPtr<OptionalChain> Parser::parse_optional_chain(NonnullRefPtr<Expression> base)
- {
- auto rule_start = push_start();
- Vector<OptionalChain::Reference> chain;
- do {
- if (match(TokenType::QuestionMarkPeriod)) {
- consume(TokenType::QuestionMarkPeriod);
- switch (m_state.current_token.type()) {
- case TokenType::ParenOpen:
- chain.append(OptionalChain::Call { parse_arguments(), OptionalChain::Mode::Optional });
- break;
- case TokenType::BracketOpen:
- consume();
- chain.append(OptionalChain::ComputedReference { parse_expression(0), OptionalChain::Mode::Optional });
- consume(TokenType::BracketClose);
- break;
- case TokenType::PrivateIdentifier: {
- if (!is_private_identifier_valid())
- syntax_error(String::formatted("Reference to undeclared private field or method '{}'", m_state.current_token.value()));
- auto start = position();
- auto private_identifier = consume();
- chain.append(OptionalChain::PrivateMemberReference {
- create_ast_node<PrivateIdentifier>({ m_state.current_token.filename(), start, position() }, private_identifier.value()),
- OptionalChain::Mode::Optional });
- break;
- }
- case TokenType::TemplateLiteralStart:
- // 13.3.1.1 - Static Semantics: Early Errors
- // OptionalChain :
- // ?. TemplateLiteral
- // OptionalChain TemplateLiteral
- // This is a hard error.
- syntax_error("Invalid tagged template literal after ?.", position());
- break;
- default:
- if (match_identifier_name()) {
- auto start = position();
- auto identifier = consume();
- chain.append(OptionalChain::MemberReference {
- create_ast_node<Identifier>({ m_state.current_token.filename(), start, position() }, identifier.value()),
- OptionalChain::Mode::Optional,
- });
- } else {
- syntax_error("Invalid optional chain reference after ?.", position());
- }
- break;
- }
- } else if (match(TokenType::ParenOpen)) {
- chain.append(OptionalChain::Call { parse_arguments(), OptionalChain::Mode::NotOptional });
- } else if (match(TokenType::Period)) {
- consume();
- if (match(TokenType::PrivateIdentifier)) {
- auto start = position();
- auto private_identifier = consume();
- chain.append(OptionalChain::PrivateMemberReference {
- create_ast_node<PrivateIdentifier>({ m_state.current_token.filename(), start, position() }, private_identifier.value()),
- OptionalChain::Mode::NotOptional,
- });
- } else if (match_identifier_name()) {
- auto start = position();
- auto identifier = consume();
- chain.append(OptionalChain::MemberReference {
- create_ast_node<Identifier>({ m_state.current_token.filename(), start, position() }, identifier.value()),
- OptionalChain::Mode::NotOptional,
- });
- } else {
- expected("an identifier");
- break;
- }
- } else if (match(TokenType::TemplateLiteralStart)) {
- // 13.3.1.1 - Static Semantics: Early Errors
- // OptionalChain :
- // ?. TemplateLiteral
- // OptionalChain TemplateLiteral
- syntax_error("Invalid tagged template literal after optional chain", position());
- break;
- } else if (match(TokenType::BracketOpen)) {
- consume();
- chain.append(OptionalChain::ComputedReference { parse_expression(2), OptionalChain::Mode::NotOptional });
- consume(TokenType::BracketClose);
- } else {
- break;
- }
- } while (!done());
- return create_ast_node<OptionalChain>(
- { m_state.current_token.filename(), rule_start.position(), position() },
- move(base),
- move(chain));
- }
- NonnullRefPtr<TryStatement> Parser::parse_try_statement()
- {
- auto rule_start = push_start();
- consume(TokenType::Try);
- auto block = parse_block_statement();
- RefPtr<CatchClause> handler;
- if (match(TokenType::Catch))
- handler = parse_catch_clause();
- RefPtr<BlockStatement> finalizer;
- if (match(TokenType::Finally)) {
- consume();
- finalizer = parse_block_statement();
- }
- if (!handler && !finalizer)
- syntax_error("try statement must have a 'catch' or 'finally' clause");
- return create_ast_node<TryStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(block), move(handler), move(finalizer));
- }
- NonnullRefPtr<DoWhileStatement> Parser::parse_do_while_statement()
- {
- auto rule_start = push_start();
- consume(TokenType::Do);
- auto body = [&]() -> NonnullRefPtr<Statement> {
- TemporaryChange break_change(m_state.in_break_context, true);
- TemporaryChange continue_change(m_state.in_continue_context, true);
- return parse_statement();
- }();
- consume(TokenType::While);
- consume(TokenType::ParenOpen);
- auto test = parse_expression(0);
- consume(TokenType::ParenClose);
- // Since ES 2015 a missing semicolon is inserted here, despite the regular ASI rules not applying
- if (match(TokenType::Semicolon))
- consume();
- return create_ast_node<DoWhileStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(test), move(body));
- }
- NonnullRefPtr<WhileStatement> Parser::parse_while_statement()
- {
- auto rule_start = push_start();
- consume(TokenType::While);
- consume(TokenType::ParenOpen);
- auto test = parse_expression(0);
- consume(TokenType::ParenClose);
- TemporaryChange break_change(m_state.in_break_context, true);
- TemporaryChange continue_change(m_state.in_continue_context, true);
- auto body = parse_statement();
- return create_ast_node<WhileStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(test), move(body));
- }
- NonnullRefPtr<SwitchStatement> Parser::parse_switch_statement()
- {
- auto rule_start = push_start();
- consume(TokenType::Switch);
- consume(TokenType::ParenOpen);
- auto determinant = parse_expression(0);
- consume(TokenType::ParenClose);
- consume(TokenType::CurlyOpen);
- NonnullRefPtrVector<SwitchCase> cases;
- auto switch_statement = create_ast_node<SwitchStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(determinant));
- ScopePusher switch_scope = ScopePusher::block_scope(*this, switch_statement);
- auto has_default = false;
- while (match(TokenType::Case) || match(TokenType::Default)) {
- if (match(TokenType::Default)) {
- if (has_default)
- syntax_error("Multiple 'default' clauses in switch statement");
- has_default = true;
- }
- switch_statement->add_case(parse_switch_case());
- }
- consume(TokenType::CurlyClose);
- return switch_statement;
- }
- NonnullRefPtr<WithStatement> Parser::parse_with_statement()
- {
- auto rule_start = push_start();
- consume(TokenType::With);
- consume(TokenType::ParenOpen);
- auto object = parse_expression(0);
- consume(TokenType::ParenClose);
- auto body = parse_statement();
- return create_ast_node<WithStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(object), move(body));
- }
- NonnullRefPtr<SwitchCase> Parser::parse_switch_case()
- {
- auto rule_start = push_start();
- RefPtr<Expression> test;
- if (consume().type() == TokenType::Case) {
- test = parse_expression(0);
- }
- consume(TokenType::Colon);
- NonnullRefPtrVector<Statement> consequent;
- TemporaryChange break_change(m_state.in_break_context, true);
- auto switch_case = create_ast_node<SwitchCase>({ m_state.current_token.filename(), rule_start.position(), position() }, move(test));
- parse_statement_list(switch_case);
- return switch_case;
- }
- NonnullRefPtr<CatchClause> Parser::parse_catch_clause()
- {
- auto rule_start = push_start();
- consume(TokenType::Catch);
- FlyString parameter;
- RefPtr<BindingPattern> pattern_parameter;
- auto should_expect_parameter = false;
- if (match(TokenType::ParenOpen)) {
- should_expect_parameter = true;
- consume();
- if (match_identifier_name() && (!match(TokenType::Yield) || !m_state.in_generator_function_context))
- parameter = consume().value();
- else
- pattern_parameter = parse_binding_pattern(AllowDuplicates::No, AllowMemberExpressions::No);
- consume(TokenType::ParenClose);
- }
- if (should_expect_parameter && parameter.is_empty() && !pattern_parameter)
- expected("an identifier or a binding pattern");
- HashTable<FlyString> bound_names;
- if (pattern_parameter) {
- pattern_parameter->for_each_bound_name(
- [&](auto& name) {
- check_identifier_name_for_assignment_validity(name);
- bound_names.set(name);
- });
- }
- if (!parameter.is_empty()) {
- check_identifier_name_for_assignment_validity(parameter);
- bound_names.set(parameter);
- }
- ScopePusher catch_scope = ScopePusher::catch_scope(*this, pattern_parameter, parameter);
- auto body = parse_block_statement();
- body->for_each_lexically_declared_name([&](auto const& name) {
- if (bound_names.contains(name))
- syntax_error(String::formatted("Identifier '{}' already declared as catch parameter", name));
- });
- if (pattern_parameter) {
- return create_ast_node<CatchClause>(
- { m_state.current_token.filename(), rule_start.position(), position() },
- pattern_parameter.release_nonnull(),
- move(body));
- }
- return create_ast_node<CatchClause>(
- { m_state.current_token.filename(), rule_start.position(), position() },
- move(parameter),
- move(body));
- }
- NonnullRefPtr<IfStatement> Parser::parse_if_statement()
- {
- auto rule_start = push_start();
- auto parse_function_declaration_as_block_statement = [&] {
- // https://tc39.es/ecma262/#sec-functiondeclarations-in-ifstatement-statement-clauses
- // Code matching this production is processed as if each matching occurrence of
- // FunctionDeclaration[?Yield, ?Await, ~Default] was the sole StatementListItem
- // of a BlockStatement occupying that position in the source code.
- VERIFY(match(TokenType::Function));
- auto block = create_ast_node<BlockStatement>({ m_state.current_token.filename(), rule_start.position(), position() });
- ScopePusher block_scope = ScopePusher::block_scope(*this, *block);
- auto declaration = parse_declaration();
- VERIFY(m_state.current_scope_pusher);
- block_scope.add_declaration(declaration);
- VERIFY(is<FunctionDeclaration>(*declaration));
- auto& function_declaration = static_cast<FunctionDeclaration const&>(*declaration);
- if (function_declaration.kind() == FunctionKind::Generator)
- syntax_error("Generator functions can only be declared in top-level or within a block");
- block->append(move(declaration));
- return block;
- };
- consume(TokenType::If);
- consume(TokenType::ParenOpen);
- auto predicate = parse_expression(0);
- consume(TokenType::ParenClose);
- RefPtr<Statement> consequent;
- if (!m_state.strict_mode && match(TokenType::Function))
- consequent = parse_function_declaration_as_block_statement();
- else
- consequent = parse_statement();
- RefPtr<Statement> alternate;
- if (match(TokenType::Else)) {
- consume();
- if (!m_state.strict_mode && match(TokenType::Function))
- alternate = parse_function_declaration_as_block_statement();
- else
- alternate = parse_statement();
- }
- return create_ast_node<IfStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(predicate), move(*consequent), move(alternate));
- }
- NonnullRefPtr<Statement> Parser::parse_for_statement()
- {
- auto rule_start = push_start();
- auto match_for_in_of = [&]() {
- return match(TokenType::In) || (match(TokenType::Identifier) && m_state.current_token.original_value() == "of");
- };
- consume(TokenType::For);
- consume(TokenType::ParenOpen);
- Optional<ScopePusher> scope_pusher;
- RefPtr<ASTNode> init;
- if (!match(TokenType::Semicolon)) {
- if (match_variable_declaration()) {
- auto declaration = parse_variable_declaration(true);
- if (declaration->declaration_kind() == DeclarationKind::Var) {
- m_state.current_scope_pusher->add_declaration(declaration);
- } else {
- // This does not follow the normal declaration structure so we need additional checks.
- HashTable<FlyString> bound_names;
- declaration->for_each_bound_name([&](auto const& name) {
- if (bound_names.set(name) != AK::HashSetResult::InsertedNewEntry)
- syntax_error(String::formatted("Identifier '{}' already declared in for loop initializer", name), declaration->source_range().start);
- });
- }
- init = move(declaration);
- if (match_for_in_of())
- return parse_for_in_of_statement(*init);
- if (static_cast<VariableDeclaration&>(*init).declaration_kind() == DeclarationKind::Const) {
- for (auto& variable : static_cast<VariableDeclaration&>(*init).declarations()) {
- if (!variable.init())
- syntax_error("Missing initializer in 'const' variable declaration");
- }
- }
- } else if (match_expression()) {
- init = parse_expression(0, Associativity::Right, { TokenType::In });
- if (match_for_in_of())
- return parse_for_in_of_statement(*init);
- } else {
- syntax_error("Unexpected token in for loop");
- }
- }
- consume(TokenType::Semicolon);
- RefPtr<Expression> test;
- if (!match(TokenType::Semicolon))
- test = parse_expression(0);
- consume(TokenType::Semicolon);
- RefPtr<Expression> update;
- if (!match(TokenType::ParenClose))
- update = parse_expression(0);
- consume(TokenType::ParenClose);
- TemporaryChange break_change(m_state.in_break_context, true);
- TemporaryChange continue_change(m_state.in_continue_context, true);
- ScopePusher for_loop_scope = ScopePusher::for_loop_scope(*this, init);
- auto body = parse_statement();
- return create_ast_node<ForStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(init), move(test), move(update), move(body));
- }
- NonnullRefPtr<Statement> Parser::parse_for_in_of_statement(NonnullRefPtr<ASTNode> lhs)
- {
- Variant<NonnullRefPtr<ASTNode>, NonnullRefPtr<BindingPattern>> for_declaration = lhs;
- auto rule_start = push_start();
- auto has_annexB_for_in_init_extension = false;
- if (is<VariableDeclaration>(*lhs)) {
- auto& declaration = static_cast<VariableDeclaration&>(*lhs);
- if (declaration.declarations().size() > 1) {
- syntax_error("Multiple declarations not allowed in for..in/of");
- } else if (declaration.declarations().size() < 1) {
- syntax_error("Need exactly one variable declaration in for..in/of");
- } else {
- // AnnexB extension B.3.5 Initializers in ForIn Statement Heads, https://tc39.es/ecma262/#sec-initializers-in-forin-statement-heads
- auto& variable = declaration.declarations().first();
- if (variable.init()) {
- if (m_state.strict_mode || declaration.declaration_kind() != DeclarationKind::Var || !variable.target().has<NonnullRefPtr<Identifier>>())
- syntax_error("Variable initializer not allowed in for..in/of");
- else
- has_annexB_for_in_init_extension = true;
- }
- }
- } else if (!lhs->is_identifier() && !is<MemberExpression>(*lhs)) {
- bool valid = false;
- if (is<ObjectExpression>(*lhs) || is<ArrayExpression>(*lhs)) {
- auto synthesized_binding_pattern = synthesize_binding_pattern(static_cast<Expression const&>(*lhs));
- if (synthesized_binding_pattern) {
- for_declaration = synthesized_binding_pattern.release_nonnull();
- valid = true;
- }
- }
- if (!valid)
- syntax_error(String::formatted("Invalid left-hand side in for-loop ('{}')", lhs->class_name()));
- }
- auto in_or_of = consume();
- auto is_in = in_or_of.type() == TokenType::In;
- if (!is_in) {
- if (is<MemberExpression>(*lhs)) {
- auto& member = static_cast<MemberExpression const&>(*lhs);
- if (member.object().is_identifier() && static_cast<Identifier const&>(member.object()).string() == "let"sv)
- syntax_error("For of statement may not start with let.");
- }
- if (has_annexB_for_in_init_extension)
- syntax_error("Variable initializer not allowed in for..of", rule_start.position());
- }
- auto rhs = parse_expression(is_in ? 0 : 2);
- consume(TokenType::ParenClose);
- TemporaryChange break_change(m_state.in_break_context, true);
- TemporaryChange continue_change(m_state.in_continue_context, true);
- ScopePusher for_loop_scope = ScopePusher::for_loop_scope(*this, lhs);
- auto body = parse_statement();
- if (is_in)
- return create_ast_node<ForInStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(for_declaration), move(rhs), move(body));
- return create_ast_node<ForOfStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(for_declaration), move(rhs), move(body));
- }
- NonnullRefPtr<DebuggerStatement> Parser::parse_debugger_statement()
- {
- auto rule_start = push_start();
- consume(TokenType::Debugger);
- consume_or_insert_semicolon();
- return create_ast_node<DebuggerStatement>({ m_state.current_token.filename(), rule_start.position(), position() });
- }
- bool Parser::match(TokenType type) const
- {
- return m_state.current_token.type() == type;
- }
- bool Parser::match_expression() const
- {
- auto type = m_state.current_token.type();
- return type == TokenType::BoolLiteral
- || type == TokenType::NumericLiteral
- || type == TokenType::BigIntLiteral
- || type == TokenType::StringLiteral
- || type == TokenType::TemplateLiteralStart
- || type == TokenType::NullLiteral
- || match_identifier()
- || (type == TokenType::PrivateIdentifier && next_token().type() == TokenType::In)
- || type == TokenType::Await
- || type == TokenType::New
- || type == TokenType::Class
- || type == TokenType::CurlyOpen
- || type == TokenType::BracketOpen
- || type == TokenType::ParenOpen
- || type == TokenType::Function
- || type == TokenType::This
- || type == TokenType::Super
- || type == TokenType::RegexLiteral
- || type == TokenType::Slash // Wrongly recognized regex by lexer
- || type == TokenType::SlashEquals // Wrongly recognized regex by lexer (/=a/ is a valid regex)
- || type == TokenType::Yield
- || match_unary_prefixed_expression();
- }
- bool Parser::match_unary_prefixed_expression() const
- {
- auto type = m_state.current_token.type();
- return type == TokenType::PlusPlus
- || type == TokenType::MinusMinus
- || type == TokenType::ExclamationMark
- || type == TokenType::Tilde
- || type == TokenType::Plus
- || type == TokenType::Minus
- || type == TokenType::Typeof
- || type == TokenType::Void
- || type == TokenType::Delete;
- }
- bool Parser::match_secondary_expression(const Vector<TokenType>& forbidden) const
- {
- auto type = m_state.current_token.type();
- if (forbidden.contains_slow(type))
- return false;
- return type == TokenType::Plus
- || type == TokenType::PlusEquals
- || type == TokenType::Minus
- || type == TokenType::MinusEquals
- || type == TokenType::Asterisk
- || type == TokenType::AsteriskEquals
- || type == TokenType::Slash
- || type == TokenType::SlashEquals
- || type == TokenType::Percent
- || type == TokenType::PercentEquals
- || type == TokenType::DoubleAsterisk
- || type == TokenType::DoubleAsteriskEquals
- || type == TokenType::Equals
- || type == TokenType::EqualsEqualsEquals
- || type == TokenType::ExclamationMarkEqualsEquals
- || type == TokenType::EqualsEquals
- || type == TokenType::ExclamationMarkEquals
- || type == TokenType::GreaterThan
- || type == TokenType::GreaterThanEquals
- || type == TokenType::LessThan
- || type == TokenType::LessThanEquals
- || type == TokenType::ParenOpen
- || type == TokenType::Period
- || type == TokenType::BracketOpen
- || (type == TokenType::PlusPlus && !m_state.current_token.trivia_contains_line_terminator())
- || (type == TokenType::MinusMinus && !m_state.current_token.trivia_contains_line_terminator())
- || type == TokenType::In
- || type == TokenType::Instanceof
- || type == TokenType::QuestionMark
- || type == TokenType::Ampersand
- || type == TokenType::AmpersandEquals
- || type == TokenType::Pipe
- || type == TokenType::PipeEquals
- || type == TokenType::Caret
- || type == TokenType::CaretEquals
- || type == TokenType::ShiftLeft
- || type == TokenType::ShiftLeftEquals
- || type == TokenType::ShiftRight
- || type == TokenType::ShiftRightEquals
- || type == TokenType::UnsignedShiftRight
- || type == TokenType::UnsignedShiftRightEquals
- || type == TokenType::DoubleAmpersand
- || type == TokenType::DoubleAmpersandEquals
- || type == TokenType::DoublePipe
- || type == TokenType::DoublePipeEquals
- || type == TokenType::DoubleQuestionMark
- || type == TokenType::DoubleQuestionMarkEquals
- || type == TokenType::QuestionMarkPeriod;
- }
- bool Parser::match_statement() const
- {
- auto type = m_state.current_token.type();
- return match_expression()
- || type == TokenType::Return
- || type == TokenType::Yield
- || type == TokenType::Do
- || type == TokenType::If
- || type == TokenType::Throw
- || type == TokenType::Try
- || type == TokenType::While
- || type == TokenType::With
- || type == TokenType::For
- || type == TokenType::CurlyOpen
- || type == TokenType::Switch
- || type == TokenType::Break
- || type == TokenType::Continue
- || type == TokenType::Var
- || type == TokenType::Debugger
- || type == TokenType::Semicolon;
- }
- bool Parser::match_export_or_import() const
- {
- auto type = m_state.current_token.type();
- return type == TokenType::Export
- || type == TokenType::Import;
- }
- bool Parser::match_declaration() const
- {
- auto type = m_state.current_token.type();
- if (type == TokenType::Let && !m_state.strict_mode) {
- return try_match_let_declaration();
- }
- return type == TokenType::Function
- || type == TokenType::Class
- || type == TokenType::Const
- || type == TokenType::Let;
- }
- Token Parser::next_token() const
- {
- Lexer lookahead_lexer = m_state.lexer;
- auto token_after = lookahead_lexer.next();
- return token_after;
- }
- bool Parser::try_match_let_declaration() const
- {
- VERIFY(m_state.current_token.type() == TokenType::Let);
- auto token_after = next_token();
- if (token_after.is_identifier_name() && token_after.value() != "in"sv)
- return true;
- if (token_after.type() == TokenType::CurlyOpen || token_after.type() == TokenType::BracketOpen)
- return true;
- return false;
- }
- bool Parser::match_variable_declaration() const
- {
- auto type = m_state.current_token.type();
- if (type == TokenType::Let && !m_state.strict_mode) {
- return try_match_let_declaration();
- }
- return type == TokenType::Var
- || type == TokenType::Let
- || type == TokenType::Const;
- }
- bool Parser::match_identifier() const
- {
- if (m_state.current_token.type() == TokenType::EscapedKeyword) {
- if (m_state.current_token.value() == "let"sv)
- return !m_state.strict_mode;
- if (m_state.current_token.value() == "yield"sv)
- return !m_state.strict_mode && !m_state.in_generator_function_context;
- if (m_state.current_token.value() == "await"sv)
- return m_program_type != Program::Type::Module;
- return true;
- }
- return m_state.current_token.type() == TokenType::Identifier
- || (m_state.current_token.type() == TokenType::Let && !m_state.strict_mode)
- || (m_state.current_token.type() == TokenType::Await && m_program_type != Program::Type::Module)
- || (m_state.current_token.type() == TokenType::Yield && !m_state.in_generator_function_context && !m_state.strict_mode); // See note in Parser::parse_identifier().
- }
- bool Parser::match_identifier_name() const
- {
- return m_state.current_token.is_identifier_name();
- }
- bool Parser::match_property_key() const
- {
- auto type = m_state.current_token.type();
- return match_identifier_name()
- || type == TokenType::BracketOpen
- || type == TokenType::StringLiteral
- || type == TokenType::NumericLiteral
- || type == TokenType::BigIntLiteral;
- }
- bool Parser::done() const
- {
- return match(TokenType::Eof);
- }
- Token Parser::consume()
- {
- auto old_token = m_state.current_token;
- m_state.current_token = m_state.lexer.next();
- // NOTE: This is the bare minimum needed to decide whether we might need an arguments object
- // in a function expression or declaration. ("might" because the AST implements some further
- // conditions from the spec that rule out the need for allocating one)
- if (old_token.type() == TokenType::Identifier && old_token.value().is_one_of("arguments"sv, "eval"sv))
- m_state.function_might_need_arguments_object = true;
- return old_token;
- }
- void Parser::consume_or_insert_semicolon()
- {
- // Semicolon was found and will be consumed
- if (match(TokenType::Semicolon)) {
- consume();
- return;
- }
- // Insert semicolon if...
- // ...token is preceded by one or more newlines
- if (m_state.current_token.trivia_contains_line_terminator())
- return;
- // ...token is a closing curly brace
- if (match(TokenType::CurlyClose))
- return;
- // ...token is eof
- if (match(TokenType::Eof))
- return;
- // No rule for semicolon insertion applies -> syntax error
- expected("Semicolon");
- }
- Token Parser::consume_identifier()
- {
- if (match(TokenType::Identifier))
- return consume(TokenType::Identifier);
- if (match(TokenType::EscapedKeyword))
- return consume(TokenType::EscapedKeyword);
- // Note that 'let' is not a reserved keyword, but our lexer considers it such
- // As it's pretty nice to have that (for syntax highlighting and such), we'll
- // special-case it here instead.
- if (match(TokenType::Let)) {
- if (m_state.strict_mode)
- syntax_error("'let' is not allowed as an identifier in strict mode");
- return consume();
- }
- if (match(TokenType::Yield)) {
- if (m_state.strict_mode || m_state.in_generator_function_context)
- syntax_error("Identifier must not be a reserved word in strict mode ('yield')");
- return consume();
- }
- expected("Identifier");
- return consume();
- }
- // https://tc39.es/ecma262/#prod-IdentifierReference
- Token Parser::consume_identifier_reference()
- {
- if (match(TokenType::Identifier))
- return consume(TokenType::Identifier);
- if (match(TokenType::EscapedKeyword)) {
- auto name = m_state.current_token.value();
- if (m_state.strict_mode && (name == "let"sv || name == "yield"sv))
- syntax_error(String::formatted("'{}' is not allowed as an identifier in strict mode", name));
- if (m_program_type == Program::Type::Module && name == "await"sv)
- syntax_error("'await' is not allowed as an identifier in module");
- return consume();
- }
- // See note in Parser::parse_identifier().
- if (match(TokenType::Let)) {
- if (m_state.strict_mode)
- syntax_error("'let' is not allowed as an identifier in strict mode");
- return consume();
- }
- if (match(TokenType::Yield)) {
- if (m_state.strict_mode)
- syntax_error("Identifier reference may not be 'yield' in strict mode");
- return consume();
- }
- if (match(TokenType::Await)) {
- if (m_program_type == Program::Type::Module)
- syntax_error("'await' is not allowed as an identifier in module");
- return consume();
- }
- expected(Token::name(TokenType::Identifier));
- return consume();
- }
- Token Parser::consume(TokenType expected_type)
- {
- if (!match(expected_type)) {
- expected(Token::name(expected_type));
- }
- auto token = consume();
- if (expected_type == TokenType::Identifier) {
- if (m_state.strict_mode && is_strict_reserved_word(token.value()))
- syntax_error(String::formatted("Identifier must not be a reserved word in strict mode ('{}')", token.value()));
- }
- return token;
- }
- Token Parser::consume_and_validate_numeric_literal()
- {
- auto is_unprefixed_octal_number = [](const StringView& value) {
- return value.length() > 1 && value[0] == '0' && is_ascii_digit(value[1]);
- };
- auto literal_start = position();
- auto token = consume(TokenType::NumericLiteral);
- if (m_state.strict_mode && is_unprefixed_octal_number(token.value()))
- syntax_error("Unprefixed octal number not allowed in strict mode", literal_start);
- if (match_identifier_name() && m_state.current_token.trivia().is_empty())
- syntax_error("Numeric literal must not be immediately followed by identifier");
- return token;
- }
- void Parser::expected(const char* what)
- {
- auto message = m_state.current_token.message();
- if (message.is_empty())
- message = String::formatted("Unexpected token {}. Expected {}", m_state.current_token.name(), what);
- syntax_error(message);
- }
- Position Parser::position() const
- {
- return {
- m_state.current_token.line_number(),
- m_state.current_token.line_column(),
- m_state.current_token.offset(),
- };
- }
- bool Parser::try_parse_arrow_function_expression_failed_at_position(const Position& position) const
- {
- auto it = m_token_memoizations.find(position);
- if (it == m_token_memoizations.end())
- return false;
- return (*it).value.try_parse_arrow_function_expression_failed;
- }
- void Parser::set_try_parse_arrow_function_expression_failed_at_position(const Position& position, bool failed)
- {
- m_token_memoizations.set(position, { failed });
- }
- void Parser::syntax_error(const String& message, Optional<Position> position)
- {
- if (!position.has_value())
- position = this->position();
- m_state.errors.append({ message, position });
- }
- void Parser::save_state()
- {
- m_saved_state.append(m_state);
- }
- void Parser::load_state()
- {
- VERIFY(!m_saved_state.is_empty());
- m_state = m_saved_state.take_last();
- }
- void Parser::discard_saved_state()
- {
- m_saved_state.take_last();
- }
- void Parser::check_identifier_name_for_assignment_validity(StringView name, bool force_strict)
- {
- // FIXME: this is now called from multiple places maybe the error message should be dynamic?
- if (any_of(s_reserved_words, [&](auto& value) { return name == value; })) {
- syntax_error("Binding pattern target may not be a reserved word");
- } else if (m_state.strict_mode || force_strict) {
- if (name.is_one_of("arguments"sv, "eval"sv))
- syntax_error("Binding pattern target may not be called 'arguments' or 'eval' in strict mode");
- else if (is_strict_reserved_word(name))
- syntax_error(String::formatted("Binding pattern target may not be called '{}' in strict mode", name));
- }
- }
- NonnullRefPtr<ImportStatement> Parser::parse_import_statement(Program& program)
- {
- auto rule_start = push_start();
- if (program.type() != Program::Type::Module)
- syntax_error("Cannot use import statement outside a module");
- consume(TokenType::Import);
- if (match(TokenType::StringLiteral)) {
- auto module_name = consume(TokenType::StringLiteral).value();
- return create_ast_node<ImportStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, module_name);
- }
- auto match_imported_binding = [&] {
- return match_identifier() || match(TokenType::Yield) || match(TokenType::Await);
- };
- auto match_as = [&] {
- return match(TokenType::Identifier) && m_state.current_token.original_value() == "as"sv;
- };
- bool continue_parsing = true;
- struct ImportWithLocation {
- ImportStatement::ImportEntry entry;
- Position position;
- };
- Vector<ImportWithLocation> entries_with_location;
- if (match_imported_binding()) {
- auto id_position = position();
- auto bound_name = consume().value();
- entries_with_location.append({ { "default", bound_name }, id_position });
- if (match(TokenType::Comma)) {
- consume(TokenType::Comma);
- } else {
- continue_parsing = false;
- }
- }
- if (!continue_parsing) {
- // skip the rest
- } else if (match(TokenType::Asterisk)) {
- consume(TokenType::Asterisk);
- if (!match_as())
- syntax_error(String::formatted("Unexpected token: {}", m_state.current_token.name()));
- consume(TokenType::Identifier);
- if (match_imported_binding()) {
- auto namespace_position = position();
- auto namespace_name = consume().value();
- entries_with_location.append({ { "*", namespace_name }, namespace_position });
- } else {
- syntax_error(String::formatted("Unexpected token: {}", m_state.current_token.name()));
- }
- } else if (match(TokenType::CurlyOpen)) {
- consume(TokenType::CurlyOpen);
- while (!done() && !match(TokenType::CurlyClose)) {
- if (match_identifier_name()) {
- auto require_as = !match_imported_binding();
- auto name_position = position();
- auto name = consume().value();
- if (match_as()) {
- consume(TokenType::Identifier);
- auto alias_position = position();
- auto alias = consume_identifier().value();
- check_identifier_name_for_assignment_validity(alias);
- entries_with_location.append({ { name, alias }, alias_position });
- } else if (require_as) {
- syntax_error(String::formatted("Unexpected reserved word '{}'", name));
- } else {
- check_identifier_name_for_assignment_validity(name);
- entries_with_location.append({ { name, name }, name_position });
- }
- } else {
- expected("identifier");
- break;
- }
- if (!match(TokenType::Comma))
- break;
- consume(TokenType::Comma);
- }
- consume(TokenType::CurlyClose);
- } else {
- expected("import clauses");
- }
- auto from_statement = consume(TokenType::Identifier).original_value();
- if (from_statement != "from"sv)
- syntax_error(String::formatted("Expected 'from' got {}", from_statement));
- auto module_name = consume(TokenType::StringLiteral).value();
- Vector<ImportStatement::ImportEntry> entries;
- entries.ensure_capacity(entries_with_location.size());
- for (auto& entry : entries_with_location) {
- for (auto& import_statement : program.imports()) {
- if (import_statement.has_bound_name(entry.entry.local_name))
- syntax_error(String::formatted("Identifier '{}' already declared", entry.entry.local_name), entry.position);
- }
- for (auto& new_entry : entries) {
- if (new_entry.local_name == entry.entry.local_name)
- syntax_error(String::formatted("Identifier '{}' already declared", entry.entry.local_name), entry.position);
- }
- entries.append(move(entry.entry));
- }
- return create_ast_node<ImportStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, module_name, move(entries));
- }
- NonnullRefPtr<ExportStatement> Parser::parse_export_statement(Program& program)
- {
- auto rule_start = push_start();
- if (program.type() != Program::Type::Module)
- syntax_error("Cannot use export statement outside a module");
- auto match_as = [&] {
- return match(TokenType::Identifier) && m_state.current_token.original_value() == "as"sv;
- };
- auto match_from = [&] {
- return match(TokenType::Identifier) && m_state.current_token.original_value() == "from"sv;
- };
- auto match_default = [&] {
- return match(TokenType::Default) && m_state.current_token.original_value() == "default"sv;
- };
- consume(TokenType::Export);
- struct EntryAndLocation {
- ExportStatement::ExportEntry entry;
- Position position;
- void to_module_request(String from_module)
- {
- entry.kind = ExportStatement::ExportEntry::Kind::ModuleRequest;
- entry.module_request = from_module;
- }
- };
- Vector<EntryAndLocation> entries_with_location;
- RefPtr<ASTNode> expression = {};
- if (match_default()) {
- auto default_position = position();
- consume(TokenType::Default);
- String local_name;
- if (match(TokenType::Class)) {
- auto class_expression = parse_class_expression(false);
- local_name = class_expression->name();
- expression = move(class_expression);
- } else if (match(TokenType::Function)) {
- auto func_expr = parse_function_node<FunctionExpression>();
- local_name = func_expr->name();
- expression = move(func_expr);
- // TODO: Allow async function
- } else if (match_expression()) {
- expression = parse_expression(2);
- consume_or_insert_semicolon();
- local_name = "*default*";
- } else {
- expected("Declaration or assignment expression");
- }
- entries_with_location.append({ { "default", local_name }, default_position });
- } else {
- enum FromSpecifier {
- NotAllowed,
- Optional,
- Required
- } check_for_from { NotAllowed };
- if (match(TokenType::Asterisk)) {
- auto asterisk_position = position();
- consume(TokenType::Asterisk);
- if (match_as()) {
- consume(TokenType::Identifier);
- if (match_identifier_name()) {
- auto namespace_position = position();
- auto exported_name = consume().value();
- entries_with_location.append({ { exported_name, "*" }, namespace_position });
- } else {
- expected("identifier");
- }
- } else {
- entries_with_location.append({ { {}, "*" }, asterisk_position });
- }
- check_for_from = Required;
- } else if (match_declaration()) {
- auto decl_position = position();
- auto declaration = parse_declaration();
- if (is<FunctionDeclaration>(*declaration)) {
- auto& func = static_cast<FunctionDeclaration&>(*declaration);
- entries_with_location.append({ { func.name(), func.name() }, func.source_range().start });
- } else if (is<ClassDeclaration>(*declaration)) {
- auto& class_declaration = static_cast<ClassDeclaration&>(*declaration);
- entries_with_location.append({ { class_declaration.class_name(), class_declaration.class_name() }, class_declaration.source_range().start });
- } else {
- VERIFY(is<VariableDeclaration>(*declaration));
- auto& variables = static_cast<VariableDeclaration&>(*declaration);
- for (auto& decl : variables.declarations()) {
- decl.target().visit(
- [&](NonnullRefPtr<Identifier> const& identifier) {
- entries_with_location.append({ { identifier->string(), identifier->string() }, identifier->source_range().start });
- },
- [&](NonnullRefPtr<BindingPattern> const& binding) {
- binding->for_each_bound_name([&](auto& name) {
- entries_with_location.append({ { name, name }, decl_position });
- });
- });
- }
- }
- expression = declaration;
- } else if (match(TokenType::Var)) {
- auto variable_position = position();
- auto variable_declaration = parse_variable_declaration();
- for (auto& decl : variable_declaration->declarations()) {
- decl.target().visit(
- [&](NonnullRefPtr<Identifier> const& identifier) {
- entries_with_location.append({ { identifier->string(), identifier->string() }, identifier->source_range().start });
- },
- [&](NonnullRefPtr<BindingPattern> const& binding) {
- binding->for_each_bound_name([&](auto& name) {
- entries_with_location.append({ { name, name }, variable_position });
- });
- });
- }
- expression = variable_declaration;
- } else if (match(TokenType::CurlyOpen)) {
- consume(TokenType::CurlyOpen);
- while (!done() && !match(TokenType::CurlyClose)) {
- if (match_identifier_name()) {
- auto identifier_position = position();
- auto identifier = consume().value();
- if (match_as()) {
- consume(TokenType::Identifier);
- if (match_identifier_name()) {
- auto export_name = consume().value();
- entries_with_location.append({ { export_name, identifier }, identifier_position });
- } else {
- expected("identifier name");
- }
- } else {
- entries_with_location.append({ { identifier, identifier }, identifier_position });
- }
- } else {
- expected("identifier");
- break;
- }
- if (!match(TokenType::Comma))
- break;
- consume(TokenType::Comma);
- }
- consume(TokenType::CurlyClose);
- check_for_from = Optional;
- } else {
- syntax_error("Unexpected token 'export'", rule_start.position());
- }
- if (check_for_from != NotAllowed && match_from()) {
- consume(TokenType::Identifier);
- if (match(TokenType::StringLiteral)) {
- auto from_specifier = consume().value();
- for (auto& entry : entries_with_location)
- entry.to_module_request(from_specifier);
- } else {
- expected("ModuleSpecifier");
- }
- } else if (check_for_from == Required) {
- expected("from");
- }
- if (check_for_from != NotAllowed)
- consume_or_insert_semicolon();
- }
- Vector<ExportStatement::ExportEntry> entries;
- entries.ensure_capacity(entries_with_location.size());
- for (auto& entry : entries_with_location) {
- for (auto& export_statement : program.exports()) {
- if (export_statement.has_export(entry.entry.export_name))
- syntax_error(String::formatted("Duplicate export with name: '{}'", entry.entry.export_name), entry.position);
- }
- for (auto& new_entry : entries) {
- if (new_entry.export_name == entry.entry.export_name)
- syntax_error(String::formatted("Duplicate export with name: '{}'", entry.entry.export_name), entry.position);
- }
- entries.append(move(entry.entry));
- }
- return create_ast_node<ExportStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(expression), move(entries));
- }
- }
|