// Copyright 2017 the Monicelli project authors. All rights reserved. // Use of this source code is governed by a GPLv3 license, see LICENSE.txt. #ifndef MONICELLI_PARSER_H #define MONICELLI_PARSER_H #include "ast.h" #include "errors.h" #include "lexer.h" #include "support.h" #include #include #include namespace monicelli { class Parser final : public ErrorReportingMixin { public: Parser(std::istream& input, const std::string& source_filename) : ErrorReportingMixin(source_filename), lexer_{input} {} std::unique_ptr parse() { current_token_ = lexer_.getNextToken(); return parseModule(); } void setLexerTrace(bool enabled) { lexer_.setTraceEnabled(enabled); } private: Variable parseVariable(); VarType parseType(); std::unique_ptr parseModule(); std::unique_ptr parseFunction(); std::unique_ptr parseEntryPoint(); std::unique_ptr parseStatement(); std::vector> parseStatements(); std::unique_ptr maybeParseStatement(); std::unique_ptr parseAssertStatement(); std::unique_ptr parseInputStatement(); std::unique_ptr parseAbortStatement(); BranchCase parseBranchCase(std::shared_ptr condition_lhs); std::unique_ptr parseBranchElse(); std::unique_ptr parseBranchStatement(); std::unique_ptr parseVardeclStatement(); std::unique_ptr parseLoopStatement(); std::unique_ptr parseReturnStatement(); std::unique_ptr parseExpression(); std::unique_ptr parseSemiExpression(std::shared_ptr lhs); std::unique_ptr maybeParseExpression() { return maybeParseExpressionInternal(0); } std::unique_ptr maybeParseExpressionInternal(int min_precedence); std::unique_ptr maybeParseAtomicExpression(); std::unique_ptr parseFunctionCallExpression(); std::unique_ptr getNextToken(); Token* peekNextToken() { assert(current_token_ && "Cannot peek into an empty stream."); return current_token_.get(); } void ignoreNextToken() { auto token = getNextToken(); USE(token); } Lexer lexer_; std::unique_ptr current_token_; }; } // namespace monicelli #endif