yolowsyntax 0.2.0

Pure-Rust, hardware-accelerated tree-sitter grammar runtime and code outline extractor for 41 languages.
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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! Bash 扫描器的正则、扩展 glob、参数扩展和花括号分支(拆自 `bash.rs`,逻辑不变)。

use super::*;

impl Bash {
    pub(super) fn regex(&mut self, lx: &mut ScanLexer, v: &[bool]) -> Nx {
        let _ = &self;
        if !((v[REGEX] || v[REGEX_NO_SLASH] || v[REGEX_NO_SPACE]) && !v[ERROR_RECOVERY]) {
            return Nx::Go(St::Extglob);
        }
        if v[REGEX] || v[REGEX_NO_SPACE] {
            while ws(lx.lookahead()) {
                lx.skip();
            }
        }
        let c = lx.lookahead();
        if (c != '"' && c != '\'')
            || ((c == '$' || c == '\'') && v[REGEX_NO_SLASH])
            || (c == '\'' && v[REGEX_NO_SPACE])
        {
            if c == '$' && v[REGEX_NO_SLASH] {
                lx.mark_end();
                lx.next();
                if lx.lookahead() == '(' {
                    return Nx::Ret(false);
                }
            }
            lx.mark_end();
            let (mut done, mut advanced_once, mut found_non_alnum, mut last_escape, mut in_sq) =
                (false, false, false, false, false);
            let (mut paren, mut bracket, mut brace) = (0u32, 0u32, 0u32);
            while !done {
                if in_sq && lx.lookahead() == '\'' {
                    in_sq = false;
                    lx.next();
                    lx.mark_end();
                }
                match lx.lookahead() {
                    '\\' => last_escape = true,
                    '\0' => return Nx::Ret(false),
                    '(' => {
                        paren = paren.wrapping_add(1);
                        last_escape = false;
                    }
                    '[' => {
                        bracket = bracket.wrapping_add(1);
                        last_escape = false;
                    }
                    '{' => {
                        if !last_escape {
                            brace = brace.wrapping_add(1);
                        }
                        last_escape = false;
                    }
                    ')' => {
                        if paren == 0 {
                            done = true;
                        }
                        paren = paren.wrapping_sub(1);
                        last_escape = false;
                    }
                    ']' => {
                        if bracket == 0 {
                            done = true;
                        }
                        bracket = bracket.wrapping_sub(1);
                        last_escape = false;
                    }
                    '}' => {
                        if brace == 0 {
                            done = true;
                        }
                        brace = brace.wrapping_sub(1);
                        last_escape = false;
                    }
                    '\'' => {
                        in_sq = !in_sq;
                        lx.next();
                        advanced_once = true;
                        last_escape = false;
                        continue;
                    }
                    _ => last_escape = false,
                }
                if !done {
                    if v[REGEX] {
                        let was_space = !in_sq && ws(lx.lookahead());
                        lx.next();
                        advanced_once = true;
                        if !was_space || paren > 0 {
                            lx.mark_end();
                        }
                    } else if v[REGEX_NO_SLASH] {
                        if lx.lookahead() == '/' {
                            lx.mark_end();
                            lx.set_result(REGEX_NO_SLASH as u16);
                            return Nx::Ret(advanced_once);
                        }
                        if lx.lookahead() == '\\' {
                            lx.next();
                            advanced_once = true;
                            if !lx.eof() && lx.lookahead() != '[' && lx.lookahead() != '/' {
                                lx.next();
                                lx.mark_end();
                            }
                        } else {
                            let was_space = !in_sq && ws(lx.lookahead());
                            lx.next();
                            advanced_once = true;
                            if !was_space {
                                lx.mark_end();
                            }
                        }
                    } else if v[REGEX_NO_SPACE] {
                        if lx.lookahead() == '\\' {
                            found_non_alnum = true;
                            lx.next();
                            if !lx.eof() {
                                lx.next();
                            }
                        } else if lx.lookahead() == '$' {
                            lx.mark_end();
                            lx.next();
                            if lx.lookahead() == '(' {
                                return Nx::Ret(false);
                            }
                            if ws(lx.lookahead()) {
                                lx.set_result(REGEX_NO_SPACE as u16);
                                lx.mark_end();
                                return Nx::Ret(true);
                            }
                        } else {
                            let was_space = !in_sq && ws(lx.lookahead());
                            if was_space && paren == 0 {
                                lx.mark_end();
                                lx.set_result(REGEX_NO_SPACE as u16);
                                return Nx::Ret(found_non_alnum);
                            }
                            if !alnum(lx.lookahead())
                                && lx.lookahead() != '$'
                                && lx.lookahead() != '-'
                                && lx.lookahead() != '_'
                            {
                                found_non_alnum = true;
                            }
                            lx.next();
                        }
                    }
                }
            }
            lx.set_result(if v[REGEX_NO_SLASH] {
                REGEX_NO_SLASH
            } else if v[REGEX_NO_SPACE] {
                REGEX_NO_SPACE
            } else {
                REGEX
            } as u16);
            if v[REGEX] && !advanced_once {
                return Nx::Ret(false);
            }
            return Nx::Ret(true);
        }
        Nx::Go(St::Extglob)
    }

