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
//! Hand-written lexer for Nix
//!
//! Ports the comment normalization logic from nixfmt's Lexer.hs
use crate::ast::{Token, Trivia};
mod comments;
mod cursor;
mod numbers;
mod scan;
mod trivia;
#[cfg(test)]
mod tests;
/// Intermediate trivia representation during parsing
#[derive(Debug, Clone)]
pub enum RawTrivia {
/// Multiple newlines
Newlines(usize),
/// Line comment with text and column position
LineComment { text: String, col: usize },
/// Block comment (`is_doc`, lines)
BlockComment(bool, Vec<String>),
/// Language annotation like /* lua */
LanguageAnnotation(String),
}
/// Cursor-only snapshot of the lexer (no heap state).
#[derive(Clone, Copy)]
pub struct LexerPos {
byte_pos: usize,
line: usize,
column: usize,
}
/// Saved lexer state for backtracking
#[derive(Clone)]
pub struct LexerState {
byte_pos: usize,
line: usize,
column: usize,
trivia_buffer: Trivia,
recent_newlines: usize,
recent_hspace: usize,
}
pub struct Lexer {
/// Original source. The lexer scans it byte-wise for ASCII tokens and
/// only decodes UTF-8 at the cursor when a multi-byte char is observed,
/// avoiding the up-front `Vec<char>` materialisation.
source: Box<str>,
/// Byte offset of the cursor; always on a UTF-8 char boundary.
byte_pos: usize,
pub(crate) line: usize,
pub(crate) column: usize,
/// Accumulated leading trivia for next token
pub(crate) trivia_buffer: Trivia,
pub(crate) recent_newlines: usize,
pub(crate) recent_hspace: usize,
/// Position before last `parse_trivia()` call, for rewinding.
/// Kept as a single value so the four cursor components can never
/// drift out of sync (previously four independent `Option`s).
trivia_start: Option<LexerPos>,
/// Scratch buffer reused by `parse_trivia` so the per-token trivia list
/// does not allocate on every call.
trivia_scratch: Vec<RawTrivia>,
}
impl Lexer {
pub(crate) fn new(source: &str) -> Self {
Self {
source: source.into(),
byte_pos: 0,
line: 1,
column: 0,
trivia_buffer: Trivia::new(),
recent_newlines: 0,
recent_hspace: 0,
trivia_start: None,
trivia_scratch: Vec::new(),
}
}
/// Save current state for backtracking
pub(crate) fn save_state(&self) -> LexerState {
LexerState {
byte_pos: self.byte_pos,
line: self.line,
column: self.column,
trivia_buffer: self.trivia_buffer.clone(),
recent_newlines: self.recent_newlines,
recent_hspace: self.recent_hspace,
}
}
/// Restore saved state
pub(crate) fn restore_state(&mut self, state: LexerState) {
self.byte_pos = state.byte_pos;
self.line = state.line;
self.column = state.column;
self.trivia_buffer = state.trivia_buffer;
self.recent_newlines = state.recent_newlines;
self.recent_hspace = state.recent_hspace;
}
/// Parse a lexeme (token with trivia annotations)
/// This is the main entry point for the parser
pub(crate) fn lexeme(&mut self) -> crate::error::Result<crate::ast::Annotated<Token>> {
let mut leading_trivia = std::mem::take(&mut self.trivia_buffer);
let _ = self.skip_hspace();
// Re-sync: when entering expression mode mid-source (after `${` in a
// string), the lexer has not yet consumed the trivia before the first
// body token. There is no preceding Nix token here, so treat all of it
// as leading trivia rather than splitting off a discarded "trailing".
if matches!(self.peek_byte(), Some(b'\n' | b'\r' | b'#' | b'/')) {
self.parse_trivia();
leading_trivia.extend(trivia::convert_leading(&self.trivia_scratch));
let _ = self.skip_hspace();
}
let token_start = self.byte_pos;
let start_line = self.line;
// next_token() also skips hspace; redundant here but harmless.
let token = self.next_token()?;
let token_end = self.byte_pos;
let end_line = self.line;
let token_span = crate::ast::Span::with_lines(token_start, token_end, start_line, end_line);
// String/path delimiters: defer trivia so the parser sees raw source content.
let skip_trivia = matches!(token, Token::DoubleQuote | Token::DoubleSingleQuote);
let trailing_comment;
if skip_trivia {
trailing_comment = None;
self.trivia_buffer = Trivia::new();
} else if let Some(newlines) = self.fast_ws_trivia() {
// Fast path hit: only whitespace between this token and the next.
trailing_comment = None;
self.trivia_buffer = if newlines > 1 {
Trivia::one(crate::ast::TriviaPiece::EmptyLine)
} else {
Trivia::new()
};
} else {
self.parse_trivia();
let (tc, next) =
trivia::convert_trivia(&self.trivia_scratch, end_line > start_line, self.column);
trailing_comment = tc;
self.trivia_buffer = next;
}
Ok(crate::ast::Annotated {
pre_trivia: leading_trivia,
span: token_span,
value: token,
trail_comment: trailing_comment,
})
}
/// Parse a whole file (expression + final trivia)
pub(crate) fn start_parse(&mut self) {
self.parse_trivia();
let mut leading: Vec<_> = trivia::convert_leading(&self.trivia_scratch).into();
// Leading blank lines never reach the output but make `is_simple` false
// on the file's first term, which flips `prettyApp`'s layout between
// passes. Mirrors `nix/patches/0001-*.patch` on the reference.
let n = leading
.iter()
.take_while(|p| matches!(p, crate::ast::TriviaPiece::EmptyLine))
.count();
leading.drain(..n);
self.trivia_buffer = leading.into();
}
/// Parse trivia and classify it into `(trailing, next_leading)` so the
/// parser does not need direct access to the scratch buffer.
pub(crate) fn parse_and_convert_trivia(
&mut self,
prev_multiline: bool,
) -> (Option<crate::ast::TrailingComment>, Trivia) {
self.parse_trivia();
trivia::convert_trivia(&self.trivia_scratch, prev_multiline, self.column)
}
/// Get current position as a zero-length span (in byte offsets)
pub(crate) const fn current_pos(&self) -> crate::ast::Span {
crate::ast::Span::point(self.byte_pos)
}
/// Skip horizontal whitespace (spaces and tabs, but not newlines)
#[inline]
fn skip_hspace(&mut self) -> usize {
self.take_ascii_while(|b| matches!(b, b' ' | b'\t')).len()
}
/// Consume trivia when it is purely horizontal/vertical whitespace.
/// Returns `Some(newlines)` and leaves the cursor on the next token if no
/// `#` / `/*` was encountered; returns `None` *without consuming anything*
/// otherwise so the slow `parse_trivia` can handle comments.
///
/// This is the overwhelmingly common inter-token case and lets `lexeme`
/// skip both the scratch-vector bookkeeping and `convert_trivia`.
#[inline]
fn fast_ws_trivia(&mut self) -> Option<usize> {
let bytes = self.source.as_bytes();
let mut i = self.byte_pos;
let mut newlines = 0usize;
let mut last_hspace = 0usize;
let mut line = self.line;
while i < bytes.len() {
match bytes[i] {
b' ' | b'\t' => {
i += 1;
last_hspace += 1;
}
b'\n' => {
i += 1;
newlines += 1;
line += 1;
last_hspace = 0;
}
// Comment start (or rare `\r`): bail out to the full path.
b'#' | b'\r' => return None,
b'/' if bytes.get(i + 1) == Some(&b'*') => return None,
_ => break,
}
}
self.trivia_start = Some(self.mark());
if newlines > 0 {
self.line = line;
self.column = last_hspace;
} else {
self.column += last_hspace;
}
self.byte_pos = i;
self.recent_newlines = newlines;
self.recent_hspace = last_hspace;
Some(newlines)
}
/// Parse trivia (comments and whitespace) into `self.trivia_scratch`.
fn parse_trivia(&mut self) {
// Save position before parsing trivia, so we can rewind if needed
self.trivia_start = Some(self.mark());
self.trivia_scratch.clear();
self.recent_newlines = 0;
self.recent_hspace = 0;
loop {
let hspace = self.skip_hspace();
self.recent_hspace = hspace;
if self.is_eof() {
break;
}
match self.peek() {
Some('\n' | '\r') => {
let count = self.parse_newlines();
self.recent_newlines = count;
self.trivia_scratch.push(RawTrivia::Newlines(count));
}
Some('#') => {
let c = self.parse_line_comment();
self.trivia_scratch.push(c);
}
Some('/') if self.at("/*") => {
// try_parse_language_annotation already restores state on
// failure, so no outer save/restore is needed here.
if let Some(lang_annot) = self.try_parse_language_annotation() {
self.trivia_scratch.push(lang_annot);
} else {
let c = self.parse_block_comment();
self.trivia_scratch.push(c);
}
}
_ => break,
}
}
}
/// Parse consecutive newlines, return count
fn parse_newlines(&mut self) -> usize {
let mut count = 0;
while self.eat_one_eol() {
count += 1;
}
count
}
/// Consume a single end-of-line sequence (`\n`, `\r\n`, or bare `\r`).
/// A bare `\r` advances `column` but not `line`, matching the historical
/// behaviour of `parse_newlines`.
#[inline]
pub(super) fn eat_one_eol(&mut self) -> bool {
let bytes = self.source.as_bytes();
match bytes.get(self.byte_pos) {
Some(&b'\n') => {
self.byte_pos += 1;
self.line += 1;
self.column = 0;
true
}
Some(&b'\r') => {
self.byte_pos += 1;
self.column += 1;
if bytes.get(self.byte_pos) == Some(&b'\n') {
self.byte_pos += 1;
self.line += 1;
self.column = 0;
}
true
}
_ => false,
}
}
/// Rewind the last trivia consumed (horizontal spaces, newlines, and comments)
/// Also clears the trivia buffer since rewound trivia should not be attached to next token
pub(crate) fn rewind_trivia(&mut self) {
if let Some(mark) = self.trivia_start {
self.reset(mark);
}
self.recent_hspace = 0;
self.recent_newlines = 0;
self.trivia_buffer.clear();
}
}