oak-bash 0.0.11

High-performance incremental Bash parser for the oak ecosystem with flexible configuration, supporting shell scripting and automation workflows.
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
#![doc = include_str!("readme.md")]
/// Bash token types and role definitions.
pub mod token_type;

pub use token_type::BashTokenType;

use crate::language::BashLanguage;
use oak_core::{Lexer, LexerCache, LexerState, OakError, lexer::LexOutput, source::Source};
use std::sync::LazyLock;

pub(crate) type State<'a, S> = LexerState<'a, S, BashLanguage>;

/// Lexer for the Bash language.
#[derive(Clone)]
pub struct BashLexer<'config> {
    config: &'config BashLanguage,
}

impl<'config> Lexer<BashLanguage> for BashLexer<'config> {
    fn lex<'a, S: Source + ?Sized>(&self, source: &S, _edits: &[oak_core::source::TextEdit], cache: &'a mut impl LexerCache<BashLanguage>) -> LexOutput<BashLanguage> {
        let mut state = LexerState::new_with_cache(source, 0, cache);
        let result = self.run(&mut state);
        if result.is_ok() {
            state.add_eof()
        }
        state.finish_with_cache(result, cache)
    }
}

impl<'config> BashLexer<'config> {
    /// Creates a new `BashLexer` instance.
    pub fn new(config: &'config BashLanguage) -> Self {
        Self { config }
    }

