more parser stuff
This commit is contained in:
parent
2452d78205
commit
5dfc8df9ca
@ -1,9 +1,28 @@
|
|||||||
#include "Parser.h"
|
#include "Parser.h"
|
||||||
|
#include "Error.h"
|
||||||
|
|
||||||
Parser::Parser(/* args */)
|
Parser::Parser(const TokenStream& tokens)
|
||||||
|
: tokens(tokens)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
Parser::~Parser()
|
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();
|
||||||
|
}
|
||||||
|
36
src/Parser.h
36
src/Parser.h
@ -1,14 +1,44 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include "Lexer.h"
|
#include "Lexer.h"
|
||||||
#include "AST/ASTNode.h"
|
#include "AST/NumberNode.h"
|
||||||
|
|
||||||
class Parser
|
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:
|
||||||
|
bool m_is_error;
|
||||||
|
std::string m_error;
|
||||||
|
Token error_tok;
|
||||||
|
std::shared_ptr<T> m_ptr;
|
||||||
|
};
|
||||||
private:
|
private:
|
||||||
/* data */
|
Parser(const TokenStream& tokens);
|
||||||
|
TokenStream tokens;
|
||||||
|
|
||||||
|
ErrorOr<ExprNode> walk_expr();
|
||||||
|
ErrorOr<NumberNode> walk_number();
|
||||||
public:
|
public:
|
||||||
Parser(/* args */);
|
|
||||||
~Parser();
|
~Parser();
|
||||||
|
|
||||||
static std::shared_ptr<Parser> new_parser(const TokenStream& tokens);
|
static std::shared_ptr<Parser> new_parser(const TokenStream& tokens);
|
||||||
|
Loading…
Reference in New Issue
Block a user