antlr-rust-runtime 0.14.1

High performance Rust runtime and target support for ANTLR v4 generated parsers
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
use crate::int_stream::{EOF, IntStream, UNKNOWN_SOURCE_NAME};
use std::cell::Cell;

use crate::token::{
    DEFAULT_CHANNEL, TOKEN_EOF, Token, TokenId, TokenSink, TokenSource, TokenSourceError,
    TokenSpec, TokenStore, TokenStoreError, TokenView,
};

#[derive(Debug)]
struct BufferedSourceError {
    token_index: usize,
    error: TokenSourceError,
}

#[derive(Debug)]
pub struct CommonTokenStream<S> {
    source: S,
    store: TokenStore,
    source_token_count: usize,
    next_visible_after: Vec<usize>,
    cursor: usize,
    channel: i32,
    requested_token_count: Cell<usize>,
    source_errors: Vec<BufferedSourceError>,
}

const UNKNOWN_NEXT_VISIBLE: usize = usize::MAX;

fn buffer_token_source<S>(
    source: &mut S,
) -> Result<(TokenStore, Vec<BufferedSourceError>), TokenStoreError>
where
    S: TokenSource,
{
    let source_name = source.source_name().to_owned();
    let mut store = TokenStore::new(source.source_text(), source_name);
    let mut source_errors = Vec::new();
    loop {
        let expected_id = store.len();
        let mut sink = TokenSink::new(&mut store);
        let id = source.next_token(&mut sink)?;
        let appended = sink.token_count().saturating_sub(expected_id);
        if appended != 1 || id.index() != expected_id {
            return Err(TokenStoreError::invalid_source_output(
                expected_id,
                id.index(),
                appended,
            ));
        }
        source_errors.extend(
            source
                .drain_errors()
                .into_iter()
                .map(|error| BufferedSourceError {
                    token_index: id.index(),
                    error,
                }),
        );
        let token = sink
            .view(id)
            .expect("token source returned an ID it did not emit");
        if token.token_type() == TOKEN_EOF {
            break;
        }
    }
    Ok((store, source_errors))
}

