diff --git a/src/parser/parser.c b/src/parser/parser.c index deacd31..68d9ba6 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -17,3 +17,27 @@ ResultType(SolsParser, charptr) createSolsParser(SolsTokens* input) { parser.currentParent = &parser.output; return Success(SolsParser, charptr, parser); } + +ResultType(Nothing, charptr) parse(SolsParser* parser) { + return Error(Nothing, charptr, "Work in progress"); +} + +ResultType(SolsToken, Nothing) parserPeek(SolsParser* parser, size_t ahead) { + if (parser->input == NULL) { + return Error(SolsToken, Nothing, {}); + } + if (parser->current + ahead - 1 >= parser->input->count) { + return Error(SolsToken, Nothing, {}); + } + return Success(SolsToken, Nothing, parser->input->at[parser->current + ahead - 1]); +} + +ResultType(SolsToken, Nothing) parserConsume(SolsParser* parser) { + if (parser->input == NULL) { + return Error(SolsToken, Nothing, {}); + } + if (parser->current + 1 >= parser->input->count) { + return Error(SolsToken, Nothing, {}); + } + return Success(SolsToken, Nothing, parser->input->at[parser->current ++]); +} diff --git a/src/parser/parser.h b/src/parser/parser.h index d67fd06..c81e74d 100644 --- a/src/parser/parser.h +++ b/src/parser/parser.h @@ -4,6 +4,7 @@ #include "SolsNode.h" #include "../lexer/SolsToken.h" #include "../include/error.h" +#include "../include/nothing.h" // Holds information about the parser. // .input is lexed tokens, produced by a lexer. @@ -25,4 +26,24 @@ Result(SolsParser, charptr); // Failure: char* detailing what went wrong (usually memory failure) ResultType(SolsParser, charptr) createSolsParser(SolsTokens* input); +// Parses the tokens in the SolsParser into an AST. +// Returns: +// Success: Nothing (output is stored in the passed SolsLexer) +// Failure: char* detailing what went wrong (usually user error) +ResultType(Nothing, charptr) parse(SolsParser* parser); + +Result(SolsToken, Nothing); + +// Peeks at future tokens in the parser, 0 meaning current token, 1 the next. +// Returns: +// Success: The requested token +// Failure: Nothing (token is out of bounds) +ResultType(SolsToken, Nothing) parserPeek(SolsParser* parser, size_t ahead); + +// Consumes the next token in the parser. +// Returns: +// Success: The requested token +// Failure: Nothing (we have reached the end of the tokens passed) +ResultType(SolsToken, Nothing) parserConsume(SolsParser* parser); + #endif