From ad901c2cdbcb421355f5ecef5dc86c14de293c9a Mon Sep 17 00:00:00 2001 From: Heinenen <37484430+Heinenen@users.noreply.github.com> Date: Tue, 16 Jan 2024 09:52:56 +0100 Subject: [PATCH] Syntax error if function name is parenthesized (#3058) Co-authored-by: Laurenz --- crates/typst-syntax/src/parser.rs | 22 +++++++++------------- tests/typ/compiler/let.typ | 7 +++++++ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/crates/typst-syntax/src/parser.rs b/crates/typst-syntax/src/parser.rs index 9e718b300..b77c5068f 100644 --- a/crates/typst-syntax/src/parser.rs +++ b/crates/typst-syntax/src/parser.rs @@ -984,8 +984,7 @@ fn args(p: &mut Parser) { enum PatternKind { Ident, - Placeholder, - Destructuring, + Other, } fn pattern(p: &mut Parser) -> PatternKind { @@ -994,14 +993,12 @@ fn pattern(p: &mut Parser) -> PatternKind { let kind = collection(p, false); validate_pattern_at(p, m, true); - if kind == SyntaxKind::Parenthesized { - PatternKind::Ident - } else { + if kind != SyntaxKind::Parenthesized { p.wrap(m, SyntaxKind::Destructuring); - PatternKind::Destructuring } + PatternKind::Other } else if p.eat_if(SyntaxKind::Underscore) { - PatternKind::Placeholder + PatternKind::Other } else { p.expect(SyntaxKind::Ident); PatternKind::Ident @@ -1014,22 +1011,21 @@ fn let_binding(p: &mut Parser) { let m2 = p.marker(); let mut closure = false; - let mut destructuring = false; + let mut other = false; match pattern(p) { PatternKind::Ident => { - closure = p.directly_at(SyntaxKind::LeftParen); - if closure { + if p.directly_at(SyntaxKind::LeftParen) { let m3 = p.marker(); collection(p, false); validate_params_at(p, m3); p.wrap(m3, SyntaxKind::Params); + closure = true; } } - PatternKind::Placeholder => {} - PatternKind::Destructuring => destructuring = true, + PatternKind::Other => other = true, } - let f = if closure || destructuring { Parser::expect } else { Parser::eat_if }; + let f = if closure || other { Parser::expect } else { Parser::eat_if }; if f(p, SyntaxKind::Eq) { code_expr(p); } diff --git a/tests/typ/compiler/let.typ b/tests/typ/compiler/let.typ index accd9d042..06f07394e 100644 --- a/tests/typ/compiler/let.typ +++ b/tests/typ/compiler/let.typ @@ -275,3 +275,10 @@ Three // Error: 15 expected expression #let func(x) = +--- +// Error: 12 expected equals sign +#let (func)(x) +--- +// Error: 12 expected equals sign +// Error: 15-15 expected semicolon or line break +#let (func)(x) = 3