Documentation Project

let's document all public functions in Sapphire :)
This commit is contained in:
apio 2022-06-08 19:34:16 +02:00
parent 6672a10c08
commit 46a79b6efe
2 changed files with 17 additions and 8 deletions

View File

@ -1,5 +1,4 @@
#include "Parser.h"
#include "Error.h"
Parser::Parser(const TokenStream& tokens)
: tokens(tokens)
@ -21,7 +20,7 @@ std::shared_ptr<ASTNode> Parser::parse()
if(result.is_error())
{
Error::throw_error(result.errtok().loc,result.errtok().line(),result.error());
result.ethrow();
}
return result.get();

View File

@ -2,28 +2,36 @@
#include <memory>
#include "Lexer.h"
#include "AST/NumberNode.h"
#include "Error.h"
#include <cassert>
/* Parser class for the Sapphire compiler. */
class Parser
{
/* 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
{
std::string error()
{
return m_error;
}
/* Return the stored pointer. */
std::shared_ptr<T> get()
{
assert(!m_is_error);
return m_ptr;
}
Token errtok()
/* Call Error::throw_error() with the stored error's location, line text, and the error string provided to this struct instance. */
void ethrow()
{
return *(error_tok.get());
assert(m_is_error);
Error::throw_error(error_tok->loc,error_tok->line(),m_error);
}
/* 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) {}
/* Is this ErrorOr instance successful or failed? */
bool is_error() { return m_is_error; }
private:
@ -47,6 +55,8 @@ private:
public:
~Parser();
/* 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();
};