sapphire/src/Parser.h

73 lines
2.1 KiB
C
Raw Normal View History

2022-06-02 16:25:01 +00:00
#pragma once
2022-06-08 15:39:51 +00:00
#include "AST/NumberNode.h"
#include "Error.h"
2022-06-14 14:29:51 +00:00
#include "Lexer.h"
2022-06-16 13:56:10 +00:00
#include "sapphirepch.h"
2022-06-02 16:25:01 +00:00
/* Parser class for the Sapphire compiler. */
2022-06-02 16:25:01 +00:00
class Parser
{
2022-06-16 13:56:10 +00:00
/* Struct to store a parsing result which can be either a parsing error or a success, in which case it contains a
* pointer to the result. */
template<typename T> struct ErrorOr
{
/* Return the stored pointer. */
std::shared_ptr<T> get()
{
assert(!m_is_error);
return m_ptr;
}
2022-06-14 14:29:51 +00:00
2022-06-16 13:56:10 +00:00
/* Call Error::throw_error() with the stored error's location, line text, and the error string provided to this
* struct instance. */
void ethrow()
{
assert(m_is_error);
Error::throw_error(error_tok->loc, error_tok->line(), m_error);
}
2022-06-14 14:29:51 +00:00
2022-06-16 13:56:10 +00:00
/* Construct a new successful ErrorOr with a heap-allocated pointer to the result class. */
ErrorOr(T* ptr) : m_ptr(ptr), m_is_error(false)
{
}
/* Construct a new failed ErrorOr with the error details and the token where parsing failed. */
ErrorOr(const std::string& error, const Token& error_tok)
: m_error(error), m_is_error(true), error_tok(error_tok)
{
}
2022-06-14 14:29:51 +00:00
2022-06-16 13:56:10 +00:00
/* Is this ErrorOr instance successful or failed? */
bool is_error()
{
return m_is_error;
}
2022-06-14 14:29:51 +00:00
2022-06-16 13:56:10 +00:00
private:
bool m_is_error;
std::string m_error;
std::unique_ptr<Token> error_tok;
std::shared_ptr<T> m_ptr;
};
2022-06-14 14:29:51 +00:00
private:
2022-06-16 13:56:10 +00:00
Parser(const TokenStream& tokens);
TokenStream tokens;
2022-06-14 14:29:51 +00:00
2022-06-16 13:56:10 +00:00
ErrorOr<ExprNode> walk_expr();
ErrorOr<NumberNode> walk_number();
2022-06-14 14:29:51 +00:00
2022-06-16 13:56:10 +00:00
int m_index;
int saved_m_index;
2022-06-14 14:29:51 +00:00
2022-06-16 13:56:10 +00:00
void save_current_position();
void restore_current_position();
2022-06-14 14:29:51 +00:00
public:
2022-06-16 13:56:10 +00:00
~Parser();
2022-06-14 14:29:51 +00:00
2022-06-16 13:56:10 +00:00
/* Construct a new Parser with the given TokenStream. */
static std::shared_ptr<Parser> new_parser(const TokenStream& tokens);
/* Parse the stored TokenStream and return the top-level node of the result Abstract Syntax Tree. */
std::shared_ptr<ASTNode> parse();
2022-06-02 16:25:01 +00:00
};