diff options
| author | Marmare314 <49279081+Marmare314@users.noreply.github.com> | 2023-04-11 18:44:17 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-11 18:44:17 +0200 |
| commit | 72d8785abeff66f6b832725e71fe65d9ded803ce (patch) | |
| tree | 5e9177d56e3fb34ac0888f76754a4061dacec04f | |
| parent | 9984e73ff396b7da71bb19416f5ed15296d50d98 (diff) | |
fix parenthesized binding (#707)
| -rw-r--r-- | docs/src/reference/scripting.md | 2 | ||||
| -rw-r--r-- | src/syntax/ast.rs | 9 | ||||
| -rw-r--r-- | src/syntax/parser.rs | 8 | ||||
| -rw-r--r-- | tests/typ/compiler/let.typ | 14 |
4 files changed, 25 insertions, 8 deletions
diff --git a/docs/src/reference/scripting.md b/docs/src/reference/scripting.md index ca56103c..7d40f256 100644 --- a/docs/src/reference/scripting.md +++ b/docs/src/reference/scripting.md @@ -96,7 +96,7 @@ The last element is #b. "Homer": "The Odyssey", "Austen": "Persuasion", ) -#let (Austen) = books +#let (Austen,) = books Austen wrote #Austen. #let (Homer: h) = books diff --git a/src/syntax/ast.rs b/src/syntax/ast.rs index 94114958..4119802e 100644 --- a/src/syntax/ast.rs +++ b/src/syntax/ast.rs @@ -1620,7 +1620,14 @@ pub enum DestructuringKind { impl Pattern { /// The kind of the pattern. pub fn kind(&self) -> PatternKind { - if self.0.children().len() <= 1 { + if self + .0 + .children() + .map(SyntaxNode::kind) + .skip_while(|&kind| kind == SyntaxKind::LeftParen) + .take_while(|&kind| kind != SyntaxKind::RightParen) + .eq([SyntaxKind::Ident]) + { return PatternKind::Ident(self.0.cast_first_match().unwrap_or_default()); } diff --git a/src/syntax/parser.rs b/src/syntax/parser.rs index 16d519fe..42183f3a 100644 --- a/src/syntax/parser.rs +++ b/src/syntax/parser.rs @@ -847,11 +847,15 @@ fn pattern(p: &mut Parser) -> PatternKind { let m = p.marker(); if p.at(SyntaxKind::LeftParen) { - collection(p, false); + let kind = collection(p, false); validate_destruct_pattern(p, m); p.wrap(m, SyntaxKind::Pattern); - PatternKind::Destructuring + if kind == SyntaxKind::Parenthesized { + PatternKind::Normal + } else { + PatternKind::Destructuring + } } else { if p.expect(SyntaxKind::Ident) { p.wrap(m, SyntaxKind::Pattern); diff --git a/tests/typ/compiler/let.typ b/tests/typ/compiler/let.typ index 4518f3d4..70657617 100644 --- a/tests/typ/compiler/let.typ +++ b/tests/typ/compiler/let.typ @@ -33,6 +33,11 @@ Three #test(v3, 3) --- +// Test parenthesised assignments. +// Ref: false +#let (a) = (1, 2) + +--- // Ref: false // Simple destructuring. #let (a, b) = (1, 2) @@ -41,6 +46,11 @@ Three --- // Ref: false +#let (a,) = (1,) +#test(a, 1) + +--- +// Ref: false // Destructuring with multiple placeholders. #let (a, _, c, _) = (1, 2, 3, 4) #test(a, 1) @@ -116,10 +126,6 @@ Three #let (a, b, c) = (1, 2) --- -// Error: 6-9 too many elements to destructure -#let (a) = (1, 2) - ---- // Error: 6-20 not enough elements to destructure #let (..a, b, c, d) = (1, 2) |
