nixfmt_rs 0.4.1

Rust implementation of nixfmt with exact Haskell compatibility
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//! Hand-written recursive descent parser for Nix
//!
//! Ports parsing logic from nixfmt's Parser.hs

mod binders;
mod containers;
mod expressions;
mod operators;
mod parameters;
mod path_uri;
mod spans;
mod strings;
mod term;

use crate::ast::{
    Annotated, Binder, Expression, File, Item, Items, Leaf, Parameter, Span, Term, Token, Trailed,
    Trivia,
};
use crate::error::{ParseError, Result};
use crate::lexer::Lexer;

pub struct Parser {
    lexer: Lexer,
    /// Current token
    current: Annotated<Token>,
}

/// Outcome of `finish_abstraction`: either a full lambda was parsed, or no
/// `:`/`@` followed and the tentative parameter is handed back so the caller
/// can reinterpret the surrounding `{ ... }` as a set literal (or report an
/// error). A named enum reads better at call sites than `ControlFlow`, which
/// is conventionally tied to `?`-style short-circuit semantics.
enum SetOrLambda {
    /// A complete `param: body` (optionally with `@`) abstraction.
    Lambda(Expression),
    /// No colon followed; the tentative parameter is returned untouched.
    Set(Parameter),
}

/// Saved parser state for checkpointing
struct ParserState {
    lexer_state: crate::lexer::LexerState,
    current: Annotated<Token>,
}

impl Parser {
    pub(crate) fn new(source: &str) -> Result<Self> {
        let mut lexer = Lexer::new(source);
        lexer.start_parse();
        let current = lexer.lexeme()?;
        Ok(Self { lexer, current })
    }

    /// Parse a complete Nix file
    pub(crate) fn parse_file(&mut self) -> Result<File> {
        let expr = self.parse_expression()?;
        self.expect_eof()?;
        // `lexeme()` already moved trivia after the last real token into the
        // EOF token's `pre_trivia`; that is the file's trailing trivia.
        let trailing_trivia = std::mem::take(&mut self.current.pre_trivia);

        Ok(Trailed {
            value: expr,
            trailing_trivia,
        })
    }

    /// Parse an expression (top-level)
    fn parse_expression(&mut self) -> Result<Expression> {
        // Match Haskell's order: try operation, then abstraction, then keywords
        match &self.current.value {
            Token::Let => {
                // Old-style `let { }` vs modern `let ... in ...`
                if self.lexer.peek() == Some('{') {
                    self.parse_abstraction_or_operation()
                } else {
                    self.parse_let()
                }
            }
            Token::If => self.parse_if(),
            Token::With => self.parse_with(),
            Token::Assert => self.parse_assert(),
            _ => self.parse_abstraction_or_operation(),
        }
    }

    /// Parse abstraction or operation (handles ambiguity)
    fn parse_abstraction_or_operation(&mut self) -> Result<Expression> {
        match &self.current.value {
            Token::BraceOpen => self.parse_set_parameter_or_literal(),
            Token::Identifier(_) => {
                // URI check must precede the lambda-parameter check: both look for `:`.
                if self.looks_like_uri() {
                    return self.parse_operation_or_lambda();
                }

                if self.lexer.peek() == Some('/') && !self.lexer.at("//") {
                    return self.parse_operation_or_lambda();
                }

                let ident = self.take_and_advance()?;

                match self.finish_abstraction(Parameter::Id(ident))? {
                    SetOrLambda::Lambda(abs) => Ok(abs),
                    SetOrLambda::Set(Parameter::Id(ident)) => {
                        let term = self.parse_postfix_selection(Term::Token(ident))?;
                        self.continue_operation_from(Expression::Term(term))
                    }
                    SetOrLambda::Set(_) => unreachable!(),
                }
            }
            _ => self.parse_operation_or_lambda(),
        }
    }

