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
470
471
472
473
474
475
476
477
478
//! OCaml 外部扫描器(`ocaml`、`interface`、`type` 三个语法共用),移植自 tree-sitter-ocaml 的
//! `common/scanner.h`(MIT,Copyright (c) 2020 Max Brunsfeld and Pieter Goetschalckx)。
//!
//! 官方序列化把引用字符串的分隔符数组按“元素个数”当字节数拷贝(只拷前几个字节),反序列化时再按字节数
//! 当元素个数还原;这里照原样保留,多出来的元素补 0。

use super::{ScanLexer, Scanner, is_space};

const COMMENT: usize = 0;
const LEFT_QUOTED_STRING_DELIM: usize = 1;
const RIGHT_QUOTED_STRING_DELIM: usize = 2;
const STRING_DELIM: usize = 3;
const LINE_NUMBER_DIRECTIVE: usize = 4;
const NULL_CHARACTER: usize = 5;
const ERROR_SENTINEL: usize = 6;

const LOWER_UTF8_CHARS: [u32; 35] = [
    0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee,
    0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
    0x153, 0x161, 0x17e,
];

const LOWER_UTF8_PAIRS: [(u32, u32, u32); 29] = [
    ('a' as u32, 0x300, 0xe0),
    ('a' as u32, 0x301, 0xe1),
    ('a' as u32, 0x302, 0xe2),
    ('a' as u32, 0x303, 0xe3),
    ('a' as u32, 0x308, 0xe4),
    ('a' as u32, 0x30a, 0xe5),
    ('c' as u32, 0x327, 0xe7),
    ('e' as u32, 0x300, 0xe8),
    ('e' as u32, 0x301, 0xe9),
    ('e' as u32, 0x302, 0xea),
    ('e' as u32, 0x308, 0xeb),
    ('i' as u32, 0x300, 0xec),
    ('i' as u32, 0x301, 0xed),
    ('i' as u32, 0x302, 0xee),
    ('i' as u32, 0x308, 0xef),
    ('n' as u32, 0x303, 0xf1),
    ('o' as u32, 0x300, 0xf2),
    ('o' as u32, 0x301, 0xf3),
    ('o' as u32, 0x302, 0xf4),
    ('o' as u32, 0x303, 0xf5),
    ('o' as u32, 0x308, 0xf6),
    ('s' as u32, 0x30c, 0x161),
    ('u' as u32, 0x300, 0xf9),
    ('u' as u32, 0x301, 0xfa),
    ('u' as u32, 0x302, 0xfb),
    ('u' as u32, 0x308, 0xfc),
    ('y' as u32, 0x301, 0xfd),
    ('y' as u32, 0x308, 0xff),
    ('z' as u32, 0x30c, 0x17e),
];

#[derive(Default)]
pub struct Ocaml {
    in_string: bool,
    /// 分隔符数组的底层内存:清空只改 `len`,内容保留(官方如此,反序列化的“半截覆盖”靠它复原)。
    mem: Vec<u32>,
    len: usize,
}

impl Ocaml {
    fn grow(&mut self, n: usize) {
        if self.mem.len() < n {
            self.mem.resize(n, 0);
        }
    }
}

fn la(lx: &ScanLexer) -> u32 {
    lx.lookahead() as u32
}

fn digit(lx: &ScanLexer) -> bool {
    lx.lookahead().is_ascii_digit()
}

fn scan_string(lx: &mut ScanLexer) {
    loop {
        match lx.lookahead() {
            '\\' => {
                lx.next();
                lx.next();
            }
            '"' => {
                lx.next();
                return;
            }
            '\0' => {
                if lx.eof() {
                    return;
                }
                lx.next();
            }
            _ => lx.next(),
        }
    }
}

/// 引用字符串分隔符里的一个字符;遇到 `|` 或不合法字符返回 0。
fn scan_delim_char(lx: &mut ScanLexer) -> u32 {
    let c = la(lx);
    if c == '|' as u32 {
        return 0;
    }
    if c == '_' as u32 {
        lx.next();
        return c;
    }
    if (b'a' as u32..=b'z' as u32).contains(&c) {
        lx.next();
        let d = la(lx);
        if (0x300..=0x327).contains(&d)
            && let Some(&(_, _, r)) = LOWER_UTF8_PAIRS.iter().find(|p| p.0 == c && p.1 == d)
        {
            lx.next();
            return r;
        }
        return c;
    }
    if LOWER_UTF8_CHARS.contains(&c) {
        lx.next();
        return c;
    }
    0
}

