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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//! Record expression parsing for Nash.
//!
//! Ported from Elm's record parsing in `Parse/Expression.hs`.
//!
//! Handles:
//! - `{}` → empty record
//! - `{ x = 1, y = 2 }` → record literal
//! - `{ model | count = 5 }` → record update
use bumpalo::collections::Vec as BumpVec;
use nash_region::{Located, Position};
use nash_source::{Expr, FieldAssign};
use crate::Parser;
use crate::error::{self, Record};
impl<'a> Parser<'a> {
/// Parse a record expression.
///
/// Mirrors Elm's `record`:
/// ```haskell
/// record start =
/// inContext E.Record (word1 0x7B {- { -} E.Start) $
/// do Space.chompAndCheckIndent E.RecordSpace E.RecordIndentOpen
/// oneOf E.RecordOpen
/// [ do word1 0x7D {-}-} E.RecordOpen
/// addEnd start (Src.Record [])
/// , do starter <- addLocation (Var.lower E.RecordField)
/// Space.chompAndCheckIndent E.RecordSpace E.RecordIndentEquals
/// oneOf E.RecordEquals
/// [ do word1 0x7C {-|-} E.RecordEquals
/// ...
/// , do word1 0x3D {-=-} E.RecordEquals
/// ...
/// ]
/// ]
/// ```
pub(crate) fn record(
&mut self,
start: Position,
) -> Result<&'a Located<Expr<'a>>, error::Expr<'a>> {
self.in_context(
// Wrap Record errors with Expr::Record context
|bump, record_err, row, col| error::Expr::Record(bump.alloc(record_err), row, col),
// Start parser: parse '{'
|p| p.word1(0x7B, error::Expr::Start),
// Body parser: parse record contents
|p| p.record_body(start),
)
}
/// Parse the body of a record after the opening '{'.
fn record_body(&mut self, start: Position) -> Result<&'a Located<Expr<'a>>, Record<'a>> {
// Chomp whitespace and check indent
self.chomp_and_check_indent(Record::Space, Record::IndentOpen)?;
// Check what comes next
self.one_of(
Record::Open,
vec![
// Empty record: just '}'
Box::new(|p: &mut Parser<'a>| {
p.word1(0x7D, Record::Open)?;
let empty: &'a [&'a FieldAssign<'a>] = &[];
Ok(p.add_end(
start,
Expr::Record {
fields: empty,
grouped: false,
},
))
}),
// Non-empty: field name first
Box::new(|p: &mut Parser<'a>| p.record_starter(start)),
],
)
}
/// Parse after the first lowercase identifier in a record.
///
/// At this point we have `{ name` and need to determine:
/// - `{ name | ... }` → record update
/// - `{ name = ... }` → record literal
fn record_starter(&mut self, start: Position) -> Result<&'a Located<Expr<'a>>, Record<'a>> {
// Parse the starter field name
let field_start = self.get_position();
let starter = self.lower_name(Record::Field)?;
let starter_located = self.add_end(field_start, starter);
// Chomp whitespace and check indent
self.chomp_and_check_indent(Record::Space, Record::IndentEquals)?;
// Decide: '|' for update, '=' for literal
self.one_of(
Record::Equals,
vec![
// Record update: '|'
Box::new(|p: &mut Parser<'a>| {
p.word1(0x7C, Record::Equals)?;
p.record_update(start, starter_located)
}),
// Record literal: '='
Box::new(|p: &mut Parser<'a>| {
p.word1(0x3D, Record::Equals)?;
p.record_literal(start, starter_located)
}),
],
)
}
/// Parse a record update: `{ name | field = expr, ... }`.
fn record_update(
&mut self,
start: Position,
target: &'a Located<&'a str>,
) -> Result<&'a Located<Expr<'a>>, Record<'a>> {
// Chomp whitespace and check indent after '|'
self.chomp_and_check_indent(Record::Space, Record::IndentField)?;
// Parse first field
let first_field = self.chomp_field()?;
let mut fields: BumpVec<'a, &'a FieldAssign<'a>> = BumpVec::new_in(self.bump);
fields.push(first_field);
// Parse remaining fields
self.chomp_fields(&mut fields)?;
let slice = fields.into_bump_slice();
Ok(self.add_end(
start,
Expr::Update {
record: target,
fields: slice,
},
))
}
/// Parse a record literal starting after the first '='.
fn record_literal(
&mut self,
start: Position,
first_name: &'a Located<&'a str>,
) -> Result<&'a Located<Expr<'a>>, Record<'a>> {
// Chomp whitespace and check indent after '='
self.chomp_and_check_indent(Record::Space, Record::IndentExpr)?;
// Parse the first field's value
let first_value = self.record_expr()?;
// Check indent after expression
let (end_row, end_col) = self.position();
self.check_indent(end_row, end_col, Record::IndentEnd)?;
// Build first field
let first_field = self.alloc(FieldAssign {
field: first_name,
value: first_value,
});
let mut fields: BumpVec<'a, &'a FieldAssign<'a>> = BumpVec::new_in(self.bump);
fields.push(first_field);
// Parse remaining fields
self.chomp_fields(&mut fields)?;
let slice = fields.into_bump_slice();
Ok(self.add_end(
start,
Expr::Record {
fields: slice,
grouped: false,
},
))
}
/// Parse a single field: `name = expr`.
///
/// Mirrors Elm's `chompField`.
fn chomp_field(&mut self) -> Result<&'a FieldAssign<'a>, Record<'a>> {
// Parse field name
let field_start = self.get_position();
let name = self.lower_name(Record::Field)?;
let field = self.add_end(field_start, name);
// Chomp whitespace and check indent
self.chomp_and_check_indent(Record::Space, Record::IndentEquals)?;
// Parse '='
self.word1(0x3D, Record::Equals)?;
// Chomp whitespace and check indent after '='
self.chomp_and_check_indent(Record::Space, Record::IndentExpr)?;
// Parse the expression
let value = self.record_expr()?;
// Check indent after expression
let (end_row, end_col) = self.position();
self.check_indent(end_row, end_col, Record::IndentEnd)?;
Ok(self.alloc(FieldAssign { field, value }))
}
/// Parse remaining fields after the first.
///
/// Mirrors Elm's `chompFields`.
fn chomp_fields(
&mut self,
fields: &mut BumpVec<'a, &'a FieldAssign<'a>>,
) -> Result<(), Record<'a>> {
loop {
// Chomp whitespace
self.chomp(Record::Space)?;
// Expect comma or closing brace
let done = self.one_of(
Record::End,
vec![
// Comma - parse another field
Box::new(|p: &mut Parser<'a>| {
p.word1(0x2C, Record::End)?;
// Chomp whitespace and check indent after comma
p.chomp_and_check_indent(Record::Space, Record::IndentField)?;
// Parse the field
let field = p.chomp_field()?;
fields.push(field);
Ok(false) // Not done, continue loop
}),
// Closing brace - done
Box::new(|p: &mut Parser<'a>| {
p.word1(0x7D, Record::End)?;
Ok(true) // Done
}),
],
)?;
if done {
return Ok(());
}
}
}
/// Parse a record field expression.
///
/// Mirrors Elm's `specialize E.RecordExpr expression`.
fn record_expr(&mut self) -> Result<&'a Located<Expr<'a>>, Record<'a>> {
self.specialize(
|bump, expr_err, row, col| Record::Expr(bump.alloc(expr_err), row, col),
|p| p.term(),
)
}
}
#[cfg(test)]
mod tests {
use crate::expression::{assert_expr_error_snapshot, assert_expr_snapshot};
#[test]
fn empty() {
assert_expr_snapshot!("{}");
}
#[test]
fn single_field() {
assert_expr_snapshot!("{ x = 1 }");
}
#[test]
fn two_fields() {
assert_expr_snapshot!("{ x = 1, y = 2 }");
}
#[test]
fn many_fields() {
assert_expr_snapshot!("{ a = 1, b = 2, c = 3 }");
}
#[test]
fn nested_record() {
assert_expr_snapshot!("{ inner = { x = 1 } }");
}
#[test]
fn multiline() {
assert_expr_snapshot!(
"{
x = 1,
y = 2,
z = 3
}"
);
}
#[test]
fn update_single() {
assert_expr_snapshot!("{ model | count = 5 }");
}
#[test]
fn update_multiple() {
assert_expr_snapshot!("{ model | x = 1, y = 2 }");
}
#[test]
fn update_multiline() {
assert_expr_snapshot!(
"{
model
| x = 1,
y = 2
}"
);
}
#[test]
fn with_comments() {
assert_expr_snapshot!(
"{
x = 1, -- first field
y = 2 -- second field
}"
);
}
#[test]
fn error_unclosed() {
assert_expr_error_snapshot!("{ x = 1");
}
#[test]
fn error_trailing_comma() {
assert_expr_error_snapshot!("{ x = 1, }");
}
#[test]
fn error_missing_equals() {
assert_expr_error_snapshot!("{ x 1 }");
}
#[test]
fn error_missing_value() {
assert_expr_error_snapshot!("{ x = }");
}
#[test]
fn error_uppercase_field() {
assert_expr_error_snapshot!("{ X = 1 }");
}
}