more parser stuff

This commit is contained in:
apio 2022-06-08 17:39:51 +02:00
parent 2452d78205
commit 5dfc8df9ca
2 changed files with 54 additions and 5 deletions

View File

@ -1,9 +1,28 @@
#include "Parser.h"
#include "Error.h"
Parser::Parser(/* args */)
Parser::Parser(const TokenStream& tokens)
: tokens(tokens)
{
}
Parser::~Parser()
{
}
std::shared_ptr<Parser> Parser::new_parser(const TokenStream& tokens)
{
return std::shared_ptr<Parser>(new Parser(tokens)); // As always, not using std::make_shared 'cause constructor is private
}
std::shared_ptr<ASTNode> Parser::parse()
{
auto result = walk_expr();
if(result.is_error())
{
Error::throw_error(result.errtok().loc,result.errtok().line(),result.error());
}
return result.get();
}

View File

@ -1,14 +1,44 @@
#pragma once
#include <memory>
#include "Lexer.h"
#include "AST/ASTNode.h"
#include "AST/NumberNode.h"
class Parser
{
template<typename T>
struct ErrorOr
{
std::string error()
{
return m_error;
}
std::shared_ptr<T> get()
{
return m_ptr;
}
Token errtok()
{
return error_tok;
}
ErrorOr(std::shared_ptr<T>& 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:
/* data */
bool m_is_error;
std::string m_error;
Token error_tok;
std::shared_ptr<T> m_ptr;
};
private:
Parser(const TokenStream& tokens);
TokenStream tokens;
ErrorOr<ExprNode> walk_expr();
ErrorOr<NumberNode> walk_number();
public:
Parser(/* args */);
~Parser();
static std::shared_ptr<Parser> new_parser(const TokenStream& tokens);