Parser.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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/StdLibExtras.h>
  28. #include <stdio.h>
  29. namespace JS {
  30. Parser::Parser(Lexer lexer)
  31. : m_lexer(move(lexer))
  32. , m_current_token(m_lexer.next())
  33. {
  34. }
  35. NonnullOwnPtr<Program> Parser::parse_program()
  36. {
  37. auto program = make<Program>();
  38. while (!done()) {
  39. if (match(TokenType::Semicolon)) {
  40. consume();
  41. } else if (match_statement()) {
  42. program->append(parse_statement());
  43. } else {
  44. expected("statement");
  45. consume();
  46. }
  47. }
  48. return program;
  49. }
  50. NonnullOwnPtr<Statement> Parser::parse_statement()
  51. {
  52. if (match_expression()) {
  53. return make<JS::ExpressionStatement>(parse_expression());
  54. }
  55. switch (m_current_token.type()) {
  56. case TokenType::Function:
  57. return parse_function_declaration();
  58. case TokenType::CurlyOpen:
  59. return parse_block_statement();
  60. case TokenType::Return:
  61. return parse_return_statement();
  62. case TokenType::Var:
  63. case TokenType::Let:
  64. case TokenType::Const:
  65. return parse_variable_declaration();
  66. case TokenType::For:
  67. return parse_for_statement();
  68. default:
  69. m_has_errors = true;
  70. expected("statement (missing switch case)");
  71. consume();
  72. return make<ErrorStatement>();
  73. }
  74. }
  75. NonnullOwnPtr<Expression> Parser::parse_primary_expression()
  76. {
  77. switch (m_current_token.type()) {
  78. case TokenType::ParenOpen: {
  79. consume(TokenType::ParenOpen);
  80. auto expression = parse_expression();
  81. consume(TokenType::ParenClose);
  82. return expression;
  83. }
  84. case TokenType::Identifier:
  85. return make<Identifier>(consume().value());
  86. case TokenType::NumericLiteral:
  87. return make<NumericLiteral>(consume().double_value());
  88. case TokenType::BoolLiteral:
  89. return make<BooleanLiteral>(consume().bool_value());
  90. case TokenType::StringLiteral:
  91. return make<StringLiteral>(consume().string_value());
  92. case TokenType::CurlyOpen:
  93. return parse_object_expression();
  94. default:
  95. m_has_errors = true;
  96. expected("primary expression (missing switch case)");
  97. consume();
  98. return make<ErrorExpression>();
  99. }
  100. }
  101. NonnullOwnPtr<ObjectExpression> Parser::parse_object_expression()
  102. {
  103. // FIXME: Parse actual object expression
  104. consume(TokenType::CurlyOpen);
  105. consume(TokenType::CurlyClose);
  106. return make<ObjectExpression>();
  107. }
  108. NonnullOwnPtr<Expression> Parser::parse_expression()
  109. {
  110. auto expression = parse_primary_expression();
  111. while (match_secondary_expression()) {
  112. expression = parse_secondary_expression(move(expression));
  113. }
  114. return expression;
  115. }
  116. NonnullOwnPtr<Expression> Parser::parse_secondary_expression(NonnullOwnPtr<Expression> lhs)
  117. {
  118. switch (m_current_token.type()) {
  119. case TokenType::Plus:
  120. consume();
  121. return make<BinaryExpression>(BinaryOp::Plus, move(lhs), parse_expression());
  122. case TokenType::PlusEquals:
  123. consume();
  124. return make<AssignmentExpression>(AssignmentOp::AdditionAssignment, move(lhs), parse_expression());
  125. case TokenType::Minus:
  126. consume();
  127. return make<BinaryExpression>(BinaryOp::Minus, move(lhs), parse_expression());
  128. case TokenType::MinusEquals:
  129. consume();
  130. return make<AssignmentExpression>(AssignmentOp::SubtractionAssignment, move(lhs), parse_expression());
  131. case TokenType::Asterisk:
  132. consume();
  133. return make<BinaryExpression>(BinaryOp::Asterisk, move(lhs), parse_expression());
  134. case TokenType::AsteriskEquals:
  135. consume();
  136. return make<AssignmentExpression>(AssignmentOp::MultiplicationAssignment, move(lhs), parse_expression());
  137. case TokenType::Slash:
  138. consume();
  139. return make<BinaryExpression>(BinaryOp::Slash, move(lhs), parse_expression());
  140. case TokenType::SlashEquals:
  141. consume();
  142. return make<AssignmentExpression>(AssignmentOp::DivisionAssignment, move(lhs), parse_expression());
  143. case TokenType::GreaterThan:
  144. consume();
  145. return make<BinaryExpression>(BinaryOp::GreaterThan, move(lhs), parse_expression());
  146. case TokenType::GreaterThanEquals:
  147. consume();
  148. return make<BinaryExpression>(BinaryOp::GreaterThanEquals, move(lhs), parse_expression());
  149. case TokenType::LessThan:
  150. consume();
  151. return make<BinaryExpression>(BinaryOp::LessThan, move(lhs), parse_expression());
  152. case TokenType::LessThanEquals:
  153. consume();
  154. return make<BinaryExpression>(BinaryOp::LessThanEquals, move(lhs), parse_expression());
  155. case TokenType::EqualsEqualsEquals:
  156. consume();
  157. return make<BinaryExpression>(BinaryOp::TypedEquals, move(lhs), parse_expression());
  158. case TokenType::ExclamationMarkEqualsEquals:
  159. consume();
  160. return make<BinaryExpression>(BinaryOp::TypedInequals, move(lhs), parse_expression());
  161. case TokenType::ParenOpen:
  162. return parse_call_expression(move(lhs));
  163. case TokenType::Equals:
  164. consume();
  165. return make<AssignmentExpression>(AssignmentOp::Assignment, move(lhs), parse_expression());
  166. case TokenType::Period:
  167. consume();
  168. return make<MemberExpression>(move(lhs), parse_expression());
  169. case TokenType::PlusPlus:
  170. consume();
  171. return make<UpdateExpression>(UpdateOp::Increment, move(lhs));
  172. case TokenType::MinusMinus:
  173. consume();
  174. return make<UpdateExpression>(UpdateOp::Decrement, move(lhs));
  175. default:
  176. m_has_errors = true;
  177. expected("secondary expression (missing switch case)");
  178. consume();
  179. return make<ErrorExpression>();
  180. }
  181. }
  182. NonnullOwnPtr<CallExpression> Parser::parse_call_expression(NonnullOwnPtr<Expression> lhs)
  183. {
  184. // FIXME: allow arguments
  185. consume(TokenType::ParenOpen);
  186. consume(TokenType::ParenClose);
  187. // FIXME: Allow lhs expression instead of just a string
  188. if (lhs->is_identifier()) {
  189. return make<CallExpression>(static_cast<Identifier*>(lhs.ptr())->string());
  190. }
  191. m_has_errors = true;
  192. return make<CallExpression>("***ERROR***");
  193. }
  194. NonnullOwnPtr<ReturnStatement> Parser::parse_return_statement()
  195. {
  196. consume(TokenType::Return);
  197. if (match_expression()) {
  198. return make<ReturnStatement>(parse_expression());
  199. }
  200. return make<ReturnStatement>(nullptr);
  201. }
  202. NonnullOwnPtr<BlockStatement> Parser::parse_block_statement()
  203. {
  204. auto block = make<BlockStatement>();
  205. consume(TokenType::CurlyOpen);
  206. while (!done() && !match(TokenType::CurlyClose)) {
  207. if (match(TokenType::Semicolon)) {
  208. consume();
  209. } else if (match_statement()) {
  210. block->append(parse_statement());
  211. } else {
  212. expected("statement");
  213. consume();
  214. }
  215. }
  216. consume(TokenType::CurlyClose);
  217. return block;
  218. }
  219. NonnullOwnPtr<FunctionDeclaration> Parser::parse_function_declaration()
  220. {
  221. consume(TokenType::Function);
  222. auto name = consume(TokenType::Identifier).value();
  223. consume(TokenType::ParenOpen);
  224. while (match(TokenType::Identifier)) {
  225. // FIXME: actually add parameters to function
  226. consume(TokenType::Identifier);
  227. if (match(TokenType::ParenClose)) {
  228. break;
  229. }
  230. consume(TokenType::Comma);
  231. }
  232. consume(TokenType::ParenClose);
  233. auto body = parse_block_statement();
  234. return make<FunctionDeclaration>(name, move(body));
  235. }
  236. NonnullOwnPtr<VariableDeclaration> Parser::parse_variable_declaration()
  237. {
  238. DeclarationType declaration_type;
  239. switch (m_current_token.type()) {
  240. case TokenType::Var:
  241. declaration_type = DeclarationType::Var;
  242. consume(TokenType::Var);
  243. break;
  244. case TokenType::Let:
  245. declaration_type = DeclarationType::Let;
  246. consume(TokenType::Let);
  247. break;
  248. case TokenType::Const:
  249. declaration_type = DeclarationType::Const;
  250. consume(TokenType::Const);
  251. break;
  252. default:
  253. ASSERT_NOT_REACHED();
  254. }
  255. auto name = consume(TokenType::Identifier).value();
  256. OwnPtr<Expression> initializer;
  257. if (match(TokenType::Equals)) {
  258. consume();
  259. initializer = parse_expression();
  260. }
  261. return make<VariableDeclaration>(make<Identifier>(name), move(initializer), declaration_type);
  262. }
  263. NonnullOwnPtr<ForStatement> Parser::parse_for_statement()
  264. {
  265. consume(TokenType::For);
  266. consume(TokenType::ParenOpen);
  267. OwnPtr<Statement> init = nullptr;
  268. switch (m_current_token.type()) {
  269. case TokenType::Var:
  270. init = parse_variable_declaration();
  271. break;
  272. case TokenType::Semicolon:
  273. break;
  274. default:
  275. init = parse_statement();
  276. break;
  277. }
  278. consume(TokenType::Semicolon);
  279. OwnPtr<Expression> test = nullptr;
  280. switch (m_current_token.type()) {
  281. case TokenType::Semicolon:
  282. break;
  283. default:
  284. test = parse_expression();
  285. break;
  286. }
  287. consume(TokenType::Semicolon);
  288. OwnPtr<Expression> update = nullptr;
  289. switch (m_current_token.type()) {
  290. case TokenType::Semicolon:
  291. break;
  292. default:
  293. update = parse_expression();
  294. break;
  295. }
  296. consume(TokenType::ParenClose);
  297. auto body = parse_block_statement();
  298. return make<ForStatement>(move(init), move(test), move(update), move(body));
  299. }
  300. bool Parser::match(TokenType type) const
  301. {
  302. return m_current_token.type() == type;
  303. }
  304. bool Parser::match_expression() const
  305. {
  306. auto type = m_current_token.type();
  307. return type == TokenType::BoolLiteral
  308. || type == TokenType::NumericLiteral
  309. || type == TokenType::StringLiteral
  310. || type == TokenType::NullLiteral
  311. || type == TokenType::Identifier
  312. || type == TokenType::New
  313. || type == TokenType::CurlyOpen
  314. || type == TokenType::BracketOpen
  315. || type == TokenType::ParenOpen;
  316. }
  317. bool Parser::match_secondary_expression() const
  318. {
  319. auto type = m_current_token.type();
  320. return type == TokenType::Plus
  321. || type == TokenType::PlusEquals
  322. || type == TokenType::Minus
  323. || type == TokenType::MinusEquals
  324. || type == TokenType::Asterisk
  325. || type == TokenType::AsteriskEquals
  326. || type == TokenType::Slash
  327. || type == TokenType::SlashEquals
  328. || type == TokenType::Equals
  329. || type == TokenType::EqualsEqualsEquals
  330. || type == TokenType::ExclamationMarkEqualsEquals
  331. || type == TokenType::GreaterThan
  332. || type == TokenType::GreaterThanEquals
  333. || type == TokenType::LessThan
  334. || type == TokenType::LessThanEquals
  335. || type == TokenType::ParenOpen
  336. || type == TokenType::Period
  337. || type == TokenType::PlusPlus
  338. || type == TokenType::MinusMinus;
  339. }
  340. bool Parser::match_statement() const
  341. {
  342. auto type = m_current_token.type();
  343. return match_expression()
  344. || type == TokenType::Function
  345. || type == TokenType::Return
  346. || type == TokenType::Let
  347. || type == TokenType::Catch
  348. || type == TokenType::Class
  349. || type == TokenType::Delete
  350. || type == TokenType::Do
  351. || type == TokenType::If
  352. || type == TokenType::Try
  353. || type == TokenType::While
  354. || type == TokenType::For
  355. || type == TokenType::Const
  356. || type == TokenType::CurlyOpen
  357. || type == TokenType::Var;
  358. }
  359. bool Parser::done() const
  360. {
  361. return match(TokenType::Eof);
  362. }
  363. Token Parser::consume()
  364. {
  365. auto oldToken = m_current_token;
  366. m_current_token = m_lexer.next();
  367. return oldToken;
  368. }
  369. Token Parser::consume(TokenType type)
  370. {
  371. if (m_current_token.type() != type) {
  372. m_has_errors = true;
  373. fprintf(stderr, "Error: Unexpected token %s. Expected %s\n", m_current_token.name(), Token::name(type));
  374. }
  375. return consume();
  376. }
  377. void Parser::expected(const char* what)
  378. {
  379. m_has_errors = true;
  380. fprintf(stderr, "Error: Unexpected token %s. Expected %s\n", m_current_token.name(), what);
  381. }
  382. }