do_not_use_antlr_rust/int_stream.rs
1//! <ost generic stream of symbols
2
3/// `IntStream::la` must return EOF in the end of stream
4pub const EOF: isize = -1;
5
6/// A simple stream of symbols whose values are represented as integers. This
7/// interface provides *marked ranges* with support for a minimum level
8/// of buffering necessary to implement arbitrary lookahead during prediction.
9pub trait IntStream {
10 /// Consumes the current symbol in the stream.
11 /// Advances this stream to the next element.
12 ///
13 /// This method has the following
14 /// effects:
15 ///
16 /// - Forward movement: The value of `index`
17 /// before calling this method is less than the value of `index`
18 /// after calling this method.
19 /// - Ordered lookahead: The value of {@code LA(1)} before
20 /// calling this method becomes the value of {@code LA(-1)} after calling
21 /// this method.
22 ///
23 /// Note that calling this method does not guarantee that `index()` is
24 /// incremented by exactly 1.
25 ///
26 /// Allowed to panic if trying to consume EOF
27 fn consume(&mut self);
28
29 /// Lookaheads (or loopbacks if `i` is negative)
30 ///
31 /// Gets the value of the symbol at offset {@code i} from the current
32 /// position. When {@code i==1}, this method returns the value of the current
33 /// symbol in the stream (which is the next symbol to be consumed). When
34 /// {@code i==-1}, this method returns the value of the previously read
35 /// symbol in the stream. It is not valid to call this method with
36 /// {@code i==0}, but the specific behavior is unspecified because this
37 /// method is frequently called from performance-critical code.
38 ///
39 /// Note that default Lexer does not call this method with anything other than `-1`
40 /// so it can be used for optimizations in downstream implementations.
41 ///
42 /// Must return `EOF` if `i` points to position at or beyond the end of the stream
43 fn la(&mut self, i: isize) -> isize;
44
45 /// After this call subsequent calls to seek must succeed if seek index is greater than mark index
46 ///
47 /// Returns marker that should be used later by `release` call to release this stream from
48 fn mark(&mut self) -> isize;
49
50 /// Releases `marker`
51 fn release(&mut self, marker: isize);
52
53 /// Returns current position of the input stream
54 ///
55 /// If there is active marker from `mark` then calling `seek` later with result of this call
56 /// should put stream in same state it is currently in.
57 fn index(&self) -> isize;
58 /// Put stream back in state it was when it was in `index` position
59 ///
60 /// Allowed to panic if `index` does not belong to marked region(via `mark`-`release` calls)
61 fn seek(&mut self, index: isize);
62
63 /// Returns the total number of symbols in the stream.
64 fn size(&self) -> isize;
65
66 /// Returns name of the source this stream operates over if any
67 fn get_source_name(&self) -> String;
68}
69
70/// Iterator over `IntStream`
71#[derive(Debug)]
72pub struct IterWrapper<'a, T: IntStream>(pub &'a mut T);
73
74impl<'a, T: IntStream> Iterator for IterWrapper<'a, T> {
75 type Item = isize;
76
77 fn next(&mut self) -> Option<Self::Item> {
78 let result = self.0.la(1);
79 self.0.consume();
80 match result {
81 EOF => None,
82 x => Some(x),
83 }
84 }
85}