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

View File

@ -2,28 +2,36 @@
#include <memory> #include <memory>
#include "Lexer.h" #include "Lexer.h"
#include "AST/NumberNode.h" #include "AST/NumberNode.h"
#include "Error.h"
#include <cassert>
/* Parser class for the Sapphire compiler. */
class Parser 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> template<typename T>
struct ErrorOr struct ErrorOr
{ {
std::string error() /* Return the stored pointer. */
{
return m_error;
}
std::shared_ptr<T> get() std::shared_ptr<T> get()
{ {
assert(!m_is_error);
return m_ptr; 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) {} 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) {} 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; } bool is_error() { return m_is_error; }
private: private:
@ -47,6 +55,8 @@ private:
public: public:
~Parser(); ~Parser();
/* Construct a new Parser with the given TokenStream. */
static std::shared_ptr<Parser> new_parser(const TokenStream& tokens); 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(); std::shared_ptr<ASTNode> parse();
}; };