nash-parse 0.7.1

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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//! Whitespace and comment handling for Nash.
//!
//! Ported from Elm's `Parse/Space.hs`.
//!
//! Handles:
//! - Spaces and newlines
//! - Line comments (`--`)
//! - Multi-line comments (`{- ... -}`) with nesting
//! - Doc comments (`{-| ... -}`)
//! - Indentation checking
//! - Tab detection (tabs are not allowed)

use crate::error::Space;
use crate::{Col, Parser, Row};
use nash_source::{Comment, Snippet};

/// Result of eating spaces.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SpaceStatus {
    /// Successfully consumed whitespace.
    Good,
    /// Encountered a tab character (not allowed).
    HasTab,
    /// Encountered an unclosed multi-line comment.
    EndlessMultiComment(nash_region::Position),
}

impl<'a> Parser<'a> {
    /// Consume whitespace and comments.
    ///
    /// Returns an error if tabs or unclosed comments are found.
    /// Mirrors Elm's `Space.chomp`.
    pub fn chomp<E>(&mut self, to_error: impl FnOnce(Space, Row, Col) -> E) -> Result<(), E> {
        let (status, new_row, new_col) = self.eat_spaces();

        match status {
            SpaceStatus::Good => Ok(()),
            SpaceStatus::HasTab => Err(to_error(Space::HasTab, new_row, new_col)),
            SpaceStatus::EndlessMultiComment(opening) => Err(to_error(
                Space::EndlessMultiComment(opening),
                new_row,
                new_col,
            )),
        }
    }

    /// Consume whitespace and check that we're indented past the current indent level.
    ///
    /// Mirrors Elm's `Space.chompAndCheckIndent`.
    pub fn chomp_and_check_indent<E>(
        &mut self,
        to_space_error: impl FnOnce(Space, Row, Col) -> E,
        to_indent_error: impl FnOnce(Row, Col) -> E,
    ) -> Result<(), E> {
        let (row_before, col_before) = self.position();
        let (status, new_row, new_col) = self.eat_spaces();

        match status {
            SpaceStatus::Good => {
                if new_col > self.indent && new_col > 1 {
                    Ok(())
                } else {
                    let (row, col) = if matches!(self.peek(), Some(b')' | b']' | b'}')) {
                        (new_row, new_col)
                    } else {
                        (row_before, col_before)
                    };
                    Err(to_indent_error(row, col))
                }
            }
            SpaceStatus::HasTab => Err(to_space_error(Space::HasTab, new_row, new_col)),
            SpaceStatus::EndlessMultiComment(opening) => Err(to_space_error(
                Space::EndlessMultiComment(opening),
                new_row,
                new_col,
            )),
        }
    }

    /// Check that the parser's CURRENT column is past the indent level and
    /// not at column 1 (which starts a fresh top-level declaration).
    ///
    /// Mirrors Elm's `Space.checkIndent`: the passed position is only the
    /// location to report on failure; the check itself is `col > indent
    /// && col > 1` on the current state.
    pub fn check_indent<E>(
        &self,
        end_row: Row,
        end_col: Col,
        to_error: impl FnOnce(Row, Col) -> E,
    ) -> Result<(), E> {
        if self.col > self.indent && self.col > 1 {
            Ok(())
        } else {
            // A closing token is present but underindented. Keep its actual
            // position even when intervening whitespace contains comments.
            let (row, col) = if matches!(self.peek(), Some(b')' | b']' | b'}')) {
                self.position()
            } else {
                (end_row, end_col)
            };
            Err(to_error(row, col))
        }
    }

    /// Check that current column equals indent level (for alignment).
    ///
    /// Mirrors Elm's `Space.checkAligned`.
    pub fn check_aligned<E>(&self, to_error: impl FnOnce(usize, Row, Col) -> E) -> Result<(), E> {
        if self.col == self.indent {
            Ok(())
        } else {
            Err(to_error(self.indent, self.row, self.col))
        }
    }

    /// Check that we're at column 1 (start of a fresh line).
    ///
    /// Mirrors Elm's `Space.checkFreshLine`.
    pub fn check_fresh_line<E>(&self, to_error: impl FnOnce(Row, Col) -> E) -> Result<(), E> {
        if self.col == 1 {
            Ok(())
        } else {
            Err(to_error(self.row, self.col))
        }
    }