impl<S> CommonTokenStream<S>
where
    S: TokenSource,
{
    /// Creates and fills a token stream that filters lookahead to the default
    /// channel.
    ///
    /// Use [`Self::try_new`] when token/source limit errors should be handled
    /// instead of reported as a construction panic.
    pub fn new(source: S) -> Self {
        Self::try_new(source).unwrap_or_else(|error| panic!("failed to buffer tokens: {error}"))
    }

    pub fn try_new(source: S) -> Result<Self, TokenStoreError> {
        Self::try_with_channel(source, DEFAULT_CHANNEL)
    }

    /// Creates and fills a token stream whose `LT/LA` operations see only
    /// `channel`.
    pub fn with_channel(source: S, channel: i32) -> Self {
        Self::try_with_channel(source, channel)
            .unwrap_or_else(|error| panic!("failed to buffer tokens: {error}"))
    }

    pub fn try_with_channel(mut source: S, channel: i32) -> Result<Self, TokenStoreError> {
        let (store, source_errors) = buffer_token_source(&mut source)?;
        let source_token_count = store.len();
        let mut stream = Self {
            source,
            store,
            source_token_count,
            next_visible_after: vec![UNKNOWN_NEXT_VISIBLE; source_token_count],
            cursor: 0,
            channel,
            requested_token_count: Cell::new(0),
            source_errors,
        };
        stream.cursor = stream.adjust_seek_index(0);
        stream.requested_token_count.set(0);
        Ok(stream)
    }

    /// Replaces the token source and eagerly buffers it through EOF.
    ///
    /// The configured channel is retained; cursor, token storage, requested
    /// lookahead, and buffered source errors are reset.
    pub fn set_token_source(&mut self, source: S) {
        self.try_set_token_source(source)
            .unwrap_or_else(|error| panic!("failed to buffer tokens: {error}"));
    }

    /// Fallible form of [`Self::set_token_source`].
    pub fn try_set_token_source(&mut self, source: S) -> Result<(), TokenStoreError> {
        let replacement = Self::try_with_channel(source, self.channel)?;
        *self = replacement;
        Ok(())
    }

    /// Rebuffers the current token source after it has been reset or re-fed.
    ///
    /// This supports fully owned recognizer stacks: mutate the nested source
    /// through [`Self::token_source_mut`], then call `refill` without moving the
    /// lexer out of the stream.
    pub fn refill(&mut self) {
        self.try_refill()
            .unwrap_or_else(|error| panic!("failed to buffer tokens: {error}"));
    }

    /// Fallible form of [`Self::refill`].
    pub fn try_refill(&mut self) -> Result<(), TokenStoreError> {
        let (store, source_errors) = buffer_token_source(&mut self.source)?;
        self.store = store;
        self.source_token_count = self.store.len();
        self.next_visible_after = vec![UNKNOWN_NEXT_VISIBLE; self.source_token_count];
        self.cursor = self.adjust_seek_index(0);
        self.requested_token_count.set(0);
        self.source_errors = source_errors;
        Ok(())
    }

    /// Idempotent eager-buffering operation. Construction already buffers
    /// through EOF so the store can be shared with CST nodes.
    pub fn fill(&mut self) {
        self.note_requested_count(self.source_token_count);
        self.cursor = self.adjust_seek_index(self.cursor);
    }

    /// Returns a borrowing view of the token at an absolute buffered index.
    pub fn get(&self, index: usize) -> Option<TokenView<'_>> {
        self.get_id(index).and_then(|id| self.store.view(id))
    }

    /// Returns the compact ID at an absolute buffered index.
    pub fn get_id(&self, index: usize) -> Option<TokenId> {
        self.note_requested_count(index.saturating_add(1));
        (index < self.source_token_count)
            .then(|| TokenId::try_from(index).ok())
            .flatten()
    }

    /// Returns the token at one-based lookahead/lookbehind offset, skipping
    /// tokens outside the configured channel for positive offsets.
    pub fn lt(&self, offset: isize) -> Option<TokenView<'_>> {
        self.lt_id(offset).and_then(|id| self.store.view(id))
    }

    /// Returns the compact token ID at one-based lookahead/lookbehind offset.
    pub fn lt_id(&self, offset: isize) -> Option<TokenId> {
        if offset == 0 {
            return None;
        }
        if offset < 0 {
            return offset
                .checked_neg()
                .map(isize::cast_unsigned)
                .and_then(|offset| self.lb_id(offset));
        }

        let mut index = self.next_token_on_channel(self.cursor, self.channel);
        let mut remaining = offset;
        while remaining > 1 {
            index = self.next_token_on_channel(index + 1, self.channel);
            remaining -= 1;
        }
        self.get_id(index)
    }

    pub fn lb(&self, offset: usize) -> Option<TokenView<'_>> {
        self.lb_id(offset).and_then(|id| self.store.view(id))
    }

    fn lb_id(&self, offset: usize) -> Option<TokenId> {
        if offset == 0 || self.cursor == 0 {
            return None;
        }
        let mut index = self.cursor;
        let mut remaining = offset;
        while remaining > 0 {
            index = self.previous_token_on_channel(index, self.channel)?;
            remaining -= 1;
        }
        self.get_id(index)
    }

    pub const fn token_source(&self) -> &S {
        &self.source
    }

    /// Returns the current source for in-place lexer re-feeding.
    pub const fn token_source_mut(&mut self) -> &mut S {
        &mut self.source
    }

    /// Iterates borrowing views of the original buffered token sequence.
    pub fn tokens(&self) -> TokenIter<'_> {
        self.note_requested_count(self.source_token_count);
        TokenIter {
            store: &self.store,
            next: 0,
            stop: self.source_token_count,
        }
    }

    pub const fn token_count(&self) -> usize {
        self.source_token_count
    }

    /// Returns the canonical token store owned by this stream.
    #[must_use]
    pub const fn token_store(&self) -> &TokenStore {
        &self.store
    }

    /// Consumes the stream and returns its canonical token store.
    #[must_use]
    pub fn into_token_store(self) -> TokenStore {
        self.store
    }

    pub(crate) fn token_view(&self, id: TokenId) -> Option<TokenView<'_>> {
        self.store.view(id)
    }

    pub(crate) fn insert(&mut self, spec: TokenSpec) -> Result<TokenId, TokenStoreError> {
        self.store.push(spec)
    }

    fn note_requested_count(&self, count: usize) {
        self.requested_token_count.set(
            self.requested_token_count
                .get()
                .max(count.min(self.source_token_count)),
        );
    }

    /// Moves a raw token index to the next token visible on this stream's
    /// channel.
    fn adjust_seek_index(&self, index: usize) -> usize {
        self.next_token_on_channel(index, self.channel)
    }

    /// Finds the next buffered token on `channel`.
    fn next_token_on_channel(&self, mut index: usize, channel: i32) -> usize {
        while let Some(id) = self.get_id(index) {
            if self.store.token_type(id) == Some(TOKEN_EOF)
                || self.store.channel(id) == Some(channel)
            {
                return index;
            }
            index += 1;
        }
        index
    }

    /// Finds the previous buffered token on `channel`.
    fn previous_token_on_channel(&self, mut index: usize, channel: i32) -> Option<usize> {
        while index > 0 {
            index -= 1;
            let id = self.get_id(index)?;
            if self.store.token_type(id) == Some(TOKEN_EOF)
                || self.store.channel(id) == Some(channel)
            {
                return Some(index);
            }
        }
        None
    }

    /// Finds the previous buffered token visible to this stream before
    /// `index`.
    pub fn previous_visible_token_index(&self, index: usize) -> Option<usize> {
        self.previous_token_on_channel(index, self.channel)
    }
}

