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
impl Lexer {
pub fn new(input: &str) -> Self {
Self {
input: input.chars().collect(),
position: 0,
line: 1,
column: 1,
}
}
pub fn tokenize(&mut self) -> Result<Vec<Token>, LexerError> {
if self.input.is_empty() {
return Ok(vec![Token::Eof]);
}
// Contract: parser-soundness-v1.yaml precondition (pv codegen)
contract_pre_lex!(self.input);
let mut tokens = Vec::new();
loop {
self.skip_whitespace_except_newline();
if self.is_at_end() {
tokens.push(Token::Eof);
break;
}
let token = self.next_token()?;
tokens.push(token.clone());
if token == Token::Eof {
break;
}
}
Ok(tokens)
}
/// Tokenize with character positions for each token.
/// Returns (tokens, positions) where positions[i] is the byte offset of tokens[i].
pub fn tokenize_with_positions(&mut self) -> Result<(Vec<Token>, Vec<usize>), LexerError> {
let mut tokens = Vec::new();
let mut positions = Vec::new();
loop {
self.skip_whitespace_except_newline();
if self.is_at_end() {
positions.push(self.position);
tokens.push(Token::Eof);
break;
}
let pos = self.position;
let token = self.next_token()?;
positions.push(pos);
tokens.push(token.clone());
if token == Token::Eof {
break;
}
}
Ok((tokens, positions))
}
fn next_token(&mut self) -> Result<Token, LexerError> {
if self.is_at_end() {
return Ok(Token::Eof);
}
let ch = self.current_char();
// Comments
if ch == '#' {
return Ok(self.read_comment());
}
// Newlines
if ch == '\n' {
self.advance();
return Ok(Token::Newline);
}
// Variables
if ch == '$' {
return self.read_variable();
}
// Strings
if ch == '"' || ch == '\'' {
return self.read_string(ch);
}
// Numbers
if ch.is_ascii_digit() {
return self.read_number();
}
// Identifiers and keywords
if ch.is_alphabetic() || ch == '_' {
return Ok(self.read_identifier_or_keyword());
}
// Bare words (paths, globs, etc) - must come before operators
// These are unquoted strings that can contain / * . - : + % \ , = etc
// Note: ':' is included for bash builtin no-op command (BUILTIN-001)
// Note: '+' and '%' are included for flags like date +%FORMAT (PARSER-ENH-001)
// Note: '\\' is included for escaped chars like \\; in find -exec
// Issue #131: ',' is included for Docker mount options like type=bind,source=...,target=...
// BUG-012 FIX: Don't treat '+=' as bare word - it's the append operator
let is_append_op = ch == '+' && self.peek_char(1) == Some('=');
if !is_append_op
&& (ch == '/'
|| ch == '.'
|| ch == '-'
|| ch == '*'
|| ch == '~'
|| ch == ':'
|| ch == '+'
|| ch == '%'
|| ch == '\\'
|| ch == ',')
{
return Ok(self.read_bare_word());
}
// Operators and symbols
self.read_operator()
}
fn current_char(&self) -> char {
self.input[self.position]
}
fn peek_char(&self, offset: usize) -> Option<char> {
self.input.get(self.position + offset).copied()
}
fn advance(&mut self) -> char {
let ch = self.current_char();
self.position += 1;
if ch == '\n' {
self.line += 1;
self.column = 1;
} else {
self.column += 1;
}
ch
}
fn is_at_end(&self) -> bool {
self.position >= self.input.len()
}
fn skip_whitespace_except_newline(&mut self) {
while !self.is_at_end() {
let ch = self.current_char();
if ch == ' ' || ch == '\t' || ch == '\r' {
self.advance();
} else if ch == '\\' && self.peek_char(1) == Some('\n') {
// Backslash-newline is line continuation — skip both characters
// and continue reading the next line as part of the current command
self.advance(); // skip backslash
self.advance(); // skip newline
self.line += 1;
self.column = 1;
} else {
break;
}
}
}
fn read_comment(&mut self) -> Token {
self.advance(); // skip '#'
let mut comment = String::new();
while !self.is_at_end() && self.current_char() != '\n' {
comment.push(self.advance());
}
Token::Comment(comment)
}
fn read_variable(&mut self) -> Result<Token, LexerError> {
self.advance(); // skip '$'
// Handle $'...' ANSI-C quoting: $'\t' $'\n' etc.
if !self.is_at_end() && self.current_char() == '\'' {
return Ok(self.read_ansi_c_string());
}
// Check for arithmetic expansion $((...)) vs command substitution $(cmd)
if !self.is_at_end() && self.current_char() == '(' {
if let Some('(') = self.peek_char(1) {
return self.read_arithmetic_expansion();
} else {
return self.read_command_substitution();
}
}
// Check for $$ (process ID special variable)
if !self.is_at_end() && self.current_char() == '$' {
self.advance();
return Ok(Token::Variable("$".to_string()));
}
// Check for $@ (all positional parameters special variable)
if !self.is_at_end() && self.current_char() == '@' {
self.advance();
return Ok(Token::Variable("@".to_string()));
}
// Handle shell special variables: $#, $?, $!, $-
if !self.is_at_end() && matches!(self.current_char(), '#' | '?' | '!' | '-') {
let special = self.advance();
return Ok(Token::Variable(special.to_string()));
}
// Handle ${VAR} syntax (with nested expansion support)
// BUG-001 FIX: Handle nested parameter expansion like ${foo:-${bar:-default}}
let var_name = if !self.is_at_end() && self.current_char() == '{' {
self.read_braced_variable()
} else {
self.read_simple_variable_name()
};
Ok(Token::Variable(var_name))
}
/// Read ANSI-C quoted string: $'\t' $'\n' etc.
fn read_ansi_c_string(&mut self) -> Token {
self.advance(); // skip opening '
let mut value = String::new();
while !self.is_at_end() && self.current_char() != '\'' {
if self.current_char() == '\\' {
self.advance(); // skip backslash
if !self.is_at_end() {
let escaped = self.decode_ansi_c_escape();
value.push_str(&escaped);
self.advance();
}
} else {
value.push(self.advance());
}
}
if !self.is_at_end() {
self.advance(); // skip closing '
}
Token::String(value)
}
/// Decode a single ANSI-C escape character at the current position.
/// Returns the replacement string (usually one char, two for unknown escapes).
fn decode_ansi_c_escape(&self) -> String {
match self.current_char() {
'n' => "\n".to_string(),
't' => "\t".to_string(),
'r' => "\r".to_string(),
'a' => "\x07".to_string(),
'b' => "\x08".to_string(),
'e' | 'E' => "\x1b".to_string(),
'f' => "\x0c".to_string(),
'v' => "\x0b".to_string(),
'\\' => "\\".to_string(),
'\'' => "'".to_string(),
'"' => "\"".to_string(),
other => format!("\\{}", other),
}
}
/// Read a braced variable expansion: ${VAR}, ${foo:-default}, ${foo:-${bar:-x}}
fn read_braced_variable(&mut self) -> String {
self.advance(); // skip '{'
let mut var_name = String::new();
let mut brace_depth = 1;
while !self.is_at_end() && brace_depth > 0 {
let ch = self.current_char();
if ch == '{' {
brace_depth += 1;
var_name.push(self.advance());
} else if ch == '}' {
brace_depth -= 1;
if brace_depth > 0 {
var_name.push(self.advance());
} else {
self.advance(); // skip final '}'
}
} else if ch == '$' {
var_name.push(self.advance());
if !self.is_at_end() && self.current_char() == '{' {
brace_depth += 1;
var_name.push(self.advance());
}
} else {
var_name.push(self.advance());
}
}
var_name
}
/// Read a simple (unbraced) variable name: alphanumeric and underscore chars.
fn read_simple_variable_name(&mut self) -> String {
let mut var_name = String::new();
while !self.is_at_end() {
let ch = self.current_char();
if ch.is_alphanumeric() || ch == '_' {
var_name.push(self.advance());
} else {
break;
}
}
var_name
}
fn read_arithmetic_expansion(&mut self) -> Result<Token, LexerError> {
// Skip '(('
self.advance(); // skip first '('
self.advance(); // skip second '('
let mut expr = String::new();
let mut paren_depth = 0;
while !self.is_at_end() {
let ch = self.current_char();
// Handle nested parentheses
if ch == '(' {
paren_depth += 1;
expr.push(self.advance());
} else if ch == ')' {
// Check if this closes the arithmetic expansion
if paren_depth == 0 && self.peek_char(1) == Some(')') {
self.advance(); // skip first ')'
self.advance(); // skip second ')'
break;
} else {
paren_depth -= 1;
expr.push(self.advance());
}
} else {
expr.push(self.advance());
}
}
Ok(Token::ArithmeticExpansion(expr))
}
fn read_command_substitution(&mut self) -> Result<Token, LexerError> {
// Skip '('
self.advance(); // skip '('
let mut command = String::new();
let mut paren_depth = 0;
while !self.is_at_end() {
let ch = self.current_char();
// Handle nested command substitutions: $(outer $(inner))
if ch == '(' {
paren_depth += 1;
command.push(self.advance());
} else if ch == ')' {
// Check if this closes the command substitution
if paren_depth == 0 {
self.advance(); // skip closing ')'
break;
} else {
paren_depth -= 1;
command.push(self.advance());
}
} else {
command.push(self.advance());
}
}
Ok(Token::CommandSubstitution(command))
}
}
include!("lexer_read_string.rs");