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
//! Binder parsing utilities
//!
//! This module handles parsing of bindings in Nix attribute sets and let expressions,
//! including both `inherit` statements and attribute assignments (name = value).
use crate::ast::{Binder, Expression, Items, Selector, SimpleSelector, StringPart, Term, Token};
use crate::error::{ParseError, Result};
use super::Parser;
impl Parser {
/// Parse a list of binders (for let expressions and attribute sets)
pub(super) fn parse_binders(&mut self) -> Result<Items<Binder>> {
self.parse_items(
|t| matches!(t, Token::In | Token::BraceClose | Token::Sof),
Self::parse_binder,
)
}
/// Parse a single binder (inherit or assignment)
fn parse_binder(&mut self) -> Result<Binder> {
if matches!(self.current.value, Token::Inherit) {
self.parse_inherit()
} else {
self.parse_assignment()
}
}
/// Parse inherit statement: inherit [ (expr) ] names... ;
fn parse_inherit(&mut self) -> Result<Binder> {
let inherit_tok = self.expect_token(Token::Inherit, "'inherit'")?;
let from = if matches!(self.current.value, Token::ParenOpen) {
let open = self.take_and_advance()?;
let expr = self.parse_expression()?;
let close = self.expect_token(Token::ParenClose, "')'")?;
Some(Term::Parenthesized {
open,
expr: Box::new(expr),
close,
})
} else {
None
};
let mut selectors = Vec::new();
while self.is_simple_selector_start() {
let span = self.current.span;
let sel = self.parse_simple_selector()?;
// Haskell `simpleSelector interpolationRestricted`: an `inherit`
// name written as `${…}` is only accepted when the body is a
// plain string literal (`Term (SimpleString _)`).
if let SimpleSelector::Interpol(ann) = &sel {
let ok = matches!(
&ann.value,
StringPart::Interpolation(w)
if matches!(&w.value, Expression::Term(Term::SimpleString(_)))
);
if !ok {
return Err(ParseError::unexpected(
span,
vec!["identifier".into(), "string".into()],
"interpolation",
));
}
}
selectors.push(sel);
}
let semi = self.expect_token(Token::Semicolon, "';'")?;
Ok(Binder::Inherit {
kw: inherit_tok,
from,
attrs: selectors,
semi,
})
}
/// Parse assignment: selector = expr ;
fn parse_assignment(&mut self) -> Result<Binder> {
let mut selectors = Vec::new();
let first_sel = self.parse_selector()?;
selectors.push(first_sel);
self.parse_dotted_tail(&mut selectors)?;
// Check for common mistake: attribute path followed by semicolon (forgot = and value)
if matches!(self.current.value, Token::Semicolon) {
return Err(ParseError::unexpected(
self.current.span,
vec!["'='".to_string()],
"';'",
));
}
let eq = self.expect_token(Token::Assign, "'='")?;
let expr = self.parse_expression()?;
// Special case: if the expression is an Apply, the user likely forgot
// a semicolon and the parser treated the next line as a function argument.
// Point to the end of the LEFT side (the function) instead of the RIGHT side.
let expr_end_span = match &expr {
Expression::Apply { func, .. } => func.end_span(),
_ => expr.end_span(),
};
if matches!(self.current.value, Token::Semicolon) {
let semi = self.take_and_advance()?;
Ok(Binder::Assignment {
path: selectors,
eq,
value: expr,
semi,
})
} else if matches!(self.current.value, Token::Sof) {
// EOF found - check if this is an unclosed nested set
// If the expression is a set, the closing brace might have belonged to an outer scope
if let Expression::Term(Term::Set {
open: open_brace,
close: close_brace,
..
}) = &expr
{
Err(ParseError::unclosed(close_brace.span, '{', open_brace.span))
} else {
Err(ParseError::unexpected(
expr_end_span,
vec!["';'".to_string()],
"'end of file'",
))
}
} else {
// Missing semicolon - point to the END of the expression
Err(ParseError::unexpected(
expr_end_span,
vec!["';'".to_string()],
format!("'{}'", self.current.value.text()),
))
}
}
/// Parse a selector (with optional dot)
pub(super) fn parse_selector(&mut self) -> Result<Selector> {
let simple_sel = self.parse_simple_selector()?;
Ok(Selector {
dot: None,
selector: simple_sel,
})
}
/// Parse simple selector (identifier, string, or interpolation)
pub(super) fn parse_simple_selector(&mut self) -> Result<SimpleSelector> {
match &self.current.value {
Token::Identifier(_) => {
let ident = self.take_and_advance()?;
Ok(SimpleSelector::ID(ident))
}
Token::DoubleQuote => {
let string = self.parse_simple_string_literal()?;
Ok(SimpleSelector::String(string))
}
Token::InterOpen => {
let interpol = self.parse_selector_interpolation()?;
Ok(SimpleSelector::Interpol(interpol))
}
_ => Err(ParseError::unexpected(
self.current.span,
vec![
"identifier".to_string(),
"string".to_string(),
"interpolation".to_string(),
],
format!("'{}'", self.current.value.text()),
)),
}
}
/// Parse selector path (used in ? member checks)
pub(super) fn parse_selector_path(&mut self) -> Result<Vec<Selector>> {
let mut selectors = Vec::new();
let first_sel = self.parse_simple_selector()?;
selectors.push(Selector {
dot: None,
selector: first_sel,
});
self.parse_dotted_tail(&mut selectors)?;
Ok(selectors)
}
/// Consume zero or more `.selector` segments and append them to `selectors`.
///
/// Shared by attribute-path parsing in assignments and `?` member checks.
/// `parse_postfix_selection` has its own backtracking variant and does not
/// use this.
fn parse_dotted_tail(&mut self, selectors: &mut Vec<Selector>) -> Result<()> {
while matches!(self.current.value, Token::Dot) {
let dot = self.take_and_advance()?;
let simple_sel = self.parse_simple_selector()?;
selectors.push(Selector {
dot: Some(dot),
selector: simple_sel,
});
}
Ok(())
}
/// Check if current token can start a simple selector
pub(super) const fn is_simple_selector_start(&self) -> bool {
matches!(
self.current.value,
Token::Identifier(_) | Token::DoubleQuote | Token::InterOpen
)
}
/// Check if the current token represents the `or` keyword (identifier or actual keyword)
pub(super) fn is_or_token(&self) -> bool {
matches!(self.current.value, Token::OrDefault)
|| matches!(&self.current.value, Token::Identifier(name) if name == "or")
}
}