Skip to content

Commit 72d8785

Browse files
authored
fix parenthesized binding (typst#707)
1 parent 9984e73 commit 72d8785

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

docs/src/reference/scripting.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ The last element is #b.
9696
"Homer": "The Odyssey",
9797
"Austen": "Persuasion",
9898
)
99-
#let (Austen) = books
99+
#let (Austen,) = books
100100
Austen wrote #Austen.
101101
102102
#let (Homer: h) = books

src/syntax/ast.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1620,7 +1620,14 @@ pub enum DestructuringKind {
16201620
impl Pattern {
16211621
/// The kind of the pattern.
16221622
pub fn kind(&self) -> PatternKind {
1623-
if self.0.children().len() <= 1 {
1623+
if self
1624+
.0
1625+
.children()
1626+
.map(SyntaxNode::kind)
1627+
.skip_while(|&kind| kind == SyntaxKind::LeftParen)
1628+
.take_while(|&kind| kind != SyntaxKind::RightParen)
1629+
.eq([SyntaxKind::Ident])
1630+
{
16241631
return PatternKind::Ident(self.0.cast_first_match().unwrap_or_default());
16251632
}
16261633

src/syntax/parser.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -847,11 +847,15 @@ fn pattern(p: &mut Parser) -> PatternKind {
847847
let m = p.marker();
848848

849849
if p.at(SyntaxKind::LeftParen) {
850-
collection(p, false);
850+
let kind = collection(p, false);
851851
validate_destruct_pattern(p, m);
852852
p.wrap(m, SyntaxKind::Pattern);
853853

854-
PatternKind::Destructuring
854+
if kind == SyntaxKind::Parenthesized {
855+
PatternKind::Normal
856+
} else {
857+
PatternKind::Destructuring
858+
}
855859
} else {
856860
if p.expect(SyntaxKind::Ident) {
857861
p.wrap(m, SyntaxKind::Pattern);

tests/typ/compiler/let.typ

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,23 @@ Three
3232
#test(v2, 2)
3333
#test(v3, 3)
3434

35+
---
36+
// Test parenthesised assignments.
37+
// Ref: false
38+
#let (a) = (1, 2)
39+
3540
---
3641
// Ref: false
3742
// Simple destructuring.
3843
#let (a, b) = (1, 2)
3944
#test(a, 1)
4045
#test(b, 2)
4146

47+
---
48+
// Ref: false
49+
#let (a,) = (1,)
50+
#test(a, 1)
51+
4252
---
4353
// Ref: false
4454
// Destructuring with multiple placeholders.
@@ -115,10 +125,6 @@ Three
115125
// Error: 13-14 not enough elements to destructure
116126
#let (a, b, c) = (1, 2)
117127

118-
---
119-
// Error: 6-9 too many elements to destructure
120-
#let (a) = (1, 2)
121-
122128
---
123129
// Error: 6-20 not enough elements to destructure
124130
#let (..a, b, c, d) = (1, 2)

0 commit comments

Comments
 (0)