    /// Parse a doc comment `{-| ... -}`.
    ///
    /// Returns the Comment containing a Snippet of the content.
    /// Mirrors Elm's `Space.docComment`.
    pub fn doc_comment<E>(
        &mut self,
        to_expectation: impl FnOnce(Row, Col) -> E,
        to_space_error: impl FnOnce(Space, Row, Col) -> E,
    ) -> Result<&'a Comment<'a>, E> {
        // Check for {-| at current position
        if self.peek() == Some(0x7B)
            && self.peek_at(1) == Some(0x2D)
            && self.peek_at(2) == Some(0x7C)
        {
            let start_row = self.row;
            let start_col = self.col + 3; // Column after {-|

            // Skip {-|
            self.advance();
            self.advance();
            self.advance();

            let content_start = self.pos;

            // Use the existing multi-comment helper with nesting=1
            let status = self
                .eat_multi_comment_help(1, nash_region::Position::new(start_row, start_col - 3));

            match status {
                SpaceStatus::Good => {
                    // Content ends 2 bytes before current position (before -})
                    let content_end = self.pos - 2;
                    let content = &self.src[content_start..content_end];

                    let snippet = self.alloc(Snippet {
                        data: content,
                        off_row: start_row,
                        off_col: start_col,
                    });
                    let comment = self.alloc(Comment(snippet));

                    Ok(comment)
                }
                SpaceStatus::HasTab => Err(to_space_error(Space::HasTab, self.row, self.col)),
                SpaceStatus::EndlessMultiComment(opening) => Err(to_space_error(
                    Space::EndlessMultiComment(opening),
                    self.row,
                    self.col,
                )),
            }
        } else {
            Err(to_expectation(self.row, self.col))
        }
    }

    /// Core function to eat spaces, newlines, and comments.
    ///
    /// Updates parser position and returns status.
    fn eat_spaces(&mut self) -> (SpaceStatus, Row, Col) {
        loop {
            match self.peek() {
                // Space
                Some(0x20) => {
                    self.advance();
                }

                // Newline
                Some(0x0A) => {
                    self.advance();
                }

                // Carriage return (skip)
                Some(0x0D) => {
                    self.advance();
                }

                // Potential comment start: { or -
                Some(0x7B) => {
                    // Check for {-
                    if self.peek_at(1) == Some(0x2D) {
                        // Check for {-| (doc comment marker - don't consume, let caller handle)
                        if self.peek_at(2) == Some(0x7C) {
                            return (SpaceStatus::Good, self.row, self.col);
                        }
                        // Multi-line comment
                        match self.eat_multi_comment() {
                            SpaceStatus::Good => {
                                // Continue eating spaces
                            }
                            status => return (status, self.row, self.col),
                        }
                    } else {
                        return (SpaceStatus::Good, self.row, self.col);
                    }
                }

                Some(0x2D) => {
                    // Check for --
                    if self.peek_at(1) == Some(0x2D) {
                        self.eat_line_comment();
                        // Continue eating spaces
                    } else {
                        return (SpaceStatus::Good, self.row, self.col);
                    }
                }

                // Tab (not allowed)
                Some(0x09) => {
                    return (SpaceStatus::HasTab, self.row, self.col);
                }

                // Anything else (including EOF)
                _ => {
                    return (SpaceStatus::Good, self.row, self.col);
                }
            }
        }
    }

    /// Eat a line comment (from -- to end of line).
    fn eat_line_comment(&mut self) {
        // Skip the --
        self.advance();
        self.advance();

        loop {
            match self.peek() {
                Some(0x0A) => {
                    // Newline ends the comment
                    self.advance();
                    return;
                }
                Some(_) => {
                    self.advance();
                }
                None => {
                    // EOF ends the comment
                    return;
                }
            }
        }
    }

    /// Eat a multi-line comment ({- ... -}).
    ///
    /// Supports nested comments.
    fn eat_multi_comment(&mut self) -> SpaceStatus {
        let opening = nash_region::Position::new(self.row(), self.col());
        // Skip the {-
        self.advance();
        self.advance();

        self.eat_multi_comment_help(1, opening)
    }