    /// Parse { as either set parameter or set literal
    fn parse_set_parameter_or_literal(&mut self) -> Result<Expression> {
        let saved_state = self.save_state();
        let open_brace = self.take_and_advance()?;
        let open_span = open_brace.span;

        match &self.current.value {
            Token::BraceClose => {
                // Empty set: {} - could be parameter or literal
                let close_brace = self.take_and_advance()?;

                match self.finish_abstraction(Parameter::Set {
                    open: open_brace,
                    attrs: Vec::new(),
                    close: close_brace,
                })? {
                    SetOrLambda::Lambda(abs) => Ok(abs),
                    SetOrLambda::Set(Parameter::Set {
                        open: open_brace,
                        close: mut close_brace,
                        ..
                    }) => {
                        // Empty set literal: trivia on `}` becomes the set's comment items.
                        let items = if close_brace.pre_trivia.is_empty() {
                            Vec::new()
                        } else {
                            vec![Item::Comments(std::mem::take(&mut close_brace.pre_trivia))]
                        };
                        self.finish_set_literal_expr(open_brace, Items(items), close_brace)
                    }
                    SetOrLambda::Set(_) => unreachable!(),
                }
            }
            Token::Identifier(_) => {
                // Try to parse as parameter attributes first
                // If it fails (sees = or .), parse as bindings
                if let Some(attrs) = self.try_parse_param_attrs()? {
                    Self::check_duplicate_formals(&attrs)?;

                    let close_brace =
                        self.expect_closing_delimiter(open_span, '{', Token::BraceClose)?;
                    let close_span = close_brace.span;

                    match self.finish_abstraction(Parameter::Set { open: open_brace, attrs, close: close_brace })? {
                        SetOrLambda::Lambda(abs) => Ok(abs),
                        SetOrLambda::Set(_) => Err(ParseError::invalid(
                            close_span,
                            "set with parameter-like syntax but no colon",
                            Some("use '{ x = ...; }' for set literals or '{ x }: body' for parameters".to_string()),
                        )),
                    }
                } else {
                    // Failed to parse as parameters (saw `=` or `.`); retry as set literal
                    self.restore_state(saved_state);

                    let open_brace = self.take_and_advance()?;

                    let bindings = self.parse_binders()?;
                    let close_brace =
                        self.expect_closing_delimiter(open_span, '{', Token::BraceClose)?;
                    self.finish_set_literal_expr(open_brace, bindings, close_brace)
                }
            }
            Token::Ellipsis => {
                // Definitely a parameter: { ... }
                let attrs = self.parse_param_attrs()?;
                Self::check_duplicate_formals(&attrs)?;

                let close_brace =
                    self.expect_closing_delimiter(open_span, '{', Token::BraceClose)?;
                let close_span = close_brace.span;

                match self.finish_abstraction(Parameter::Set {
                    open: open_brace,
                    attrs,
                    close: close_brace,
                })? {
                    SetOrLambda::Lambda(abs) => Ok(abs),
                    SetOrLambda::Set(_) => Err(ParseError::invalid(
                        close_span,
                        "{ ... } must be followed by ':' or '@'",
                        Some("use '{ x }: body' for function parameters".to_string()),
                    )),
                }
            }
            _ => {
                // Must be set literal with bindings
                let bindings = self.parse_binders()?;
                let close_brace =
                    self.expect_closing_delimiter(open_span, '{', Token::BraceClose)?;
                self.finish_set_literal_expr(open_brace, bindings, close_brace)
            }
        }
    }

    /// After a candidate lambda parameter `first` has been parsed, try to
    /// consume the optional `@ second` part and the mandatory `: body` and
    /// build an `Expression::Lambda`.
    ///
    /// * `Lambda(expr)` – a full abstraction was parsed.
    /// * `Set(first)` – neither `:` nor `@` follows; `first` is handed
    ///   back untouched so the caller can reinterpret it (set literal,
    ///   operation, or a hard error).
    fn finish_abstraction(&mut self, first: Parameter) -> Result<SetOrLambda> {
        match self.current.value {
            Token::Colon => {
                let colon = self.take_and_advance()?;
                let body = self.parse_expression()?;
                Ok(SetOrLambda::Lambda(Expression::Lambda {
                    param: first,
                    colon,
                    body: Box::new(body),
                }))
            }
            Token::At => {
                let at_tok = self.take_and_advance()?;
                let second = self.parse_context_second(&first)?;
                if !matches!(self.current.value, Token::Colon) {
                    if matches!(self.current.value, Token::At) {
                        return Err(ParseError::unexpected(
                            self.current.span,
                            vec!["':'".to_string()],
                            "'@'",
                        ));
                    }
                    return Err(ParseError::invalid(
                        at_tok.span,
                        "@ is only valid in lambda parameters",
                        Some("use 'name1 @ name2: body' for function parameters".to_string()),
                    ));
                }
                let colon = self.expect_token(Token::Colon, "':'")?;
                let body = self.parse_expression()?;
                Ok(SetOrLambda::Lambda(Expression::Lambda {
                    param: Parameter::Context {
                        lhs: Box::new(first),
                        at: at_tok,
                        rhs: Box::new(second),
                    },
                    colon,
                    body: Box::new(body),
                }))
            }
            _ => Ok(SetOrLambda::Set(first)),
        }
    }