    pub(super) fn extglob(&mut self, lx: &mut ScanLexer, v: &[bool]) -> Nx {
        if !(v[EXTGLOB_PATTERN] && !v[ERROR_RECOVERY]) {
            return Nx::Go(St::Expansion);
        }
        while ws(lx.lookahead()) {
            lx.skip();
        }
        let c = lx.lookahead();
        if matches!(
            c,
            '?' | '*' | '+' | '@' | '!' | '-' | ')' | '\\' | '.' | '['
        ) || alpha(c)
        {
            if lx.lookahead() == '\\' {
                lx.next();
                if (ws(lx.lookahead()) || lx.lookahead() == '"')
                    && lx.lookahead() != '\r'
                    && lx.lookahead() != '\n'
                {
                    lx.next();
                } else {
                    return Nx::Ret(false);
                }
            }
            if lx.lookahead() == ')' && self.last_glob_paren_depth == 0 {
                lx.mark_end();
                lx.next();
                if ws(lx.lookahead()) {
                    return Nx::Ret(false);
                }
            }
            lx.mark_end();
            let was_non_alpha = !alpha(lx.lookahead());
            if lx.lookahead() != '[' {
                if lx.lookahead() == 'e' {
                    lx.mark_end();
                    lx.next();
                    if lx.lookahead() == 's' {
                        lx.next();
                        if lx.lookahead() == 'a' {
                            lx.next();
                            if lx.lookahead() == 'c' {
                                lx.next();
                                if ws(lx.lookahead()) {
                                    return Nx::Ret(false);
                                }
                            }
                        }
                    }
                } else {
                    lx.next();
                }
            }
            if lx.lookahead() == '-' {
                lx.mark_end();
                lx.next();
                while alnum(lx.lookahead()) {
                    lx.next();
                }
                if matches!(lx.lookahead(), ')' | '\\' | '.') {
                    return Nx::Ret(false);
                }
                lx.mark_end();
            }
            if lx.lookahead() == ')' && self.last_glob_paren_depth == 0 {
                lx.mark_end();
                lx.next();
                if ws(lx.lookahead()) {
                    lx.set_result(EXTGLOB_PATTERN as u16);
                    return Nx::Ret(was_non_alpha);
                }
            }
            if ws(lx.lookahead()) {
                lx.mark_end();
                lx.set_result(EXTGLOB_PATTERN as u16);
                self.last_glob_paren_depth = 0;
                return Nx::Ret(true);
            }
            if lx.lookahead() == '$' {
                lx.mark_end();
                lx.next();
                if lx.lookahead() == '{' || lx.lookahead() == '(' {
                    lx.set_result(EXTGLOB_PATTERN as u16);
                    return Nx::Ret(true);
                }
            }
            if lx.lookahead() == '|' {
                lx.mark_end();
                lx.next();
                lx.set_result(EXTGLOB_PATTERN as u16);
                return Nx::Ret(true);
            }
            let c = lx.lookahead();
            if !alnum(c) && !matches!(c, '(' | '"' | '[' | '?' | '/' | '\\' | '_' | '*') {
                return Nx::Ret(false);
            }
            let (mut done, mut saw_non_alphadot) = (false, was_non_alpha);
            let (mut paren, mut bracket, mut brace) =
                (self.last_glob_paren_depth as u32, 0u32, 0u32);
            while !done {
                match lx.lookahead() {
                    '\0' => return Nx::Ret(false),
                    '(' => paren = paren.wrapping_add(1),
                    '[' => bracket = bracket.wrapping_add(1),
                    '{' => brace = brace.wrapping_add(1),
                    ')' => {
                        if paren == 0 {
                            done = true;
                        }
                        paren = paren.wrapping_sub(1);
                    }
                    ']' => {
                        if bracket == 0 {
                            done = true;
                        }
                        bracket = bracket.wrapping_sub(1);
                    }
                    '}' => {
                        if brace == 0 {
                            done = true;
                        }
                        brace = brace.wrapping_sub(1);
                    }
                    _ => {}
                }
                if lx.lookahead() == '|' {
                    lx.mark_end();
                    lx.next();
                    if paren == 0 && bracket == 0 && brace == 0 {
                        lx.set_result(EXTGLOB_PATTERN as u16);
                        return Nx::Ret(true);
                    }
                }
                if !done {
                    let was_space = ws(lx.lookahead());
                    if lx.lookahead() == '$' {
                        lx.mark_end();
                        if !alpha(lx.lookahead()) && lx.lookahead() != '.' && lx.lookahead() != '\\'
                        {
                            saw_non_alphadot = true;
                        }
                        lx.next();
                        if lx.lookahead() == '(' || lx.lookahead() == '{' {
                            lx.set_result(EXTGLOB_PATTERN as u16);
                            if self.last_glob_paren_depth != paren as u8 {
                                self.last_glob_paren_depth = paren as u8;
                                self.mutated = true;
                            }
                            return Nx::Ret(saw_non_alphadot);
                        }
                    }
                    if was_space {
                        lx.mark_end();
                        lx.set_result(EXTGLOB_PATTERN as u16);
                        if self.last_glob_paren_depth != 0 {
                            self.last_glob_paren_depth = 0;
                            self.mutated = true;
                        }
                        return Nx::Ret(saw_non_alphadot);
                    }
                    if lx.lookahead() == '"' {
                        lx.mark_end();
                        lx.set_result(EXTGLOB_PATTERN as u16);
                        if self.last_glob_paren_depth != 0 {
                            self.last_glob_paren_depth = 0;
                            self.mutated = true;
                        }
                        return Nx::Ret(saw_non_alphadot);
                    }
                    if lx.lookahead() == '\\' {
                        if !alpha(lx.lookahead()) && lx.lookahead() != '.' && lx.lookahead() != '\\'
                        {
                            saw_non_alphadot = true;
                        }
                        lx.next();
                        if ws(lx.lookahead()) || lx.lookahead() == '"' {
                            lx.next();
                        }
                    } else {
                        if !alpha(lx.lookahead()) && lx.lookahead() != '.' && lx.lookahead() != '\\'
                        {
                            saw_non_alphadot = true;
                        }
                        lx.next();
                    }
                    if !was_space {
                        lx.mark_end();
                    }
                }
            }
            lx.set_result(EXTGLOB_PATTERN as u16);
            if self.last_glob_paren_depth != 0 {
                self.last_glob_paren_depth = 0;
                self.mutated = true;
            }
            return Nx::Ret(saw_non_alphadot);
        }
        if self.last_glob_paren_depth != 0 {
            self.last_glob_paren_depth = 0;
            self.mutated = true;
        }
        Nx::Ret(false)
    }

