nash-parse 0.2.2

Parser for the nash programming language
Documentation
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! Pattern parsing for Nash.
//!
//! Ported from Elm's `Parse/Pattern.hs`.
//!
//! Provides:
//! - `pattern_term` - atomic patterns (wildcard, var, ctor, literal, record, tuple, list)
//! - `pattern_expr` - full patterns including cons (::), as-patterns, and ctor with args

mod list;
mod record;
mod term;
mod tuple;

use bumpalo::collections::Vec as BumpVec;
use nash_region::{Located, Position, Region};
use nash_source::Pattern;

use crate::Parser;
use crate::error;

impl<'a> Parser<'a> {
    /// Parse an atomic pattern (no cons, as, or ctor args).
    ///
    /// Mirrors Elm's `Pattern.term`:
    /// ```haskell
    /// term =
    ///   do  start <- getPosition
    ///       oneOf E.PStart
    ///         [ record start
    ///         , tuple start
    ///         , list start
    ///         , termHelp start
    ///         ]
    /// ```
    pub fn pattern_term(&mut self) -> Result<&'a Located<Pattern<'a>>, error::Pattern<'a>> {
        let start = self.get_position();

        self.one_of(
            error::Pattern::Start,
            vec![
                Box::new(|p: &mut Parser<'a>| p.pattern_record(start)),
                Box::new(|p| p.pattern_tuple(start)),
                Box::new(|p| p.pattern_list(start)),
                Box::new(|p| p.pattern_term_help(start)),
            ],
        )
    }

