diff --git a/src/parser/parser.c b/src/parser/parser.c index 0752f12..e489004 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -119,14 +119,14 @@ void createParserError(SolsParser* parser, char* what) { 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); @@ -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) { return Error(Nothing, charptr, "Expecting identifier before '='"); } - + // Collect tokens for node ResultType(SolsTokens, charptr) tokens = createSolsTokens(); if (tokens.error) { @@ -195,10 +195,10 @@ static inline ResultType(Nothing, charptr) parseSet(SolsParser* parser) { for (;;) { ResultType(SolsToken, Nothing) peek = parserPeek(parser, 1); if (peek.error) break; - + if (peek.as.success.type == STT_OPEN_CURLY) bracketCount++; else if (peek.as.success.type == STT_CLOSE_CURLY && bracketCount > 0) bracketCount--; - + if (bracketCount == 0 && getPrecedence(&peek.as.success) <= STP_SET) { break; } @@ -964,13 +964,13 @@ static inline ResultType(Nothing, charptr) parsePuts(SolsParser* parser) { } static inline ResultType(Nothing, charptr) parseCodeBlock(SolsParser* parser) { - + // Collect tokens for node ResultType(SolsTokens, charptr) tokens = createSolsTokens(); if (tokens.error) { return Error(Nothing, charptr, tokens.as.error); } - + ResultType(SolsToken, Nothing) peek = parserPeek(parser, 0); 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 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(parser->currentParent, node.as.success); return Success(Nothing, charptr, {}); @@ -1063,11 +1063,11 @@ static inline ResultType(Nothing, charptr) parseWhile(SolsParser* parser) { } addChildToSolsNode(parser->currentParent, node.as.success); - + if(parserPeek(parser, 1).as.success.type != STT_OPEN_CURLY) { return Error(Nothing, charptr, "Expecting opening curly brace for while loop body"); } - + parserConsume(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); - + if(parserPeek(parser, 1).as.success.type != STT_OPEN_CURLY) { return Error(Nothing, charptr, "Expecting opening curly brace for if statement body"); } - + parserConsume(parser); ResultType(Nothing, charptr) res = parseCodeBlock(parser); @@ -1204,7 +1204,7 @@ static inline ResultType(Nothing, charptr) parseLambda(SolsParser* parser) { next = parserPeek(parser, 1); if (next.as.success.type == STT_IDENTIFIER) { - argName = next.as.success.as.idName; + argName = next.as.success.as.idName; } else { 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 SolsNode codeBlock = parser->currentParent->children.at[parser->currentParent->children.count - 1]; parser->currentParent->children.count--; - + // We need to get the actual node from the parent to modify it SolsNode* lambdaNode = &parser->currentParent->children.at[parser->currentParent->children.count - 1]; addChildToSolsNode(lambdaNode, codeBlock); @@ -1693,7 +1693,7 @@ static inline ResultType(Nothing, charptr) parseStruct(SolsParser* parser) { } // Ignore new lines between struct and opening curly brace for (;;) { - ResultType(SolsToken, Nothing) token = parserPeek(parser, 0); + ResultType(SolsToken, Nothing) token = parserPeek(parser, 1); if (token.error) { return Error(Nothing, charptr, "Expecting '{' after 'struct'"); } @@ -1707,7 +1707,7 @@ static inline ResultType(Nothing, charptr) parseStruct(SolsParser* parser) { bool done = false; // Skip newlines between struct values for (;;) { - ResultType(SolsToken, Nothing) token = parserPeek(parser, 0); + ResultType(SolsToken, Nothing) token = parserPeek(parser, 1); if (token.error) { 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) { done = true; + parserConsume(parser); break; } else { break; } } - parserConsume(parser); if (done) break; // key = value\n @@ -1745,7 +1745,6 @@ static inline ResultType(Nothing, charptr) parseStruct(SolsParser* parser) { if (setTok.type != STT_OP_SET) { return Error(Nothing, charptr, "Expecting '=' after struct key"); } - parserConsume(parser); SolsTokens tokens = ({ ResultType(SolsTokens, charptr) _result = createSolsTokens(); if (_result.error) { @@ -1754,11 +1753,11 @@ static inline ResultType(Nothing, charptr) parseStruct(SolsParser* parser) { _result.as.success; }); // Collect tokens for value - if (parserPeek(parser, 0).error) { + if (parserPeek(parser, 1).error) { return Error(Nothing, charptr, "Expecting value after '='"); } for (;;) { - ResultType(SolsToken, Nothing) token = parserPeek(parser, 0); + ResultType(SolsToken, Nothing) token = parserPeek(parser, 1); if (token.error) { break; } @@ -1800,12 +1799,6 @@ static inline ResultType(Nothing, charptr) parseStruct(SolsParser* parser) { // Add set node to struct node 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 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) { return Error(SolsToken, Nothing, {}); - } + } 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) { return Error(SolsToken, Nothing, {}); - } + } return Success(SolsToken, Nothing, parser->input->at[parser->current ++]); }