    pub(super) fn expansion_word(&mut self, lx: &mut ScanLexer, v: &[bool]) -> Nx {
        let _ = &self;
        if !v[EXPANSION_WORD] {
            return Nx::Go(St::Brace);
        }
        let (mut advanced_once, mut advance_once_space) = (false, false);
        loop {
            if lx.lookahead() == '"' {
                return Nx::Ret(false);
            }
            if lx.lookahead() == '$' {
                lx.mark_end();
                lx.next();
                if matches!(lx.lookahead(), '{' | '(' | '\'') || alnum(lx.lookahead()) {
                    lx.set_result(EXPANSION_WORD as u16);
                    return Nx::Ret(advanced_once);
                }
                advanced_once = true;
            }
            if lx.lookahead() == '}' {
                lx.mark_end();
                lx.set_result(EXPANSION_WORD as u16);
                return Nx::Ret(advanced_once || advance_once_space);
            }
            if lx.lookahead() == '(' && !(advanced_once || advance_once_space) {
                lx.mark_end();
                lx.next();
                while lx.lookahead() != ')' && !lx.eof() {
                    if lx.lookahead() == '$' {
                        lx.mark_end();
                        lx.next();
                        if matches!(lx.lookahead(), '{' | '(' | '\'') || alnum(lx.lookahead()) {
                            lx.set_result(EXPANSION_WORD as u16);
                            return Nx::Ret(advanced_once);
                        }
                        advanced_once = true;
                    } else {
                        advanced_once = advanced_once || !ws(lx.lookahead());
                        advance_once_space = advance_once_space || ws(lx.lookahead());
                        lx.next();
                    }
                }
                lx.mark_end();
                if lx.lookahead() == ')' {
                    advanced_once = true;
                    lx.next();
                    lx.mark_end();
                    if lx.lookahead() == '}' {
                        return Nx::Ret(false);
                    }
                } else {
                    return Nx::Ret(false);
                }
            }
            if lx.lookahead() == '\'' {
                return Nx::Ret(false);
            }
            if lx.eof() {
                return Nx::Ret(false);
            }
            advanced_once = advanced_once || !ws(lx.lookahead());
            advance_once_space = advance_once_space || ws(lx.lookahead());
            lx.next();
        }
    }

    pub(super) fn brace_start(&mut self, lx: &mut ScanLexer, v: &[bool]) -> bool {
        let _ = &self;
        if !(v[BRACE_START] && !v[ERROR_RECOVERY]) {
            return false;
        }
        while ws(lx.lookahead()) {
            lx.skip();
        }
        if lx.lookahead() != '{' {
            return false;
        }
        lx.next();
        lx.mark_end();
        while lx.lookahead().is_ascii_digit() {
            lx.next();
        }
        if lx.lookahead() != '.' {
            return false;
        }
        lx.next();
        if lx.lookahead() != '.' {
            return false;
        }
        lx.next();
        while lx.lookahead().is_ascii_digit() {
            lx.next();
        }
        if lx.lookahead() != '}' {
            return false;
        }
        lx.set_result(BRACE_START as u16);
        true
    }
}