    /// Parse a full pattern expression including cons (::), as-patterns, and ctor with args.
    ///
    /// Returns `(pattern, end)` where `end` is the position at the end of the pattern
    /// (before any trailing whitespace was chomped). This is important for indent checking.
    ///
    /// Mirrors Elm's `Pattern.expression`:
    /// ```haskell
    /// expression :: Space.Parser E.Pattern Src.Pattern
    /// expression =
    ///   do  start <- getPosition
    ///       ePart <- exprPart
    ///       exprHelp start [] ePart
    /// ```
    pub fn pattern_expr(
        &mut self,
    ) -> Result<(&'a Located<Pattern<'a>>, Position), error::Pattern<'a>> {
        let start = self.get_position();
        let (first_pattern, first_end) = self.pattern_expr_part()?;
        self.pattern_expr_help(start, first_pattern, first_end)
    }

    /// Parse a pattern expression part (term or ctor with args).
    ///
    /// Mirrors Elm's `exprPart`.
    fn pattern_expr_part(
        &mut self,
    ) -> Result<(&'a Located<Pattern<'a>>, Position), error::Pattern<'a>> {
        let start = self.get_position();

        // Check if it starts with uppercase (constructor that might have args)
        if matches!(self.peek(), Some(b) if b.is_ascii_uppercase()) {
            let ctor_start = self.pos;
            self.advance();
            self.chomp_inner_chars();

            // Check for qualification
            let (region, module, name) = if self.is_dot_upper() || self.is_dot_lower() {
                self.pattern_ctor_qualified_parts(start, ctor_start)?
            } else {
                let name = self.slice_from(ctor_start);
                let end = self.get_position();
                (Region::new(start, end), None, name)
            };

            // Now try to parse arguments
            self.pattern_ctor_with_args(start, region, module, name)
        } else {
            // Regular term - chomp whitespace but return the end position
            // from BEFORE chomping (extracted from pattern's region)
            let pattern = self.pattern_term()?;
            let end = pattern.region.end;
            self.chomp(error::Pattern::Space)?;
            Ok((pattern, end))
        }
    }

    /// Parse qualified constructor parts, returning (region, module, name).
    fn pattern_ctor_qualified_parts(
        &mut self,
        start: Position,
        ctor_start: usize,
    ) -> Result<(Region, Option<&'a str>, &'a str), error::Pattern<'a>> {
        let (row, col) = self.position();

        // Keep chomping Module.Module... until we hit the final name
        loop {
            if self.is_dot_upper() {
                self.advance(); // consume dot
                self.advance(); // consume first uppercase char
                self.chomp_inner_chars();
            } else if self.is_dot_lower() {
                // Qualified lowercase - error for patterns
                return Err(error::Pattern::Start(row, col));
            } else {
                break;
            }
        }

        let full = self.slice_from(ctor_start);
        let end = self.get_position();
        let region = Region::new(start, end);

        if let Some(last_dot) = full.rfind('.') {
            let module = &full[..last_dot];
            let name = &full[last_dot + 1..];
            Ok((region, Some(module), name))
        } else {
            Ok((region, None, full))
        }
    }

    /// Parse constructor with potential arguments.
    fn pattern_ctor_with_args(
        &mut self,
        start: Position,
        region: Region,
        module: Option<&'a str>,
        name: &'a str,
    ) -> Result<(&'a Located<Pattern<'a>>, Position), error::Pattern<'a>> {
        let mut end = self.get_position();
        self.chomp(error::Pattern::Space)?;

        let mut args: BumpVec<'a, &'a Located<Pattern<'a>>> = BumpVec::new_in(self.bump);

        // Try to parse arguments (terms only, not full expressions)
        loop {
            let arg_result = self.one_of_with_fallback(
                vec![Box::new(|p: &mut Parser<'a>| {
                    // Check indent before trying to parse arg
                    let (check_row, check_col) = p.position();
                    p.check_indent(check_row, check_col, error::Pattern::IndentStart)?;

                    let arg = p.pattern_term()?;
                    Ok(Some(arg))
                })],
                None,
            )?;

            match arg_result {
                Some(arg) => {
                    args.push(arg);
                    end = self.get_position();
                    self.chomp(error::Pattern::Space)?;
                }
                None => break,
            }
        }

        let args_slice = args.into_bump_slice();
        let pattern = match module {
            Some(module) => Pattern::CtorQual {
                region,
                module,
                name,
                args: args_slice,
            },
            None => Pattern::Ctor {
                region,
                name,
                args: args_slice,
            },
        };

        Ok((self.add_end(start, pattern), end))
    }

    /// Parse the rest of a pattern expression (cons and as).
    ///
    /// Returns `(pattern, end)` where `end` is the position at the end of the pattern.
    ///
    /// Mirrors Elm's `exprHelp`.
    fn pattern_expr_help(
        &mut self,
        start: Position,
        pattern: &'a Located<Pattern<'a>>,
        end: Position,
    ) -> Result<(&'a Located<Pattern<'a>>, Position), error::Pattern<'a>> {
        let mut patterns: BumpVec<'a, &'a Located<Pattern<'a>>> = BumpVec::new_in(self.bump);
        let mut current = pattern;
        let mut current_end = end;

        loop {
            // Check indent at the end position (before chomp), not current parser position.
            // If indent check fails, we're done - return what we have.
            if self
                .check_indent(
                    current_end.line,
                    current_end.column,
                    error::Pattern::IndentStart,
                )
                .is_err()
            {
                let result = self.build_cons_chain(&mut patterns, current);
                return Ok((result, current_end));
            }

            // Try to parse :: or as
            let result = self.one_of_with_fallback(
                vec![
                    // Cons: `::`
                    Box::new(|p: &mut Parser<'a>| {
                        p.word2(0x3A, 0x3A, error::Pattern::Start)?; // ::
                        p.chomp_and_check_indent(error::Pattern::Space, error::Pattern::IndentStart)?;

                        let (next_pattern, next_end) = p.pattern_expr_part()?;
                        Ok(ConsOrAs::Cons(next_pattern, next_end))
                    }),
                    // As: `as name`
                    Box::new(|p: &mut Parser<'a>| {
                        // Check for "as" keyword
                        let (row, col) = p.position();
                        if !p.remaining().starts_with(b"as")
                            || matches!(p.peek_at(2), Some(b) if b.is_ascii_alphanumeric() || b == b'_')
                        {
                            return Err(error::Pattern::Start(row, col));
                        }
                        p.advance_by(2);

                        p.chomp_and_check_indent(error::Pattern::Space, error::Pattern::IndentAlias)?;

                        let name_start = p.get_position();
                        let name = p.lower_name(error::Pattern::Alias)?;
                        let name_end = p.get_position();
                        p.chomp(error::Pattern::Space)?;

                        let alias = p.add_end(name_start, name);
                        Ok(ConsOrAs::As(alias, name_end))
                    }),
                ],
                ConsOrAs::Done,
            )?;

            match result {
                ConsOrAs::Cons(next_pattern, next_end) => {
                    patterns.push(current);
                    current = next_pattern;
                    current_end = next_end;
                }
                ConsOrAs::As(alias, alias_end) => {
                    // Build up cons chain first
                    let base = self.build_cons_chain(&mut patterns, current);
                    // Then wrap in alias
                    return Ok((
                        self.add_end(
                            start,
                            Pattern::Alias {
                                pattern: base,
                                name: alias,
                            },
                        ),
                        alias_end,
                    ));
                }
                ConsOrAs::Done => {
                    // Build up cons chain
                    let result = self.build_cons_chain(&mut patterns, current);
                    return Ok((result, current_end));
                }
            }
        }
    }

    /// Build a cons chain from accumulated patterns.
    fn build_cons_chain(
        &self,
        patterns: &mut BumpVec<'a, &'a Located<Pattern<'a>>>,
        tail: &'a Located<Pattern<'a>>,
    ) -> &'a Located<Pattern<'a>> {
        if patterns.is_empty() {
            tail
        } else {
            // Build right-to-left: a :: b :: c => Cons(a, Cons(b, c))
            let mut result = tail;
            while let Some(head) = patterns.pop() {
                let region = Region::new(
                    Position::new(head.region.start.line, head.region.start.column),
                    Position::new(result.region.end.line, result.region.end.column),
                );
                result = self.alloc(Located::at(region, Pattern::Cons { head, tail: result }));
            }
            result
        }
    }
}

