bashrs 6.66.0

Rust-to-Shell transpiler for deterministic bootstrap scripts
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
impl Lexer {
    /// Characters that are valid in bare words (unquoted strings).
    /// Includes alphanumeric, path separators, globs, dots, dashes, plus, percent, etc.
    fn is_bare_word_char(ch: char) -> bool {
        ch.is_alphanumeric()
            || matches!(
                ch,
                '/' | '.' | '-' | '_' | '*' | '?' | '~' | ':' | '+' | '%' | ',' | '=' | '@'
            )
    }

    /// Issue #69: Check if current position starts a brace expansion
    /// Brace expansion: {a,b,c} or {1..10}
    fn is_brace_expansion(&self) -> bool {
        // Look ahead to see if there's a comma or .. inside the braces
        // Must skip quoted strings to avoid false positives like { echo "a,b" }
        let mut i = self.position + 1; // Skip the '{'
        let mut depth = 1;
        let mut in_single_quote = false;
        let mut in_double_quote = false;

        while i < self.input.len() && depth > 0 {
            let ch = self.input[i];

            // Handle quote state
            if ch == '\'' && !in_double_quote {
                in_single_quote = !in_single_quote;
                i += 1;
                continue;
            }
            if ch == '"' && !in_single_quote {
                in_double_quote = !in_double_quote;
                i += 1;
                continue;
            }

            // Skip content inside quotes
            if in_single_quote || in_double_quote {
                i += 1;
                continue;
            }

            // Check for newlines - function bodies have newlines, brace expansion doesn't
            if ch == '\n' {
                return false; // Not brace expansion - likely function body
            }

            match ch {
                '{' => depth += 1,
                '}' => {
                    depth -= 1;
                    if depth == 0 {
                        return false; // No expansion marker found before closing brace
                    }
                }
                ',' => return true, // Found comma - it's brace expansion
                '.' if i + 1 < self.input.len() && self.input[i + 1] == '.' => {
                    return true; // Found .. - it's sequence expansion
                }
                _ => {}
            }
            i += 1;
        }
        false
    }

    /// Issue #67: Read process substitution <(cmd) or >(cmd)
    fn read_process_substitution(&mut self, direction: char) -> Result<Token, LexerError> {
        self.advance(); // skip '<' or '>'
        self.advance(); // skip '('

        let mut content = String::new();
        let mut depth = 1;

        while !self.is_at_end() && depth > 0 {
            let ch = self.current_char();
            if ch == '(' {
                depth += 1;
            } else if ch == ')' {
                depth -= 1;
                if depth == 0 {
                    self.advance();
                    break;
                }
            }
            content.push(self.advance());
        }

        // Return as identifier for now - proper AST support would need a new variant
        Ok(Token::Identifier(format!("{}({})", direction, content)))
    }

    /// Issue #67: Read standalone arithmetic ((expr))
    fn read_standalone_arithmetic(&mut self) -> Result<Token, LexerError> {
        self.advance(); // skip first '('
        self.advance(); // skip second '('

        let mut content = String::new();
        let mut depth = 2; // Started with ((

        while !self.is_at_end() && depth > 0 {
            let ch = self.current_char();
            if ch == '(' {
                depth += 1;
                content.push(self.advance());
            } else if ch == ')' {
                depth -= 1;
                if depth <= 1 {
                    // depth <= 1 means we've seen the first ) of ))
                    // Don't push it, just advance past both closing parens
                    self.advance(); // skip first ')'
                    if !self.is_at_end() && self.current_char() == ')' {
                        self.advance(); // skip second ')'
                    }
                    break;
                }
                content.push(self.advance());
            } else {
                content.push(self.advance());
            }
        }

        // Return as arithmetic expansion token
        Ok(Token::ArithmeticExpansion(content))
    }

    /// Issue #69: Read brace expansion as a single identifier token
    /// {a,b,c} -> Token::Identifier("{a,b,c}")
    fn read_brace_expansion(&mut self) -> Result<Token, LexerError> {
        let mut expansion = String::new();
        let mut depth = 0;

        while !self.is_at_end() {
            let ch = self.current_char();

            if ch == '{' {
                depth += 1;
            } else if ch == '}' {
                depth -= 1;
                if depth == 0 {
                    expansion.push(self.advance());
                    break;
                }
            }

            expansion.push(self.advance());
        }

        Ok(Token::Identifier(expansion))
    }

