From ff4d518973e660ce46e0c4959b17ef5926f21616 Mon Sep 17 00:00:00 2001 From: Tim Fennis Date: Wed, 2 Dec 2026 07:13:29 +0100 Subject: [PATCH] =?UTF-8?q?fix(parser):=20end=20a=20named=20function=20sta?= =?UTF-8?q?tement=20at=20its=20closing=20brace=20=F0=9F=A7=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- ndc_parser/src/parser.rs | 25 ++++++++++++++----- ...med_function_ends_at_its_closing_brace.ndc | 12 +++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 tests/functional/programs/004_basic/112_a_named_function_ends_at_its_closing_brace.ndc diff --git a/ndc_parser/src/parser.rs b/ndc_parser/src/parser.rs index 07fe8bf2..0149999b 100644 --- a/ndc_parser/src/parser.rs +++ b/ndc_parser/src/parser.rs @@ -93,6 +93,17 @@ impl Parser { ) } + /// Whether the current tokens start a named function, as in `fn f(x)` + /// or `pure fn f(x)`, rather than an anonymous one, as in `fn(x)`. + fn at_named_function(&self) -> bool { + let mut tokens = self.tokens[self.current..].iter().map(|t| &t.token); + let mut token = tokens.next(); + while token == Some(&Token::Pure) { + token = tokens.next(); + } + token == Some(&Token::Fn) && matches!(tokens.next(), Some(Token::Identifier(_))) + } + fn peek_next_token(&self) -> Option<&Token> { self.tokens.get(self.current + 1).map(|t| &t.token) } @@ -330,13 +341,15 @@ impl Parser { } else if self .match_token(&[Token::If, Token::While, Token::For, Token::LeftCurlyBracket]) .is_some() + || self.at_named_function() { - // A statement that starts with an `if`, a loop or a block ends - // at its closing brace, as in Rust: what follows starts the next - // statement, so `for x in xs { ... }` and then `[x]` on the next - // line is a list, not an index into the loop's value. Within an - // expression they still take an index or a call, as in - // `print(if c { [1] } else { [2] }[0])`. + // A statement that starts with an `if`, a loop, a block or a named + // function ends at its closing brace, as in Rust: what follows + // starts the next statement, so `for x in xs { ... }` and then + // `[x]` on the next line is a list, not an index into the loop's + // value, and `fn f(x) { ... }` and then `(1, 2).print` is not a + // call of `f`. Within an expression they still take an index or a + // call, as in `print(if c { [1] } else { [2] }[0])`. self.primary()? } else { self.expression()? diff --git a/tests/functional/programs/004_basic/112_a_named_function_ends_at_its_closing_brace.ndc b/tests/functional/programs/004_basic/112_a_named_function_ends_at_its_closing_brace.ndc new file mode 100644 index 00000000..223331fe --- /dev/null +++ b/tests/functional/programs/004_basic/112_a_named_function_ends_at_its_closing_brace.ndc @@ -0,0 +1,12 @@ +// A statement that starts with a named function ends at its closing brace, so +// a tuple or a list on the next line starts a statement of its own instead of +// calling or indexing the function. An anonymous function can still be called +// where it's written. +// expect-output: (1,2) +// expect-output: [3,4] +// expect-output: 6 +fn f(x) { x + 1 } +(1, 2).print; +pure fn g(x) { x * 2 } +[3, 4].print; +fn(x) { x + 1 }(5).print;