impl<S> IntStream for CommonTokenStream<S>
where
    S: TokenSource,
{
    fn consume(&mut self) {
        if self.la(1) == EOF {
            return;
        }
        let current = self.next_token_on_channel(self.cursor, self.channel);
        self.cursor = self.adjust_seek_index(current + 1);
    }

    fn la(&mut self, offset: isize) -> i32 {
        self.la_token(offset)
    }

    fn index(&self) -> usize {
        self.cursor
    }

    fn seek(&mut self, index: usize) {
        self.cursor = self.adjust_seek_index(index);
    }

    fn size(&self) -> usize {
        self.source_token_count
    }

    fn source_name(&self) -> &str {
        let source_name = self.source.source_name();
        if source_name.is_empty() {
            UNKNOWN_SOURCE_NAME
        } else {
            source_name
        }
    }
}

impl<S> CommonTokenStream<S>
where
    S: TokenSource,
{
    pub fn la_token(&self, offset: isize) -> i32 {
        self.lt_id(offset)
            .and_then(|id| self.store.token_type(id))
            .unwrap_or(TOKEN_EOF)
    }

    /// Returns the token type at a buffered absolute index. Past-EOF reads are
    /// reported as `TOKEN_EOF`.
    pub fn token_type_at_index(&self, index: usize) -> i32 {
        self.get_id(index)
            .and_then(|id| self.store.token_type(id))
            .unwrap_or(TOKEN_EOF)
    }

    /// Returns the token channel visible to `LT/LA` operations.
    pub const fn channel(&self) -> i32 {
        self.channel
    }

    /// Returns the next parser-visible token index after consuming the token
    /// at `index`, skipping hidden-channel tokens.
    pub fn next_visible_after(&mut self, index: usize) -> usize {
        if let Some(cached) = self
            .next_visible_after
            .get(index)
            .copied()
            .filter(|cached| *cached != UNKNOWN_NEXT_VISIBLE)
        {
            return cached;
        }

        let mut next = index + 1;
        let found = loop {
            match self.get_id(next) {
                Some(id)
                    if self.store.token_type(id) != Some(TOKEN_EOF)
                        && self.store.channel(id) != Some(self.channel) =>
                {
                    next += 1;
                    continue;
                }
                _ => break next,
            }
        };
        if let Some(slot) = self.next_visible_after.get_mut(index) {
            *slot = found;
        }
        found
    }

    pub fn text(&self, start: usize, stop: usize) -> String {
        if start > stop || start >= self.source_token_count {
            return String::new();
        }
        (start..=stop.min(self.source_token_count.saturating_sub(1)))
            .filter_map(|index| self.get(index))
            .take_while(|token| token.token_type() != TOKEN_EOF)
            .map(|token| token.text())
            .collect()
    }

    /// Concatenated text of every buffered token except EOF.
    pub fn text_all(&self) -> String {
        self.tokens()
            .filter(|token| token.token_type() != TOKEN_EOF)
            .map(|token| token.text())
            .collect()
    }

    /// Returns and clears diagnostics emitted while producing requested tokens.
    pub fn drain_source_errors(&mut self) -> Vec<TokenSourceError> {
        let requested = self.requested_token_count.get();
        let ready = self
            .source_errors
            .partition_point(|buffered| buffered.token_index < requested);
        self.source_errors
            .drain(..ready)
            .map(|buffered| buffered.error)
            .collect()
    }

    pub const fn is_filled(&self) -> bool {
        true
    }
}