    fn run<'a, S: Source + ?Sized>(&self, state: &mut State<'a, S>) -> Result<(), OakError> {
        while state.not_at_end() {
            let safe_point = state.get_position();
            if self.skip_whitespace(state) {
                continue;
            }

            if self.skip_comment(state) {
                continue;
            }

            if self.lex_newline(state) {
                continue;
            }

            if self.lex_string(state) {
                continue;
            }

            if self.lex_variable(state) {
                continue;
            }

            if self.lex_number(state) {
                continue;
            }

            if self.lex_keyword_or_identifier(state) {
                continue;
            }

            if self.lex_operator_or_delimiter(state) {
                continue;
            }

            if self.lex_heredoc(state) {
                continue;
            }

            if self.lex_glob_pattern(state) {
                continue;
            }

            if self.lex_special_char(state) {
                continue;
            }

            if self.lex_text(state) {
                continue;
            }

            // If no pattern matches, skip one character and generate an Error token
            let start_pos = state.get_position();
            if let Some(ch) = state.peek() {
                state.advance(ch.len_utf8());
                state.add_token(BashTokenType::Error, start_pos, state.get_position())
            }

            state.advance_if_dead_lock(safe_point)
        }
        Ok(())
    }

    fn skip_whitespace<'a, S: Source + ?Sized>(&self, state: &mut State<'a, S>) -> bool {
        let start_pos = state.get_position();

        while let Some(ch) = state.peek() {
            if ch == ' ' || ch == '\t' { state.advance(ch.len_utf8()) } else { break }
        }

        if state.get_position() > start_pos {
            state.add_token(BashTokenType::Whitespace, start_pos, state.get_position());
            true
        }
        else {
            false
        }
    }

    fn skip_comment<'a, S: Source + ?Sized>(&self, state: &mut State<'a, S>) -> bool {
        let start_pos = state.get_position();

        if let Some('#') = state.peek() {
            state.advance(1);
            while let Some(ch) = state.peek() {
                if ch == '\n' || ch == '\r' {
                    break;
                }
                state.advance(ch.len_utf8())
            }
            state.add_token(BashTokenType::Comment, start_pos, state.get_position());
            true
        }
        else {
            false
        }
    }

    fn lex_newline<'a, S: Source + ?Sized>(&self, state: &mut State<'a, S>) -> bool {
        let start_pos = state.get_position();

        if let Some('\n') = state.peek() {
            state.advance(1);
            state.add_token(BashTokenType::Newline, start_pos, state.get_position());
            true
        }
        else if let Some('\r') = state.peek() {
            state.advance(1);
            if let Some('\n') = state.peek() {
                state.advance(1)
            }
            state.add_token(BashTokenType::Newline, start_pos, state.get_position());
            true
        }
        else {
            false
        }
    }

    fn lex_string<'a, S: Source + ?Sized>(&self, state: &mut State<'a, S>) -> bool {
        let start_pos = state.get_position();

        if let Some(quote) = state.peek() {
            if quote == '"' || quote == '\'' {
                state.advance(1);
                let mut escaped = false;

                while let Some(ch) = state.peek() {
                    if escaped {
                        escaped = false;
                        state.advance(ch.len_utf8());
                        continue;
                    }

                    if ch == '\\' {
                        escaped = true;
                        state.advance(1);
                        continue;
                    }

                    if ch == quote {
                        state.advance(1);
                        break;
                    }

                    state.advance(ch.len_utf8())
                }

                state.add_token(BashTokenType::StringLiteral, start_pos, state.get_position());
                return true;
            }
        }

        false
    }

    fn lex_variable<'a, S: Source + ?Sized>(&self, state: &mut State<'a, S>) -> bool {
        let start_pos = state.get_position();

        if let Some('$') = state.peek() {
            state.advance(1);

            // Handle special variables like $0, $1, $?, $$, etc.
            if let Some(ch) = state.peek() {
                if ch.is_ascii_digit() || ch == '?' || ch == '$' || ch == '#' || ch == '@' || ch == '*' {
                    state.advance(1);
                    state.add_token(BashTokenType::Variable, start_pos, state.get_position());
                    return true;
                }
            }

            // Handle ${var} format
            if let Some('{') = state.peek() {
                state.advance(1);
                while let Some(ch) = state.peek() {
                    if ch == '}' {
                        state.advance(1);
                        break;
                    }
                    state.advance(ch.len_utf8())
                }
                state.add_token(BashTokenType::Variable, start_pos, state.get_position());
                return true;
            }

            // Handle normal variable names
            if let Some(ch) = state.peek() {
                if ch.is_alphabetic() || ch == '_' {
                    state.advance(ch.len_utf8());
                    while let Some(ch) = state.peek() {
                        if ch.is_alphanumeric() || ch == '_' { state.advance(ch.len_utf8()) } else { break }
                    }
                    state.add_token(BashTokenType::Variable, start_pos, state.get_position());
                    return true;
                }
            }

            // If there is only $ without a valid variable name, backtrack
            state.set_position(start_pos);
        }

        false
    }

    fn lex_number<'a, S: Source + ?Sized>(&self, state: &mut State<'a, S>) -> bool {
        let start_pos = state.get_position();

        if let Some(ch) = state.peek() {
            if ch.is_ascii_digit() {
                state.advance(1);
                while let Some(ch) = state.peek() {
                    if ch.is_ascii_digit() { state.advance(1) } else { break }
                }
                state.add_token(BashTokenType::NumberLiteral, start_pos, state.get_position());
                return true;
            }
        }

        false
    }

    fn lex_keyword_or_identifier<'a, S: Source + ?Sized>(&self, state: &mut State<'a, S>) -> bool {
        let start_pos = state.get_position();

        if let Some(ch) = state.peek() {
            if ch.is_ascii_alphabetic() || ch == '_' {
                state.advance(ch.len_utf8());
                while let Some(ch) = state.peek() {
                    if ch.is_ascii_alphanumeric() || ch == '_' { state.advance(ch.len_utf8()) } else { break }
                }

                let text = state.get_text_in((start_pos..state.get_position()).into());
                let kind = if BASH_KEYWORDS.contains(&text.as_ref()) { BashTokenType::Keyword } else { BashTokenType::Identifier };

                state.add_token(kind, start_pos, state.get_position());
                return true;
            }
        }

        false
    }

    fn lex_operator_or_delimiter<'a, S: Source + ?Sized>(&self, state: &mut State<'a, S>) -> bool {
        let start_pos = state.get_position();

        if let Some(ch) = state.peek() {
            let two_char = if let Some(next_ch) = state.peek_next_n(1) { format!("{}{}", ch, next_ch) } else { String::new() };

            // Check for two-character operators
            if BASH_TWO_CHAR_OPERATORS.contains(&two_char.as_str()) {
                state.advance(2);
                state.add_token(BashTokenType::Operator, start_pos, state.get_position());
                return true;
            }

            // Check for single-character operators and delimiters
            let ch_str = ch.to_string();
            if BASH_OPERATORS.contains(&ch_str.as_str()) {
                state.advance(1);
                state.add_token(BashTokenType::Operator, start_pos, state.get_position());
                return true;
            }

            if BASH_DELIMITERS.contains(&ch_str.as_str()) {
                state.advance(1);
                state.add_token(BashTokenType::Delimiter, start_pos, state.get_position());
                return true;
            }
        }

        false
    }

    fn lex_heredoc<'a, S: Source + ?Sized>(&self, state: &mut State<'a, S>) -> bool {
        let start_pos = state.get_position();

        // Check for heredoc starting with <<
        if let Some('<') = state.peek() {
            if let Some('<') = state.peek_next_n(1) {
                state.advance(2);

                // Skip optional -
                if let Some('-') = state.peek() {
                    state.advance(1)
                }

                // Read identifier
                while let Some(ch) = state.peek() {
                    if ch.is_alphanumeric() || ch == '_' { state.advance(ch.len_utf8()) } else { break }
                }

                state.add_token(BashTokenType::Heredoc, start_pos, state.get_position());
                return true;
            }
        }

        false
    }

    fn lex_glob_pattern<'a, S: Source + ?Sized>(&self, state: &mut State<'a, S>) -> bool {
        let start_pos = state.get_position();

        if let Some(ch) = state.peek() {
            if ch == '*' || ch == '?' || ch == '[' {
                state.advance(1);

                if ch == '[' {
                    // Handle character classes [abc] or [!abc]
                    if let Some('!') = state.peek() {
                        state.advance(1)
                    }
                    while let Some(ch) = state.peek() {
                        if ch == ']' {
                            state.advance(1);
                            break;
                        }
                        state.advance(ch.len_utf8())
                    }
                }

                state.add_token(BashTokenType::GlobPattern, start_pos, state.get_position());
                return true;
            }
        }

        false
    }

    fn lex_special_char<'a, S: Source + ?Sized>(&self, state: &mut State<'a, S>) -> bool {
        let start_pos = state.get_position();

        if let Some(ch) = state.peek() {
            if BASH_SPECIAL_CHARS.contains(&ch) {
                state.advance(1);
                state.add_token(BashTokenType::SpecialChar, start_pos, state.get_position());
                return true;
            }
        }

        false
    }

    fn lex_text<'a, S: Source + ?Sized>(&self, state: &mut State<'a, S>) -> bool {
        let start_pos = state.get_position();

        if let Some(ch) = state.peek() {
            if !ch.is_whitespace() && !BASH_SPECIAL_CHARS.contains(&ch) {
                state.advance(ch.len_utf8());
                state.add_token(BashTokenType::Text, start_pos, state.get_position());
                return true;
            }
        }

        false
    }
}