    /// Helper for eating multi-line comments with nesting.
    fn eat_multi_comment_help(
        &mut self,
        mut open_comments: usize,
        opening: nash_region::Position,
    ) -> SpaceStatus {
        loop {
            match self.peek() {
                // Newline
                Some(0x0A) => {
                    self.advance();
                }

                // Tab (not allowed even in comments)
                Some(0x09) => {
                    return SpaceStatus::HasTab;
                }

                // Potential close: -}
                Some(0x2D) => {
                    if self.peek_at(1) == Some(0x7D) {
                        self.advance();
                        self.advance();
                        if open_comments == 1 {
                            return SpaceStatus::Good;
                        } else {
                            open_comments -= 1;
                        }
                    } else {
                        self.advance();
                    }
                }

                // Potential nested open: {-
                Some(0x7B) => {
                    if self.peek_at(1) == Some(0x2D) {
                        self.advance();
                        self.advance();
                        open_comments += 1;
                    } else {
                        self.advance();
                    }
                }

                // Any other character
                Some(_) => {
                    self.advance();
                }

                // EOF without closing
                None => {
                    return SpaceStatus::EndlessMultiComment(opening);
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bumpalo::Bump;

    fn parse_and_chomp(input: &str) -> (SpaceStatus, usize, Row, Col) {
        let bump = Bump::new();
        let src = bump.alloc_str(input);
        let mut parser = Parser::new(&bump, src);

        let (status, row, col) = parser.eat_spaces();
        (status, parser.pos, row, col)
    }

    #[test]
    fn empty() {
        let (status, pos, _, _) = parse_and_chomp("");
        assert_eq!(status, SpaceStatus::Good);
        assert_eq!(pos, 0);
    }

    #[test]
    fn spaces_only() {
        let (status, pos, _, _) = parse_and_chomp("   ");
        assert_eq!(status, SpaceStatus::Good);
        assert_eq!(pos, 3);
    }

    #[test]
    fn newlines() {
        let (status, pos, row, col) = parse_and_chomp("  \n  \n  ");
        assert_eq!(status, SpaceStatus::Good);
        assert_eq!(pos, 8);
        assert_eq!(row, 3);
        assert_eq!(col, 3);
    }

    #[test]
    fn line_comment() {
        let (status, pos, _, _) = parse_and_chomp("-- comment\nfoo");
        assert_eq!(status, SpaceStatus::Good);
        assert_eq!(pos, 11); // After the newline
    }

    #[test]
    fn multi_comment() {
        let (status, pos, _, _) = parse_and_chomp("{- comment -}foo");
        assert_eq!(status, SpaceStatus::Good);
        assert_eq!(pos, 13);
    }

    #[test]
    fn nested_multi_comment() {
        let (status, pos, _, _) = parse_and_chomp("{- outer {- inner -} outer -}foo");
        assert_eq!(status, SpaceStatus::Good);
        assert_eq!(pos, 29);
    }

    #[test]
    fn tab_error() {
        let (status, _, _, _) = parse_and_chomp("  \t  ");
        assert_eq!(status, SpaceStatus::HasTab);
    }

    #[test]
    fn endless_multi_comment() {
        let (status, _, _, _) = parse_and_chomp("{- never closed");
        assert_eq!(
            status,
            SpaceStatus::EndlessMultiComment(nash_region::Position::new(1, 1))
        );
    }

    #[test]
    fn doc_comment_not_consumed() {
        // {-| should not be consumed - it's a doc comment for the caller
        let (status, pos, _, _) = parse_and_chomp("{-| doc -}");
        assert_eq!(status, SpaceStatus::Good);
        assert_eq!(pos, 0); // Not consumed
    }

    #[test]
    fn stops_at_content() {
        let (status, pos, _, _) = parse_and_chomp("  foo");
        assert_eq!(status, SpaceStatus::Good);
        assert_eq!(pos, 2); // Stopped at 'f'
    }

    #[test]
    fn doc_comment_simple() {
        let bump = Bump::new();
        let src = bump.alloc_str("{-| hello -}");
        let mut parser = Parser::new(&bump, src);

        let result = parser.doc_comment(|_, _| "expected", |_, _, _| "space error");
        assert!(result.is_ok());
        let comment = result.unwrap();
        // Content is " hello " (between {-| and -})
        assert_eq!(comment.0.data, b" hello ");
        assert_eq!(comment.0.off_row, 1);
        assert_eq!(comment.0.off_col, 4); // Column after {-|
    }

    #[test]
    fn doc_comment_multiline() {
        let bump = Bump::new();
        let src = bump.alloc_str("{-| line one\nline two -}");
        let mut parser = Parser::new(&bump, src);

        let result = parser.doc_comment(|_, _| "expected", |_, _, _| "space error");
        assert!(result.is_ok());
        let comment = result.unwrap();
        assert_eq!(comment.0.data, b" line one\nline two ");
    }

    #[test]
    fn doc_comment_not_doc() {
        let bump = Bump::new();
        let src = bump.alloc_str("{- not a doc comment -}");
        let mut parser = Parser::new(&bump, src);

        let result = parser.doc_comment(|_, _| "expected", |_, _, _| "space error");
        assert!(result.is_err());
        assert_eq!(result.unwrap_err(), "expected");
    }
}