    /// Shared tail of the set-literal branches in `parse_set_parameter_or_literal`.
    fn finish_set_literal_expr(
        &mut self,
        open: Leaf,
        bindings: Items<Binder>,
        close: Leaf,
    ) -> Result<Expression> {
        let set_term = Term::Set {
            rec: None,
            open,
            items: bindings,
            close,
        };
        let term = self.parse_postfix_selection(set_term)?;
        self.continue_operation_from(Expression::Term(term))
    }

    /// Parse operation or lambda (needs lookahead for :)
    fn parse_operation_or_lambda(&mut self) -> Result<Expression> {
        let expr = self.parse_application()?;

        // Member check (?) is handled in parse_application so that `?` binds tighter than prefix `!`/`-`.

        if matches!(self.current.value, Token::Colon | Token::At) {
            return Err(Self::reject_non_parameter_expr(&expr));
        }

        self.maybe_parse_binary_operation(expr)
    }

    /// Parse trivia after manually consuming content (strings, paths, etc.)
    /// and return the trailing comment for the previous construct.
    /// This also stores leading trivia for the next token and advances to it.
    fn parse_trailing_trivia_and_advance(
        &mut self,
        prev_multiline: bool,
    ) -> Result<Option<crate::ast::TrailingComment>> {
        let (trail_comment, next_leading) = self.lexer.parse_and_convert_trivia(prev_multiline);

        self.lexer.trivia_buffer = next_leading;
        self.current = self.lexer.lexeme()?;

        Ok(trail_comment)
    }

    /// Wrap a raw-content scanner (strings, paths, URIs) in the common
    /// `Annotated` prologue/epilogue: capture span and leading trivia, run the
    /// scanner, then collect the trailing comment and advance.
    fn with_raw_ann<T>(&mut self, f: impl FnOnce(&mut Self) -> Result<T>) -> Result<Annotated<T>> {
        let span = self.current.span;
        let pre_trivia = std::mem::take(&mut self.current.pre_trivia);
        let value = f(self)?;
        let prev_multiline = self.lexer.line > span.start_line();
        let trail_comment = self.parse_trailing_trivia_and_advance(prev_multiline)?;
        Ok(Annotated {
            pre_trivia,
            span,
            value,
            trail_comment,
        })
    }

    /// Advance to next token
    fn advance(&mut self) -> Result<()> {
        self.current = self.lexer.lexeme()?;
        Ok(())
    }

    /// Take current token (consumes it)
    const fn take_current(&mut self) -> Annotated<Token> {
        let dummy = Annotated {
            pre_trivia: Trivia::new(),
            span: Span::point(0),
            value: Token::Sof,
            trail_comment: None,
        };
        std::mem::replace(&mut self.current, dummy)
    }

    /// Take current token and advance to next (common pattern).
    /// Fused so `self.current` is overwritten once with the next lexeme
    /// instead of once with a dummy and again with the lexeme.
    #[inline]
    fn take_and_advance(&mut self) -> Result<Annotated<Token>> {
        let next = self.lexer.lexeme()?;
        Ok(std::mem::replace(&mut self.current, next))
    }

    /// Collect `pre_trivia` as Comments item if not empty (common pattern)
    /// Parse a sequence of items separated by trivia until `is_end` matches
    /// the current token. Leading trivia on each item (and on the closing
    /// token) is hoisted into `Item::Comments`. Callers must include
    /// `Token::Sof` in `is_end` so EOF terminates the loop; trailing trivia
    /// is not collected at EOF since there is no closing delimiter to own it.
    fn parse_items<T>(
        &mut self,
        is_end: impl Fn(&Token) -> bool,
        mut one: impl FnMut(&mut Self) -> Result<T>,
    ) -> Result<Items<T>> {
        let mut items = Vec::new();
        while !is_end(&self.current.value) {
            self.collect_trivia_as_comments(&mut items);
            let item = one(self)?;
            items.push(Item::Item(item));
        }
        if !matches!(self.current.value, Token::Sof) {
            self.collect_trivia_as_comments(&mut items);
        }
        Ok(Items(items))
    }

