Parser.cpp 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610
  1. /*
  2. * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "Parser.h"
  8. #include <AK/Array.h>
  9. #include <AK/CharacterTypes.h>
  10. #include <AK/HashTable.h>
  11. #include <AK/ScopeGuard.h>
  12. #include <AK/StdLibExtras.h>
  13. #include <AK/TemporaryChange.h>
  14. namespace JS {
  15. static bool statement_is_use_strict_directive(NonnullRefPtr<Statement> statement)
  16. {
  17. if (!is<ExpressionStatement>(*statement))
  18. return false;
  19. auto& expression_statement = static_cast<ExpressionStatement&>(*statement);
  20. auto& expression = expression_statement.expression();
  21. if (!is<StringLiteral>(expression))
  22. return false;
  23. return static_cast<const StringLiteral&>(expression).is_use_strict_directive();
  24. }
  25. class ScopePusher {
  26. public:
  27. enum Type {
  28. Var = 1,
  29. Let = 2,
  30. };
  31. ScopePusher(Parser& parser, unsigned mask, Parser::Scope::Type scope_type)
  32. : m_parser(parser)
  33. , m_mask(mask)
  34. {
  35. if (m_mask & Var)
  36. m_parser.m_state.var_scopes.append(NonnullRefPtrVector<VariableDeclaration>());
  37. if (m_mask & Let)
  38. m_parser.m_state.let_scopes.append(NonnullRefPtrVector<VariableDeclaration>());
  39. m_parser.m_state.current_scope = create<Parser::Scope>(scope_type, m_parser.m_state.current_scope);
  40. }
  41. ~ScopePusher()
  42. {
  43. if (m_mask & Var)
  44. m_parser.m_state.var_scopes.take_last();
  45. if (m_mask & Let)
  46. m_parser.m_state.let_scopes.take_last();
  47. auto& popped = m_parser.m_state.current_scope;
  48. // Manual clear required to resolve circular references
  49. popped->hoisted_function_declarations.clear();
  50. m_parser.m_state.current_scope = popped->parent;
  51. }
  52. void add_to_scope_node(NonnullRefPtr<ScopeNode> scope_node)
  53. {
  54. if (m_mask & Var)
  55. scope_node->add_variables(m_parser.m_state.var_scopes.last());
  56. if (m_mask & Let)
  57. scope_node->add_variables(m_parser.m_state.let_scopes.last());
  58. auto& scope = m_parser.m_state.current_scope;
  59. scope_node->add_functions(scope->function_declarations);
  60. for (auto& hoistable_function : scope->hoisted_function_declarations) {
  61. if (is_hoistable(hoistable_function)) {
  62. scope_node->add_hoisted_function(hoistable_function.declaration);
  63. }
  64. }
  65. }
  66. static bool is_hoistable(Parser::Scope::HoistableDeclaration& declaration)
  67. {
  68. auto& name = declaration.declaration->name();
  69. // See if we find any conflicting lexical declaration on the way up
  70. for (RefPtr<Parser::Scope> scope = declaration.scope; !scope.is_null(); scope = scope->parent) {
  71. if (scope->lexical_declarations.contains(name)) {
  72. return false;
  73. }
  74. }
  75. return true;
  76. }
  77. Parser& m_parser;
  78. unsigned m_mask { 0 };
  79. };
  80. class OperatorPrecedenceTable {
  81. public:
  82. constexpr OperatorPrecedenceTable()
  83. : m_token_precedence()
  84. {
  85. for (size_t i = 0; i < array_size(m_operator_precedence); ++i) {
  86. auto& op = m_operator_precedence[i];
  87. m_token_precedence[static_cast<size_t>(op.token)] = op.precedence;
  88. }
  89. }
  90. constexpr int get(TokenType token) const
  91. {
  92. int p = m_token_precedence[static_cast<size_t>(token)];
  93. if (p == 0) {
  94. warnln("Internal Error: No precedence for operator {}", Token::name(token));
  95. VERIFY_NOT_REACHED();
  96. return -1;
  97. }
  98. return p;
  99. }
  100. private:
  101. int m_token_precedence[cs_num_of_js_tokens];
  102. struct OperatorPrecedence {
  103. TokenType token;
  104. int precedence;
  105. };
  106. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
  107. static constexpr const OperatorPrecedence m_operator_precedence[] = {
  108. { TokenType::Period, 20 },
  109. { TokenType::BracketOpen, 20 },
  110. { TokenType::ParenOpen, 20 },
  111. { TokenType::QuestionMarkPeriod, 20 },
  112. { TokenType::New, 19 },
  113. { TokenType::PlusPlus, 18 },
  114. { TokenType::MinusMinus, 18 },
  115. { TokenType::ExclamationMark, 17 },
  116. { TokenType::Tilde, 17 },
  117. { TokenType::Typeof, 17 },
  118. { TokenType::Void, 17 },
  119. { TokenType::Delete, 17 },
  120. { TokenType::Await, 17 },
  121. { TokenType::DoubleAsterisk, 16 },
  122. { TokenType::Asterisk, 15 },
  123. { TokenType::Slash, 15 },
  124. { TokenType::Percent, 15 },
  125. { TokenType::Plus, 14 },
  126. { TokenType::Minus, 14 },
  127. { TokenType::ShiftLeft, 13 },
  128. { TokenType::ShiftRight, 13 },
  129. { TokenType::UnsignedShiftRight, 13 },
  130. { TokenType::LessThan, 12 },
  131. { TokenType::LessThanEquals, 12 },
  132. { TokenType::GreaterThan, 12 },
  133. { TokenType::GreaterThanEquals, 12 },
  134. { TokenType::In, 12 },
  135. { TokenType::Instanceof, 12 },
  136. { TokenType::EqualsEquals, 11 },
  137. { TokenType::ExclamationMarkEquals, 11 },
  138. { TokenType::EqualsEqualsEquals, 11 },
  139. { TokenType::ExclamationMarkEqualsEquals, 11 },
  140. { TokenType::Ampersand, 10 },
  141. { TokenType::Caret, 9 },
  142. { TokenType::Pipe, 8 },
  143. { TokenType::DoubleQuestionMark, 7 },
  144. { TokenType::DoubleAmpersand, 6 },
  145. { TokenType::DoublePipe, 5 },
  146. { TokenType::QuestionMark, 4 },
  147. { TokenType::Equals, 3 },
  148. { TokenType::PlusEquals, 3 },
  149. { TokenType::MinusEquals, 3 },
  150. { TokenType::DoubleAsteriskEquals, 3 },
  151. { TokenType::AsteriskEquals, 3 },
  152. { TokenType::SlashEquals, 3 },
  153. { TokenType::PercentEquals, 3 },
  154. { TokenType::ShiftLeftEquals, 3 },
  155. { TokenType::ShiftRightEquals, 3 },
  156. { TokenType::UnsignedShiftRightEquals, 3 },
  157. { TokenType::AmpersandEquals, 3 },
  158. { TokenType::CaretEquals, 3 },
  159. { TokenType::PipeEquals, 3 },
  160. { TokenType::DoubleAmpersandEquals, 3 },
  161. { TokenType::DoublePipeEquals, 3 },
  162. { TokenType::DoubleQuestionMarkEquals, 3 },
  163. { TokenType::Yield, 2 },
  164. { TokenType::Comma, 1 },
  165. };
  166. };
  167. constexpr OperatorPrecedenceTable g_operator_precedence;
  168. Parser::ParserState::ParserState(Lexer l)
  169. : lexer(move(l))
  170. , current_token(lexer.next())
  171. {
  172. }
  173. Parser::Scope::Scope(Parser::Scope::Type type, RefPtr<Parser::Scope> parent_scope)
  174. : type(type)
  175. , parent(move(parent_scope))
  176. {
  177. }
  178. RefPtr<Parser::Scope> Parser::Scope::get_current_function_scope()
  179. {
  180. if (this->type == Parser::Scope::Function) {
  181. return *this;
  182. }
  183. auto result = this->parent;
  184. while (result->type != Parser::Scope::Function) {
  185. result = result->parent;
  186. }
  187. return result;
  188. }
  189. Parser::Parser(Lexer lexer)
  190. : m_state(move(lexer))
  191. {
  192. }
  193. Associativity Parser::operator_associativity(TokenType type) const
  194. {
  195. switch (type) {
  196. case TokenType::Period:
  197. case TokenType::BracketOpen:
  198. case TokenType::ParenOpen:
  199. case TokenType::QuestionMarkPeriod:
  200. case TokenType::Asterisk:
  201. case TokenType::Slash:
  202. case TokenType::Percent:
  203. case TokenType::Plus:
  204. case TokenType::Minus:
  205. case TokenType::ShiftLeft:
  206. case TokenType::ShiftRight:
  207. case TokenType::UnsignedShiftRight:
  208. case TokenType::LessThan:
  209. case TokenType::LessThanEquals:
  210. case TokenType::GreaterThan:
  211. case TokenType::GreaterThanEquals:
  212. case TokenType::In:
  213. case TokenType::Instanceof:
  214. case TokenType::EqualsEquals:
  215. case TokenType::ExclamationMarkEquals:
  216. case TokenType::EqualsEqualsEquals:
  217. case TokenType::ExclamationMarkEqualsEquals:
  218. case TokenType::Typeof:
  219. case TokenType::Void:
  220. case TokenType::Delete:
  221. case TokenType::Ampersand:
  222. case TokenType::Caret:
  223. case TokenType::Pipe:
  224. case TokenType::DoubleQuestionMark:
  225. case TokenType::DoubleAmpersand:
  226. case TokenType::DoublePipe:
  227. case TokenType::Comma:
  228. return Associativity::Left;
  229. default:
  230. return Associativity::Right;
  231. }
  232. }
  233. NonnullRefPtr<Program> Parser::parse_program(bool starts_in_strict_mode)
  234. {
  235. auto rule_start = push_start();
  236. ScopePusher scope(*this, ScopePusher::Var | ScopePusher::Let, Scope::Function);
  237. auto program = adopt_ref(*new Program({ m_filename, rule_start.position(), position() }));
  238. if (starts_in_strict_mode) {
  239. program->set_strict_mode();
  240. m_state.strict_mode = true;
  241. }
  242. bool first = true;
  243. while (!done()) {
  244. if (match_declaration()) {
  245. program->append(parse_declaration());
  246. } else if (match_statement()) {
  247. auto statement = parse_statement();
  248. program->append(statement);
  249. if (statement_is_use_strict_directive(statement)) {
  250. if (first) {
  251. program->set_strict_mode();
  252. m_state.strict_mode = true;
  253. }
  254. if (m_state.string_legacy_octal_escape_sequence_in_scope)
  255. syntax_error("Octal escape sequence in string literal not allowed in strict mode");
  256. }
  257. } else {
  258. expected("statement or declaration");
  259. consume();
  260. }
  261. first = false;
  262. }
  263. if (m_state.var_scopes.size() == 1) {
  264. scope.add_to_scope_node(program);
  265. } else {
  266. syntax_error("Unclosed lexical_environment");
  267. }
  268. program->source_range().end = position();
  269. return program;
  270. }
  271. NonnullRefPtr<Declaration> Parser::parse_declaration()
  272. {
  273. auto rule_start = push_start();
  274. switch (m_state.current_token.type()) {
  275. case TokenType::Class:
  276. return parse_class_declaration();
  277. case TokenType::Function: {
  278. auto declaration = parse_function_node<FunctionDeclaration>();
  279. m_state.current_scope->function_declarations.append(declaration);
  280. auto hoisting_target = m_state.current_scope->get_current_function_scope();
  281. hoisting_target->hoisted_function_declarations.append({ declaration, *m_state.current_scope });
  282. return declaration;
  283. }
  284. case TokenType::Let:
  285. case TokenType::Const:
  286. return parse_variable_declaration();
  287. default:
  288. expected("declaration");
  289. consume();
  290. return create_ast_node<ErrorDeclaration>({ m_state.current_token.filename(), rule_start.position(), position() });
  291. }
  292. }
  293. NonnullRefPtr<Statement> Parser::parse_statement()
  294. {
  295. auto rule_start = push_start();
  296. switch (m_state.current_token.type()) {
  297. case TokenType::CurlyOpen:
  298. return parse_block_statement();
  299. case TokenType::Return:
  300. return parse_return_statement();
  301. case TokenType::Var:
  302. return parse_variable_declaration();
  303. case TokenType::For:
  304. return parse_for_statement();
  305. case TokenType::If:
  306. return parse_if_statement();
  307. case TokenType::Throw:
  308. return parse_throw_statement();
  309. case TokenType::Try:
  310. return parse_try_statement();
  311. case TokenType::Break:
  312. return parse_break_statement();
  313. case TokenType::Continue:
  314. return parse_continue_statement();
  315. case TokenType::Switch:
  316. return parse_switch_statement();
  317. case TokenType::Do:
  318. return parse_do_while_statement();
  319. case TokenType::While:
  320. return parse_while_statement();
  321. case TokenType::With:
  322. if (m_state.strict_mode)
  323. syntax_error("'with' statement not allowed in strict mode");
  324. return parse_with_statement();
  325. case TokenType::Debugger:
  326. return parse_debugger_statement();
  327. case TokenType::Semicolon:
  328. consume();
  329. return create_ast_node<EmptyStatement>({ m_state.current_token.filename(), rule_start.position(), position() });
  330. default:
  331. if (match(TokenType::Identifier)) {
  332. auto result = try_parse_labelled_statement();
  333. if (!result.is_null())
  334. return result.release_nonnull();
  335. }
  336. if (match_expression()) {
  337. if (match(TokenType::Function))
  338. syntax_error("Function declaration not allowed in single-statement context");
  339. auto expr = parse_expression(0);
  340. consume_or_insert_semicolon();
  341. return create_ast_node<ExpressionStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(expr));
  342. }
  343. expected("statement");
  344. consume();
  345. return create_ast_node<ErrorStatement>({ m_state.current_token.filename(), rule_start.position(), position() });
  346. }
  347. }
  348. RefPtr<FunctionExpression> Parser::try_parse_arrow_function_expression(bool expect_parens)
  349. {
  350. save_state();
  351. auto rule_start = push_start();
  352. ArmedScopeGuard state_rollback_guard = [&] {
  353. load_state();
  354. };
  355. Vector<FunctionNode::Parameter> parameters;
  356. i32 function_length = -1;
  357. if (expect_parens) {
  358. // We have parens around the function parameters and can re-use the same parsing
  359. // logic used for regular functions: multiple parameters, default values, rest
  360. // parameter, maybe a trailing comma. If we have a new syntax error afterwards we
  361. // check if it's about a wrong token (something like duplicate parameter name must
  362. // not abort), know parsing failed and rollback the parser state.
  363. auto previous_syntax_errors = m_state.errors.size();
  364. parameters = parse_formal_parameters(function_length, FunctionNodeParseOptions::IsArrowFunction);
  365. if (m_state.errors.size() > previous_syntax_errors && m_state.errors[previous_syntax_errors].message.starts_with("Unexpected token"))
  366. return nullptr;
  367. if (!match(TokenType::ParenClose))
  368. return nullptr;
  369. consume();
  370. } else {
  371. // No parens - this must be an identifier followed by arrow. That's it.
  372. if (!match_identifier() && !match(TokenType::Yield) && !match(TokenType::Await))
  373. return nullptr;
  374. auto token = consume_identifier_reference();
  375. if (m_state.strict_mode && token.value().is_one_of("arguments"sv, "eval"sv))
  376. syntax_error("BindingIdentifier may not be 'arguments' or 'eval' in strict mode");
  377. parameters.append({ FlyString { token.value() }, {} });
  378. }
  379. // If there's a newline between the closing paren and arrow it's not a valid arrow function,
  380. // ASI should kick in instead (it'll then fail with "Unexpected token Arrow")
  381. if (m_state.current_token.trivia_contains_line_terminator())
  382. return nullptr;
  383. if (!match(TokenType::Arrow))
  384. return nullptr;
  385. consume();
  386. if (function_length == -1)
  387. function_length = parameters.size();
  388. m_state.function_parameters.append(parameters);
  389. auto old_labels_in_scope = move(m_state.labels_in_scope);
  390. ScopeGuard guard([&]() {
  391. m_state.labels_in_scope = move(old_labels_in_scope);
  392. });
  393. bool is_strict = false;
  394. auto function_body_result = [&]() -> RefPtr<BlockStatement> {
  395. TemporaryChange change(m_state.in_arrow_function_context, true);
  396. if (match(TokenType::CurlyOpen)) {
  397. // Parse a function body with statements
  398. ScopePusher scope(*this, ScopePusher::Var, Scope::Function);
  399. auto body = parse_block_statement(is_strict);
  400. scope.add_to_scope_node(body);
  401. return body;
  402. }
  403. if (match_expression()) {
  404. // Parse a function body which returns a single expression
  405. // FIXME: We synthesize a block with a return statement
  406. // for arrow function bodies which are a single expression.
  407. // Esprima generates a single "ArrowFunctionExpression"
  408. // with a "body" property.
  409. auto return_expression = parse_expression(2);
  410. auto return_block = create_ast_node<BlockStatement>({ m_state.current_token.filename(), rule_start.position(), position() });
  411. return_block->append<ReturnStatement>({ m_filename, rule_start.position(), position() }, move(return_expression));
  412. return return_block;
  413. }
  414. // Invalid arrow function body
  415. return nullptr;
  416. }();
  417. m_state.function_parameters.take_last();
  418. if (!function_body_result.is_null()) {
  419. state_rollback_guard.disarm();
  420. discard_saved_state();
  421. auto body = function_body_result.release_nonnull();
  422. return create_ast_node<FunctionExpression>(
  423. { m_state.current_token.filename(), rule_start.position(), position() }, "", move(body),
  424. move(parameters), function_length, FunctionKind::Regular, is_strict, true);
  425. }
  426. return nullptr;
  427. }
  428. RefPtr<Statement> Parser::try_parse_labelled_statement()
  429. {
  430. save_state();
  431. auto rule_start = push_start();
  432. ArmedScopeGuard state_rollback_guard = [&] {
  433. load_state();
  434. };
  435. auto identifier = consume_identifier_reference().value();
  436. if (!match(TokenType::Colon))
  437. return {};
  438. consume(TokenType::Colon);
  439. if (!match_statement())
  440. return {};
  441. m_state.labels_in_scope.set(identifier);
  442. auto statement = parse_statement();
  443. m_state.labels_in_scope.remove(identifier);
  444. statement->set_label(identifier);
  445. state_rollback_guard.disarm();
  446. discard_saved_state();
  447. return statement;
  448. }
  449. RefPtr<MetaProperty> Parser::try_parse_new_target_expression()
  450. {
  451. save_state();
  452. auto rule_start = push_start();
  453. ArmedScopeGuard state_rollback_guard = [&] {
  454. load_state();
  455. };
  456. consume(TokenType::New);
  457. if (!match(TokenType::Period))
  458. return {};
  459. consume();
  460. if (!match(TokenType::Identifier))
  461. return {};
  462. if (consume().value() != "target")
  463. return {};
  464. state_rollback_guard.disarm();
  465. discard_saved_state();
  466. return create_ast_node<MetaProperty>({ m_state.current_token.filename(), rule_start.position(), position() }, MetaProperty::Type::NewTarget);
  467. }
  468. NonnullRefPtr<ClassDeclaration> Parser::parse_class_declaration()
  469. {
  470. auto rule_start = push_start();
  471. return create_ast_node<ClassDeclaration>({ m_state.current_token.filename(), rule_start.position(), position() }, parse_class_expression(true));
  472. }
  473. NonnullRefPtr<ClassExpression> Parser::parse_class_expression(bool expect_class_name)
  474. {
  475. auto rule_start = push_start();
  476. // Classes are always in strict mode.
  477. TemporaryChange strict_mode_rollback(m_state.strict_mode, true);
  478. consume(TokenType::Class);
  479. NonnullRefPtrVector<ClassMethod> methods;
  480. RefPtr<Expression> super_class;
  481. RefPtr<FunctionExpression> constructor;
  482. String class_name = expect_class_name || match_identifier() || match(TokenType::Yield) || match(TokenType::Await)
  483. ? consume_identifier_reference().value().to_string()
  484. : "";
  485. if (match(TokenType::Extends)) {
  486. consume();
  487. auto [expression, should_continue_parsing] = parse_primary_expression();
  488. // Basically a (much) simplified parse_secondary_expression().
  489. for (;;) {
  490. if (match(TokenType::TemplateLiteralStart)) {
  491. auto template_literal = parse_template_literal(true);
  492. expression = create_ast_node<TaggedTemplateLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, move(expression), move(template_literal));
  493. continue;
  494. }
  495. if (match(TokenType::BracketOpen) || match(TokenType::Period) || match(TokenType::ParenOpen)) {
  496. auto precedence = g_operator_precedence.get(m_state.current_token.type());
  497. expression = parse_secondary_expression(move(expression), precedence);
  498. continue;
  499. }
  500. break;
  501. }
  502. super_class = move(expression);
  503. (void)should_continue_parsing;
  504. }
  505. consume(TokenType::CurlyOpen);
  506. while (!done() && !match(TokenType::CurlyClose)) {
  507. RefPtr<Expression> property_key;
  508. bool is_static = false;
  509. bool is_constructor = false;
  510. bool is_generator = false;
  511. auto method_kind = ClassMethod::Kind::Method;
  512. if (match(TokenType::Semicolon)) {
  513. consume();
  514. continue;
  515. }
  516. if (match(TokenType::Asterisk)) {
  517. consume();
  518. is_generator = true;
  519. }
  520. if (match_property_key()) {
  521. StringView name;
  522. if (!is_generator && m_state.current_token.value() == "static"sv) {
  523. if (match(TokenType::Identifier)) {
  524. consume();
  525. is_static = true;
  526. if (match(TokenType::Asterisk)) {
  527. consume();
  528. is_generator = true;
  529. }
  530. }
  531. }
  532. if (match(TokenType::Identifier)) {
  533. auto identifier_name = m_state.current_token.value();
  534. if (identifier_name == "get") {
  535. method_kind = ClassMethod::Kind::Getter;
  536. consume();
  537. } else if (identifier_name == "set") {
  538. method_kind = ClassMethod::Kind::Setter;
  539. consume();
  540. }
  541. }
  542. if (match_property_key()) {
  543. switch (m_state.current_token.type()) {
  544. case TokenType::Identifier:
  545. name = consume().value();
  546. property_key = create_ast_node<StringLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, name);
  547. break;
  548. case TokenType::StringLiteral: {
  549. auto string_literal = parse_string_literal(consume());
  550. name = string_literal->value();
  551. property_key = move(string_literal);
  552. break;
  553. }
  554. default:
  555. property_key = parse_property_key();
  556. break;
  557. }
  558. } else {
  559. expected("property key");
  560. }
  561. // Constructor may be a StringLiteral or an Identifier.
  562. if (!is_static && name == "constructor") {
  563. if (method_kind != ClassMethod::Kind::Method)
  564. syntax_error("Class constructor may not be an accessor");
  565. if (!constructor.is_null())
  566. syntax_error("Classes may not have more than one constructor");
  567. if (is_generator)
  568. syntax_error("Class constructor may not be a generator");
  569. is_constructor = true;
  570. }
  571. }
  572. if (match(TokenType::ParenOpen)) {
  573. u8 parse_options = FunctionNodeParseOptions::AllowSuperPropertyLookup;
  574. if (!super_class.is_null())
  575. parse_options |= FunctionNodeParseOptions::AllowSuperConstructorCall;
  576. if (method_kind == ClassMethod::Kind::Getter)
  577. parse_options |= FunctionNodeParseOptions::IsGetterFunction;
  578. if (method_kind == ClassMethod::Kind::Setter)
  579. parse_options |= FunctionNodeParseOptions::IsSetterFunction;
  580. if (is_generator)
  581. parse_options |= FunctionNodeParseOptions::IsGeneratorFunction;
  582. auto function = parse_function_node<FunctionExpression>(parse_options);
  583. if (is_constructor) {
  584. constructor = move(function);
  585. } else if (!property_key.is_null()) {
  586. methods.append(create_ast_node<ClassMethod>({ m_state.current_token.filename(), rule_start.position(), position() }, property_key.release_nonnull(), move(function), method_kind, is_static));
  587. } else {
  588. syntax_error("No key for class method");
  589. }
  590. } else {
  591. expected("ParenOpen");
  592. consume();
  593. }
  594. }
  595. consume(TokenType::CurlyClose);
  596. if (constructor.is_null()) {
  597. auto constructor_body = create_ast_node<BlockStatement>({ m_state.current_token.filename(), rule_start.position(), position() });
  598. if (!super_class.is_null()) {
  599. // Set constructor to the result of parsing the source text
  600. // constructor(... args){ super (...args);}
  601. auto super_call = create_ast_node<SuperCall>(
  602. { m_state.current_token.filename(), rule_start.position(), position() },
  603. Vector { CallExpression::Argument { create_ast_node<Identifier>({ m_state.current_token.filename(), rule_start.position(), position() }, "args"), true } });
  604. constructor_body->append(create_ast_node<ExpressionStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(super_call)));
  605. constructor_body->add_variables(m_state.var_scopes.last());
  606. constructor = create_ast_node<FunctionExpression>(
  607. { m_state.current_token.filename(), rule_start.position(), position() }, class_name, move(constructor_body),
  608. Vector { FunctionNode::Parameter { FlyString { "args" }, nullptr, true } }, 0, FunctionKind::Regular, true);
  609. } else {
  610. constructor = create_ast_node<FunctionExpression>(
  611. { m_state.current_token.filename(), rule_start.position(), position() }, class_name, move(constructor_body),
  612. Vector<FunctionNode::Parameter> {}, 0, FunctionKind::Regular, true);
  613. }
  614. }
  615. return create_ast_node<ClassExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, move(class_name), move(constructor), move(super_class), move(methods));
  616. }
  617. Parser::PrimaryExpressionParseResult Parser::parse_primary_expression()
  618. {
  619. auto rule_start = push_start();
  620. if (match_unary_prefixed_expression())
  621. return { parse_unary_prefixed_expression() };
  622. switch (m_state.current_token.type()) {
  623. case TokenType::ParenOpen: {
  624. auto paren_position = position();
  625. consume(TokenType::ParenOpen);
  626. if ((match(TokenType::ParenClose) || match_identifier() || match(TokenType::TripleDot) || match(TokenType::CurlyOpen) || match(TokenType::BracketOpen))
  627. && !try_parse_arrow_function_expression_failed_at_position(paren_position)) {
  628. auto arrow_function_result = try_parse_arrow_function_expression(true);
  629. if (!arrow_function_result.is_null())
  630. return { arrow_function_result.release_nonnull() };
  631. set_try_parse_arrow_function_expression_failed_at_position(paren_position, true);
  632. }
  633. auto expression = parse_expression(0);
  634. consume(TokenType::ParenClose);
  635. if (is<FunctionExpression>(*expression)) {
  636. static_cast<FunctionExpression&>(*expression).set_cannot_auto_rename();
  637. }
  638. return { move(expression) };
  639. }
  640. case TokenType::This:
  641. consume();
  642. return { create_ast_node<ThisExpression>({ m_state.current_token.filename(), rule_start.position(), position() }) };
  643. case TokenType::Class:
  644. return { parse_class_expression(false) };
  645. case TokenType::Super:
  646. consume();
  647. if (!m_state.allow_super_property_lookup)
  648. syntax_error("'super' keyword unexpected here");
  649. return { create_ast_node<SuperExpression>({ m_state.current_token.filename(), rule_start.position(), position() }) };
  650. case TokenType::Identifier: {
  651. read_as_identifier:;
  652. if (!try_parse_arrow_function_expression_failed_at_position(position())) {
  653. auto arrow_function_result = try_parse_arrow_function_expression(false);
  654. if (!arrow_function_result.is_null())
  655. return { arrow_function_result.release_nonnull() };
  656. set_try_parse_arrow_function_expression_failed_at_position(position(), true);
  657. }
  658. auto string = consume().value();
  659. return { create_ast_node<Identifier>({ m_state.current_token.filename(), rule_start.position(), position() }, string) };
  660. }
  661. case TokenType::NumericLiteral:
  662. return { create_ast_node<NumericLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, consume_and_validate_numeric_literal().double_value()) };
  663. case TokenType::BigIntLiteral:
  664. return { create_ast_node<BigIntLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, consume().value()) };
  665. case TokenType::BoolLiteral:
  666. return { create_ast_node<BooleanLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, consume().bool_value()) };
  667. case TokenType::StringLiteral:
  668. return { parse_string_literal(consume()) };
  669. case TokenType::NullLiteral:
  670. consume();
  671. return { create_ast_node<NullLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }) };
  672. case TokenType::CurlyOpen:
  673. return { parse_object_expression() };
  674. case TokenType::Function:
  675. return { parse_function_node<FunctionExpression>() };
  676. case TokenType::BracketOpen:
  677. return { parse_array_expression() };
  678. case TokenType::RegexLiteral:
  679. return { parse_regexp_literal() };
  680. case TokenType::TemplateLiteralStart:
  681. return { parse_template_literal(false) };
  682. case TokenType::New: {
  683. auto new_start = position();
  684. auto new_target_result = try_parse_new_target_expression();
  685. if (!new_target_result.is_null()) {
  686. if (!m_state.in_function_context)
  687. syntax_error("'new.target' not allowed outside of a function", new_start);
  688. return { new_target_result.release_nonnull() };
  689. }
  690. return { parse_new_expression() };
  691. }
  692. case TokenType::Yield:
  693. if (!m_state.in_generator_function_context)
  694. goto read_as_identifier;
  695. return { parse_yield_expression(), false };
  696. default:
  697. if (match_identifier_name())
  698. goto read_as_identifier;
  699. expected("primary expression");
  700. consume();
  701. return { create_ast_node<ErrorExpression>({ m_state.current_token.filename(), rule_start.position(), position() }) };
  702. }
  703. }
  704. NonnullRefPtr<RegExpLiteral> Parser::parse_regexp_literal()
  705. {
  706. auto rule_start = push_start();
  707. auto pattern = consume().value();
  708. // Remove leading and trailing slash.
  709. pattern = pattern.substring_view(1, pattern.length() - 2);
  710. auto flags = String::empty();
  711. if (match(TokenType::RegexFlags)) {
  712. auto flags_start = position();
  713. flags = consume().value();
  714. HashTable<char> seen_flags;
  715. for (size_t i = 0; i < flags.length(); ++i) {
  716. auto flag = flags.substring_view(i, 1);
  717. if (!flag.is_one_of("d", "g", "i", "m", "s", "u", "y"))
  718. syntax_error(String::formatted("Invalid RegExp flag '{}'", flag), Position { flags_start.line, flags_start.column + i });
  719. if (seen_flags.contains(*flag.characters_without_null_termination()))
  720. syntax_error(String::formatted("Repeated RegExp flag '{}'", flag), Position { flags_start.line, flags_start.column + i });
  721. seen_flags.set(*flag.characters_without_null_termination());
  722. }
  723. }
  724. return create_ast_node<RegExpLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, pattern, flags);
  725. }
  726. NonnullRefPtr<Expression> Parser::parse_unary_prefixed_expression()
  727. {
  728. auto rule_start = push_start();
  729. auto precedence = g_operator_precedence.get(m_state.current_token.type());
  730. auto associativity = operator_associativity(m_state.current_token.type());
  731. switch (m_state.current_token.type()) {
  732. case TokenType::PlusPlus: {
  733. consume();
  734. auto rhs_start = position();
  735. auto rhs = parse_expression(precedence, associativity);
  736. // FIXME: Apparently for functions this should also not be enforced on a parser level,
  737. // other engines throw ReferenceError for ++foo()
  738. if (!is<Identifier>(*rhs) && !is<MemberExpression>(*rhs))
  739. syntax_error(String::formatted("Right-hand side of prefix increment operator must be identifier or member expression, got {}", rhs->class_name()), rhs_start);
  740. return create_ast_node<UpdateExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, UpdateOp::Increment, move(rhs), true);
  741. }
  742. case TokenType::MinusMinus: {
  743. consume();
  744. auto rhs_start = position();
  745. auto rhs = parse_expression(precedence, associativity);
  746. // FIXME: Apparently for functions this should also not be enforced on a parser level,
  747. // other engines throw ReferenceError for --foo()
  748. if (!is<Identifier>(*rhs) && !is<MemberExpression>(*rhs))
  749. syntax_error(String::formatted("Right-hand side of prefix decrement operator must be identifier or member expression, got {}", rhs->class_name()), rhs_start);
  750. return create_ast_node<UpdateExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, UpdateOp::Decrement, move(rhs), true);
  751. }
  752. case TokenType::ExclamationMark:
  753. consume();
  754. return create_ast_node<UnaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, UnaryOp::Not, parse_expression(precedence, associativity));
  755. case TokenType::Tilde:
  756. consume();
  757. return create_ast_node<UnaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, UnaryOp::BitwiseNot, parse_expression(precedence, associativity));
  758. case TokenType::Plus:
  759. consume();
  760. return create_ast_node<UnaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, UnaryOp::Plus, parse_expression(precedence, associativity));
  761. case TokenType::Minus:
  762. consume();
  763. return create_ast_node<UnaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, UnaryOp::Minus, parse_expression(precedence, associativity));
  764. case TokenType::Typeof:
  765. consume();
  766. return create_ast_node<UnaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, UnaryOp::Typeof, parse_expression(precedence, associativity));
  767. case TokenType::Void:
  768. consume();
  769. return create_ast_node<UnaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, UnaryOp::Void, parse_expression(precedence, associativity));
  770. case TokenType::Delete:
  771. consume();
  772. return create_ast_node<UnaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, UnaryOp::Delete, parse_expression(precedence, associativity));
  773. default:
  774. expected("primary expression");
  775. consume();
  776. return create_ast_node<ErrorExpression>({ m_state.current_token.filename(), rule_start.position(), position() });
  777. }
  778. }
  779. NonnullRefPtr<Expression> Parser::parse_property_key()
  780. {
  781. auto rule_start = push_start();
  782. if (match(TokenType::StringLiteral)) {
  783. return parse_string_literal(consume());
  784. } else if (match(TokenType::NumericLiteral)) {
  785. return create_ast_node<NumericLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, consume().double_value());
  786. } else if (match(TokenType::BigIntLiteral)) {
  787. return create_ast_node<BigIntLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, consume().value());
  788. } else if (match(TokenType::BracketOpen)) {
  789. consume(TokenType::BracketOpen);
  790. auto result = parse_expression(2);
  791. consume(TokenType::BracketClose);
  792. return result;
  793. } else {
  794. if (!match_identifier_name())
  795. expected("IdentifierName");
  796. return create_ast_node<StringLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, consume().value());
  797. }
  798. }
  799. NonnullRefPtr<ObjectExpression> Parser::parse_object_expression()
  800. {
  801. auto rule_start = push_start();
  802. consume(TokenType::CurlyOpen);
  803. NonnullRefPtrVector<ObjectProperty> properties;
  804. ObjectProperty::Type property_type;
  805. Optional<SourceRange> invalid_object_literal_property_range;
  806. auto skip_to_next_property = [&] {
  807. while (!done() && !match(TokenType::Comma) && !match(TokenType::CurlyOpen))
  808. consume();
  809. };
  810. while (!done() && !match(TokenType::CurlyClose)) {
  811. property_type = ObjectProperty::Type::KeyValue;
  812. RefPtr<Expression> property_name;
  813. RefPtr<Expression> property_value;
  814. FunctionKind function_kind { FunctionKind::Regular };
  815. if (match(TokenType::TripleDot)) {
  816. consume();
  817. property_name = parse_expression(4);
  818. properties.append(create_ast_node<ObjectProperty>({ m_state.current_token.filename(), rule_start.position(), position() }, *property_name, nullptr, ObjectProperty::Type::Spread, false));
  819. if (!match(TokenType::Comma))
  820. break;
  821. consume(TokenType::Comma);
  822. continue;
  823. }
  824. if (match(TokenType::Asterisk)) {
  825. consume();
  826. property_type = ObjectProperty::Type::KeyValue;
  827. property_name = parse_property_key();
  828. function_kind = FunctionKind ::Generator;
  829. } else if (match(TokenType::Identifier)) {
  830. auto identifier = consume().value();
  831. if (identifier == "get" && match_property_key()) {
  832. property_type = ObjectProperty::Type::Getter;
  833. property_name = parse_property_key();
  834. } else if (identifier == "set" && match_property_key()) {
  835. property_type = ObjectProperty::Type::Setter;
  836. property_name = parse_property_key();
  837. } else {
  838. property_name = create_ast_node<StringLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, identifier);
  839. property_value = create_ast_node<Identifier>({ m_state.current_token.filename(), rule_start.position(), position() }, identifier);
  840. }
  841. } else {
  842. property_name = parse_property_key();
  843. }
  844. if (property_type == ObjectProperty::Type::Getter || property_type == ObjectProperty::Type::Setter) {
  845. if (!match(TokenType::ParenOpen)) {
  846. expected("'(' for object getter or setter property");
  847. skip_to_next_property();
  848. continue;
  849. }
  850. }
  851. if (match(TokenType::Equals)) {
  852. // Not a valid object literal, but a valid assignment target
  853. consume();
  854. // Parse the expression and throw it away
  855. auto expression = parse_expression(2);
  856. if (!invalid_object_literal_property_range.has_value())
  857. invalid_object_literal_property_range = expression->source_range();
  858. } else if (match(TokenType::ParenOpen)) {
  859. VERIFY(property_name);
  860. u8 parse_options = FunctionNodeParseOptions::AllowSuperPropertyLookup;
  861. if (property_type == ObjectProperty::Type::Getter)
  862. parse_options |= FunctionNodeParseOptions::IsGetterFunction;
  863. if (property_type == ObjectProperty::Type::Setter)
  864. parse_options |= FunctionNodeParseOptions::IsSetterFunction;
  865. if (function_kind == FunctionKind::Generator)
  866. parse_options |= FunctionNodeParseOptions::IsGeneratorFunction;
  867. auto function = parse_function_node<FunctionExpression>(parse_options);
  868. properties.append(create_ast_node<ObjectProperty>({ m_state.current_token.filename(), rule_start.position(), position() }, *property_name, function, property_type, true));
  869. } else if (match(TokenType::Colon)) {
  870. if (!property_name) {
  871. expected("a property name");
  872. skip_to_next_property();
  873. continue;
  874. }
  875. consume();
  876. properties.append(create_ast_node<ObjectProperty>({ m_state.current_token.filename(), rule_start.position(), position() }, *property_name, parse_expression(2), property_type, false));
  877. } else if (property_name && property_value) {
  878. properties.append(create_ast_node<ObjectProperty>({ m_state.current_token.filename(), rule_start.position(), position() }, *property_name, *property_value, property_type, false));
  879. } else {
  880. expected("a property");
  881. skip_to_next_property();
  882. continue;
  883. }
  884. if (!match(TokenType::Comma))
  885. break;
  886. consume(TokenType::Comma);
  887. }
  888. consume(TokenType::CurlyClose);
  889. return create_ast_node<ObjectExpression>(
  890. { m_state.current_token.filename(), rule_start.position(), position() },
  891. move(properties),
  892. move(invalid_object_literal_property_range));
  893. }
  894. NonnullRefPtr<ArrayExpression> Parser::parse_array_expression()
  895. {
  896. auto rule_start = push_start();
  897. consume(TokenType::BracketOpen);
  898. Vector<RefPtr<Expression>> elements;
  899. while (match_expression() || match(TokenType::TripleDot) || match(TokenType::Comma)) {
  900. RefPtr<Expression> expression;
  901. if (match(TokenType::TripleDot)) {
  902. consume(TokenType::TripleDot);
  903. expression = create_ast_node<SpreadExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, parse_expression(2));
  904. } else if (match_expression()) {
  905. expression = parse_expression(2);
  906. }
  907. elements.append(expression);
  908. if (!match(TokenType::Comma))
  909. break;
  910. consume(TokenType::Comma);
  911. }
  912. consume(TokenType::BracketClose);
  913. return create_ast_node<ArrayExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, move(elements));
  914. }
  915. NonnullRefPtr<StringLiteral> Parser::parse_string_literal(const Token& token, bool in_template_literal)
  916. {
  917. auto rule_start = push_start();
  918. auto status = Token::StringValueStatus::Ok;
  919. auto string = token.string_value(status);
  920. if (status != Token::StringValueStatus::Ok) {
  921. String message;
  922. if (status == Token::StringValueStatus::LegacyOctalEscapeSequence) {
  923. m_state.string_legacy_octal_escape_sequence_in_scope = true;
  924. if (in_template_literal)
  925. message = "Octal escape sequence not allowed in template literal";
  926. else if (m_state.strict_mode)
  927. message = "Octal escape sequence in string literal not allowed in strict mode";
  928. } else if (status == Token::StringValueStatus::MalformedHexEscape || status == Token::StringValueStatus::MalformedUnicodeEscape) {
  929. auto type = status == Token::StringValueStatus::MalformedUnicodeEscape ? "unicode" : "hexadecimal";
  930. message = String::formatted("Malformed {} escape sequence", type);
  931. } else if (status == Token::StringValueStatus::UnicodeEscapeOverflow) {
  932. message = "Unicode code_point must not be greater than 0x10ffff in escape sequence";
  933. } else {
  934. VERIFY_NOT_REACHED();
  935. }
  936. if (!message.is_empty())
  937. syntax_error(message, Position { token.line_number(), token.line_column() });
  938. }
  939. auto is_use_strict_directive = !in_template_literal && (token.value() == "'use strict'" || token.value() == "\"use strict\"");
  940. return create_ast_node<StringLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, string, is_use_strict_directive);
  941. }
  942. NonnullRefPtr<TemplateLiteral> Parser::parse_template_literal(bool is_tagged)
  943. {
  944. auto rule_start = push_start();
  945. consume(TokenType::TemplateLiteralStart);
  946. NonnullRefPtrVector<Expression> expressions;
  947. NonnullRefPtrVector<Expression> raw_strings;
  948. auto append_empty_string = [this, &rule_start, &expressions, &raw_strings, is_tagged]() {
  949. auto string_literal = create_ast_node<StringLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, "");
  950. expressions.append(string_literal);
  951. if (is_tagged)
  952. raw_strings.append(string_literal);
  953. };
  954. if (!match(TokenType::TemplateLiteralString))
  955. append_empty_string();
  956. while (!done() && !match(TokenType::TemplateLiteralEnd) && !match(TokenType::UnterminatedTemplateLiteral)) {
  957. if (match(TokenType::TemplateLiteralString)) {
  958. auto token = consume();
  959. expressions.append(parse_string_literal(token, true));
  960. if (is_tagged)
  961. raw_strings.append(create_ast_node<StringLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, token.value()));
  962. } else if (match(TokenType::TemplateLiteralExprStart)) {
  963. consume(TokenType::TemplateLiteralExprStart);
  964. if (match(TokenType::TemplateLiteralExprEnd)) {
  965. syntax_error("Empty template literal expression block");
  966. return create_ast_node<TemplateLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, expressions);
  967. }
  968. expressions.append(parse_expression(0));
  969. if (match(TokenType::UnterminatedTemplateLiteral)) {
  970. syntax_error("Unterminated template literal");
  971. return create_ast_node<TemplateLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, expressions);
  972. }
  973. consume(TokenType::TemplateLiteralExprEnd);
  974. if (!match(TokenType::TemplateLiteralString))
  975. append_empty_string();
  976. } else {
  977. expected("Template literal string or expression");
  978. break;
  979. }
  980. }
  981. if (match(TokenType::UnterminatedTemplateLiteral)) {
  982. syntax_error("Unterminated template literal");
  983. } else {
  984. consume(TokenType::TemplateLiteralEnd);
  985. }
  986. if (is_tagged)
  987. return create_ast_node<TemplateLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, expressions, raw_strings);
  988. return create_ast_node<TemplateLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, expressions);
  989. }
  990. NonnullRefPtr<Expression> Parser::parse_expression(int min_precedence, Associativity associativity, const Vector<TokenType>& forbidden)
  991. {
  992. auto rule_start = push_start();
  993. auto [expression, should_continue_parsing] = parse_primary_expression();
  994. auto check_for_invalid_object_property = [&](auto& expression) {
  995. if (is<ObjectExpression>(*expression)) {
  996. if (auto range = static_cast<ObjectExpression&>(*expression).invalid_property_range(); range.has_value())
  997. syntax_error("Invalid property in object literal", range->start);
  998. }
  999. };
  1000. while (match(TokenType::TemplateLiteralStart)) {
  1001. auto template_literal = parse_template_literal(true);
  1002. expression = create_ast_node<TaggedTemplateLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, move(expression), move(template_literal));
  1003. }
  1004. if (should_continue_parsing) {
  1005. while (match_secondary_expression(forbidden)) {
  1006. int new_precedence = g_operator_precedence.get(m_state.current_token.type());
  1007. if (new_precedence < min_precedence)
  1008. break;
  1009. if (new_precedence == min_precedence && associativity == Associativity::Left)
  1010. break;
  1011. check_for_invalid_object_property(expression);
  1012. Associativity new_associativity = operator_associativity(m_state.current_token.type());
  1013. expression = parse_secondary_expression(move(expression), new_precedence, new_associativity);
  1014. while (match(TokenType::TemplateLiteralStart)) {
  1015. auto template_literal = parse_template_literal(true);
  1016. expression = create_ast_node<TaggedTemplateLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, move(expression), move(template_literal));
  1017. }
  1018. }
  1019. }
  1020. check_for_invalid_object_property(expression);
  1021. if (match(TokenType::Comma) && min_precedence <= 1) {
  1022. NonnullRefPtrVector<Expression> expressions;
  1023. expressions.append(expression);
  1024. while (match(TokenType::Comma)) {
  1025. consume();
  1026. expressions.append(parse_expression(2));
  1027. }
  1028. expression = create_ast_node<SequenceExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, move(expressions));
  1029. }
  1030. return expression;
  1031. }
  1032. NonnullRefPtr<Expression> Parser::parse_secondary_expression(NonnullRefPtr<Expression> lhs, int min_precedence, Associativity associativity)
  1033. {
  1034. auto rule_start = push_start();
  1035. switch (m_state.current_token.type()) {
  1036. case TokenType::Plus:
  1037. consume();
  1038. return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::Addition, move(lhs), parse_expression(min_precedence, associativity));
  1039. case TokenType::PlusEquals:
  1040. return parse_assignment_expression(AssignmentOp::AdditionAssignment, move(lhs), min_precedence, associativity);
  1041. case TokenType::Minus:
  1042. consume();
  1043. return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::Subtraction, move(lhs), parse_expression(min_precedence, associativity));
  1044. case TokenType::MinusEquals:
  1045. return parse_assignment_expression(AssignmentOp::SubtractionAssignment, move(lhs), min_precedence, associativity);
  1046. case TokenType::Asterisk:
  1047. consume();
  1048. return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::Multiplication, move(lhs), parse_expression(min_precedence, associativity));
  1049. case TokenType::AsteriskEquals:
  1050. return parse_assignment_expression(AssignmentOp::MultiplicationAssignment, move(lhs), min_precedence, associativity);
  1051. case TokenType::Slash:
  1052. consume();
  1053. return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::Division, move(lhs), parse_expression(min_precedence, associativity));
  1054. case TokenType::SlashEquals:
  1055. return parse_assignment_expression(AssignmentOp::DivisionAssignment, move(lhs), min_precedence, associativity);
  1056. case TokenType::Percent:
  1057. consume();
  1058. return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::Modulo, move(lhs), parse_expression(min_precedence, associativity));
  1059. case TokenType::PercentEquals:
  1060. return parse_assignment_expression(AssignmentOp::ModuloAssignment, move(lhs), min_precedence, associativity);
  1061. case TokenType::DoubleAsterisk:
  1062. consume();
  1063. return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::Exponentiation, move(lhs), parse_expression(min_precedence, associativity));
  1064. case TokenType::DoubleAsteriskEquals:
  1065. return parse_assignment_expression(AssignmentOp::ExponentiationAssignment, move(lhs), min_precedence, associativity);
  1066. case TokenType::GreaterThan:
  1067. consume();
  1068. return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::GreaterThan, move(lhs), parse_expression(min_precedence, associativity));
  1069. case TokenType::GreaterThanEquals:
  1070. consume();
  1071. return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::GreaterThanEquals, move(lhs), parse_expression(min_precedence, associativity));
  1072. case TokenType::LessThan:
  1073. consume();
  1074. return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::LessThan, move(lhs), parse_expression(min_precedence, associativity));
  1075. case TokenType::LessThanEquals:
  1076. consume();
  1077. return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::LessThanEquals, move(lhs), parse_expression(min_precedence, associativity));
  1078. case TokenType::EqualsEqualsEquals:
  1079. consume();
  1080. return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::TypedEquals, move(lhs), parse_expression(min_precedence, associativity));
  1081. case TokenType::ExclamationMarkEqualsEquals:
  1082. consume();
  1083. return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::TypedInequals, move(lhs), parse_expression(min_precedence, associativity));
  1084. case TokenType::EqualsEquals:
  1085. consume();
  1086. return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::AbstractEquals, move(lhs), parse_expression(min_precedence, associativity));
  1087. case TokenType::ExclamationMarkEquals:
  1088. consume();
  1089. return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::AbstractInequals, move(lhs), parse_expression(min_precedence, associativity));
  1090. case TokenType::In:
  1091. consume();
  1092. return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::In, move(lhs), parse_expression(min_precedence, associativity));
  1093. case TokenType::Instanceof:
  1094. consume();
  1095. return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::InstanceOf, move(lhs), parse_expression(min_precedence, associativity));
  1096. case TokenType::Ampersand:
  1097. consume();
  1098. return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::BitwiseAnd, move(lhs), parse_expression(min_precedence, associativity));
  1099. case TokenType::AmpersandEquals:
  1100. return parse_assignment_expression(AssignmentOp::BitwiseAndAssignment, move(lhs), min_precedence, associativity);
  1101. case TokenType::Pipe:
  1102. consume();
  1103. return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::BitwiseOr, move(lhs), parse_expression(min_precedence, associativity));
  1104. case TokenType::PipeEquals:
  1105. return parse_assignment_expression(AssignmentOp::BitwiseOrAssignment, move(lhs), min_precedence, associativity);
  1106. case TokenType::Caret:
  1107. consume();
  1108. return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::BitwiseXor, move(lhs), parse_expression(min_precedence, associativity));
  1109. case TokenType::CaretEquals:
  1110. return parse_assignment_expression(AssignmentOp::BitwiseXorAssignment, move(lhs), min_precedence, associativity);
  1111. case TokenType::ShiftLeft:
  1112. consume();
  1113. return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::LeftShift, move(lhs), parse_expression(min_precedence, associativity));
  1114. case TokenType::ShiftLeftEquals:
  1115. return parse_assignment_expression(AssignmentOp::LeftShiftAssignment, move(lhs), min_precedence, associativity);
  1116. case TokenType::ShiftRight:
  1117. consume();
  1118. return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::RightShift, move(lhs), parse_expression(min_precedence, associativity));
  1119. case TokenType::ShiftRightEquals:
  1120. return parse_assignment_expression(AssignmentOp::RightShiftAssignment, move(lhs), min_precedence, associativity);
  1121. case TokenType::UnsignedShiftRight:
  1122. consume();
  1123. return create_ast_node<BinaryExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, BinaryOp::UnsignedRightShift, move(lhs), parse_expression(min_precedence, associativity));
  1124. case TokenType::UnsignedShiftRightEquals:
  1125. return parse_assignment_expression(AssignmentOp::UnsignedRightShiftAssignment, move(lhs), min_precedence, associativity);
  1126. case TokenType::ParenOpen:
  1127. return parse_call_expression(move(lhs));
  1128. case TokenType::Equals:
  1129. return parse_assignment_expression(AssignmentOp::Assignment, move(lhs), min_precedence, associativity);
  1130. case TokenType::Period:
  1131. consume();
  1132. if (!match_identifier_name())
  1133. expected("IdentifierName");
  1134. 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()));
  1135. case TokenType::BracketOpen: {
  1136. consume(TokenType::BracketOpen);
  1137. auto expression = create_ast_node<MemberExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, move(lhs), parse_expression(0), true);
  1138. consume(TokenType::BracketClose);
  1139. return expression;
  1140. }
  1141. case TokenType::PlusPlus:
  1142. // FIXME: Apparently for functions this should also not be enforced on a parser level,
  1143. // other engines throw ReferenceError for foo()++
  1144. if (!is<Identifier>(*lhs) && !is<MemberExpression>(*lhs))
  1145. syntax_error(String::formatted("Left-hand side of postfix increment operator must be identifier or member expression, got {}", lhs->class_name()));
  1146. consume();
  1147. return create_ast_node<UpdateExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, UpdateOp::Increment, move(lhs));
  1148. case TokenType::MinusMinus:
  1149. // FIXME: Apparently for functions this should also not be enforced on a parser level,
  1150. // other engines throw ReferenceError for foo()--
  1151. if (!is<Identifier>(*lhs) && !is<MemberExpression>(*lhs))
  1152. syntax_error(String::formatted("Left-hand side of postfix increment operator must be identifier or member expression, got {}", lhs->class_name()));
  1153. consume();
  1154. return create_ast_node<UpdateExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, UpdateOp::Decrement, move(lhs));
  1155. case TokenType::DoubleAmpersand:
  1156. consume();
  1157. return create_ast_node<LogicalExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, LogicalOp::And, move(lhs), parse_expression(min_precedence, associativity));
  1158. case TokenType::DoubleAmpersandEquals:
  1159. return parse_assignment_expression(AssignmentOp::AndAssignment, move(lhs), min_precedence, associativity);
  1160. case TokenType::DoublePipe:
  1161. consume();
  1162. return create_ast_node<LogicalExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, LogicalOp::Or, move(lhs), parse_expression(min_precedence, associativity));
  1163. case TokenType::DoublePipeEquals:
  1164. return parse_assignment_expression(AssignmentOp::OrAssignment, move(lhs), min_precedence, associativity);
  1165. case TokenType::DoubleQuestionMark:
  1166. consume();
  1167. return create_ast_node<LogicalExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, LogicalOp::NullishCoalescing, move(lhs), parse_expression(min_precedence, associativity));
  1168. case TokenType::DoubleQuestionMarkEquals:
  1169. return parse_assignment_expression(AssignmentOp::NullishAssignment, move(lhs), min_precedence, associativity);
  1170. case TokenType::QuestionMark:
  1171. return parse_conditional_expression(move(lhs));
  1172. default:
  1173. expected("secondary expression");
  1174. consume();
  1175. return create_ast_node<ErrorExpression>({ m_state.current_token.filename(), rule_start.position(), position() });
  1176. }
  1177. }
  1178. NonnullRefPtr<AssignmentExpression> Parser::parse_assignment_expression(AssignmentOp assignment_op, NonnullRefPtr<Expression> lhs, int min_precedence, Associativity associativity)
  1179. {
  1180. auto rule_start = push_start();
  1181. VERIFY(match(TokenType::Equals)
  1182. || match(TokenType::PlusEquals)
  1183. || match(TokenType::MinusEquals)
  1184. || match(TokenType::AsteriskEquals)
  1185. || match(TokenType::SlashEquals)
  1186. || match(TokenType::PercentEquals)
  1187. || match(TokenType::DoubleAsteriskEquals)
  1188. || match(TokenType::AmpersandEquals)
  1189. || match(TokenType::PipeEquals)
  1190. || match(TokenType::CaretEquals)
  1191. || match(TokenType::ShiftLeftEquals)
  1192. || match(TokenType::ShiftRightEquals)
  1193. || match(TokenType::UnsignedShiftRightEquals)
  1194. || match(TokenType::DoubleAmpersandEquals)
  1195. || match(TokenType::DoublePipeEquals)
  1196. || match(TokenType::DoubleQuestionMarkEquals));
  1197. consume();
  1198. if (assignment_op == AssignmentOp::Assignment) {
  1199. auto synthesize_binding_pattern = [this](Expression const& expression) -> RefPtr<BindingPattern> {
  1200. // Clear any syntax error that has occurred in the range that 'expression' spans.
  1201. m_state.errors.remove_all_matching([range = expression.source_range()](auto const& error) {
  1202. return error.position.has_value() && range.contains(*error.position);
  1203. });
  1204. // Make a parser and parse the source for this expression as a binding pattern.
  1205. auto source = m_state.lexer.source().substring_view(expression.source_range().start.offset - 2, expression.source_range().end.offset - expression.source_range().start.offset);
  1206. Lexer lexer { source, m_state.lexer.filename(), expression.source_range().start.line, expression.source_range().start.column };
  1207. Parser parser { lexer };
  1208. parser.m_state.strict_mode = m_state.strict_mode;
  1209. parser.m_state.allow_super_property_lookup = m_state.allow_super_property_lookup;
  1210. parser.m_state.allow_super_constructor_call = m_state.allow_super_constructor_call;
  1211. parser.m_state.in_function_context = m_state.in_function_context;
  1212. parser.m_state.in_generator_function_context = m_state.in_generator_function_context;
  1213. parser.m_state.in_arrow_function_context = m_state.in_arrow_function_context;
  1214. parser.m_state.in_break_context = m_state.in_break_context;
  1215. parser.m_state.in_continue_context = m_state.in_continue_context;
  1216. parser.m_state.string_legacy_octal_escape_sequence_in_scope = m_state.string_legacy_octal_escape_sequence_in_scope;
  1217. auto result = parser.parse_binding_pattern();
  1218. if (parser.has_errors())
  1219. m_state.errors.extend(parser.errors());
  1220. return result;
  1221. };
  1222. if (is<ArrayExpression>(*lhs) || is<ObjectExpression>(*lhs)) {
  1223. auto binding_pattern = synthesize_binding_pattern(*lhs);
  1224. if (binding_pattern) {
  1225. auto rhs = parse_expression(min_precedence, associativity);
  1226. return create_ast_node<AssignmentExpression>(
  1227. { m_state.current_token.filename(), rule_start.position(), position() },
  1228. assignment_op,
  1229. binding_pattern.release_nonnull(),
  1230. move(rhs));
  1231. }
  1232. }
  1233. }
  1234. if (!is<Identifier>(*lhs) && !is<MemberExpression>(*lhs) && !is<CallExpression>(*lhs)) {
  1235. syntax_error("Invalid left-hand side in assignment");
  1236. } else if (m_state.strict_mode && is<Identifier>(*lhs)) {
  1237. auto name = static_cast<const Identifier&>(*lhs).string();
  1238. if (name == "eval" || name == "arguments")
  1239. syntax_error(String::formatted("'{}' cannot be assigned to in strict mode code", name));
  1240. } else if (m_state.strict_mode && is<CallExpression>(*lhs)) {
  1241. syntax_error("Cannot assign to function call");
  1242. }
  1243. auto rhs = parse_expression(min_precedence, associativity);
  1244. if (assignment_op == AssignmentOp::Assignment && is<FunctionExpression>(*rhs)) {
  1245. auto ident = lhs;
  1246. if (is<MemberExpression>(*lhs)) {
  1247. ident = static_cast<MemberExpression&>(*lhs).property();
  1248. }
  1249. if (is<Identifier>(*ident))
  1250. static_cast<FunctionExpression&>(*rhs).set_name_if_possible(static_cast<Identifier&>(*ident).string());
  1251. }
  1252. return create_ast_node<AssignmentExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, assignment_op, move(lhs), move(rhs));
  1253. }
  1254. NonnullRefPtr<Identifier> Parser::parse_identifier()
  1255. {
  1256. auto identifier_start = position();
  1257. auto token = consume_identifier();
  1258. return create_ast_node<Identifier>(
  1259. { m_state.current_token.filename(), identifier_start, position() },
  1260. token.value());
  1261. }
  1262. NonnullRefPtr<CallExpression> Parser::parse_call_expression(NonnullRefPtr<Expression> lhs)
  1263. {
  1264. auto rule_start = push_start();
  1265. if (!m_state.allow_super_constructor_call && is<SuperExpression>(*lhs))
  1266. syntax_error("'super' keyword unexpected here");
  1267. consume(TokenType::ParenOpen);
  1268. Vector<CallExpression::Argument> arguments;
  1269. while (match_expression() || match(TokenType::TripleDot)) {
  1270. if (match(TokenType::TripleDot)) {
  1271. consume();
  1272. arguments.append({ parse_expression(2), true });
  1273. } else {
  1274. arguments.append({ parse_expression(2), false });
  1275. }
  1276. if (!match(TokenType::Comma))
  1277. break;
  1278. consume();
  1279. }
  1280. consume(TokenType::ParenClose);
  1281. if (is<SuperExpression>(*lhs))
  1282. return create_ast_node<SuperCall>({ m_state.current_token.filename(), rule_start.position(), position() }, move(arguments));
  1283. return create_ast_node<CallExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, move(lhs), move(arguments));
  1284. }
  1285. NonnullRefPtr<NewExpression> Parser::parse_new_expression()
  1286. {
  1287. auto rule_start = push_start();
  1288. consume(TokenType::New);
  1289. auto callee = parse_expression(g_operator_precedence.get(TokenType::New), Associativity::Right, { TokenType::ParenOpen });
  1290. Vector<CallExpression::Argument> arguments;
  1291. if (match(TokenType::ParenOpen)) {
  1292. consume(TokenType::ParenOpen);
  1293. while (match_expression() || match(TokenType::TripleDot)) {
  1294. if (match(TokenType::TripleDot)) {
  1295. consume();
  1296. arguments.append({ parse_expression(2), true });
  1297. } else {
  1298. arguments.append({ parse_expression(2), false });
  1299. }
  1300. if (!match(TokenType::Comma))
  1301. break;
  1302. consume();
  1303. }
  1304. consume(TokenType::ParenClose);
  1305. }
  1306. return create_ast_node<NewExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, move(callee), move(arguments));
  1307. }
  1308. NonnullRefPtr<YieldExpression> Parser::parse_yield_expression()
  1309. {
  1310. auto rule_start = push_start();
  1311. consume(TokenType::Yield);
  1312. RefPtr<Expression> argument;
  1313. bool yield_from = false;
  1314. if (!m_state.current_token.trivia_contains_line_terminator()) {
  1315. if (match(TokenType::Asterisk)) {
  1316. consume();
  1317. yield_from = true;
  1318. }
  1319. if (yield_from || match_expression())
  1320. argument = parse_expression(0);
  1321. }
  1322. return create_ast_node<YieldExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, move(argument), yield_from);
  1323. }
  1324. NonnullRefPtr<ReturnStatement> Parser::parse_return_statement()
  1325. {
  1326. auto rule_start = push_start();
  1327. if (!m_state.in_function_context && !m_state.in_arrow_function_context)
  1328. syntax_error("'return' not allowed outside of a function");
  1329. consume(TokenType::Return);
  1330. // Automatic semicolon insertion: terminate statement when return is followed by newline
  1331. if (m_state.current_token.trivia_contains_line_terminator())
  1332. return create_ast_node<ReturnStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, nullptr);
  1333. if (match_expression()) {
  1334. auto expression = parse_expression(0);
  1335. consume_or_insert_semicolon();
  1336. return create_ast_node<ReturnStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(expression));
  1337. }
  1338. consume_or_insert_semicolon();
  1339. return create_ast_node<ReturnStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, nullptr);
  1340. }
  1341. NonnullRefPtr<BlockStatement> Parser::parse_block_statement()
  1342. {
  1343. auto rule_start = push_start();
  1344. bool dummy = false;
  1345. return parse_block_statement(dummy);
  1346. }
  1347. NonnullRefPtr<BlockStatement> Parser::parse_block_statement(bool& is_strict)
  1348. {
  1349. auto rule_start = push_start();
  1350. ScopePusher scope(*this, ScopePusher::Let, Parser::Scope::Block);
  1351. auto block = create_ast_node<BlockStatement>({ m_state.current_token.filename(), rule_start.position(), position() });
  1352. consume(TokenType::CurlyOpen);
  1353. bool first = true;
  1354. bool initial_strict_mode_state = m_state.strict_mode;
  1355. if (initial_strict_mode_state)
  1356. is_strict = true;
  1357. while (!done() && !match(TokenType::CurlyClose)) {
  1358. if (match_declaration()) {
  1359. block->append(parse_declaration());
  1360. } else if (match_statement()) {
  1361. auto statement = parse_statement();
  1362. block->append(statement);
  1363. if (statement_is_use_strict_directive(statement)) {
  1364. if (first && !initial_strict_mode_state) {
  1365. is_strict = true;
  1366. m_state.strict_mode = true;
  1367. }
  1368. if (m_state.string_legacy_octal_escape_sequence_in_scope)
  1369. syntax_error("Octal escape sequence in string literal not allowed in strict mode");
  1370. }
  1371. } else {
  1372. expected("statement or declaration");
  1373. consume();
  1374. }
  1375. first = false;
  1376. }
  1377. m_state.strict_mode = initial_strict_mode_state;
  1378. m_state.string_legacy_octal_escape_sequence_in_scope = false;
  1379. consume(TokenType::CurlyClose);
  1380. scope.add_to_scope_node(block);
  1381. return block;
  1382. }
  1383. template<typename FunctionNodeType>
  1384. NonnullRefPtr<FunctionNodeType> Parser::parse_function_node(u8 parse_options)
  1385. {
  1386. auto rule_start = push_start();
  1387. VERIFY(!(parse_options & FunctionNodeParseOptions::IsGetterFunction && parse_options & FunctionNodeParseOptions::IsSetterFunction));
  1388. TemporaryChange super_property_access_rollback(m_state.allow_super_property_lookup, !!(parse_options & FunctionNodeParseOptions::AllowSuperPropertyLookup));
  1389. TemporaryChange super_constructor_call_rollback(m_state.allow_super_constructor_call, !!(parse_options & FunctionNodeParseOptions::AllowSuperConstructorCall));
  1390. ScopePusher scope(*this, ScopePusher::Var, Parser::Scope::Function);
  1391. constexpr auto is_function_expression = IsSame<FunctionNodeType, FunctionExpression>;
  1392. auto is_generator = (parse_options & FunctionNodeParseOptions::IsGeneratorFunction) != 0;
  1393. String name;
  1394. if (parse_options & FunctionNodeParseOptions::CheckForFunctionAndName) {
  1395. consume(TokenType::Function);
  1396. if (!is_generator) {
  1397. is_generator = match(TokenType::Asterisk);
  1398. if (is_generator) {
  1399. consume(TokenType::Asterisk);
  1400. parse_options = parse_options | FunctionNodeParseOptions::IsGeneratorFunction;
  1401. }
  1402. }
  1403. if (FunctionNodeType::must_have_name() || match_identifier())
  1404. name = consume_identifier().value();
  1405. else if (is_function_expression && (match(TokenType::Yield) || match(TokenType::Await)))
  1406. name = consume().value();
  1407. }
  1408. consume(TokenType::ParenOpen);
  1409. i32 function_length = -1;
  1410. auto parameters = parse_formal_parameters(function_length, parse_options);
  1411. consume(TokenType::ParenClose);
  1412. if (function_length == -1)
  1413. function_length = parameters.size();
  1414. TemporaryChange change(m_state.in_function_context, true);
  1415. TemporaryChange generator_change(m_state.in_generator_function_context, m_state.in_generator_function_context || is_generator);
  1416. auto old_labels_in_scope = move(m_state.labels_in_scope);
  1417. ScopeGuard guard([&]() {
  1418. m_state.labels_in_scope = move(old_labels_in_scope);
  1419. });
  1420. m_state.function_parameters.append(parameters);
  1421. bool is_strict = false;
  1422. auto body = parse_block_statement(is_strict);
  1423. m_state.function_parameters.take_last();
  1424. scope.add_to_scope_node(body);
  1425. return create_ast_node<FunctionNodeType>(
  1426. { m_state.current_token.filename(), rule_start.position(), position() },
  1427. name, move(body), move(parameters), function_length,
  1428. is_generator ? FunctionKind::Generator : FunctionKind::Regular, is_strict);
  1429. }
  1430. Vector<FunctionNode::Parameter> Parser::parse_formal_parameters(int& function_length, u8 parse_options)
  1431. {
  1432. auto rule_start = push_start();
  1433. bool has_default_parameter = false;
  1434. bool has_rest_parameter = false;
  1435. Vector<FunctionNode::Parameter> parameters;
  1436. auto consume_identifier_or_binding_pattern = [&]() -> Variant<FlyString, NonnullRefPtr<BindingPattern>> {
  1437. if (auto pattern = parse_binding_pattern())
  1438. return pattern.release_nonnull();
  1439. auto token = consume_identifier();
  1440. auto parameter_name = token.value();
  1441. for (auto& parameter : parameters) {
  1442. if (auto* ptr = parameter.binding.get_pointer<FlyString>(); !ptr || parameter_name != *ptr)
  1443. continue;
  1444. String message;
  1445. if (parse_options & FunctionNodeParseOptions::IsArrowFunction)
  1446. message = String::formatted("Duplicate parameter '{}' not allowed in arrow function", parameter_name);
  1447. else if (m_state.strict_mode)
  1448. message = String::formatted("Duplicate parameter '{}' not allowed in strict mode", parameter_name);
  1449. else if (has_default_parameter || match(TokenType::Equals))
  1450. message = String::formatted("Duplicate parameter '{}' not allowed in function with default parameter", parameter_name);
  1451. else if (has_rest_parameter)
  1452. message = String::formatted("Duplicate parameter '{}' not allowed in function with rest parameter", parameter_name);
  1453. if (!message.is_empty())
  1454. syntax_error(message, Position { token.line_number(), token.line_column() });
  1455. break;
  1456. }
  1457. return FlyString { token.value() };
  1458. };
  1459. while (match(TokenType::CurlyOpen) || match(TokenType::BracketOpen) || match_identifier() || match(TokenType::TripleDot)) {
  1460. if (parse_options & FunctionNodeParseOptions::IsGetterFunction)
  1461. syntax_error("Getter function must have no arguments");
  1462. if (parse_options & FunctionNodeParseOptions::IsSetterFunction && (parameters.size() >= 1 || match(TokenType::TripleDot)))
  1463. syntax_error("Setter function must have one argument");
  1464. auto is_rest = false;
  1465. if (match(TokenType::TripleDot)) {
  1466. consume();
  1467. has_rest_parameter = true;
  1468. function_length = parameters.size();
  1469. is_rest = true;
  1470. }
  1471. auto parameter = consume_identifier_or_binding_pattern();
  1472. RefPtr<Expression> default_value;
  1473. if (match(TokenType::Equals)) {
  1474. consume();
  1475. TemporaryChange change(m_state.in_function_context, true);
  1476. has_default_parameter = true;
  1477. function_length = parameters.size();
  1478. default_value = parse_expression(2);
  1479. bool is_generator = parse_options & FunctionNodeParseOptions::IsGeneratorFunction;
  1480. if (is_generator && default_value && default_value->fast_is<Identifier>() && static_cast<Identifier&>(*default_value).string() == "yield"sv)
  1481. syntax_error("Generator function parameter initializer cannot contain a reference to an identifier named \"yield\"");
  1482. }
  1483. parameters.append({ move(parameter), default_value, is_rest });
  1484. if (match(TokenType::ParenClose))
  1485. break;
  1486. consume(TokenType::Comma);
  1487. if (is_rest)
  1488. break;
  1489. }
  1490. if (parse_options & FunctionNodeParseOptions::IsSetterFunction && parameters.is_empty())
  1491. syntax_error("Setter function must have one argument");
  1492. return parameters;
  1493. }
  1494. 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" };
  1495. RefPtr<BindingPattern> Parser::parse_binding_pattern()
  1496. {
  1497. auto rule_start = push_start();
  1498. TokenType closing_token;
  1499. bool is_object = true;
  1500. if (match(TokenType::BracketOpen)) {
  1501. consume();
  1502. closing_token = TokenType::BracketClose;
  1503. is_object = false;
  1504. } else if (match(TokenType::CurlyOpen)) {
  1505. consume();
  1506. closing_token = TokenType::CurlyClose;
  1507. } else {
  1508. return {};
  1509. }
  1510. Vector<BindingPattern::BindingEntry> entries;
  1511. while (!match(closing_token)) {
  1512. if (!is_object && match(TokenType::Comma)) {
  1513. consume();
  1514. entries.append(BindingPattern::BindingEntry {});
  1515. continue;
  1516. }
  1517. auto is_rest = false;
  1518. if (match(TokenType::TripleDot)) {
  1519. consume();
  1520. is_rest = true;
  1521. }
  1522. decltype(BindingPattern::BindingEntry::name) name = Empty {};
  1523. decltype(BindingPattern::BindingEntry::alias) alias = Empty {};
  1524. RefPtr<Expression> initializer = {};
  1525. if (is_object) {
  1526. if (match_identifier_name()) {
  1527. name = create_ast_node<Identifier>(
  1528. { m_state.current_token.filename(), rule_start.position(), position() },
  1529. consume().value());
  1530. } else if (match(TokenType::BracketOpen)) {
  1531. consume();
  1532. name = parse_expression(0);
  1533. consume(TokenType::BracketClose);
  1534. } else {
  1535. expected("identifier or computed property name");
  1536. return {};
  1537. }
  1538. if (!is_rest && match(TokenType::Colon)) {
  1539. consume();
  1540. if (match(TokenType::CurlyOpen) || match(TokenType::BracketOpen)) {
  1541. auto binding_pattern = parse_binding_pattern();
  1542. if (!binding_pattern)
  1543. return {};
  1544. alias = binding_pattern.release_nonnull();
  1545. } else if (match_identifier_name()) {
  1546. alias = create_ast_node<Identifier>(
  1547. { m_state.current_token.filename(), rule_start.position(), position() },
  1548. consume().value());
  1549. } else {
  1550. expected("identifier or binding pattern");
  1551. return {};
  1552. }
  1553. }
  1554. } else {
  1555. if (match_identifier_name()) {
  1556. // BindingElement must always have an Empty name field
  1557. alias = create_ast_node<Identifier>(
  1558. { m_state.current_token.filename(), rule_start.position(), position() },
  1559. consume().value());
  1560. } else if (match(TokenType::BracketOpen) || match(TokenType::CurlyOpen)) {
  1561. auto pattern = parse_binding_pattern();
  1562. if (!pattern) {
  1563. expected("binding pattern");
  1564. return {};
  1565. }
  1566. alias = pattern.release_nonnull();
  1567. } else {
  1568. expected("identifier or binding pattern");
  1569. return {};
  1570. }
  1571. }
  1572. if (match(TokenType::Equals)) {
  1573. if (is_rest) {
  1574. syntax_error("Unexpected initializer after rest element");
  1575. return {};
  1576. }
  1577. consume();
  1578. initializer = parse_expression(2);
  1579. if (!initializer) {
  1580. expected("initialization expression");
  1581. return {};
  1582. }
  1583. }
  1584. entries.append(BindingPattern::BindingEntry { move(name), move(alias), move(initializer), is_rest });
  1585. if (match(TokenType::Comma)) {
  1586. if (is_rest) {
  1587. syntax_error("Rest element may not be followed by a comma");
  1588. return {};
  1589. }
  1590. consume();
  1591. }
  1592. }
  1593. while (!is_object && match(TokenType::Comma))
  1594. consume();
  1595. consume(closing_token);
  1596. auto kind = is_object ? BindingPattern::Kind::Object : BindingPattern::Kind::Array;
  1597. auto pattern = adopt_ref(*new BindingPattern);
  1598. pattern->entries = move(entries);
  1599. pattern->kind = kind;
  1600. pattern->for_each_bound_name([this](auto& name) { check_identifier_name_for_assignment_validity(name); });
  1601. return pattern;
  1602. }
  1603. NonnullRefPtr<VariableDeclaration> Parser::parse_variable_declaration(bool for_loop_variable_declaration)
  1604. {
  1605. auto rule_start = push_start();
  1606. DeclarationKind declaration_kind;
  1607. switch (m_state.current_token.type()) {
  1608. case TokenType::Var:
  1609. declaration_kind = DeclarationKind::Var;
  1610. break;
  1611. case TokenType::Let:
  1612. declaration_kind = DeclarationKind::Let;
  1613. break;
  1614. case TokenType::Const:
  1615. declaration_kind = DeclarationKind::Const;
  1616. break;
  1617. default:
  1618. VERIFY_NOT_REACHED();
  1619. }
  1620. consume();
  1621. NonnullRefPtrVector<VariableDeclarator> declarations;
  1622. for (;;) {
  1623. Variant<NonnullRefPtr<Identifier>, NonnullRefPtr<BindingPattern>, Empty> target { Empty() };
  1624. if (match_identifier()) {
  1625. auto name = consume_identifier().value();
  1626. target = create_ast_node<Identifier>(
  1627. { m_state.current_token.filename(), rule_start.position(), position() },
  1628. name);
  1629. if ((declaration_kind == DeclarationKind::Let || declaration_kind == DeclarationKind::Const) && name == "let"sv)
  1630. syntax_error("Lexical binding may not be called 'let'");
  1631. } else if (auto pattern = parse_binding_pattern()) {
  1632. target = pattern.release_nonnull();
  1633. if ((declaration_kind == DeclarationKind::Let || declaration_kind == DeclarationKind::Const)) {
  1634. target.get<NonnullRefPtr<BindingPattern>>()->for_each_bound_name([this](auto& name) {
  1635. if (name == "let"sv)
  1636. syntax_error("Lexical binding may not be called 'let'");
  1637. });
  1638. }
  1639. } else if (!m_state.in_generator_function_context && match(TokenType::Yield)) {
  1640. target = create_ast_node<Identifier>(
  1641. { m_state.current_token.filename(), rule_start.position(), position() },
  1642. consume().value());
  1643. }
  1644. if (target.has<Empty>()) {
  1645. expected("identifier or a binding pattern");
  1646. if (match(TokenType::Comma)) {
  1647. consume();
  1648. continue;
  1649. }
  1650. break;
  1651. }
  1652. RefPtr<Expression> init;
  1653. if (match(TokenType::Equals)) {
  1654. consume();
  1655. init = parse_expression(2);
  1656. } else if (!for_loop_variable_declaration && declaration_kind == DeclarationKind::Const) {
  1657. syntax_error("Missing initializer in 'const' variable declaration");
  1658. } else if (!for_loop_variable_declaration && target.has<NonnullRefPtr<BindingPattern>>()) {
  1659. syntax_error("Missing initializer in destructuring assignment");
  1660. }
  1661. if (init && is<FunctionExpression>(*init) && target.has<NonnullRefPtr<Identifier>>()) {
  1662. static_cast<FunctionExpression&>(*init).set_name_if_possible(target.get<NonnullRefPtr<Identifier>>()->string());
  1663. }
  1664. declarations.append(create_ast_node<VariableDeclarator>(
  1665. { m_state.current_token.filename(), rule_start.position(), position() },
  1666. move(target).downcast<NonnullRefPtr<Identifier>, NonnullRefPtr<BindingPattern>>(),
  1667. move(init)));
  1668. if (match(TokenType::Comma)) {
  1669. consume();
  1670. continue;
  1671. }
  1672. break;
  1673. }
  1674. if (!for_loop_variable_declaration)
  1675. consume_or_insert_semicolon();
  1676. auto declaration = create_ast_node<VariableDeclaration>({ m_state.current_token.filename(), rule_start.position(), position() }, declaration_kind, move(declarations));
  1677. if (declaration_kind == DeclarationKind::Var) {
  1678. m_state.var_scopes.last().append(declaration);
  1679. } else {
  1680. m_state.let_scopes.last().append(declaration);
  1681. for (auto& declarator : declaration->declarations()) {
  1682. declarator.target().visit(
  1683. [&](const NonnullRefPtr<Identifier>& id) {
  1684. m_state.current_scope->lexical_declarations.set(id->string());
  1685. },
  1686. [&](const NonnullRefPtr<BindingPattern>& binding) {
  1687. binding->for_each_bound_name([&](const auto& name) {
  1688. m_state.current_scope->lexical_declarations.set(name);
  1689. });
  1690. });
  1691. }
  1692. }
  1693. return declaration;
  1694. }
  1695. NonnullRefPtr<ThrowStatement> Parser::parse_throw_statement()
  1696. {
  1697. auto rule_start = push_start();
  1698. consume(TokenType::Throw);
  1699. // Automatic semicolon insertion: terminate statement when throw is followed by newline
  1700. if (m_state.current_token.trivia_contains_line_terminator()) {
  1701. syntax_error("No line break is allowed between 'throw' and its expression");
  1702. 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() }));
  1703. }
  1704. auto expression = parse_expression(0);
  1705. consume_or_insert_semicolon();
  1706. return create_ast_node<ThrowStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(expression));
  1707. }
  1708. NonnullRefPtr<BreakStatement> Parser::parse_break_statement()
  1709. {
  1710. auto rule_start = push_start();
  1711. consume(TokenType::Break);
  1712. FlyString target_label;
  1713. if (match(TokenType::Semicolon)) {
  1714. consume();
  1715. } else {
  1716. if (match(TokenType::Identifier) && !m_state.current_token.trivia_contains_line_terminator()) {
  1717. target_label = consume().value();
  1718. if (!m_state.labels_in_scope.contains(target_label))
  1719. syntax_error(String::formatted("Label '{}' not found", target_label));
  1720. }
  1721. consume_or_insert_semicolon();
  1722. }
  1723. if (target_label.is_null() && !m_state.in_break_context)
  1724. syntax_error("Unlabeled 'break' not allowed outside of a loop or switch statement");
  1725. return create_ast_node<BreakStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, target_label);
  1726. }
  1727. NonnullRefPtr<ContinueStatement> Parser::parse_continue_statement()
  1728. {
  1729. auto rule_start = push_start();
  1730. if (!m_state.in_continue_context)
  1731. syntax_error("'continue' not allow outside of a loop");
  1732. consume(TokenType::Continue);
  1733. FlyString target_label;
  1734. if (match(TokenType::Semicolon)) {
  1735. consume();
  1736. return create_ast_node<ContinueStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, target_label);
  1737. }
  1738. if (match(TokenType::Identifier) && !m_state.current_token.trivia_contains_line_terminator()) {
  1739. target_label = consume().value();
  1740. if (!m_state.labels_in_scope.contains(target_label))
  1741. syntax_error(String::formatted("Label '{}' not found", target_label));
  1742. }
  1743. consume_or_insert_semicolon();
  1744. return create_ast_node<ContinueStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, target_label);
  1745. }
  1746. NonnullRefPtr<ConditionalExpression> Parser::parse_conditional_expression(NonnullRefPtr<Expression> test)
  1747. {
  1748. auto rule_start = push_start();
  1749. consume(TokenType::QuestionMark);
  1750. auto consequent = parse_expression(2);
  1751. consume(TokenType::Colon);
  1752. auto alternate = parse_expression(2);
  1753. return create_ast_node<ConditionalExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, move(test), move(consequent), move(alternate));
  1754. }
  1755. NonnullRefPtr<TryStatement> Parser::parse_try_statement()
  1756. {
  1757. auto rule_start = push_start();
  1758. consume(TokenType::Try);
  1759. auto block = parse_block_statement();
  1760. RefPtr<CatchClause> handler;
  1761. if (match(TokenType::Catch))
  1762. handler = parse_catch_clause();
  1763. RefPtr<BlockStatement> finalizer;
  1764. if (match(TokenType::Finally)) {
  1765. consume();
  1766. finalizer = parse_block_statement();
  1767. }
  1768. if (!handler && !finalizer)
  1769. syntax_error("try statement must have a 'catch' or 'finally' clause");
  1770. return create_ast_node<TryStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(block), move(handler), move(finalizer));
  1771. }
  1772. NonnullRefPtr<DoWhileStatement> Parser::parse_do_while_statement()
  1773. {
  1774. auto rule_start = push_start();
  1775. consume(TokenType::Do);
  1776. auto body = [&]() -> NonnullRefPtr<Statement> {
  1777. TemporaryChange break_change(m_state.in_break_context, true);
  1778. TemporaryChange continue_change(m_state.in_continue_context, true);
  1779. return parse_statement();
  1780. }();
  1781. consume(TokenType::While);
  1782. consume(TokenType::ParenOpen);
  1783. auto test = parse_expression(0);
  1784. consume(TokenType::ParenClose);
  1785. // Since ES 2015 a missing semicolon is inserted here, despite the regular ASI rules not applying
  1786. if (match(TokenType::Semicolon))
  1787. consume();
  1788. return create_ast_node<DoWhileStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(test), move(body));
  1789. }
  1790. NonnullRefPtr<WhileStatement> Parser::parse_while_statement()
  1791. {
  1792. auto rule_start = push_start();
  1793. consume(TokenType::While);
  1794. consume(TokenType::ParenOpen);
  1795. auto test = parse_expression(0);
  1796. consume(TokenType::ParenClose);
  1797. TemporaryChange break_change(m_state.in_break_context, true);
  1798. TemporaryChange continue_change(m_state.in_continue_context, true);
  1799. auto body = parse_statement();
  1800. return create_ast_node<WhileStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(test), move(body));
  1801. }
  1802. NonnullRefPtr<SwitchStatement> Parser::parse_switch_statement()
  1803. {
  1804. auto rule_start = push_start();
  1805. consume(TokenType::Switch);
  1806. consume(TokenType::ParenOpen);
  1807. auto determinant = parse_expression(0);
  1808. consume(TokenType::ParenClose);
  1809. consume(TokenType::CurlyOpen);
  1810. NonnullRefPtrVector<SwitchCase> cases;
  1811. auto has_default = false;
  1812. while (match(TokenType::Case) || match(TokenType::Default)) {
  1813. if (match(TokenType::Default)) {
  1814. if (has_default)
  1815. syntax_error("Multiple 'default' clauses in switch statement");
  1816. has_default = true;
  1817. }
  1818. cases.append(parse_switch_case());
  1819. }
  1820. consume(TokenType::CurlyClose);
  1821. return create_ast_node<SwitchStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(determinant), move(cases));
  1822. }
  1823. NonnullRefPtr<WithStatement> Parser::parse_with_statement()
  1824. {
  1825. auto rule_start = push_start();
  1826. consume(TokenType::With);
  1827. consume(TokenType::ParenOpen);
  1828. auto object = parse_expression(0);
  1829. consume(TokenType::ParenClose);
  1830. auto body = parse_statement();
  1831. return create_ast_node<WithStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(object), move(body));
  1832. }
  1833. NonnullRefPtr<SwitchCase> Parser::parse_switch_case()
  1834. {
  1835. auto rule_start = push_start();
  1836. RefPtr<Expression> test;
  1837. if (consume().type() == TokenType::Case) {
  1838. test = parse_expression(0);
  1839. }
  1840. consume(TokenType::Colon);
  1841. NonnullRefPtrVector<Statement> consequent;
  1842. TemporaryChange break_change(m_state.in_break_context, true);
  1843. for (;;) {
  1844. if (match_declaration())
  1845. consequent.append(parse_declaration());
  1846. else if (match_statement())
  1847. consequent.append(parse_statement());
  1848. else
  1849. break;
  1850. }
  1851. return create_ast_node<SwitchCase>({ m_state.current_token.filename(), rule_start.position(), position() }, move(test), move(consequent));
  1852. }
  1853. NonnullRefPtr<CatchClause> Parser::parse_catch_clause()
  1854. {
  1855. auto rule_start = push_start();
  1856. consume(TokenType::Catch);
  1857. FlyString parameter;
  1858. RefPtr<BindingPattern> pattern_parameter;
  1859. auto should_expect_parameter = false;
  1860. if (match(TokenType::ParenOpen)) {
  1861. should_expect_parameter = true;
  1862. consume();
  1863. if (match_identifier_name())
  1864. parameter = consume().value();
  1865. else
  1866. pattern_parameter = parse_binding_pattern();
  1867. consume(TokenType::ParenClose);
  1868. }
  1869. if (should_expect_parameter && parameter.is_empty() && !pattern_parameter)
  1870. expected("an identifier or a binding pattern");
  1871. if (pattern_parameter)
  1872. pattern_parameter->for_each_bound_name([this](auto& name) { check_identifier_name_for_assignment_validity(name); });
  1873. if (!parameter.is_empty())
  1874. check_identifier_name_for_assignment_validity(parameter);
  1875. auto body = parse_block_statement();
  1876. if (pattern_parameter) {
  1877. return create_ast_node<CatchClause>(
  1878. { m_state.current_token.filename(), rule_start.position(), position() },
  1879. pattern_parameter.release_nonnull(),
  1880. move(body));
  1881. }
  1882. return create_ast_node<CatchClause>(
  1883. { m_state.current_token.filename(), rule_start.position(), position() },
  1884. move(parameter),
  1885. move(body));
  1886. }
  1887. NonnullRefPtr<IfStatement> Parser::parse_if_statement()
  1888. {
  1889. auto rule_start = push_start();
  1890. auto parse_function_declaration_as_block_statement = [&] {
  1891. // https://tc39.es/ecma262/#sec-functiondeclarations-in-ifstatement-statement-clauses
  1892. // Code matching this production is processed as if each matching occurrence of
  1893. // FunctionDeclaration[?Yield, ?Await, ~Default] was the sole StatementListItem
  1894. // of a BlockStatement occupying that position in the source code.
  1895. ScopePusher scope(*this, ScopePusher::Let, Parser::Scope::Block);
  1896. auto block = create_ast_node<BlockStatement>({ m_state.current_token.filename(), rule_start.position(), position() });
  1897. block->append(parse_declaration());
  1898. scope.add_to_scope_node(block);
  1899. return block;
  1900. };
  1901. consume(TokenType::If);
  1902. consume(TokenType::ParenOpen);
  1903. auto predicate = parse_expression(0);
  1904. consume(TokenType::ParenClose);
  1905. RefPtr<Statement> consequent;
  1906. if (!m_state.strict_mode && match(TokenType::Function))
  1907. consequent = parse_function_declaration_as_block_statement();
  1908. else
  1909. consequent = parse_statement();
  1910. RefPtr<Statement> alternate;
  1911. if (match(TokenType::Else)) {
  1912. consume();
  1913. if (!m_state.strict_mode && match(TokenType::Function))
  1914. alternate = parse_function_declaration_as_block_statement();
  1915. else
  1916. alternate = parse_statement();
  1917. }
  1918. return create_ast_node<IfStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(predicate), move(*consequent), move(alternate));
  1919. }
  1920. NonnullRefPtr<Statement> Parser::parse_for_statement()
  1921. {
  1922. auto rule_start = push_start();
  1923. auto match_for_in_of = [&]() {
  1924. return match(TokenType::In) || (match(TokenType::Identifier) && m_state.current_token.value() == "of");
  1925. };
  1926. consume(TokenType::For);
  1927. consume(TokenType::ParenOpen);
  1928. bool in_scope = false;
  1929. ScopeGuard guard([&]() {
  1930. if (in_scope)
  1931. m_state.let_scopes.take_last();
  1932. });
  1933. RefPtr<ASTNode> init;
  1934. if (!match(TokenType::Semicolon)) {
  1935. if (match_variable_declaration()) {
  1936. if (!match(TokenType::Var)) {
  1937. m_state.let_scopes.append(NonnullRefPtrVector<VariableDeclaration>());
  1938. in_scope = true;
  1939. }
  1940. init = parse_variable_declaration(true);
  1941. if (match_for_in_of())
  1942. return parse_for_in_of_statement(*init);
  1943. if (static_cast<VariableDeclaration&>(*init).declaration_kind() == DeclarationKind::Const) {
  1944. for (auto& declaration : static_cast<VariableDeclaration&>(*init).declarations()) {
  1945. if (!declaration.init())
  1946. syntax_error("Missing initializer in 'const' variable declaration");
  1947. }
  1948. }
  1949. } else if (match_expression()) {
  1950. init = parse_expression(0, Associativity::Right, { TokenType::In });
  1951. if (match_for_in_of())
  1952. return parse_for_in_of_statement(*init);
  1953. } else {
  1954. syntax_error("Unexpected token in for loop");
  1955. }
  1956. }
  1957. consume(TokenType::Semicolon);
  1958. RefPtr<Expression> test;
  1959. if (!match(TokenType::Semicolon))
  1960. test = parse_expression(0);
  1961. consume(TokenType::Semicolon);
  1962. RefPtr<Expression> update;
  1963. if (!match(TokenType::ParenClose))
  1964. update = parse_expression(0);
  1965. consume(TokenType::ParenClose);
  1966. TemporaryChange break_change(m_state.in_break_context, true);
  1967. TemporaryChange continue_change(m_state.in_continue_context, true);
  1968. auto body = parse_statement();
  1969. return create_ast_node<ForStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(init), move(test), move(update), move(body));
  1970. }
  1971. NonnullRefPtr<Statement> Parser::parse_for_in_of_statement(NonnullRefPtr<ASTNode> lhs)
  1972. {
  1973. auto rule_start = push_start();
  1974. if (is<VariableDeclaration>(*lhs)) {
  1975. auto declarations = static_cast<VariableDeclaration&>(*lhs).declarations();
  1976. if (declarations.size() > 1)
  1977. syntax_error("multiple declarations not allowed in for..in/of");
  1978. if (declarations.size() < 1)
  1979. syntax_error("need exactly one variable declaration in for..in/of");
  1980. else if (declarations.first().init() != nullptr)
  1981. syntax_error("variable initializer not allowed in for..in/of");
  1982. }
  1983. auto in_or_of = consume();
  1984. auto rhs = parse_expression(0);
  1985. consume(TokenType::ParenClose);
  1986. TemporaryChange break_change(m_state.in_break_context, true);
  1987. TemporaryChange continue_change(m_state.in_continue_context, true);
  1988. auto body = parse_statement();
  1989. if (in_or_of.type() == TokenType::In)
  1990. return create_ast_node<ForInStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(lhs), move(rhs), move(body));
  1991. return create_ast_node<ForOfStatement>({ m_state.current_token.filename(), rule_start.position(), position() }, move(lhs), move(rhs), move(body));
  1992. }
  1993. NonnullRefPtr<DebuggerStatement> Parser::parse_debugger_statement()
  1994. {
  1995. auto rule_start = push_start();
  1996. consume(TokenType::Debugger);
  1997. consume_or_insert_semicolon();
  1998. return create_ast_node<DebuggerStatement>({ m_state.current_token.filename(), rule_start.position(), position() });
  1999. }
  2000. bool Parser::match(TokenType type) const
  2001. {
  2002. return m_state.current_token.type() == type;
  2003. }
  2004. bool Parser::match_expression() const
  2005. {
  2006. auto type = m_state.current_token.type();
  2007. return type == TokenType::BoolLiteral
  2008. || type == TokenType::NumericLiteral
  2009. || type == TokenType::BigIntLiteral
  2010. || type == TokenType::StringLiteral
  2011. || type == TokenType::TemplateLiteralStart
  2012. || type == TokenType::NullLiteral
  2013. || match_identifier()
  2014. || type == TokenType::New
  2015. || type == TokenType::CurlyOpen
  2016. || type == TokenType::BracketOpen
  2017. || type == TokenType::ParenOpen
  2018. || type == TokenType::Function
  2019. || type == TokenType::This
  2020. || type == TokenType::Super
  2021. || type == TokenType::RegexLiteral
  2022. || type == TokenType::Yield
  2023. || match_unary_prefixed_expression();
  2024. }
  2025. bool Parser::match_unary_prefixed_expression() const
  2026. {
  2027. auto type = m_state.current_token.type();
  2028. return type == TokenType::PlusPlus
  2029. || type == TokenType::MinusMinus
  2030. || type == TokenType::ExclamationMark
  2031. || type == TokenType::Tilde
  2032. || type == TokenType::Plus
  2033. || type == TokenType::Minus
  2034. || type == TokenType::Typeof
  2035. || type == TokenType::Void
  2036. || type == TokenType::Delete;
  2037. }
  2038. bool Parser::match_secondary_expression(const Vector<TokenType>& forbidden) const
  2039. {
  2040. auto type = m_state.current_token.type();
  2041. if (forbidden.contains_slow(type))
  2042. return false;
  2043. return type == TokenType::Plus
  2044. || type == TokenType::PlusEquals
  2045. || type == TokenType::Minus
  2046. || type == TokenType::MinusEquals
  2047. || type == TokenType::Asterisk
  2048. || type == TokenType::AsteriskEquals
  2049. || type == TokenType::Slash
  2050. || type == TokenType::SlashEquals
  2051. || type == TokenType::Percent
  2052. || type == TokenType::PercentEquals
  2053. || type == TokenType::DoubleAsterisk
  2054. || type == TokenType::DoubleAsteriskEquals
  2055. || type == TokenType::Equals
  2056. || type == TokenType::EqualsEqualsEquals
  2057. || type == TokenType::ExclamationMarkEqualsEquals
  2058. || type == TokenType::EqualsEquals
  2059. || type == TokenType::ExclamationMarkEquals
  2060. || type == TokenType::GreaterThan
  2061. || type == TokenType::GreaterThanEquals
  2062. || type == TokenType::LessThan
  2063. || type == TokenType::LessThanEquals
  2064. || type == TokenType::ParenOpen
  2065. || type == TokenType::Period
  2066. || type == TokenType::BracketOpen
  2067. || type == TokenType::PlusPlus
  2068. || type == TokenType::MinusMinus
  2069. || type == TokenType::In
  2070. || type == TokenType::Instanceof
  2071. || type == TokenType::QuestionMark
  2072. || type == TokenType::Ampersand
  2073. || type == TokenType::AmpersandEquals
  2074. || type == TokenType::Pipe
  2075. || type == TokenType::PipeEquals
  2076. || type == TokenType::Caret
  2077. || type == TokenType::CaretEquals
  2078. || type == TokenType::ShiftLeft
  2079. || type == TokenType::ShiftLeftEquals
  2080. || type == TokenType::ShiftRight
  2081. || type == TokenType::ShiftRightEquals
  2082. || type == TokenType::UnsignedShiftRight
  2083. || type == TokenType::UnsignedShiftRightEquals
  2084. || type == TokenType::DoubleAmpersand
  2085. || type == TokenType::DoubleAmpersandEquals
  2086. || type == TokenType::DoublePipe
  2087. || type == TokenType::DoublePipeEquals
  2088. || type == TokenType::DoubleQuestionMark
  2089. || type == TokenType::DoubleQuestionMarkEquals;
  2090. }
  2091. bool Parser::match_statement() const
  2092. {
  2093. auto type = m_state.current_token.type();
  2094. return match_expression()
  2095. || type == TokenType::Return
  2096. || type == TokenType::Yield
  2097. || type == TokenType::Do
  2098. || type == TokenType::If
  2099. || type == TokenType::Throw
  2100. || type == TokenType::Try
  2101. || type == TokenType::While
  2102. || type == TokenType::With
  2103. || type == TokenType::For
  2104. || type == TokenType::CurlyOpen
  2105. || type == TokenType::Switch
  2106. || type == TokenType::Break
  2107. || type == TokenType::Continue
  2108. || type == TokenType::Var
  2109. || type == TokenType::Debugger
  2110. || type == TokenType::Semicolon;
  2111. }
  2112. bool Parser::match_declaration() const
  2113. {
  2114. auto type = m_state.current_token.type();
  2115. return type == TokenType::Function
  2116. || type == TokenType::Class
  2117. || type == TokenType::Const
  2118. || type == TokenType::Let;
  2119. }
  2120. bool Parser::match_variable_declaration() const
  2121. {
  2122. auto type = m_state.current_token.type();
  2123. return type == TokenType::Var
  2124. || type == TokenType::Let
  2125. || type == TokenType::Const;
  2126. }
  2127. bool Parser::match_identifier() const
  2128. {
  2129. return m_state.current_token.type() == TokenType::Identifier
  2130. || m_state.current_token.type() == TokenType::Let; // See note in Parser::parse_identifier().
  2131. }
  2132. bool Parser::match_identifier_name() const
  2133. {
  2134. return m_state.current_token.is_identifier_name();
  2135. }
  2136. bool Parser::match_property_key() const
  2137. {
  2138. auto type = m_state.current_token.type();
  2139. return match_identifier_name()
  2140. || type == TokenType::BracketOpen
  2141. || type == TokenType::StringLiteral
  2142. || type == TokenType::NumericLiteral
  2143. || type == TokenType::BigIntLiteral;
  2144. }
  2145. bool Parser::done() const
  2146. {
  2147. return match(TokenType::Eof);
  2148. }
  2149. Token Parser::consume()
  2150. {
  2151. auto old_token = m_state.current_token;
  2152. m_state.current_token = m_state.lexer.next();
  2153. return old_token;
  2154. }
  2155. void Parser::consume_or_insert_semicolon()
  2156. {
  2157. // Semicolon was found and will be consumed
  2158. if (match(TokenType::Semicolon)) {
  2159. consume();
  2160. return;
  2161. }
  2162. // Insert semicolon if...
  2163. // ...token is preceded by one or more newlines
  2164. if (m_state.current_token.trivia_contains_line_terminator())
  2165. return;
  2166. // ...token is a closing curly brace
  2167. if (match(TokenType::CurlyClose))
  2168. return;
  2169. // ...token is eof
  2170. if (match(TokenType::Eof))
  2171. return;
  2172. // No rule for semicolon insertion applies -> syntax error
  2173. expected("Semicolon");
  2174. }
  2175. static constexpr AK::Array<StringView, 9> strict_reserved_words = { "implements", "interface", "let", "package", "private", "protected", "public", "static", "yield" };
  2176. Token Parser::consume_identifier()
  2177. {
  2178. if (match(TokenType::Identifier))
  2179. return consume(TokenType::Identifier);
  2180. // Note that 'let' is not a reserved keyword, but our lexer considers it such
  2181. // As it's pretty nice to have that (for syntax highlighting and such), we'll
  2182. // special-case it here instead.
  2183. if (match(TokenType::Let)) {
  2184. if (m_state.strict_mode)
  2185. syntax_error("'let' is not allowed as an identifier in strict mode");
  2186. return consume();
  2187. }
  2188. expected("Identifier");
  2189. return consume();
  2190. }
  2191. // https://tc39.es/ecma262/#prod-IdentifierReference
  2192. Token Parser::consume_identifier_reference()
  2193. {
  2194. if (match(TokenType::Identifier))
  2195. return consume(TokenType::Identifier);
  2196. // See note in Parser::parse_identifier().
  2197. if (match(TokenType::Let)) {
  2198. if (m_state.strict_mode)
  2199. syntax_error("'let' is not allowed as an identifier in strict mode");
  2200. return consume();
  2201. }
  2202. if (match(TokenType::Yield)) {
  2203. if (m_state.strict_mode)
  2204. syntax_error("Identifier reference may not be 'yield' in strict mode");
  2205. return consume();
  2206. }
  2207. if (match(TokenType::Await)) {
  2208. syntax_error("Identifier reference may not be 'await'");
  2209. return consume();
  2210. }
  2211. expected(Token::name(TokenType::Identifier));
  2212. return consume();
  2213. }
  2214. Token Parser::consume(TokenType expected_type)
  2215. {
  2216. if (!match(expected_type)) {
  2217. expected(Token::name(expected_type));
  2218. }
  2219. auto token = consume();
  2220. if (expected_type == TokenType::Identifier) {
  2221. if (m_state.strict_mode && any_of(strict_reserved_words.begin(), strict_reserved_words.end(), [&](auto const& word) { return word == token.value(); }))
  2222. syntax_error("Identifier must not be a class-related reserved word in strict mode");
  2223. }
  2224. return token;
  2225. }
  2226. Token Parser::consume_and_validate_numeric_literal()
  2227. {
  2228. auto is_unprefixed_octal_number = [](const StringView& value) {
  2229. return value.length() > 1 && value[0] == '0' && is_ascii_digit(value[1]);
  2230. };
  2231. auto literal_start = position();
  2232. auto token = consume(TokenType::NumericLiteral);
  2233. if (m_state.strict_mode && is_unprefixed_octal_number(token.value()))
  2234. syntax_error("Unprefixed octal number not allowed in strict mode", literal_start);
  2235. if (match_identifier_name() && m_state.current_token.trivia().is_empty())
  2236. syntax_error("Numeric literal must not be immediately followed by identifier");
  2237. return token;
  2238. }
  2239. void Parser::expected(const char* what)
  2240. {
  2241. auto message = m_state.current_token.message();
  2242. if (message.is_empty())
  2243. message = String::formatted("Unexpected token {}. Expected {}", m_state.current_token.name(), what);
  2244. syntax_error(message);
  2245. }
  2246. Position Parser::position() const
  2247. {
  2248. return {
  2249. m_state.current_token.line_number(),
  2250. m_state.current_token.line_column(),
  2251. m_state.current_token.offset(),
  2252. };
  2253. }
  2254. bool Parser::try_parse_arrow_function_expression_failed_at_position(const Position& position) const
  2255. {
  2256. auto it = m_token_memoizations.find(position);
  2257. if (it == m_token_memoizations.end())
  2258. return false;
  2259. return (*it).value.try_parse_arrow_function_expression_failed;
  2260. }
  2261. void Parser::set_try_parse_arrow_function_expression_failed_at_position(const Position& position, bool failed)
  2262. {
  2263. m_token_memoizations.set(position, { failed });
  2264. }
  2265. void Parser::syntax_error(const String& message, Optional<Position> position)
  2266. {
  2267. if (!position.has_value())
  2268. position = this->position();
  2269. m_state.errors.append({ message, position });
  2270. }
  2271. void Parser::save_state()
  2272. {
  2273. m_saved_state.append(m_state);
  2274. }
  2275. void Parser::load_state()
  2276. {
  2277. VERIFY(!m_saved_state.is_empty());
  2278. m_state = m_saved_state.take_last();
  2279. }
  2280. void Parser::discard_saved_state()
  2281. {
  2282. m_saved_state.take_last();
  2283. }
  2284. void Parser::check_identifier_name_for_assignment_validity(StringView name)
  2285. {
  2286. if (any_of(s_reserved_words.begin(), s_reserved_words.end(), [&](auto& value) { return name == value; })) {
  2287. syntax_error("Binding pattern target may not be a reserved word");
  2288. } else if (m_state.strict_mode) {
  2289. if (name.is_one_of("arguments"sv, "eval"sv))
  2290. syntax_error("Binding pattern target may not be called 'arguments' or 'eval' in strict mode");
  2291. else if (name == "yield"sv)
  2292. syntax_error("Binding pattern target may not be called 'yield' in strict mode");
  2293. }
  2294. }
  2295. }