Fix struct parsing

This commit is contained in:
2026-04-09 17:13:13 +10:00
parent 5b61a11f00
commit f694f50d70

View File

@@ -119,14 +119,14 @@ void createParserError(SolsParser* parser, char* what) {
parser->errors.at = tmp; parser->errors.at = tmp;
} }
Estr err = CREATE_ESTR(ESC_BOLD ESC_RED_FG "error: " ESC_RESET ESC_BOLD); Estr err = CREATE_ESTR(ESC_BOLD ESC_RED_FG "error: " ESC_RESET ESC_BOLD);
// Append the main error message and a newline // Append the main error message and a newline
APPEND_ESTR(err, what); APPEND_ESTR(err, what);
APPEND_ESTR(err, "\n" ESC_RESET); APPEND_ESTR(err, "\n" ESC_RESET);
APPEND_ESTR(err, ESC_CYAN_FG "-> " ESC_RESET); APPEND_ESTR(err, ESC_CYAN_FG "-> " ESC_RESET);
APPEND_ESTR(err, "on line "); APPEND_ESTR(err, "on line ");
// Format the line number // Format the line number
char line_buf[16]; char line_buf[16];
snprintf(line_buf, sizeof(line_buf), "%zu", token.line.num); snprintf(line_buf, sizeof(line_buf), "%zu", token.line.num);
@@ -178,7 +178,7 @@ static inline ResultType(Nothing, charptr) parseSet(SolsParser* parser) {
if (parser->currentParent->children.at[parser->output.children.count - 1].type != SNT_IDENTIFIER) { if (parser->currentParent->children.at[parser->output.children.count - 1].type != SNT_IDENTIFIER) {
return Error(Nothing, charptr, "Expecting identifier before '='"); return Error(Nothing, charptr, "Expecting identifier before '='");
} }
// Collect tokens for node // Collect tokens for node
ResultType(SolsTokens, charptr) tokens = createSolsTokens(); ResultType(SolsTokens, charptr) tokens = createSolsTokens();
if (tokens.error) { if (tokens.error) {
@@ -195,10 +195,10 @@ static inline ResultType(Nothing, charptr) parseSet(SolsParser* parser) {
for (;;) { for (;;) {
ResultType(SolsToken, Nothing) peek = parserPeek(parser, 1); ResultType(SolsToken, Nothing) peek = parserPeek(parser, 1);
if (peek.error) break; if (peek.error) break;
if (peek.as.success.type == STT_OPEN_CURLY) bracketCount++; if (peek.as.success.type == STT_OPEN_CURLY) bracketCount++;
else if (peek.as.success.type == STT_CLOSE_CURLY && bracketCount > 0) bracketCount--; else if (peek.as.success.type == STT_CLOSE_CURLY && bracketCount > 0) bracketCount--;
if (bracketCount == 0 && getPrecedence(&peek.as.success) <= STP_SET) { if (bracketCount == 0 && getPrecedence(&peek.as.success) <= STP_SET) {
break; break;
} }
@@ -964,13 +964,13 @@ static inline ResultType(Nothing, charptr) parsePuts(SolsParser* parser) {
} }
static inline ResultType(Nothing, charptr) parseCodeBlock(SolsParser* parser) { static inline ResultType(Nothing, charptr) parseCodeBlock(SolsParser* parser) {
// Collect tokens for node // Collect tokens for node
ResultType(SolsTokens, charptr) tokens = createSolsTokens(); ResultType(SolsTokens, charptr) tokens = createSolsTokens();
if (tokens.error) { if (tokens.error) {
return Error(Nothing, charptr, tokens.as.error); return Error(Nothing, charptr, tokens.as.error);
} }
ResultType(SolsToken, Nothing) peek = parserPeek(parser, 0); ResultType(SolsToken, Nothing) peek = parserPeek(parser, 0);
if (peek.error) return Error(Nothing, charptr, "ruh roh"); if (peek.error) return Error(Nothing, charptr, "ruh roh");
@@ -1014,7 +1014,7 @@ static inline ResultType(Nothing, charptr) parseCodeBlock(SolsParser* parser) {
// Copy nodes into the sols node // Copy nodes into the sols node
for (size_t i = 0; i < codeBlockParser.as.success.output.children.count; i++) { for (size_t i = 0; i < codeBlockParser.as.success.output.children.count; i++) {
addChildToSolsNode(&node.as.success, codeBlockParser.as.success.output.children.at[i]); addChildToSolsNode(&node.as.success, codeBlockParser.as.success.output.children.at[i]);
} }
addChildToSolsNode(parser->currentParent, node.as.success); addChildToSolsNode(parser->currentParent, node.as.success);
return Success(Nothing, charptr, {}); return Success(Nothing, charptr, {});
@@ -1063,11 +1063,11 @@ static inline ResultType(Nothing, charptr) parseWhile(SolsParser* parser) {
} }
addChildToSolsNode(parser->currentParent, node.as.success); addChildToSolsNode(parser->currentParent, node.as.success);
if(parserPeek(parser, 1).as.success.type != STT_OPEN_CURLY) { if(parserPeek(parser, 1).as.success.type != STT_OPEN_CURLY) {
return Error(Nothing, charptr, "Expecting opening curly brace for while loop body"); return Error(Nothing, charptr, "Expecting opening curly brace for while loop body");
} }
parserConsume(parser); parserConsume(parser);
ResultType(Nothing, charptr) res = parseCodeBlock(parser); ResultType(Nothing, charptr) res = parseCodeBlock(parser);
@@ -1128,11 +1128,11 @@ static inline ResultType(Nothing, charptr) parseIf(SolsParser* parser) {
} }
addChildToSolsNode(parser->currentParent, node.as.success); addChildToSolsNode(parser->currentParent, node.as.success);
if(parserPeek(parser, 1).as.success.type != STT_OPEN_CURLY) { if(parserPeek(parser, 1).as.success.type != STT_OPEN_CURLY) {
return Error(Nothing, charptr, "Expecting opening curly brace for if statement body"); return Error(Nothing, charptr, "Expecting opening curly brace for if statement body");
} }
parserConsume(parser); parserConsume(parser);
ResultType(Nothing, charptr) res = parseCodeBlock(parser); ResultType(Nothing, charptr) res = parseCodeBlock(parser);
@@ -1204,7 +1204,7 @@ static inline ResultType(Nothing, charptr) parseLambda(SolsParser* parser) {
next = parserPeek(parser, 1); next = parserPeek(parser, 1);
if (next.as.success.type == STT_IDENTIFIER) { if (next.as.success.type == STT_IDENTIFIER) {
argName = next.as.success.as.idName; argName = next.as.success.as.idName;
} else { } else {
return Error(Nothing, charptr, "Expecting identifier after type in lambda argument list"); return Error(Nothing, charptr, "Expecting identifier after type in lambda argument list");
} }
@@ -1274,7 +1274,7 @@ static inline ResultType(Nothing, charptr) parseLambda(SolsParser* parser) {
// Last child of parent is code block, we need to move it // Last child of parent is code block, we need to move it
SolsNode codeBlock = parser->currentParent->children.at[parser->currentParent->children.count - 1]; SolsNode codeBlock = parser->currentParent->children.at[parser->currentParent->children.count - 1];
parser->currentParent->children.count--; parser->currentParent->children.count--;
// We need to get the actual node from the parent to modify it // We need to get the actual node from the parent to modify it
SolsNode* lambdaNode = &parser->currentParent->children.at[parser->currentParent->children.count - 1]; SolsNode* lambdaNode = &parser->currentParent->children.at[parser->currentParent->children.count - 1];
addChildToSolsNode(lambdaNode, codeBlock); addChildToSolsNode(lambdaNode, codeBlock);
@@ -1693,7 +1693,7 @@ static inline ResultType(Nothing, charptr) parseStruct(SolsParser* parser) {
} }
// Ignore new lines between struct and opening curly brace // Ignore new lines between struct and opening curly brace
for (;;) { for (;;) {
ResultType(SolsToken, Nothing) token = parserPeek(parser, 0); ResultType(SolsToken, Nothing) token = parserPeek(parser, 1);
if (token.error) { if (token.error) {
return Error(Nothing, charptr, "Expecting '{' after 'struct'"); return Error(Nothing, charptr, "Expecting '{' after 'struct'");
} }
@@ -1707,7 +1707,7 @@ static inline ResultType(Nothing, charptr) parseStruct(SolsParser* parser) {
bool done = false; bool done = false;
// Skip newlines between struct values // Skip newlines between struct values
for (;;) { for (;;) {
ResultType(SolsToken, Nothing) token = parserPeek(parser, 0); ResultType(SolsToken, Nothing) token = parserPeek(parser, 1);
if (token.error) { if (token.error) {
return Error(Nothing, charptr, "Expecting '}' to end struct"); return Error(Nothing, charptr, "Expecting '}' to end struct");
} }
@@ -1716,12 +1716,12 @@ static inline ResultType(Nothing, charptr) parseStruct(SolsParser* parser) {
} }
else if (token.as.success.type == STT_CLOSE_CURLY) { else if (token.as.success.type == STT_CLOSE_CURLY) {
done = true; done = true;
parserConsume(parser);
break; break;
} else { } else {
break; break;
} }
} }
parserConsume(parser);
if (done) break; if (done) break;
// key = value\n // key = value\n
@@ -1745,7 +1745,6 @@ static inline ResultType(Nothing, charptr) parseStruct(SolsParser* parser) {
if (setTok.type != STT_OP_SET) { if (setTok.type != STT_OP_SET) {
return Error(Nothing, charptr, "Expecting '=' after struct key"); return Error(Nothing, charptr, "Expecting '=' after struct key");
} }
parserConsume(parser);
SolsTokens tokens = ({ SolsTokens tokens = ({
ResultType(SolsTokens, charptr) _result = createSolsTokens(); ResultType(SolsTokens, charptr) _result = createSolsTokens();
if (_result.error) { if (_result.error) {
@@ -1754,11 +1753,11 @@ static inline ResultType(Nothing, charptr) parseStruct(SolsParser* parser) {
_result.as.success; _result.as.success;
}); });
// Collect tokens for value // Collect tokens for value
if (parserPeek(parser, 0).error) { if (parserPeek(parser, 1).error) {
return Error(Nothing, charptr, "Expecting value after '='"); return Error(Nothing, charptr, "Expecting value after '='");
} }
for (;;) { for (;;) {
ResultType(SolsToken, Nothing) token = parserPeek(parser, 0); ResultType(SolsToken, Nothing) token = parserPeek(parser, 1);
if (token.error) { if (token.error) {
break; break;
} }
@@ -1800,12 +1799,6 @@ static inline ResultType(Nothing, charptr) parseStruct(SolsParser* parser) {
// Add set node to struct node // Add set node to struct node
addChildToSolsNode(&structNode, setNode); addChildToSolsNode(&structNode, setNode);
// Consume newline
ResultType(SolsToken, Nothing) newline = parserConsume(parser);
if (newline.error) {
return Error(Nothing, charptr, "Expecting newline after struct value");
}
} }
// Add struct node to parent // Add struct node to parent
addChildToSolsNode(parser->currentParent, structNode); addChildToSolsNode(parser->currentParent, structNode);
@@ -1881,7 +1874,7 @@ ResultType(SolsToken, Nothing) parserPeek(SolsParser* parser, int64_t ahead) {
} }
if (parser->current + ahead - 1 >= parser->input->count) { if (parser->current + ahead - 1 >= parser->input->count) {
return Error(SolsToken, Nothing, {}); return Error(SolsToken, Nothing, {});
} }
return Success(SolsToken, Nothing, parser->input->at[parser->current + ahead - 1]); return Success(SolsToken, Nothing, parser->input->at[parser->current + ahead - 1]);
} }
@@ -1891,6 +1884,6 @@ ResultType(SolsToken, Nothing) parserConsume(SolsParser* parser) {
} }
if (parser->current >= parser->input->count) { if (parser->current >= parser->input->count) {
return Error(SolsToken, Nothing, {}); return Error(SolsToken, Nothing, {});
} }
return Success(SolsToken, Nothing, parser->input->at[parser->current ++]); return Success(SolsToken, Nothing, parser->input->at[parser->current ++]);
} }