/// Helper enum for pattern expression parsing.
enum ConsOrAs<'a> {
    Cons(&'a Located<Pattern<'a>>, Position),
    As(&'a Located<&'a str>, Position),
    Done,
}

/// Snapshot test macro for successful pattern parsing.
#[cfg(test)]
macro_rules! assert_pattern_snapshot {
    ($code:expr) => {{
        let bump = bumpalo::Bump::new();
        let src = bump.alloc_str(indoc::indoc!($code));
        let mut parser = $crate::Parser::new(&bump, src.as_bytes());
        let (result, _end) = parser.pattern_expr().expect("expected successful parse");

        insta::with_settings!({
            description => format!("Code:\n\n{}", indoc::indoc!($code)),
            omit_expression => true,
        }, {
            insta::assert_debug_snapshot!(result);
        });
    }};
}

/// Snapshot test macro for pattern parse errors.
#[cfg(test)]
macro_rules! assert_pattern_error_snapshot {
    ($code:expr) => {{
        let bump = bumpalo::Bump::new();
        let src = bump.alloc_str(indoc::indoc!($code));
        let mut parser = $crate::Parser::new(&bump, src.as_bytes());
        let result = parser.pattern_expr().expect_err("expected parse error");

        insta::with_settings!({
            description => format!("Code:\n\n{}", indoc::indoc!($code)),
            omit_expression => true,
        }, {
            insta::assert_debug_snapshot!(result);
        });
    }};
}

/// Snapshot test macro for multiline patterns, laid out as they would
/// appear indented inside a definition (see `test_support::indent_fragment`).
#[cfg(test)]
macro_rules! assert_indented_pattern_snapshot {
    ($code:expr) => {{
        let bump = bumpalo::Bump::new();
        let fragment = indoc::indoc!($code);
        let indented = $crate::test_support::indent_fragment(fragment);
        let src = bump.alloc_str(&indented);
        let mut parser = $crate::Parser::new(&bump, src.as_bytes());
        parser
            .chomp(|_, _, _| "space error")
            .expect("expected leading indent");
        let (result, _end) = parser.pattern_expr().expect("expected successful parse");

        insta::with_settings!({
            description => format!("Code (indented inside a def):\n\n{}", indented),
            omit_expression => true,
        }, {
            insta::assert_debug_snapshot!(result);
        });
    }};
}

#[cfg(test)]
pub(crate) use assert_indented_pattern_snapshot;
#[cfg(test)]
pub(crate) use assert_pattern_error_snapshot;
#[cfg(test)]
pub(crate) use assert_pattern_snapshot;

#[cfg(test)]
mod tests {
    // Cons patterns
    #[test]
    fn cons_simple() {
        assert_pattern_snapshot!("head :: tail");
    }

    #[test]
    fn cons_multiple() {
        assert_pattern_snapshot!("a :: b :: c");
    }

    #[test]
    fn cons_with_empty_list() {
        assert_pattern_snapshot!("head :: []");
    }

    // As patterns
    #[test]
    fn as_simple() {
        assert_pattern_snapshot!("x as foo");
    }

    #[test]
    fn as_with_tuple() {
        assert_pattern_snapshot!("(a, b) as pair");
    }

    #[test]
    fn as_with_cons() {
        assert_pattern_snapshot!("head :: tail as list");
    }

    // Constructor with args
    #[test]
    fn ctor_with_one_arg() {
        assert_pattern_snapshot!("Just x");
    }

    #[test]
    fn ctor_with_multiple_args() {
        assert_pattern_snapshot!("Node left value right");
    }

    #[test]
    fn ctor_qualified_with_args() {
        assert_pattern_snapshot!("Maybe.Just x");
    }

    #[test]
    fn ctor_nested() {
        assert_pattern_snapshot!("Just (Just x)");
    }

    // Complex combinations
    #[test]
    fn complex_pattern() {
        assert_pattern_snapshot!("Just x :: rest as list");
    }
}