#[derive(Debug)]
pub struct TokenIter<'a> {
    store: &'a TokenStore,
    next: usize,
    stop: usize,
}

impl<'a> Iterator for TokenIter<'a> {
    type Item = TokenView<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.next >= self.stop {
            return None;
        }
        let id = TokenId::try_from(self.next).ok()?;
        self.next += 1;
        self.store.view(id)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = self.stop - self.next;
        (remaining, Some(remaining))
    }
}

impl DoubleEndedIterator for TokenIter<'_> {
    fn next_back(&mut self) -> Option<Self::Item> {
        if self.next >= self.stop {
            return None;
        }
        self.stop -= 1;
        let id = TokenId::try_from(self.stop).ok()?;
        self.store.view(id)
    }
}

impl ExactSizeIterator for TokenIter<'_> {}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::token::HIDDEN_CHANNEL;
    use std::collections::VecDeque;

    #[derive(Debug)]
    struct VecTokenSource {
        tokens: VecDeque<TokenSpec>,
        index: usize,
    }

    impl TokenSource for VecTokenSource {
        fn next_token(&mut self, sink: &mut TokenSink<'_>) -> Result<TokenId, TokenStoreError> {
            let spec = self
                .tokens
                .pop_front()
                .unwrap_or_else(|| TokenSpec::eof(self.index, self.index, 1, self.index));
            self.index += 1;
            sink.push(spec)
        }

        fn line(&self) -> usize {
            1
        }

        fn column(&self) -> usize {
            self.index
        }

        fn source_name(&self) -> &'static str {
            "vec"
        }
    }

    #[derive(Debug)]
    struct ErrorTokenSource {
        tokens: VecDeque<(TokenSpec, Vec<TokenSourceError>)>,
        pending_errors: Vec<TokenSourceError>,
        index: usize,
    }

    impl TokenSource for ErrorTokenSource {
        fn next_token(&mut self, sink: &mut TokenSink<'_>) -> Result<TokenId, TokenStoreError> {
            let (spec, errors) = self.tokens.pop_front().unwrap_or_else(|| {
                (
                    TokenSpec::eof(self.index, self.index, 1, self.index),
                    Vec::new(),
                )
            });
            self.index += 1;
            self.pending_errors = errors;
            sink.push(spec)
        }

        fn line(&self) -> usize {
            1
        }

        fn column(&self) -> usize {
            self.index
        }

        fn source_name(&self) -> &'static str {
            "errors"
        }

        fn drain_errors(&mut self) -> Vec<TokenSourceError> {
            std::mem::take(&mut self.pending_errors)
        }
    }

    #[derive(Debug, Default)]
    struct StaleIdTokenSource {
        previous: Option<TokenId>,
    }

    impl TokenSource for StaleIdTokenSource {
        fn next_token(&mut self, sink: &mut TokenSink<'_>) -> Result<TokenId, TokenStoreError> {
            let emitted = sink.push(TokenSpec::explicit(1, "x"))?;
            Ok(self.previous.replace(emitted).unwrap_or(emitted))
        }

        fn line(&self) -> usize {
            1
        }

        fn column(&self) -> usize {
            0
        }

        fn source_name(&self) -> &'static str {
            "stale-id"
        }
    }

    fn source(tokens: Vec<TokenSpec>) -> VecTokenSource {
        VecTokenSource {
            tokens: tokens.into(),
            index: 0,
        }
    }

    #[test]
    fn stream_skips_hidden_channel_for_lookahead() {
        let mut stream = CommonTokenStream::new(source(vec![
            TokenSpec::explicit(1, "a"),
            TokenSpec::explicit(2, " ").with_channel(HIDDEN_CHANNEL),
            TokenSpec::explicit(3, "b"),
            TokenSpec::eof(3, 3, 1, 3),
        ]));
        assert_eq!(stream.la_token(1), 1);
        stream.consume();
        assert_eq!(stream.la_token(1), 3);
        assert_eq!(
            stream
                .lt(-1)
                .expect("look-behind token should be buffered")
                .token_type(),
            1
        );
    }

    #[test]
    fn text_returns_empty_when_start_is_past_buffer() {
        let stream = CommonTokenStream::new(source(vec![
            TokenSpec::explicit(1, "a"),
            TokenSpec::eof(1, 1, 1, 1),
        ]));
        assert_eq!(stream.text(10, 12), "");
    }

    #[test]
    fn text_concatenates_borrowed_token_text() {
        let stream = CommonTokenStream::new(source(vec![
            TokenSpec::explicit(1, "a"),
            TokenSpec::explicit(2, "b"),
            TokenSpec::eof(2, 2, 1, 2),
        ]));
        assert_eq!(stream.text(0, 1), "ab");
        assert_eq!(stream.text_all(), "ab");
    }

    #[test]
    fn construction_rejects_stale_non_eof_token_id() {
        let error = CommonTokenStream::try_new(StaleIdTokenSource::default())
            .expect_err("a stale token ID must terminate buffering with an error");

        assert!(error.to_string().contains("return ID 1"));
        assert!(error.to_string().contains("returned ID 0"));
    }

    #[test]
    fn source_errors_remain_hidden_until_their_token_is_requested() {
        let suffix_error = TokenSourceError::new(1, 4, "token recognition error at: '@'");
        let mut stream = CommonTokenStream::new(ErrorTokenSource {
            tokens: [
                (TokenSpec::explicit(1, "x"), Vec::new()),
                (TokenSpec::explicit(2, "y"), Vec::new()),
                (TokenSpec::eof(3, 3, 1, 3), vec![suffix_error.clone()]),
            ]
            .into(),
            pending_errors: Vec::new(),
            index: 0,
        });

        assert!(stream.drain_source_errors().is_empty());
        assert_eq!(stream.token_type_at_index(1), 2);
        assert!(stream.drain_source_errors().is_empty());

        assert_eq!(stream.token_type_at_index(2), TOKEN_EOF);
        assert_eq!(stream.drain_source_errors(), vec![suffix_error]);
    }

    #[test]
    fn tokens_returns_borrowing_views() {
        let stream = CommonTokenStream::new(source(vec![
            TokenSpec::explicit(1, "a"),
            TokenSpec::explicit(2, "b"),
            TokenSpec::eof(2, 2, 1, 2),
        ]));
        assert_eq!(stream.tokens().len(), 3);
        assert_eq!(
            stream.tokens().next().map(|token| token.token_type()),
            Some(1)
        );
        assert_eq!(
            stream.tokens().next_back().map(|token| token.token_type()),
            Some(TOKEN_EOF)
        );
    }

    #[test]
    fn set_token_source_replaces_buffer_and_preserves_channel() {
        let mut stream = CommonTokenStream::with_channel(
            source(vec![
                TokenSpec::explicit(1, "old").with_channel(2),
                TokenSpec::eof(3, 3, 1, 3),
            ]),
            2,
        );

        stream.set_token_source(source(vec![
            TokenSpec::explicit(2, "new").with_channel(2),
            TokenSpec::eof(3, 3, 1, 3),
        ]));

        assert_eq!(stream.channel(), 2);
        assert_eq!(stream.index(), 0);
        assert_eq!(stream.la_token(1), 2);
        assert_eq!(stream.text_all(), "new");
    }

    #[test]
    fn refill_reuses_mutated_token_source_in_place() {
        let mut stream = CommonTokenStream::new(source(vec![
            TokenSpec::explicit(1, "old"),
            TokenSpec::eof(3, 3, 1, 3),
        ]));
        let source = stream.token_source_mut();
        source.tokens = vec![TokenSpec::explicit(2, "new"), TokenSpec::eof(3, 3, 1, 3)].into();
        source.index = 0;

        stream.refill();

        assert_eq!(stream.index(), 0);
        assert_eq!(stream.la_token(1), 2);
        assert_eq!(stream.text_all(), "new");
    }
}