    fn read_operator(&mut self) -> Result<Token, LexerError> {
        let ch = self.current_char();
        let next_ch = self.peek_char(1);

        // Delegate to specialized helpers based on the first character
        match ch {
            '<' | '>' => return self.read_redirect_or_comparison(ch, next_ch),
            '=' => return self.read_equality_or_assign(next_ch),
            '@' | '+' | '?' if next_ch == Some('(') => {
                return self.read_extended_glob(ch);
            }
            '!' if next_ch == Some('(') => return self.read_extended_glob(ch),
            ';' => return self.read_semicolon_operator(next_ch),
            _ => {}
        }

        // Handle remaining operators inline (simple single/double char ops)
        let token = match (ch, next_ch) {
            ('!', Some('=')) => {
                self.advance();
                self.advance();
                Token::Ne
            }
            ('!', _) => {
                self.advance();
                Token::Not
            }
            ('&', Some('&')) => {
                self.advance();
                self.advance();
                Token::And
            }
            ('&', _) => {
                self.advance();
                Token::Ampersand
            }
            ('|', Some('|')) => {
                self.advance();
                self.advance();
                Token::Or
            }
            ('|', _) => {
                self.advance();
                Token::Pipe
            }
            ('[', Some('[')) => {
                self.advance();
                self.advance();
                Token::DoubleLeftBracket
            }
            ('[', _) => {
                self.advance();
                Token::LeftBracket
            }
            (']', Some(']')) => {
                self.advance();
                self.advance();
                Token::DoubleRightBracket
            }
            (']', _) => {
                self.advance();
                Token::RightBracket
            }
            ('+', Some('=')) => {
                // BUG-012 FIX: Array append +=
                self.advance(); // skip '+'
                self.advance(); // skip '='
                Token::Identifier("+=".to_string())
            }
            ('(', Some('(')) => {
                // Issue #67: Standalone arithmetic ((expr))
                return self.read_standalone_arithmetic();
            }
            ('(', _) => {
                self.advance();
                Token::LeftParen
            }
            (')', _) => {
                self.advance();
                Token::RightParen
            }
            ('{', _) => {
                // Issue #69: Check for brace expansion {a,b,c} or {1..10}
                if self.is_brace_expansion() {
                    return self.read_brace_expansion();
                }
                self.advance();
                Token::LeftBrace
            }
            ('}', _) => {
                self.advance();
                Token::RightBrace
            }
            ('?', _) => {
                // Single-char glob: file?.txt
                self.advance();
                Token::Identifier("?".to_string())
            }
            _ => {
                return Err(LexerError::UnexpectedChar(ch, self.line, self.column));
            }
        };

        Ok(token)
    }

    /// Handle operators starting with `<` or `>`: redirects, comparisons, and
    /// process substitutions.
    fn read_redirect_or_comparison(
        &mut self,
        ch: char,
        next_ch: Option<char>,
    ) -> Result<Token, LexerError> {
        let token = match (ch, next_ch) {
            ('<', Some('<')) => {
                // Check for here-string (<<<) vs heredoc (<<) vs indented heredoc (<<-)
                // Issue #61: Here-strings must be checked before heredocs
                if self.peek_char(2) == Some('<') {
                    // Here-string: <<< "string"
                    self.advance(); // skip first '<'
                    self.advance(); // skip second '<'
                    self.advance(); // skip third '<'
                    return self.read_herestring();
                } else if self.peek_char(2) == Some('-') {
                    // BUG-007 FIX: Indented heredoc: <<-DELIMITER
                    self.advance(); // skip first '<'
                    self.advance(); // skip second '<'
                    self.advance(); // skip '-'
                    return self.read_heredoc_indented();
                } else {
                    // Heredoc: <<DELIMITER or <<'DELIMITER' or <<"DELIMITER"
                    self.advance(); // skip first '<'
                    self.advance(); // skip second '<'
                    return self.read_heredoc();
                }
            }
            ('<', Some('(')) => {
                // Issue #67: Process substitution <(cmd)
                return self.read_process_substitution('<');
            }
            ('>', Some('(')) => {
                // Issue #67: Process substitution >(cmd) (output redirection variant)
                return self.read_process_substitution('>');
            }
            ('>', Some('|')) => {
                // BUG-016 FIX: Noclobber redirect >|
                self.advance(); // skip '>'
                self.advance(); // skip '|'
                Token::Identifier(">|".to_string())
            }
            ('<', Some('>')) => {
                // BUG-017 FIX: Read-write redirect <>
                self.advance(); // skip '<'
                self.advance(); // skip '>'
                Token::Identifier("<>".to_string())
            }
            ('<', Some('=')) => {
                self.advance();
                self.advance();
                Token::Le
            }
            ('>', Some('>')) => {
                // Append redirection: >>
                self.advance();
                self.advance();
                Token::GtGt
            }
            ('>', Some('=')) => {
                self.advance();
                self.advance();
                Token::Ge
            }
            ('<', _) => {
                self.advance();
                Token::Lt
            }
            ('>', _) => {
                self.advance();
                Token::Gt
            }
            _ => return Err(LexerError::UnexpectedChar(ch, self.line, self.column)),
        };
        Ok(token)
    }