fn is_lowercase_ext(c: u32) -> bool {
    (b'a' as u32..=b'z' as u32).contains(&c) || c == '_' as u32 || c >= 192
}

fn is_identstart(c: u32) -> bool {
    is_lowercase_ext(c) || (b'A' as u32..=b'Z' as u32).contains(&c)
}

fn is_identchar(c: u32) -> bool {
    is_identstart(c) || (b'0' as u32..=b'9' as u32).contains(&c) || c == '\'' as u32
}

fn scan_identifier(lx: &mut ScanLexer) -> bool {
    if is_identstart(la(lx)) {
        lx.next();
        while is_identchar(la(lx)) {
            lx.next();
        }
        return true;
    }
    false
}

fn scan_extattrident(lx: &mut ScanLexer) -> bool {
    while scan_identifier(lx) {
        if lx.lookahead() != '.' {
            return true;
        }
        lx.next();
    }
    false
}

/// 字符字面量:返回最后一个字符(0 表示不是字符字面量或没有可回退的字符)。
fn scan_character(lx: &mut ScanLexer) -> u32 {
    let mut last = 0u32;
    match lx.lookahead() {
        '\\' => {
            lx.next();
            if digit(lx) {
                lx.next();
                for _ in 0..2 {
                    if !digit(lx) {
                        return 0;
                    }
                    lx.next();
                }
            } else {
                match lx.lookahead() {
                    'x' => {
                        lx.next();
                        for _ in 0..2 {
                            if !digit(lx)
                                && !lx.lookahead().to_ascii_uppercase().is_ascii_hexdigit()
                            {
                                return 0;
                            }
                            lx.next();
                        }
                    }
                    'o' => {
                        lx.next();
                        for _ in 0..3 {
                            if !digit(lx) || lx.lookahead() > '7' {
                                return 0;
                            }
                            lx.next();
                        }
                    }
                    '\'' | '"' | '\\' | 'n' | 't' | 'b' | 'r' | ' ' => {
                        last = la(lx);
                        lx.next();
                    }
                    _ => return 0,
                }
            }
        }
        '\'' => {}
        '\r' => {
            lx.next();
            while lx.lookahead() == '\r' {
                lx.next();
            }
            if lx.lookahead() != '\n' {
                return 0;
            }
            lx.next();
        }
        '\0' => {
            if lx.eof() {
                return 0;
            }
            lx.next();
        }
        _ => {
            if la(lx) < 256 {
                last = la(lx);
                lx.next();
            } else {
                return 0;
            }
        }
    }
    if lx.lookahead() == '\'' {
        lx.next();
        return 0;
    }
    last
}

impl Ocaml {
    fn scan_left_delim(&mut self, lx: &mut ScanLexer) -> bool {
        loop {
            let c = scan_delim_char(lx);
            if c == 0 {
                break;
            }
            self.grow(self.len + 1);
            self.mem[self.len] = c;
            self.len += 1;
        }
        if lx.lookahead() == '|' {
            lx.next();
            self.in_string = true;
            true
        } else {
            self.len = 0;
            false
        }
    }

    fn scan_right_delim(&mut self, lx: &mut ScanLexer) -> bool {
        for i in 0..self.len {
            if scan_delim_char(lx) != self.mem[i] {
                return false;
            }
        }
        if lx.lookahead() == '}' {
            self.in_string = false;
            self.len = 0;
            true
        } else {
            false
        }
    }

    fn scan_quoted_string(&mut self, lx: &mut ScanLexer) -> bool {
        if !self.scan_left_delim(lx) {
            return false;
        }
        loop {
            match lx.lookahead() {
                '|' => {
                    lx.next();
                    if self.scan_right_delim(lx) {
                        return true;
                    }
                }
                '\0' => {
                    if lx.eof() {
                        return false;
                    }
                    lx.next();
                }
                _ => lx.next(),
            }
        }
    }

