From 259774c76389e4fb9fdb4ebd1a836bc54c70dbe0 Mon Sep 17 00:00:00 2001 From: nub31 Date: Sun, 25 May 2025 02:28:15 +0200 Subject: [PATCH] Fix statement recovery infinite loop --- example/program.nub | 9 ++------ .../Nub.Lang/Frontend/Parsing/Parser.cs | 23 ++++++++----------- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/example/program.nub b/example/program.nub index 4d0de37..3ad47ba 100644 --- a/example/program.nub +++ b/example/program.nub @@ -1,17 +1,12 @@ import c -// Test -// Test2 -// Test3 -// Test4 +/// # Documentation +/// ## Documentation subtitle global func main(args: []string) { i = 0 printf("%d\n", args.count) while i < args.count { - // Test printf("%s\n", args[i]) i = i + 1 } - - i: string = "test" } diff --git a/src/compiler/Nub.Lang/Frontend/Parsing/Parser.cs b/src/compiler/Nub.Lang/Frontend/Parsing/Parser.cs index fbd7f64..2bc9076 100644 --- a/src/compiler/Nub.Lang/Frontend/Parsing/Parser.cs +++ b/src/compiler/Nub.Lang/Frontend/Parsing/Parser.cs @@ -82,20 +82,17 @@ public class Parser ExpectSymbol(Symbol.OpenParen); - if (!TryExpectSymbol(Symbol.CloseParen)) + while (!TryExpectSymbol(Symbol.CloseParen)) { - while (!TryExpectSymbol(Symbol.CloseParen)) - { - parameters.Add(ParseFuncParameter()); + parameters.Add(ParseFuncParameter()); - if (!TryExpectSymbol(Symbol.Comma) && Peek().TryGetValue(out var token) && token is not SymbolToken { Symbol: Symbol.CloseParen }) - { - _diagnostics.Add(Diagnostic - .Warning("Missing comma between function parameters") - .WithHelp("Add a ',' to separate parameters") - .At(token) - .Build()); - } + if (!TryExpectSymbol(Symbol.Comma) && Peek().TryGetValue(out var token) && token is not SymbolToken { Symbol: Symbol.CloseParen }) + { + _diagnostics.Add(Diagnostic + .Warning("Missing comma between function parameters") + .WithHelp("Add a ',' to separate parameters") + .At(token) + .Build()); } } @@ -529,7 +526,7 @@ public class Parser var startIndex = _index; ExpectSymbol(Symbol.OpenBrace); List statements = []; - while (!TryExpectSymbol(Symbol.CloseBrace)) + while (Peek().HasValue && !TryExpectSymbol(Symbol.CloseBrace)) { try {