185 lines
6.4 KiB
C
185 lines
6.4 KiB
C
#include "parser.h"
|
|
#include "SolsNode.h"
|
|
|
|
#include "../include/estr.h"
|
|
#include "../include/ansii.h"
|
|
|
|
ResultType(SolsParser, charptr) createSolsParser(SolsTokens* input) {
|
|
ResultType(SolsNode, charptr) node = createSolsNode(SNT_ROOT);
|
|
if (node.error) {
|
|
Estr str = CREATE_ESTR(node.as.error);
|
|
APPEND_ESTR(str, " (in createSolsParser() function)");
|
|
return Error(SolsParser, charptr, str.str);
|
|
}
|
|
SolsParser parser = {
|
|
.input = input,
|
|
.current = 0,
|
|
.output = node.as.success,
|
|
.errors.capacity = 32,
|
|
.errors.count = 0,
|
|
.errors.at = malloc(sizeof(char*) * 32)
|
|
};
|
|
if (parser.errors.at == NULL) {
|
|
return Error(SolsParser, charptr, "Couldn't allocate memory to store errors (in createSolsParser() function)");
|
|
}
|
|
return Success(SolsParser, charptr, parser);
|
|
}
|
|
|
|
void createParserError(SolsParser* parser, char* what) {
|
|
|
|
ResultType(SolsToken, Nothing) prevToken = Error(SolsToken, Nothing, {});
|
|
SolsToken token = parserPeek(parser, 0).as.success;
|
|
ResultType(SolsToken, Nothing) nextToken = Error(SolsToken, Nothing, {});
|
|
|
|
// Find tokens for previous line
|
|
if (parser->current > 1) {
|
|
for (size_t i = parser->current - 1; i > 0; i--) {
|
|
ResultType(SolsToken, Nothing) check = parserLookAt(parser, i - 1);
|
|
if (check.error) break;
|
|
if (check.as.success.line.num != token.line.num) {
|
|
prevToken = check;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Find tokens for next line
|
|
for (size_t i = parser->current; i < parser->input->count; i++) {
|
|
ResultType(SolsToken, Nothing) check = parserLookAt(parser, i);
|
|
if (check.error) break;
|
|
if (check.as.success.line.num != token.line.num) {
|
|
nextToken = check;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (parser->errors.count + 1 >= parser->errors.capacity) {
|
|
parser->errors.capacity *= 2;
|
|
char** tmp = realloc(parser->errors.at, sizeof(char*) * parser->errors.capacity);
|
|
if (tmp == NULL) {
|
|
parser->errors.at[parser->errors.count - 1] = "Failed to allocate more memory for createParserError function";
|
|
return;
|
|
}
|
|
parser->errors.at = tmp;
|
|
}
|
|
Estr err = CREATE_ESTR(ESC_BOLD ESC_RED_FG "error: " ESC_RESET ESC_BOLD);
|
|
|
|
// Append the main error message and a newline
|
|
APPEND_ESTR(err, what);
|
|
APPEND_ESTR(err, "\n" ESC_RESET);
|
|
|
|
APPEND_ESTR(err, ESC_CYAN_FG "-> " ESC_RESET);
|
|
APPEND_ESTR(err, "on line ");
|
|
|
|
// Format the line number
|
|
char line_buf[16];
|
|
snprintf(line_buf, sizeof(line_buf), "%zu", token.line.num);
|
|
APPEND_ESTR(err, line_buf);
|
|
|
|
APPEND_ESTR(err, "\n\n");
|
|
// Append line before
|
|
if (!prevToken.error) {
|
|
APPEND_ESTR(err, prevToken.as.success.line.content);
|
|
APPEND_ESTR(err, "\n");
|
|
}
|
|
// Append the actual content of the line that failed
|
|
APPEND_ESTR(err, token.line.content);
|
|
APPEND_ESTR(err, "\n");
|
|
if (!nextToken.error) {
|
|
APPEND_ESTR(err, nextToken.as.success.line.content);
|
|
APPEND_ESTR(err, "\n");
|
|
}
|
|
|
|
// Output the final constructed error
|
|
parser->errors.at[parser->errors.count] = err.str;
|
|
parser->errors.count++;
|
|
}
|
|
|
|
static inline ResultType(Nothing, charptr) parseIdentifier(SolsParser* parser) {
|
|
ResultType(SolsToken, Nothing) peek = parserPeek(parser, 0);
|
|
if (peek.error) {
|
|
return Error(Nothing, charptr, "ruh roh");
|
|
}
|
|
ResultType(SolsNode, charptr) node = createSolsNode(SNT_IDENTIFIER, peek.as.success.as.idName);
|
|
if (node.error) {
|
|
Estr err = CREATE_ESTR(node.as.error);
|
|
APPEND_ESTR(err, " (in parseLiteral() function)");
|
|
return Error(Nothing, charptr, err.str);
|
|
}
|
|
addChildToSolsNode(parser->currentParent, node.as.success);
|
|
return Success(Nothing, charptr, {});
|
|
}
|
|
static inline ResultType(Nothing, charptr) parseLiteral(SolsParser* parser) {
|
|
ResultType(SolsToken, Nothing) peek = parserPeek(parser, 0);
|
|
if (peek.error) {
|
|
return Error(Nothing, charptr, "ruh roh");
|
|
}
|
|
ResultType(SolsNode, charptr) node = createSolsNode(SNT_LITERAL, peek.as.success.as.literal);
|
|
if (node.error) {
|
|
Estr err = CREATE_ESTR(node.as.error);
|
|
APPEND_ESTR(err, " (in parseLiteral() function)");
|
|
return Error(Nothing, charptr, err.str);
|
|
}
|
|
addChildToSolsNode(parser->currentParent, node.as.success);
|
|
return Success(Nothing, charptr, {});
|
|
}
|
|
|
|
static inline ResultType(Nothing, charptr) parsePuts(SolsParser* parser) {
|
|
return Error(Nothing, charptr, "Not an error, just curious what errors look like");
|
|
return Success(Nothing, charptr, {});
|
|
}
|
|
|
|
ResultType(Nothing, charptr) parse(SolsParser* parser) {
|
|
parser->currentParent = &parser->output;
|
|
for (;;) {
|
|
ResultType(SolsToken, Nothing) token = parserConsume(parser);
|
|
if (token.error) {
|
|
break;
|
|
}
|
|
switch (token.as.success.type) {
|
|
case STT_IDENTIFIER: PARSER_HANDLE(Identifier);
|
|
case STT_LITERAL: PARSER_HANDLE(Literal);
|
|
case STT_KW_PUTS: PARSER_HANDLE(Puts);
|
|
}
|
|
}
|
|
if (parser->errors.count > 0) {
|
|
Estr estr = CREATE_ESTR("");
|
|
for (size_t i = 0; i < parser->errors.count; i++) {
|
|
APPEND_ESTR(estr, parser->errors.at[i]);
|
|
APPEND_ESTR(estr, "\n");
|
|
}
|
|
return Error(Nothing, charptr, estr.str);
|
|
}
|
|
return Success(Nothing, charptr, {});
|
|
}
|
|
|
|
ResultType(SolsToken, Nothing) parserLookAt(SolsParser* parser, size_t where) {
|
|
if (parser->input == NULL) {
|
|
return Error(SolsToken, Nothing, {});
|
|
}
|
|
if (where >= parser->input->count) {
|
|
return Error(SolsToken, Nothing, {});
|
|
}
|
|
return Success(SolsToken, Nothing, parser->input->at[where]);
|
|
}
|
|
|
|
ResultType(SolsToken, Nothing) parserPeek(SolsParser* parser, int64_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 >= parser->input->count) {
|
|
return Error(SolsToken, Nothing, {});
|
|
}
|
|
return Success(SolsToken, Nothing, parser->input->at[parser->current ++]);
|
|
}
|