1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! List expression parsing for Nash.
//!
//! Ported from Elm's list parsing in `Parse/Expression.hs`.
use bumpalo::collections::Vec as BumpVec;
use nash_region::{Located, Position};
use nash_source::Expr;
use crate::Parser;
use crate::error::{self, List};
impl<'a> Parser<'a> {
/// Parse a list expression.
///
/// Mirrors Elm's `list`:
/// ```haskell
/// list start =
/// inContext E.List (word1 0x5B {-[-} E.Start) $
/// do Space.chompAndCheckIndent E.ListSpace E.ListIndentOpen
/// oneOf E.ListOpen
/// [ do (entry, end) <- specialize E.ListExpr expression
/// Space.checkIndent end E.ListIndentEnd
/// chompListEnd start [entry]
/// , do word1 0x5D {-]-} E.ListOpen
/// addEnd start (Src.List [])
/// ]
/// ```
pub(crate) fn list(
&mut self,
start: Position,
) -> Result<&'a Located<Expr<'a>>, error::Expr<'a>> {
self.in_context(
// Wrap List errors with Expr::List context
|bump, list_err, row, col| error::Expr::List(bump.alloc(list_err), row, col),
// Start parser: parse '['
|p| p.word1(0x5B, error::Expr::Start),
// Body parser: parse list contents
|p| p.list_body(start),
)
}
/// Parse the body of a list after the opening '['.
///
/// Returns `List` errors which get wrapped by `in_context`.
fn list_body(&mut self, start: Position) -> Result<&'a Located<Expr<'a>>, List<'a>> {
// Chomp whitespace and check indent
self.chomp_and_check_indent(List::Space, List::IndentOpen)?;
// Check for empty list or first element
self.one_of(
List::Open,
vec![
// Try to parse first element
Box::new(|p: &mut Parser<'a>| {
let (first, end) = p.list_expr()?;
// Check indent using expression's end position (not current parser position)
p.check_indent(end.line, end.column, List::IndentEnd)?;
// Parse remaining elements
let mut elements = BumpVec::new_in(p.bump);
elements.push(first);
p.chomp_list_end(&mut elements)?;
let slice = elements.into_bump_slice();
Ok(p.add_end(start, Expr::List(slice)))
}),
// Empty list: just ']'
Box::new(|p: &mut Parser<'a>| {
p.word1(0x5D, List::Open)?;
let slice: &'a [&'a Located<Expr<'a>>] = &[];
Ok(p.add_end(start, Expr::List(slice)))
}),
],
)
}
/// Parse a list entry expression.
///
/// Mirrors Elm's `specialize E.ListExpr expression`.
/// Returns both the expression and its end position for indent checking.
fn list_expr(&mut self) -> Result<(&'a Located<Expr<'a>>, Position), List<'a>> {
self.specialize(
|bump, expr_err, row, col| List::Expr(bump.alloc(expr_err), row, col),
|p| p.expression(),
)
}
/// Parse the rest of a list after the first element.
///
/// Mirrors Elm's `chompListEnd`:
/// ```haskell
/// chompListEnd start entries =
/// oneOf E.ListEnd
/// [ do word1 0x2C {-,-} E.ListEnd
/// Space.chompAndCheckIndent E.ListSpace E.ListIndentExpr
/// (entry, end) <- specialize E.ListExpr expression
/// Space.checkIndent end E.ListIndentEnd
/// chompListEnd start (entry:entries)
/// , do word1 0x5D {-]-} E.ListEnd
/// addEnd start (Src.List (reverse entries))
/// ]
/// ```
fn chomp_list_end(
&mut self,
elements: &mut BumpVec<'a, &'a Located<Expr<'a>>>,
) -> Result<(), List<'a>> {
loop {
// Chomp whitespace between elements
self.chomp(List::Space)?;
// Expect comma or closing bracket
let done = self.one_of(
List::End,
vec![
// Comma - parse another element
Box::new(|p: &mut Parser<'a>| {
p.word1(0x2C, List::End)?;
// Chomp whitespace and check indent after comma
p.chomp_and_check_indent(List::Space, List::IndentExpr)?;
// Parse the expression
let (elem, end) = p.list_expr()?;
elements.push(elem);
// Check indent using expression's end position
p.check_indent(end.line, end.column, List::IndentEnd)?;
Ok(false) // Not done, continue loop
}),
// Closing bracket - done
Box::new(|p: &mut Parser<'a>| {
p.word1(0x5D, List::End)?;
Ok(true) // Done
}),
],
)?;
if done {
return Ok(());
}
}
}
}
#[cfg(test)]
mod tests {
use crate::expression::{assert_expr_snapshot, assert_indented_expr_snapshot};
#[test]
fn empty() {
assert_expr_snapshot!("[]");
}
#[test]
fn multiple() {
assert_expr_snapshot!("[1, 2, 3]");
}
#[test]
fn nested() {
assert_expr_snapshot!("[[1], [2, 3]]");
}
#[test]
fn with_whitespace() {
assert_expr_snapshot!("[ 1 , 2 , 3 ]");
}
#[test]
fn multiline() {
assert_indented_expr_snapshot!(
"[
1,
2,
3
]"
);
}
#[test]
fn with_comments() {
assert_indented_expr_snapshot!(
"[
1, -- first
2, -- second
3 -- third
]"
);
}
#[test]
fn mixed_types() {
assert_expr_snapshot!(r#"[foo, "bar", 42]"#);
}
}