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
use std::cmp::min;
use std::convert::TryFrom;
use std::iter::FromIterator;
use std::result;
use crate::char_stream::CharStream;
use crate::errors::ANTLRError;
use crate::int_stream::IntStream;
use crate::interval_set::Interval;
use crate::token::Token;
pub struct InputStream {
name: String,
index: isize,
data: Vec<isize>,
}
impl InputStream {
pub fn new(data: String) -> InputStream {
let data = data.chars().map(|ch| ch as isize).collect();
InputStream {
name: "<empty>".to_string(),
index: 0,
data,
}
}
pub fn reset(&mut self) {
self.index = 0
}
pub fn lt(&mut self, offset: isize) -> isize {
self.la(offset)
}
}
impl CharStream for InputStream {
fn get_text(&self, _start: isize, _stop: isize) -> String {
let stop = min(self.data.len(), (_stop + 1) as usize);
String::from_iter(self.data[_start as usize..stop].iter().map(|x| char::try_from(*x as u32).unwrap()))
}
fn get_text_from_tokens(&self, _start: &dyn Token, _stop: &dyn Token) -> &str {
unimplemented!()
}
fn get_text_from_interval(&self, i: &Interval) -> String {
self.get_text(i.a, i.b)
}
}
impl IntStream for InputStream {
fn consume(&mut self) -> result::Result<(), ANTLRError> {
if self.index >= self.size() {
return Err(ANTLRError::IllegalStateError("cannot consume EOF".into()));
}
self.index += 1;
Ok(())
}
fn la(&mut self, mut offset: isize) -> isize {
if offset == 0 {
return 0;
}
if offset < 0 {
offset += 1;
if (self.index + offset - 1) < 0 {
return crate::int_stream::EOF;
}
}
if (self.index + offset - 1) >= self.size() {
return crate::int_stream::EOF;
}
return self.data[(self.index + offset - 1) as usize] as isize;
}
fn mark(&mut self) -> isize {
-1
}
fn release(&mut self, _marker: isize) {}
fn index(&self) -> isize {
self.index
}
fn seek(&mut self, mut index: isize) {
if index <= self.index {
self.index = index
}
index = index.min(self.size());
while self.index < index {
self.consume();
}
}
fn size(&self) -> isize {
self.data.len() as isize
}
fn get_source_name(&self) -> String {
self.name.clone()
}
}