Parser.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. default:
  65. m_has_errors = true;
  66. expected("statement (missing switch case)");
  67. consume();
  68. return make<ErrorStatement>();
  69. }
  70. }
  71. NonnullOwnPtr<Expression> Parser::parse_primary_expression()
  72. {
  73. switch (m_current_token.type()) {
  74. case TokenType::ParenOpen: {
  75. consume(TokenType::ParenOpen);
  76. auto expression = parse_expression();
  77. consume(TokenType::ParenClose);
  78. return expression;
  79. }
  80. case TokenType::Identifier:
  81. return make<Identifier>(consume().value());
  82. case TokenType::NumericLiteral:
  83. return make<NumericLiteral>(consume().double_value());
  84. case TokenType::BoolLiteral:
  85. return make<BooleanLiteral>(consume().bool_value());
  86. case TokenType::StringLiteral:
  87. return make<StringLiteral>(consume().string_value());
  88. case TokenType::CurlyOpen:
  89. return parse_object_expression();
  90. default:
  91. m_has_errors = true;
  92. expected("primary expression (missing switch case)");
  93. consume();
  94. return make<ErrorExpression>();
  95. }
  96. }
  97. NonnullOwnPtr<ObjectExpression> Parser::parse_object_expression()
  98. {
  99. // FIXME: Parse actual object expression
  100. consume(TokenType::CurlyOpen);
  101. consume(TokenType::CurlyClose);
  102. return make<ObjectExpression>();
  103. }
  104. NonnullOwnPtr<Expression> Parser::parse_expression()
  105. {
  106. auto expression = parse_primary_expression();
  107. while (match_secondary_expression()) {
  108. expression = parse_secondary_expression(move(expression));
  109. }
  110. return expression;
  111. }
  112. NonnullOwnPtr<Expression> Parser::parse_secondary_expression(NonnullOwnPtr<Expression> lhs)
  113. {
  114. switch (m_current_token.type()) {
  115. case TokenType::Plus:
  116. consume();
  117. return make<BinaryExpression>(BinaryOp::Plus, move(lhs), parse_expression());
  118. case TokenType::Minus:
  119. consume();
  120. return make<BinaryExpression>(BinaryOp::Minus, move(lhs), parse_expression());
  121. case TokenType::Asterisk:
  122. consume();
  123. return make<BinaryExpression>(BinaryOp::Asterisk, move(lhs), parse_expression());
  124. case TokenType::Slash:
  125. consume();
  126. return make<BinaryExpression>(BinaryOp::Slash, move(lhs), parse_expression());
  127. case TokenType::ParenOpen:
  128. return parse_call_expression(move(lhs));
  129. case TokenType::Equals:
  130. consume();
  131. return make<AssignmentExpression>(AssignmentOp::Assign, move(lhs), parse_expression());
  132. case TokenType::Period:
  133. consume();
  134. return make<MemberExpression>(move(lhs), parse_expression());
  135. default:
  136. m_has_errors = true;
  137. expected("secondary expression (missing switch case)");
  138. consume();
  139. return make<ErrorExpression>();
  140. }
  141. }
  142. NonnullOwnPtr<CallExpression> Parser::parse_call_expression(NonnullOwnPtr<Expression> lhs)
  143. {
  144. // FIXME: allow arguments
  145. consume(TokenType::ParenOpen);
  146. consume(TokenType::ParenClose);
  147. // FIXME: Allow lhs expression instead of just a string
  148. if (lhs->is_identifier()) {
  149. return make<CallExpression>(static_cast<Identifier*>(lhs.ptr())->string());
  150. }
  151. m_has_errors = true;
  152. return make<CallExpression>("***ERROR***");
  153. }
  154. NonnullOwnPtr<ReturnStatement> Parser::parse_return_statement()
  155. {
  156. consume(TokenType::Return);
  157. if (match_expression()) {
  158. return make<ReturnStatement>(parse_expression());
  159. }
  160. return make<ReturnStatement>(nullptr);
  161. }
  162. NonnullOwnPtr<BlockStatement> Parser::parse_block_statement()
  163. {
  164. auto block = make<BlockStatement>();
  165. consume(TokenType::CurlyOpen);
  166. while (!done() && !match(TokenType::CurlyClose)) {
  167. if (match(TokenType::Semicolon)) {
  168. consume();
  169. } else if (match_statement()) {
  170. block->append(parse_statement());
  171. } else {
  172. expected("statement");
  173. consume();
  174. }
  175. }
  176. consume(TokenType::CurlyClose);
  177. return block;
  178. }
  179. NonnullOwnPtr<FunctionDeclaration> Parser::parse_function_declaration()
  180. {
  181. consume(TokenType::Function);
  182. auto name = consume(TokenType::Identifier).value();
  183. consume(TokenType::ParenOpen);
  184. while (match(TokenType::Identifier)) {
  185. // FIXME: actually add parameters to function
  186. consume(TokenType::Identifier);
  187. if (match(TokenType::ParenClose)) {
  188. break;
  189. }
  190. consume(TokenType::Comma);
  191. }
  192. consume(TokenType::ParenClose);
  193. auto body = parse_block_statement();
  194. return make<FunctionDeclaration>(name, move(body));
  195. }
  196. NonnullOwnPtr<VariableDeclaration> Parser::parse_variable_declaration()
  197. {
  198. consume(TokenType::Var);
  199. auto name = consume(TokenType::Identifier).value();
  200. OwnPtr<Expression> initializer;
  201. if (match(TokenType::Equals)) {
  202. consume();
  203. initializer = parse_expression();
  204. }
  205. return make<VariableDeclaration>(make<Identifier>(name), move(initializer), DeclarationType::Var);
  206. }
  207. bool Parser::match(TokenType type) const
  208. {
  209. return m_current_token.type() == type;
  210. }
  211. bool Parser::match_expression() const
  212. {
  213. auto type = m_current_token.type();
  214. return type == TokenType::BoolLiteral
  215. || type == TokenType::NumericLiteral
  216. || type == TokenType::StringLiteral
  217. || type == TokenType::NullLiteral
  218. || type == TokenType::Identifier
  219. || type == TokenType::New
  220. || type == TokenType::CurlyOpen
  221. || type == TokenType::BracketOpen
  222. || type == TokenType::ParenOpen;
  223. }
  224. bool Parser::match_secondary_expression() const
  225. {
  226. auto type = m_current_token.type();
  227. return type == TokenType::Plus
  228. || type == TokenType::Minus
  229. || type == TokenType::Asterisk
  230. || type == TokenType::Slash
  231. || type == TokenType::Equals
  232. || type == TokenType::ParenOpen
  233. || type == TokenType::Period;
  234. }
  235. bool Parser::match_statement() const
  236. {
  237. auto type = m_current_token.type();
  238. return match_expression()
  239. || type == TokenType::Function
  240. || type == TokenType::Return
  241. || type == TokenType::Let
  242. || type == TokenType::Catch
  243. || type == TokenType::Class
  244. || type == TokenType::Delete
  245. || type == TokenType::Do
  246. || type == TokenType::If
  247. || type == TokenType::Try
  248. || type == TokenType::While
  249. || type == TokenType::Const
  250. || type == TokenType::CurlyOpen
  251. || type == TokenType::Var;
  252. }
  253. bool Parser::done() const
  254. {
  255. return match(TokenType::Eof);
  256. }
  257. Token Parser::consume()
  258. {
  259. auto oldToken = m_current_token;
  260. m_current_token = m_lexer.next();
  261. return oldToken;
  262. }
  263. Token Parser::consume(TokenType type)
  264. {
  265. if (m_current_token.type() != type) {
  266. m_has_errors = true;
  267. fprintf(stderr, "Error: Unexpected token %s. Expected %s\n", m_current_token.name(), Token::name(type));
  268. }
  269. return consume();
  270. }
  271. void Parser::expected(const char* what)
  272. {
  273. m_has_errors = true;
  274. fprintf(stderr, "Error: Unexpected token %s. Expected %s\n", m_current_token.name(), what);
  275. }
  276. }