/* * Copyright (c) 2020, Stephan Unverwerth * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "Parser.h" #include #include namespace JS { Parser::Parser(Lexer lexer) : m_lexer(move(lexer)) , m_current_token(m_lexer.next()) { } NonnullOwnPtr Parser::parse_program() { auto program = make(); while (!done()) { if (match(TokenType::Semicolon)) { consume(); } else if (match_statement()) { program->append(parse_statement()); } else { expected("statement"); consume(); } } return program; } NonnullOwnPtr Parser::parse_statement() { if (match_expression()) { return make(parse_expression()); } switch (m_current_token.type()) { case TokenType::Function: return parse_function_declaration(); case TokenType::CurlyOpen: return parse_block_statement(); case TokenType::Return: return parse_return_statement(); case TokenType::Var: return parse_variable_declaration(); default: m_has_errors = true; expected("statement (missing switch case)"); consume(); return make(); } } NonnullOwnPtr Parser::parse_primary_expression() { switch (m_current_token.type()) { case TokenType::ParenOpen: { consume(TokenType::ParenOpen); auto expression = parse_expression(); consume(TokenType::ParenClose); return expression; } case TokenType::Identifier: return make(consume().value()); case TokenType::NumericLiteral: return make(consume().double_value()); case TokenType::BoolLiteral: return make(consume().bool_value()); case TokenType::CurlyOpen: return parse_object_expression(); default: m_has_errors = true; expected("primary expression (missing switch case)"); consume(); return make(); } } NonnullOwnPtr Parser::parse_object_expression() { // FIXME: Parse actual object expression consume(TokenType::CurlyOpen); consume(TokenType::CurlyClose); return make(); } NonnullOwnPtr Parser::parse_expression() { auto expression = parse_primary_expression(); while (match_secondary_expression()) { expression = parse_secondary_expression(move(expression)); } return expression; } NonnullOwnPtr Parser::parse_secondary_expression(NonnullOwnPtr lhs) { switch (m_current_token.type()) { case TokenType::Plus: consume(); return make(BinaryOp::Plus, move(lhs), parse_expression()); case TokenType::Minus: consume(); return make(BinaryOp::Minus, move(lhs), parse_expression()); case TokenType::ParenOpen: return parse_call_expression(move(lhs)); case TokenType::Equals: consume(); return make(AssignmentOp::Assign, move(lhs), parse_expression()); default: m_has_errors = true; expected("secondary expression (missing switch case)"); consume(); return make(); } } NonnullOwnPtr Parser::parse_call_expression(NonnullOwnPtr lhs) { // FIXME: allow arguments consume(TokenType::ParenOpen); consume(TokenType::ParenClose); // FIXME: Allow lhs expression instead of just a string if (lhs->is_identifier()) { return make(static_cast(lhs.ptr())->string()); } m_has_errors = true; return make("***ERROR***"); } NonnullOwnPtr Parser::parse_return_statement() { consume(TokenType::Return); if (match_expression()) { return make(parse_expression()); } return make(nullptr); } NonnullOwnPtr Parser::parse_block_statement() { auto block = make(); consume(TokenType::CurlyOpen); while (!done() && !match(TokenType::CurlyClose)) { if (match(TokenType::Semicolon)) { consume(); } else if (match_statement()) { block->append(parse_statement()); } else { expected("statement"); consume(); } } consume(TokenType::CurlyClose); return block; } NonnullOwnPtr Parser::parse_function_declaration() { consume(TokenType::Function); auto name = consume(TokenType::Identifier).value(); consume(TokenType::ParenOpen); while (match(TokenType::Identifier)) { // FIXME: actually add parameters to function consume(TokenType::Identifier); if (match(TokenType::ParenClose)) { break; } consume(TokenType::Comma); } consume(TokenType::ParenClose); auto body = parse_block_statement(); return make(name, move(body)); } NonnullOwnPtr Parser::parse_variable_declaration() { consume(TokenType::Var); auto name = consume(TokenType::Identifier).value(); OwnPtr initializer; if (match(TokenType::Equals)) { consume(); initializer = parse_expression(); } return make(make(name), move(initializer), DeclarationType::Var); } bool Parser::match(TokenType type) const { return m_current_token.type() == type; } bool Parser::match_expression() const { auto type = m_current_token.type(); return type == TokenType::BoolLiteral || type == TokenType::NumericLiteral || type == TokenType::StringLiteral || type == TokenType::NullLiteral || type == TokenType::Identifier || type == TokenType::New || type == TokenType::CurlyOpen || type == TokenType::BracketOpen || type == TokenType::ParenOpen; } bool Parser::match_secondary_expression() const { auto type = m_current_token.type(); return type == TokenType::Plus || type == TokenType::Minus || type == TokenType::Asterisk || type == TokenType::Slash || type == TokenType::Equals || type == TokenType::ParenOpen; } bool Parser::match_statement() const { auto type = m_current_token.type(); return match_expression() || type == TokenType::Function || type == TokenType::Return || type == TokenType::Let || type == TokenType::Catch || type == TokenType::Class || type == TokenType::Delete || type == TokenType::Do || type == TokenType::If || type == TokenType::Try || type == TokenType::While || type == TokenType::Const || type == TokenType::CurlyOpen || type == TokenType::Var; } bool Parser::done() const { return match(TokenType::Eof); } Token Parser::consume() { auto oldToken = m_current_token; m_current_token = m_lexer.next(); return oldToken; } Token Parser::consume(TokenType type) { if (m_current_token.type() != type) { m_has_errors = true; fprintf(stderr, "Error: Unexpected token %s. Expected %s\n", m_current_token.name(), Token::name(type)); } return consume(); } void Parser::expected(const char* what) { m_has_errors = true; fprintf(stderr, "Error: Unexpected token %s. Expected %s\n", m_current_token.name(), what); } }