    /// Handle operators starting with `=`: equality (`==`), regex match (`=~`),
    /// and plain assignment (`=`).
    fn read_equality_or_assign(&mut self, next_ch: Option<char>) -> Result<Token, LexerError> {
        match next_ch {
            Some('=') => {
                self.advance();
                self.advance();
                Ok(Token::Eq)
            }
            Some('~') => {
                // =~ regex match operator (used in [[ ... =~ pattern ]])
                self.advance(); // skip '='
                self.advance(); // skip '~'
                self.skip_whitespace_except_newline();
                let pattern = self.read_regex_pattern();
                Ok(Token::Identifier(format!("=~ {}", pattern)))
            }
            _ => {
                self.advance();
                Ok(Token::Assign)
            }
        }
    }

    /// Read a regex pattern after `=~` until `]]`, newline, or unquoted `;`.
    /// Tracks bracket depth to avoid breaking on `]]` inside `[[:class:]]`.
    fn read_regex_pattern(&mut self) -> String {
        let mut pattern = String::new();
        let mut bracket_depth = 0i32;
        while !self.is_at_end() {
            let c = self.current_char();
            if c == '\n' {
                break;
            }
            if self.is_regex_terminator(c, bracket_depth) {
                break;
            }
            bracket_depth = Self::update_bracket_depth(c, bracket_depth);
            pattern.push(self.advance());
        }
        pattern.trim_end().to_string()
    }

    /// Check if the current character terminates a regex pattern.
    /// `]]` terminates when not inside character class brackets; `;` terminates
    /// outside brackets.
    fn is_regex_terminator(&self, c: char, bracket_depth: i32) -> bool {
        if c == ']' && bracket_depth == 0 && self.peek_char(1) == Some(']') {
            return true;
        }
        c == ';' && bracket_depth == 0
    }

    /// Update bracket depth tracking for regex pattern reading.
    fn update_bracket_depth(c: char, depth: i32) -> i32 {
        match c {
            '[' => depth + 1,
            ']' if depth > 0 => depth - 1,
            _ => depth,
        }
    }

    /// Handle extended glob patterns: `@(...)`, `+(...)`, `?(...)`, `!(...)`.
    /// The `glob_char` parameter is the leading character (`@`, `+`, `?`, or `!`).
    fn read_extended_glob(&mut self, _glob_char: char) -> Result<Token, LexerError> {
        let glob_type = self.advance(); // consume glob_char (@, +, ?, or !)
        self.advance(); // consume (
        let mut pattern = String::new();
        let mut depth = 1;
        while !self.is_at_end() && depth > 0 {
            let c = self.current_char();
            if c == '(' {
                depth += 1;
            } else if c == ')' {
                depth -= 1;
                if depth == 0 {
                    self.advance();
                    break;
                }
            }
            pattern.push(self.advance());
        }
        Ok(Token::Identifier(format!("{}({})", glob_type, pattern)))
    }

    /// Handle operators starting with `;`: double-semicolon (`;;`),
    /// case resume (`;;&`), case fall-through (`;&`), and plain semicolon.
    fn read_semicolon_operator(&mut self, next_ch: Option<char>) -> Result<Token, LexerError> {
        match next_ch {
            Some(';') => {
                // BUG-008, BUG-009 FIX: Check for ;;& (case resume) before ;;
                self.advance(); // skip first ';'
                self.advance(); // skip second ';'
                if self.peek_char(0) == Some('&') {
                    self.advance(); // skip '&'
                    Ok(Token::Identifier(";;&".to_string())) // Case resume
                } else {
                    Ok(Token::Identifier(";;".to_string())) // Case terminator
                }
            }
            Some('&') => {
                // BUG-008 FIX: Case fall-through ;&
                self.advance(); // skip ';'
                self.advance(); // skip '&'
                Ok(Token::Identifier(";&".to_string()))
            }
            _ => {
                self.advance();
                Ok(Token::Semicolon)
            }
        }
    }
}

#[cfg(test)]
#[path = "lexer_tests_tokenize_sim.rs"]
mod tests_extracted;