diff --git a/src/Parser.cpp b/src/Parser.cpp index 83dae01..ba65e9c 100644 --- a/src/Parser.cpp +++ b/src/Parser.cpp @@ -1,9 +1,28 @@ #include "Parser.h" +#include "Error.h" -Parser::Parser(/* args */) +Parser::Parser(const TokenStream& tokens) + : tokens(tokens) { } Parser::~Parser() { -} \ No newline at end of file +} + +std::shared_ptr Parser::new_parser(const TokenStream& tokens) +{ + return std::shared_ptr(new Parser(tokens)); // As always, not using std::make_shared 'cause constructor is private +} + +std::shared_ptr Parser::parse() +{ + auto result = walk_expr(); + + if(result.is_error()) + { + Error::throw_error(result.errtok().loc,result.errtok().line(),result.error()); + } + + return result.get(); +} diff --git a/src/Parser.h b/src/Parser.h index 9a23f0e..82f9e95 100644 --- a/src/Parser.h +++ b/src/Parser.h @@ -1,14 +1,44 @@ #pragma once #include #include "Lexer.h" -#include "AST/ASTNode.h" +#include "AST/NumberNode.h" class Parser { + template + struct ErrorOr + { + std::string error() + { + return m_error; + } + std::shared_ptr get() + { + return m_ptr; + } + Token errtok() + { + return error_tok; + } + + ErrorOr(std::shared_ptr& ptr) : m_ptr(ptr), m_is_error(false) {} + ErrorOr(const std::string& error, const Token& error_tok) : m_error(error), m_is_error(true), error_tok(error_tok) {} + + bool is_error() { return m_is_error; } + + private: + bool m_is_error; + std::string m_error; + Token error_tok; + std::shared_ptr m_ptr; + }; private: - /* data */ + Parser(const TokenStream& tokens); + TokenStream tokens; + + ErrorOr walk_expr(); + ErrorOr walk_number(); public: - Parser(/* args */); ~Parser(); static std::shared_ptr new_parser(const TokenStream& tokens);