Fix stmt not consume semicolon
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "parser.h"
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
|
||||
namespace camellya {
|
||||
|
||||
@@ -12,6 +13,7 @@ Program Parser::parse() {
|
||||
try {
|
||||
statements.push_back(declaration());
|
||||
} catch (const ParseError& error) {
|
||||
std::cerr << error.what() << std::endl;
|
||||
synchronize();
|
||||
}
|
||||
}
|
||||
@@ -118,7 +120,6 @@ StmtPtr Parser::class_declaration() {
|
||||
members.push_back(function_declaration());
|
||||
} else {
|
||||
members.push_back(var_declaration());
|
||||
consume(TokenType::SEMICOLON, "Expected ';' after field declaration.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,6 +169,7 @@ StmtPtr Parser::var_declaration() {
|
||||
if (match({TokenType::EQUAL})) {
|
||||
initializer = expression();
|
||||
}
|
||||
consume(TokenType::SEMICOLON, "Expected ';' after variable declaration.");
|
||||
|
||||
return std::make_unique<VarDecl>(type_name, name.lexeme, std::move(initializer));
|
||||
}
|
||||
@@ -250,12 +252,7 @@ StmtPtr Parser::block_statement() {
|
||||
std::vector<StmtPtr> statements;
|
||||
|
||||
while (!check(TokenType::RIGHT_BRACE) && !is_at_end()) {
|
||||
auto stmt = declaration();
|
||||
// If declaration returned a VarDecl (not a class/function/statement), consume semicolon
|
||||
if (dynamic_cast<VarDecl*>(stmt.get())) {
|
||||
consume(TokenType::SEMICOLON, "Expected ';' after variable declaration.");
|
||||
}
|
||||
statements.push_back(std::move(stmt));
|
||||
statements.push_back(declaration());
|
||||
}
|
||||
|
||||
consume(TokenType::RIGHT_BRACE, "Expected '}' after block.");
|
||||
|
||||
Reference in New Issue
Block a user