parserPeek, parserConsume

This commit is contained in:
2026-02-22 17:10:20 +11:00
parent d6a942367e
commit 9409086f3e
2 changed files with 45 additions and 0 deletions

View File

@@ -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 ++]);
}

View File

@@ -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