static BASH_KEYWORDS: LazyLock<&[&str]> = LazyLock::new(|| {
    &[
        "if", "then", "else", "elif", "fi", "case", "esac", "for", "while", "until", "do", "done", "function", "return", "break", "continue", "local", "export", "readonly", "declare", "typeset", "unset", "shift", "exit", "source", ".", "eval", "exec",
        "trap", "wait", "jobs", "bg", "fg", "disown", "suspend", "alias", "unalias", "history", "fc", "let", "test", "[", "[[", "]]", "time", "coproc", "select", "in",
    ]
});

static BASH_OPERATORS: LazyLock<&[&str]> = LazyLock::new(|| &["+", "-", "*", "/", "%", "=", "!", "<", ">", "&", "|", "^", "~"]);

static BASH_TWO_CHAR_OPERATORS: LazyLock<&[&str]> = LazyLock::new(|| &["==", "!=", "<=", ">=", "&&", "||", "<<", ">>", "++", "--", "+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=", "<<=", ">>=", "**"]);

static BASH_DELIMITERS: LazyLock<&[&str]> = LazyLock::new(|| &["(", ")", "{", "}", "[", "]", ";", ",", ":", "."]);

static BASH_SPECIAL_CHARS: LazyLock<&[char]> = LazyLock::new(|| &['\\', '`', '~', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '+', '=', '{', '}', '[', ']', '|', '\\', ':', ';', '"', '\'', '<', '>', ',', '.', '?', '/', '!', '`']);