Parser.cpp 12 KB

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