Parser.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@gmx.de>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Parser.h"
  27. #include <AK/HashMap.h>
  28. #include <AK/StdLibExtras.h>
  29. #include <stdio.h>
  30. namespace JS {
  31. static HashMap<TokenType, int> g_operator_precedence;
  32. Parser::Parser(Lexer lexer)
  33. : m_lexer(move(lexer))
  34. , m_current_token(m_lexer.next())
  35. {
  36. if (g_operator_precedence.is_empty()) {
  37. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
  38. g_operator_precedence.set(TokenType::Period, 20);
  39. g_operator_precedence.set(TokenType::BracketOpen, 20);
  40. g_operator_precedence.set(TokenType::ParenOpen, 20);
  41. g_operator_precedence.set(TokenType::QuestionMarkPeriod, 20);
  42. g_operator_precedence.set(TokenType::New, 19);
  43. g_operator_precedence.set(TokenType::PlusPlus, 18);
  44. g_operator_precedence.set(TokenType::MinusMinus, 18);
  45. g_operator_precedence.set(TokenType::ExclamationMark, 17);
  46. g_operator_precedence.set(TokenType::Tilde, 17);
  47. g_operator_precedence.set(TokenType::Typeof, 17);
  48. g_operator_precedence.set(TokenType::Void, 17);
  49. g_operator_precedence.set(TokenType::Delete, 17);
  50. g_operator_precedence.set(TokenType::Await, 17);
  51. g_operator_precedence.set(TokenType::DoubleAsterisk, 16);
  52. g_operator_precedence.set(TokenType::Asterisk, 15);
  53. g_operator_precedence.set(TokenType::Slash, 15);
  54. g_operator_precedence.set(TokenType::Percent, 15);
  55. g_operator_precedence.set(TokenType::Plus, 14);
  56. g_operator_precedence.set(TokenType::Minus, 14);
  57. g_operator_precedence.set(TokenType::ShiftLeft, 13);
  58. g_operator_precedence.set(TokenType::ShiftRight, 13);
  59. g_operator_precedence.set(TokenType::UnsignedShiftRight, 13);
  60. g_operator_precedence.set(TokenType::LessThan, 12);
  61. g_operator_precedence.set(TokenType::LessThanEquals, 12);
  62. g_operator_precedence.set(TokenType::GreaterThan, 12);
  63. g_operator_precedence.set(TokenType::GreaterThanEquals, 12);
  64. g_operator_precedence.set(TokenType::In, 12);
  65. g_operator_precedence.set(TokenType::Instanceof, 12);
  66. g_operator_precedence.set(TokenType::EqualsEquals, 11);
  67. g_operator_precedence.set(TokenType::ExclamationMarkEquals, 11);
  68. g_operator_precedence.set(TokenType::EqualsEqualsEquals, 11);
  69. g_operator_precedence.set(TokenType::ExclamationMarkEqualsEquals, 11);
  70. g_operator_precedence.set(TokenType::Ampersand, 10);
  71. g_operator_precedence.set(TokenType::Caret, 9);
  72. g_operator_precedence.set(TokenType::Pipe, 8);
  73. g_operator_precedence.set(TokenType::DoubleQuestionMark, 7);
  74. g_operator_precedence.set(TokenType::DoubleAmpersand, 6);
  75. g_operator_precedence.set(TokenType::DoublePipe, 5);
  76. g_operator_precedence.set(TokenType::QuestionMark, 4);
  77. g_operator_precedence.set(TokenType::Equals, 3);
  78. g_operator_precedence.set(TokenType::PlusEquals, 3);
  79. g_operator_precedence.set(TokenType::MinusEquals, 3);
  80. g_operator_precedence.set(TokenType::AsteriskAsteriskEquals, 3);
  81. g_operator_precedence.set(TokenType::AsteriskEquals, 3);
  82. g_operator_precedence.set(TokenType::SlashEquals, 3);
  83. g_operator_precedence.set(TokenType::PercentEquals, 3);
  84. g_operator_precedence.set(TokenType::ShiftLeftEquals, 3);
  85. g_operator_precedence.set(TokenType::ShiftRightEquals, 3);
  86. g_operator_precedence.set(TokenType::UnsignedShiftRightEquals, 3);
  87. g_operator_precedence.set(TokenType::PipeEquals, 3);
  88. g_operator_precedence.set(TokenType::Yield, 2);
  89. g_operator_precedence.set(TokenType::Comma, 1);
  90. }
  91. }
  92. int Parser::operator_precedence(TokenType type) const
  93. {
  94. auto it = g_operator_precedence.find(type);
  95. if (it == g_operator_precedence.end()) {
  96. fprintf(stderr, "No precedence for operator %s\n", Token::name(type));
  97. ASSERT_NOT_REACHED();
  98. return -1;
  99. }
  100. return it->value;
  101. }
  102. Associativity Parser::operator_associativity(TokenType type) const
  103. {
  104. switch (type) {
  105. case TokenType::Period:
  106. case TokenType::BracketOpen:
  107. case TokenType::ParenOpen:
  108. case TokenType::QuestionMarkPeriod:
  109. case TokenType::Asterisk:
  110. case TokenType::Slash:
  111. case TokenType::Percent:
  112. case TokenType::Plus:
  113. case TokenType::Minus:
  114. case TokenType::ShiftLeft:
  115. case TokenType::ShiftRight:
  116. case TokenType::UnsignedShiftRight:
  117. case TokenType::LessThan:
  118. case TokenType::LessThanEquals:
  119. case TokenType::GreaterThan:
  120. case TokenType::GreaterThanEquals:
  121. case TokenType::In:
  122. case TokenType::Instanceof:
  123. case TokenType::EqualsEquals:
  124. case TokenType::ExclamationMarkEquals:
  125. case TokenType::EqualsEqualsEquals:
  126. case TokenType::ExclamationMarkEqualsEquals:
  127. case TokenType::Ampersand:
  128. case TokenType::Caret:
  129. case TokenType::Pipe:
  130. case TokenType::DoubleQuestionMark:
  131. case TokenType::DoubleAmpersand:
  132. case TokenType::DoublePipe:
  133. case TokenType::Comma:
  134. return Associativity::Left;
  135. default:
  136. return Associativity::Right;
  137. }
  138. }
  139. NonnullOwnPtr<Program> Parser::parse_program()
  140. {
  141. auto program = make<Program>();
  142. while (!done()) {
  143. if (match(TokenType::Semicolon)) {
  144. consume();
  145. } else if (match_statement()) {
  146. program->append(parse_statement());
  147. } else {
  148. expected("statement");
  149. consume();
  150. }
  151. }
  152. return program;
  153. }
  154. NonnullOwnPtr<Statement> Parser::parse_statement()
  155. {
  156. if (match_expression()) {
  157. return make<JS::ExpressionStatement>(parse_expression(0));
  158. }
  159. switch (m_current_token.type()) {
  160. case TokenType::Function:
  161. return parse_function_declaration();
  162. case TokenType::CurlyOpen:
  163. return parse_block_statement();
  164. case TokenType::Return:
  165. return parse_return_statement();
  166. case TokenType::Var:
  167. case TokenType::Let:
  168. case TokenType::Const:
  169. return parse_variable_declaration();
  170. case TokenType::For:
  171. return parse_for_statement();
  172. default:
  173. m_has_errors = true;
  174. expected("statement (missing switch case)");
  175. consume();
  176. return make<ErrorStatement>();
  177. }
  178. }
  179. NonnullOwnPtr<Expression> Parser::parse_primary_expression()
  180. {
  181. switch (m_current_token.type()) {
  182. case TokenType::ParenOpen: {
  183. consume(TokenType::ParenOpen);
  184. auto expression = parse_expression(0);
  185. consume(TokenType::ParenClose);
  186. return expression;
  187. }
  188. case TokenType::Identifier:
  189. return make<Identifier>(consume().value());
  190. case TokenType::NumericLiteral:
  191. return make<NumericLiteral>(consume().double_value());
  192. case TokenType::BoolLiteral:
  193. return make<BooleanLiteral>(consume().bool_value());
  194. case TokenType::StringLiteral:
  195. return make<StringLiteral>(consume().string_value());
  196. case TokenType::NullLiteral:
  197. consume();
  198. return make<NullLiteral>();
  199. case TokenType::UndefinedLiteral:
  200. consume();
  201. return make<UndefinedLiteral>();
  202. case TokenType::CurlyOpen:
  203. return parse_object_expression();
  204. default:
  205. m_has_errors = true;
  206. expected("primary expression (missing switch case)");
  207. consume();
  208. return make<ErrorExpression>();
  209. }
  210. }
  211. NonnullOwnPtr<Expression> Parser::parse_unary_prefixed_expression()
  212. {
  213. switch (m_current_token.type()) {
  214. case TokenType::PlusPlus:
  215. consume();
  216. return make<UpdateExpression>(UpdateOp::Increment, parse_primary_expression(), true);
  217. case TokenType::MinusMinus:
  218. consume();
  219. return make<UpdateExpression>(UpdateOp::Decrement, parse_primary_expression(), true);
  220. case TokenType::ExclamationMark:
  221. consume();
  222. return make<UnaryExpression>(UnaryOp::Not, parse_primary_expression());
  223. case TokenType::Tilde:
  224. consume();
  225. return make<UnaryExpression>(UnaryOp::BitwiseNot, parse_primary_expression());
  226. case TokenType::Typeof:
  227. consume();
  228. return make<UnaryExpression>(UnaryOp::Typeof, parse_primary_expression());
  229. default:
  230. m_has_errors = true;
  231. expected("primary expression (missing switch case)");
  232. consume();
  233. return make<ErrorExpression>();
  234. }
  235. }
  236. NonnullOwnPtr<ObjectExpression> Parser::parse_object_expression()
  237. {
  238. // FIXME: Parse actual object expression
  239. consume(TokenType::CurlyOpen);
  240. consume(TokenType::CurlyClose);
  241. return make<ObjectExpression>();
  242. }
  243. NonnullOwnPtr<Expression> Parser::parse_expression(int min_precedence, Associativity associativity)
  244. {
  245. if (match_unary_prefixed_expression())
  246. return parse_unary_prefixed_expression();
  247. auto expression = parse_primary_expression();
  248. while (match_secondary_expression()) {
  249. int new_precedence = operator_precedence(m_current_token.type());
  250. if (new_precedence < min_precedence)
  251. break;
  252. if (new_precedence == min_precedence && associativity == Associativity::Left)
  253. break;
  254. Associativity new_associativity = operator_associativity(m_current_token.type());
  255. expression = parse_secondary_expression(move(expression), new_precedence, new_associativity);
  256. }
  257. return expression;
  258. }
  259. NonnullOwnPtr<Expression> Parser::parse_secondary_expression(NonnullOwnPtr<Expression> lhs, int min_precedence, Associativity associativity)
  260. {
  261. switch (m_current_token.type()) {
  262. case TokenType::Plus:
  263. consume();
  264. return make<BinaryExpression>(BinaryOp::Plus, move(lhs), parse_expression(min_precedence, associativity));
  265. case TokenType::PlusEquals:
  266. consume();
  267. return make<AssignmentExpression>(AssignmentOp::AdditionAssignment, move(lhs), parse_expression(min_precedence, associativity));
  268. case TokenType::Minus:
  269. consume();
  270. return make<BinaryExpression>(BinaryOp::Minus, move(lhs), parse_expression(min_precedence, associativity));
  271. case TokenType::MinusEquals:
  272. consume();
  273. return make<AssignmentExpression>(AssignmentOp::SubtractionAssignment, move(lhs), parse_expression(min_precedence, associativity));
  274. case TokenType::Asterisk:
  275. consume();
  276. return make<BinaryExpression>(BinaryOp::Asterisk, move(lhs), parse_expression(min_precedence, associativity));
  277. case TokenType::AsteriskEquals:
  278. consume();
  279. return make<AssignmentExpression>(AssignmentOp::MultiplicationAssignment, move(lhs), parse_expression(min_precedence, associativity));
  280. case TokenType::Slash:
  281. consume();
  282. return make<BinaryExpression>(BinaryOp::Slash, move(lhs), parse_expression(min_precedence, associativity));
  283. case TokenType::SlashEquals:
  284. consume();
  285. return make<AssignmentExpression>(AssignmentOp::DivisionAssignment, move(lhs), parse_expression(min_precedence, associativity));
  286. case TokenType::GreaterThan:
  287. consume();
  288. return make<BinaryExpression>(BinaryOp::GreaterThan, move(lhs), parse_expression(min_precedence, associativity));
  289. case TokenType::GreaterThanEquals:
  290. consume();
  291. return make<BinaryExpression>(BinaryOp::GreaterThanEquals, move(lhs), parse_expression(min_precedence, associativity));
  292. case TokenType::LessThan:
  293. consume();
  294. return make<BinaryExpression>(BinaryOp::LessThan, move(lhs), parse_expression(min_precedence, associativity));
  295. case TokenType::LessThanEquals:
  296. consume();
  297. return make<BinaryExpression>(BinaryOp::LessThanEquals, move(lhs), parse_expression(min_precedence, associativity));
  298. case TokenType::EqualsEqualsEquals:
  299. consume();
  300. return make<BinaryExpression>(BinaryOp::TypedEquals, move(lhs), parse_expression(min_precedence, associativity));
  301. case TokenType::ExclamationMarkEqualsEquals:
  302. consume();
  303. return make<BinaryExpression>(BinaryOp::TypedInequals, move(lhs), parse_expression(min_precedence, associativity));
  304. case TokenType::EqualsEquals:
  305. consume();
  306. return make<BinaryExpression>(BinaryOp::AbstractEquals, move(lhs), parse_expression(min_precedence, associativity));
  307. case TokenType::ExclamationMarkEquals:
  308. consume();
  309. return make<BinaryExpression>(BinaryOp::AbstractInequals, move(lhs), parse_expression(min_precedence, associativity));
  310. case TokenType::ParenOpen:
  311. return parse_call_expression(move(lhs));
  312. case TokenType::Equals:
  313. consume();
  314. return make<AssignmentExpression>(AssignmentOp::Assignment, move(lhs), parse_expression(min_precedence, associativity));
  315. case TokenType::Period:
  316. consume();
  317. return make<MemberExpression>(move(lhs), parse_expression(min_precedence, associativity));
  318. case TokenType::PlusPlus:
  319. consume();
  320. return make<UpdateExpression>(UpdateOp::Increment, move(lhs));
  321. case TokenType::MinusMinus:
  322. consume();
  323. return make<UpdateExpression>(UpdateOp::Decrement, move(lhs));
  324. case TokenType::DoubleAmpersand:
  325. consume();
  326. return make<LogicalExpression>(LogicalOp::And, move(lhs), parse_expression(min_precedence, associativity));
  327. case TokenType::DoublePipe:
  328. consume();
  329. return make<LogicalExpression>(LogicalOp::Or, move(lhs), parse_expression(min_precedence, associativity));
  330. default:
  331. m_has_errors = true;
  332. expected("secondary expression (missing switch case)");
  333. consume();
  334. return make<ErrorExpression>();
  335. }
  336. }
  337. NonnullOwnPtr<CallExpression> Parser::parse_call_expression(NonnullOwnPtr<Expression> lhs)
  338. {
  339. consume(TokenType::ParenOpen);
  340. NonnullOwnPtrVector<Expression> arguments;
  341. while (match_expression()) {
  342. arguments.append(parse_expression(0));
  343. if (!match(TokenType::Comma))
  344. break;
  345. consume();
  346. }
  347. consume(TokenType::ParenClose);
  348. return make<CallExpression>(move(lhs), move(arguments));
  349. }
  350. NonnullOwnPtr<ReturnStatement> Parser::parse_return_statement()
  351. {
  352. consume(TokenType::Return);
  353. if (match_expression()) {
  354. return make<ReturnStatement>(parse_expression(0));
  355. }
  356. return make<ReturnStatement>(nullptr);
  357. }
  358. NonnullOwnPtr<BlockStatement> Parser::parse_block_statement()
  359. {
  360. auto block = make<BlockStatement>();
  361. consume(TokenType::CurlyOpen);
  362. while (!done() && !match(TokenType::CurlyClose)) {
  363. if (match(TokenType::Semicolon)) {
  364. consume();
  365. } else if (match_statement()) {
  366. block->append(parse_statement());
  367. } else {
  368. expected("statement");
  369. consume();
  370. }
  371. }
  372. consume(TokenType::CurlyClose);
  373. return block;
  374. }
  375. NonnullOwnPtr<FunctionDeclaration> Parser::parse_function_declaration()
  376. {
  377. consume(TokenType::Function);
  378. auto name = consume(TokenType::Identifier).value();
  379. consume(TokenType::ParenOpen);
  380. Vector<String> parameters;
  381. while (match(TokenType::Identifier)) {
  382. auto parameter = consume(TokenType::Identifier).value();
  383. parameters.append(parameter);
  384. if (match(TokenType::ParenClose)) {
  385. break;
  386. }
  387. consume(TokenType::Comma);
  388. }
  389. consume(TokenType::ParenClose);
  390. auto body = parse_block_statement();
  391. return make<FunctionDeclaration>(name, move(body), move(parameters));
  392. }
  393. NonnullOwnPtr<VariableDeclaration> Parser::parse_variable_declaration()
  394. {
  395. DeclarationType declaration_type;
  396. switch (m_current_token.type()) {
  397. case TokenType::Var:
  398. declaration_type = DeclarationType::Var;
  399. consume(TokenType::Var);
  400. break;
  401. case TokenType::Let:
  402. declaration_type = DeclarationType::Let;
  403. consume(TokenType::Let);
  404. break;
  405. case TokenType::Const:
  406. declaration_type = DeclarationType::Const;
  407. consume(TokenType::Const);
  408. break;
  409. default:
  410. ASSERT_NOT_REACHED();
  411. }
  412. auto name = consume(TokenType::Identifier).value();
  413. OwnPtr<Expression> initializer;
  414. if (match(TokenType::Equals)) {
  415. consume();
  416. initializer = parse_expression(0);
  417. }
  418. return make<VariableDeclaration>(make<Identifier>(name), move(initializer), declaration_type);
  419. }
  420. NonnullOwnPtr<ForStatement> Parser::parse_for_statement()
  421. {
  422. consume(TokenType::For);
  423. consume(TokenType::ParenOpen);
  424. OwnPtr<Statement> init = nullptr;
  425. switch (m_current_token.type()) {
  426. case TokenType::Semicolon:
  427. break;
  428. default:
  429. init = parse_statement();
  430. break;
  431. }
  432. consume(TokenType::Semicolon);
  433. OwnPtr<Expression> test = nullptr;
  434. switch (m_current_token.type()) {
  435. case TokenType::Semicolon:
  436. break;
  437. default:
  438. test = parse_expression(0);
  439. break;
  440. }
  441. consume(TokenType::Semicolon);
  442. OwnPtr<Expression> update = nullptr;
  443. switch (m_current_token.type()) {
  444. case TokenType::Semicolon:
  445. break;
  446. default:
  447. update = parse_expression(0);
  448. break;
  449. }
  450. consume(TokenType::ParenClose);
  451. auto body = parse_block_statement();
  452. return make<ForStatement>(move(init), move(test), move(update), move(body));
  453. }
  454. bool Parser::match(TokenType type) const
  455. {
  456. return m_current_token.type() == type;
  457. }
  458. bool Parser::match_expression() const
  459. {
  460. auto type = m_current_token.type();
  461. return type == TokenType::BoolLiteral
  462. || type == TokenType::NumericLiteral
  463. || type == TokenType::StringLiteral
  464. || type == TokenType::UndefinedLiteral
  465. || type == TokenType::NullLiteral
  466. || type == TokenType::Identifier
  467. || type == TokenType::New
  468. || type == TokenType::CurlyOpen
  469. || type == TokenType::BracketOpen
  470. || type == TokenType::ParenOpen
  471. || match_unary_prefixed_expression();
  472. }
  473. bool Parser::match_unary_prefixed_expression() const
  474. {
  475. auto type = m_current_token.type();
  476. return type == TokenType::PlusPlus
  477. || type == TokenType::MinusMinus
  478. || type == TokenType::ExclamationMark
  479. || type == TokenType::Tilde
  480. || type == TokenType::Typeof;
  481. }
  482. bool Parser::match_secondary_expression() const
  483. {
  484. auto type = m_current_token.type();
  485. return type == TokenType::Plus
  486. || type == TokenType::PlusEquals
  487. || type == TokenType::Minus
  488. || type == TokenType::MinusEquals
  489. || type == TokenType::Asterisk
  490. || type == TokenType::AsteriskEquals
  491. || type == TokenType::Slash
  492. || type == TokenType::SlashEquals
  493. || type == TokenType::Equals
  494. || type == TokenType::EqualsEqualsEquals
  495. || type == TokenType::ExclamationMarkEqualsEquals
  496. || type == TokenType::EqualsEquals
  497. || type == TokenType::ExclamationMarkEquals
  498. || type == TokenType::GreaterThan
  499. || type == TokenType::GreaterThanEquals
  500. || type == TokenType::LessThan
  501. || type == TokenType::LessThanEquals
  502. || type == TokenType::ParenOpen
  503. || type == TokenType::Period
  504. || type == TokenType::PlusPlus
  505. || type == TokenType::MinusMinus;
  506. }
  507. bool Parser::match_statement() const
  508. {
  509. auto type = m_current_token.type();
  510. return match_expression()
  511. || type == TokenType::Function
  512. || type == TokenType::Return
  513. || type == TokenType::Let
  514. || type == TokenType::Catch
  515. || type == TokenType::Class
  516. || type == TokenType::Delete
  517. || type == TokenType::Do
  518. || type == TokenType::If
  519. || type == TokenType::Try
  520. || type == TokenType::While
  521. || type == TokenType::For
  522. || type == TokenType::Const
  523. || type == TokenType::CurlyOpen
  524. || type == TokenType::Var;
  525. }
  526. bool Parser::done() const
  527. {
  528. return match(TokenType::Eof);
  529. }
  530. Token Parser::consume()
  531. {
  532. auto old_token = m_current_token;
  533. m_current_token = m_lexer.next();
  534. return old_token;
  535. }
  536. Token Parser::consume(TokenType type)
  537. {
  538. if (m_current_token.type() != type) {
  539. m_has_errors = true;
  540. fprintf(stderr, "Error: Unexpected token %s. Expected %s\n", m_current_token.name(), Token::name(type));
  541. }
  542. return consume();
  543. }
  544. void Parser::expected(const char* what)
  545. {
  546. m_has_errors = true;
  547. fprintf(stderr, "Error: Unexpected token %s. Expected %s\n", m_current_token.name(), what);
  548. }
  549. }