    fn collect_trivia_as_comments<T>(&mut self, items: &mut Vec<Item<T>>) {
        if !self.current.pre_trivia.is_empty() {
            items.push(Item::Comments(std::mem::take(&mut self.current.pre_trivia)));
        }
    }

    /// Save current parser state for potential backtracking
    fn save_state(&self) -> ParserState {
        ParserState {
            lexer_state: self.lexer.save_state(),
            current: self.current.clone(),
        }
    }

    /// Restore parser state from a checkpoint
    fn restore_state(&mut self, state: ParserState) {
        self.lexer.restore_state(state.lexer_state);
        self.current = state.current;
    }
    /// Expect a closing delimiter, providing helpful error if not found
    #[allow(clippy::needless_pass_by_value)] // call sites pass `Token::TFoo` literals
    fn expect_closing_delimiter(
        &mut self,
        opening_span: Span,
        opening_char: char,
        closing_token: Token,
    ) -> Result<Annotated<Token>> {
        if self.current.value == closing_token {
            self.take_and_advance()
        } else if matches!(self.current.value, Token::Sof) {
            Err(ParseError::unclosed(
                self.current.span,
                opening_char,
                opening_span,
            ))
        } else {
            // Special case: comma inside parentheses (common mistake from other languages)
            if opening_char == '(' && matches!(self.current.value, Token::Comma) {
                return Err(ParseError::invalid(
                    self.current.span,
                    "comma not allowed inside parentheses",
                    Some("Nix doesn't use commas in parenthesized expressions. For function calls, use spaces: f x y. For multiple values, use a list [x y] or set { a = x; b = y; }".to_string()),
                ));
            }

            if opening_char == '{' && matches!(self.current.value, Token::Colon) {
                return Err(ParseError::invalid(
                    self.current.span,
                    "unexpected ':' inside '{ ... }'",
                    Some(
                        "for a function use '{ args }: body'; for an attribute use 'name = value;'"
                            .to_string(),
                    ),
                ));
            }

            if matches!(
                self.current.value,
                Token::BraceClose | Token::BrackClose | Token::ParenClose | Token::InterClose
            ) {
                return Err(ParseError::invalid(
                    self.current.span,
                    format!(
                        "mismatched delimiter: expected '{}', found '{}'",
                        closing_token.text(),
                        self.current.value.text()
                    ),
                    Some(format!(
                        "change '{}' to '{}' to match the opening '{opening_char}'",
                        self.current.value.text(),
                        closing_token.text(),
                    )),
                ));
            }

            Err(ParseError::unexpected(
                self.current.span,
                vec![format!("'{}'", closing_token.text())],
                format!("'{}'", self.current.value.text()),
            ))
        }
    }

    /// Expect a specific token, advance if it matches, otherwise emit an
    /// `UnexpectedToken` error using `label` as the expected description.
    #[allow(clippy::needless_pass_by_value)] // call sites pass `Token::TFoo` literals
    fn expect_token(&mut self, tok: Token, label: &'static str) -> Result<Leaf> {
        if self.current.value == tok {
            self.take_and_advance()
        } else {
            Err(ParseError::unexpected(
                self.current.span,
                vec![label.to_string()],
                format!("'{}'", self.current.value.text()),
            ))
        }
    }

    /// Expect a `;` and emit a `MissingToken` error mentioning the preceding
    /// construct otherwise.
    fn expect_semicolon_after(&mut self, after: &'static str) -> Result<Leaf> {
        if matches!(self.current.value, Token::Semicolon) {
            self.take_and_advance()
        } else {
            Err(ParseError::missing(self.current.span, "';'", after))
        }
    }

    /// Expect EOF
    fn expect_eof(&self) -> Result<()> {
        if matches!(self.current.value, Token::Sof) {
            Ok(())
        } else {
            Err(ParseError::unexpected(
                self.current.span,
                vec!["end of file".to_string()],
                format!("'{}'", self.current.value.text()),
            ))
        }
    }
}