    fn scan_comment(&mut self, lx: &mut ScanLexer) -> bool {
        let mut last = 0u32;
        let mut depth = 0u32;
        if lx.lookahead() != '*' {
            return false;
        }
        lx.next();
        // 官方 `last` 表示“上一个字符已读出但没前进”;非 0 时先消耗它
        let eat = |lx: &mut ScanLexer, last: &mut u32| {
            if *last != 0 {
                *last = 0;
            } else {
                lx.next();
            }
        };
        loop {
            let cur = if last != 0 { last } else { la(lx) };
            match char::from_u32(cur).unwrap_or('\u{FFFD}') {
                '(' => {
                    eat(lx, &mut last);
                    if lx.lookahead() == '*' {
                        lx.next();
                        depth += 1;
                    }
                }
                '*' => {
                    eat(lx, &mut last);
                    if lx.lookahead() == ')' {
                        lx.next();
                        if depth == 0 {
                            return true;
                        }
                        depth -= 1;
                    }
                }
                '\'' => {
                    eat(lx, &mut last);
                    last = scan_character(lx);
                }
                '"' => {
                    eat(lx, &mut last);
                    scan_string(lx);
                }
                '{' => {
                    eat(lx, &mut last);
                    if lx.lookahead() == '%' {
                        lx.next();
                        if lx.lookahead() == '%' {
                            lx.next();
                        }
                        if scan_extattrident(lx) {
                            while is_space(lx.lookahead()) {
                                lx.next();
                            }
                        } else {
                            continue;
                        }
                    }
                    if self.scan_quoted_string(lx) {
                        lx.next();
                    }
                }
                '\0' => {
                    if lx.eof() {
                        return true;
                    }
                    eat(lx, &mut last);
                }
                _ => {
                    if scan_identifier(lx) || last != 0 {
                        last = 0;
                    } else {
                        lx.next();
                    }
                }
            }
        }
    }
}

impl Scanner for Ocaml {
    fn scan(&mut self, lx: &mut ScanLexer, v: &[bool]) -> bool {
        if !v[ERROR_SENTINEL]
            && v[LEFT_QUOTED_STRING_DELIM]
            && (is_lowercase_ext(la(lx)) || lx.lookahead() == '|')
        {
            lx.set_result(LEFT_QUOTED_STRING_DELIM as u16);
            return self.scan_left_delim(lx);
        }
        if !v[ERROR_SENTINEL] && v[RIGHT_QUOTED_STRING_DELIM] && lx.lookahead() == '|' {
            lx.next();
            lx.set_result(RIGHT_QUOTED_STRING_DELIM as u16);
            return self.scan_right_delim(lx);
        }
        if self.in_string && v[STRING_DELIM] && lx.lookahead() == '"' {
            lx.next();
            self.in_string = false;
            lx.set_result(STRING_DELIM as u16);
            return true;
        }
        while is_space(lx.lookahead()) {
            lx.skip();
        }
        if !self.in_string && v[STRING_DELIM] && lx.lookahead() == '"' {
            lx.next();
            self.in_string = true;
            lx.set_result(STRING_DELIM as u16);
            return true;
        }
        if !self.in_string && v[LINE_NUMBER_DIRECTIVE] && lx.lookahead() == '#' && lx.column() == 0
        {
            lx.next();
            while matches!(lx.lookahead(), ' ' | '\t') {
                lx.next();
            }
            if !digit(lx) {
                return false;
            }
            while digit(lx) {
                lx.next();
            }
            while matches!(lx.lookahead(), ' ' | '\t') {
                lx.next();
            }
            if lx.lookahead() != '"' {
                return false;
            }
            lx.next();
            while !matches!(lx.lookahead(), '\n' | '\r' | '"') && !lx.eof() {
                lx.next();
            }
            if lx.lookahead() != '"' {
                return false;
            }
            lx.next();
            while !matches!(lx.lookahead(), '\n' | '\r') && !lx.eof() {
                lx.next();
            }
            lx.set_result(LINE_NUMBER_DIRECTIVE as u16);
            return true;
        }
        if !self.in_string && v[COMMENT] && lx.lookahead() == '(' {
            lx.next();
            lx.set_result(COMMENT as u16);
            return self.scan_comment(lx);
        }
        if v[NULL_CHARACTER] && lx.lookahead() == '\0' && !lx.eof() {
            lx.next();
            lx.set_result(NULL_CHARACTER as u16);
            return true;
        }
        false
    }

    fn serialize(&self, out: &mut Vec<u8>) {
        out.push(self.in_string as u8);
        let n = self.len;
        if n < super::MAX_SERIALIZED {
            let bytes: Vec<u8> = self.mem.iter().flat_map(|c| c.to_le_bytes()).collect();
            out.extend_from_slice(&bytes[..n.min(bytes.len())]);
        }
    }

    fn deserialize(&mut self, d: &[u8]) {
        if d.is_empty() {
            self.in_string = false;
            self.len = 0;
            return;
        }
        self.in_string = d[0] != 0;
        let n = d.len() - 1;
        if n > 0 {
            self.grow(n);
            for (i, b) in d[1..].iter().enumerate() {
                let sh = 8 * (i % 4);
                self.mem[i / 4] = (self.mem[i / 4] & !(0xff << sh)) | ((*b as u32) << sh);
            }
        }
        self.len = n;
    }
}