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
//! Channel based `TokenStream`
use std::borrow::Borrow;
use crate::int_stream::{IntStream, IterWrapper, EOF};
use crate::token::{OwningToken, Token, TOKEN_DEFAULT_CHANNEL, TOKEN_INVALID_TYPE};
use crate::token_factory::TokenFactory;
use crate::token_source::TokenSource;
use crate::token_stream::{TokenStream, UnbufferedTokenStream};
/// Default token stream that skips token that not correspond to current channel.
#[derive(Debug)]
pub struct CommonTokenStream<'input, T: TokenSource<'input>> {
base: UnbufferedTokenStream<'input, T>,
channel: i32,
}
impl<'input, T: TokenSource<'input>> CommonTokenStream<'input, T> {
pub fn base(&self) -> &UnbufferedTokenStream<'input, T> {
&self.base
}
}
crate::tid! { impl<'input,T> TidAble<'input> for CommonTokenStream<'input, T> where T: TokenSource<'input>}
impl<'input, T: TokenSource<'input>> IntStream for CommonTokenStream<'input, T> {
#[inline]
fn consume(&mut self) {
self.base.consume();
// self.base.p = self.next_token_on_channel(self.base.p,self.channel);
// self.base.current_token_index = self.base.p;
let next = self.next_token_on_channel(self.base.p, self.channel, 1);
self.base.seek(next);
// Ok(())
}
#[inline]
fn la(&mut self, i: isize) -> i32 {
self.lt(i)
.map(|t| t.borrow().get_token_type())
.unwrap_or(TOKEN_INVALID_TYPE)
}
#[inline(always)]
fn mark(&mut self) -> isize {
0
}
#[inline(always)]
fn release(&mut self, _marker: isize) {}
#[inline(always)]
fn index(&self) -> isize {
self.base.index()
}
#[inline(always)]
fn seek(&mut self, index: isize) {
self.base.seek(index);
}
#[inline(always)]
fn size(&self) -> isize {
self.base.size()
}
fn get_source_name(&self) -> String {
self.base.get_source_name()
}
}
impl<'input, T: TokenSource<'input>> TokenStream<'input> for CommonTokenStream<'input, T> {
type TF = T::TF;
#[inline(always)]
fn lt(&mut self, k: isize) -> Option<&<Self::TF as TokenFactory<'input>>::Tok> {
if k == 0 {
return None;
}
if k < 0 {
return self.lb(-k);
}
self.lt_inner(k)
}
#[inline]
fn get(&self, index: isize) -> &<Self::TF as TokenFactory<'input>>::Tok {
self.base.get(index)
}
fn get_token_source(&self) -> &dyn TokenSource<'input, TF = Self::TF> {
self.base.get_token_source()
}
fn get_text_from_interval(&self, start: isize, stop: isize) -> String {
self.base.get_text_from_interval(start, stop)
}
}
impl<'input, T: TokenSource<'input>> CommonTokenStream<'input, T> {
/// Creates CommonTokenStream that produces tokens from `TOKEN_DEFAULT_CHANNEL`
pub fn new(lexer: T) -> CommonTokenStream<'input, T> {
Self::with_channel(lexer, TOKEN_DEFAULT_CHANNEL)
}
/// Creates CommonTokenStream that produces tokens from `channel`
pub fn with_channel(lexer: T, channel: i32) -> CommonTokenStream<'input, T> {
let mut r = CommonTokenStream {
base: UnbufferedTokenStream::new_buffered(lexer),
channel,
};
r.sync(0);
// todo this is definitively not optimal
if let Some(tok) = r.lt(1) {
if tok.borrow().get_channel() != channel {
r.consume();
}
}
r
}
pub fn get_dfa_string(&self) -> String {
self.base.get_dfa_string()
}
fn lt_inner(&mut self, k: isize) -> Option<&<T::TF as TokenFactory<'input>>::Tok> {
let mut i = self.base.p;
let mut n = 1; // we know tokens[p] is a good one
// find k good tokens
while n < k {
// skip off-channel tokens, but make sure to not look past EOF
if self.sync(i + 1) {
i = self.next_token_on_channel(i + 1, self.channel, 1);
}
n += 1;
}
// if ( i>range ) range = i;
self.base.tokens.get(i as usize)
}
/// Restarts this token stream
pub fn reset(&mut self) {
self.base.p = 0;
self.base.current_token_index = 0;
}
/// Creates iterator over this token stream
pub fn iter(&mut self) -> IterWrapper<'_, Self> {
IterWrapper(self, false)
}
fn sync(&mut self, i: isize) -> bool {
let need = i - self.size() + 1;
if need > 0 {
let fetched = self.base.fill(need);
return fetched >= need;
}
true
}
//
// fn fetch(&self, n: isize) -> int { unimplemented!() }
//
// fn get_tokens(&self, start: isize, stop: isize, types: &IntervalSet) -> Vec<Token> { unimplemented!() }
//
// fn lazy_init(&self) { unimplemented!() }
//
// fn setup(&self) { unimplemented!() }
//
// fn get_token_source(&self) -> TokenSource { unimplemented!() }
//
// fn set_token_source(&self, tokenSource: TokenSource) { unimplemented!() }
//todo make this const generic over direction
fn next_token_on_channel(&mut self, mut i: isize, channel: i32, direction: isize) -> isize {
self.sync(i);
if i >= self.size() {
return self.size() - 1;
}
let mut token = self.base.tokens[i as usize].borrow();
while token.get_channel() != channel {
i += direction;
if token.get_token_type() == EOF || i < 0 {
return i;
}
self.sync(i);
token = self.base.tokens[i as usize].borrow();
}
i
}
/// Scans backwards from index `i` looking for a token on the given
/// `channel`. Returns the index of the first matching token, or -1 if
/// none is found before the start of the stream. EOF tokens are treated
/// as matching any channel.
fn previous_token_on_channel(&mut self, mut i: isize, channel: i32) -> isize {
self.sync(i);
if i >= self.size() {
return self.size() - 1;
}
while i >= 0 {
let token = self.base.tokens[i as usize].borrow();
if token.get_token_type() == EOF || token.get_channel() == channel {
return i;
}
i -= 1;
}
-1
}
/// Collects tokens in the index range `[from, to]` that are on the
/// given `channel`. If `channel` is -1, collects all tokens that are
/// *not* on `TOKEN_DEFAULT_CHANNEL`. Returns `None` if no tokens match.
fn filter_for_channel(&self, from: isize, to: isize, channel: i32) -> Option<Vec<OwningToken>> {
let mut hidden: Vec<OwningToken> = Vec::new();
for i in from..=to {
if i < 0 || i >= self.base.tokens.len() as isize {
continue;
}
let t = self.base.tokens[i as usize].borrow();
if channel == -1 {
if t.get_channel() != TOKEN_DEFAULT_CHANNEL {
hidden.push(t.to_owned());
}
} else if t.get_channel() == channel {
hidden.push(t.to_owned());
}
}
if hidden.is_empty() {
None
} else {
Some(hidden)
}
}
/// Returns hidden tokens to the left of `token_index` on the given
/// `channel`. Scans backwards to the previous on-channel token and
/// collects everything in between. Pass -1 as `channel` to get all
/// non-default-channel tokens.
pub fn get_hidden_tokens_to_left(
&mut self,
token_index: isize,
channel: i32,
) -> Option<Vec<OwningToken>> {
let prev_on_channel =
self.previous_token_on_channel(token_index - 1, TOKEN_DEFAULT_CHANNEL);
let from = if prev_on_channel == token_index - 1 || prev_on_channel < 0 {
return None;
} else {
prev_on_channel + 1
};
self.filter_for_channel(from, token_index - 1, channel)
}
/// Returns hidden tokens to the right of `token_index` on the given
/// `channel`. Scans forward to the next on-channel token and collects
/// everything in between. Pass -1 as `channel` to get all
/// non-default-channel tokens.
pub fn get_hidden_tokens_to_right(
&mut self,
token_index: isize,
channel: i32,
) -> Option<Vec<OwningToken>> {
let next_on_channel = self.next_token_on_channel(token_index + 1, TOKEN_DEFAULT_CHANNEL, 1);
let to = next_on_channel - 1;
if to <= token_index {
return None;
}
self.filter_for_channel(token_index + 1, to, channel)
}
//
// fn get_source_name(&self) -> String { unimplemented!() }
//
// fn get_all_text(&self) -> String { unimplemented!() }
//
// fn get_text_from_tokens(&self, start: Token, end: Token) -> String { unimplemented!() }
//
// fn get_text_from_rule_context(&self, interval: RuleContext) -> String { unimplemented!() }
//
// fn get_text_from_interval(&self, interval: &Interval) -> String { unimplemented!() }
//
// fn fill(&self) { unimplemented!() }
//
// fn adjust_seek_index(&self, i: isize) -> int { unimplemented!() }
fn lb(
&mut self,
k: isize,
) -> Option<&<<Self as TokenStream<'input>>::TF as TokenFactory<'input>>::Tok> {
if k == 0 || (self.base.p - k) < 0 {
return None;
}
let mut i = self.base.p;
let mut n = 1;
// find k good tokens looking backwards
while n <= k && i > 0 {
// skip off-channel tokens
i = self.next_token_on_channel(i - 1, self.channel, -1);
n += 1;
}
if i < 0 {
return None;
}
self.base.tokens.get(i as usize)
}
// fn get_number_of_on_channel_tokens(&self) -> int { unimplemented!() }
}