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
//! Symbols that parser works on
use std::borrow::{Borrow, Cow};
use std::fmt::Formatter;
use std::fmt::{Debug, Display};
use crate::token_factory::INVALID_COMMON;
/// Type of tokens that parser considers invalid
pub const TOKEN_INVALID_TYPE: i32 = 0;
/// Type of tokens that DFA can use to advance to next state without consuming actual input token.
/// Should not be created by downstream implementations.
pub const TOKEN_EPSILON: i32 = -2;
/// Min token type that can be assigned to tokens created by downstream implementations.
pub const TOKEN_MIN_USER_TOKEN_TYPE: i32 = 1;
/// Type of EOF token
pub const TOKEN_EOF: i32 = crate::int_stream::EOF;
/// Default channel lexer emits tokens to
pub const TOKEN_DEFAULT_CHANNEL: i32 = 0;
/// Predefined additional channel for lexer to assign tokens to
pub const TOKEN_HIDDEN_CHANNEL: i32 = 1;
/// Shorthand for TOKEN_HIDDEN_CHANNEL
pub const HIDDEN: i32 = TOKEN_HIDDEN_CHANNEL;
/// Implemented by tokens that are produced by a `TokenFactory`
#[allow(missing_docs)]
pub trait Token: Debug + Display {
/***
* Begin `Token` interface
*/
/// Get the text of the token
fn get_text(&self) -> &str;
/// Get the token type of the token
fn get_token_type(&self) -> i32;
/// The line number on which the first character of this token was
/// matched. The first line in the input is line 1.
fn get_line(&self) -> u32;
/// The index of the first character of this token relative to the beginning
/// of the line at which it occurs. The first character on a line has
/// position 0.
fn get_char_position_in_line(&self) -> i32;
/// Returns the channel number this token was assigned to. Each token can
/// arrive on a different channel, but the parser only "tunes" to a single
/// channel. The parser ignores everything not on DEFAULT_CHANNEL.
fn get_channel(&self) -> i32;
/// An index from 0..n-1 of the token object in the token stream.
/// This must be valid in order to print token streams and use
/// token stream rewinding.
///
/// Return -1 to indicate that the token was conjured up and does not
/// have a valid index.
fn get_token_index(&self) -> isize;
/// The starting character index of the token
///
/// This method is optional; return -1 if not implemented.
fn get_start_index(&self) -> isize;
/// The last character index of the token
///
/// This method is optional; return -1 if not implemented.
fn get_stop_index(&self) -> isize;
// fn get_token_source(&self) -> &dyn TokenSource;
// fn get_input_stream(&self) -> &dyn CharStream;
/***
* Begin `WritableToken` interface
*/
fn set_text(&mut self, _text: String);
fn set_type(&mut self, _ttype: i32);
fn set_line(&mut self, _line: u32);
fn set_char_position_in_line(&mut self, _pos: i32);
fn set_channel(&mut self, _channel: i32);
fn set_token_index(&mut self, _v: isize);
}
/// Token that owns its data
pub type OwningToken = CommonToken<'static>;
/// Most versatile Token that uses Cow to save data
/// Can be used seamlessly switch from owned to zero-copy parsing
#[derive(Clone, Debug)]
#[allow(missing_docs)]
pub struct CommonToken<'input> {
// source: Option<(Box<TokenSource>,Box<CharStream>)>,
pub token_type: i32,
pub channel: i32,
pub start: isize,
pub stop: isize,
pub token_index: isize,
pub line: u32,
pub column: i32,
pub text: Cow<'input, str>,
}
impl Display for CommonToken<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let txt = if self.token_type == TOKEN_EOF {
"<EOF>"
} else {
self.text.borrow()
};
let txt = txt.replace("\n", "\\n");
let txt = txt.replace("\r", "\\r");
let txt = txt.replace("\t", "\\t");
// let txt = escape_whitespaces(txt,false);
f.write_fmt(format_args!(
"[@{},{}:{}='{}',<{}>{},{}:{}]",
self.get_token_index(),
self.start,
self.stop,
txt,
self.token_type,
if self.channel > 0 {
",channel=".to_string() + self.channel.to_string().as_str()
} else {
String::new()
},
self.line,
self.column
))
}
}
impl<'input> Token for CommonToken<'input> {
fn get_token_type(&self) -> i32 {
self.token_type
}
fn get_channel(&self) -> i32 {
self.channel
}
fn get_start_index(&self) -> isize {
self.start
}
fn get_stop_index(&self) -> isize {
self.stop
}
fn get_line(&self) -> u32 {
self.line
}
fn get_char_position_in_line(&self) -> i32 {
self.column
}
// fn get_source(&self) -> Option<(Box<dyn TokenSource>, Box<dyn CharStream>)> {
// unimplemented!()
// }
fn get_text(&self) -> &str {
if self.token_type == TOKEN_EOF {
"<EOF>"
} else {
self.text.borrow()
}
}
fn get_token_index(&self) -> isize {
self.token_index
}
fn set_token_index(&mut self, _v: isize) {
self.token_index = _v;
}
fn set_text(&mut self, _text: String) {
self.text = Cow::from(_text);
}
fn set_type(&mut self, _ttype: i32) {
self.token_type = _ttype;
}
fn set_line(&mut self, _line: u32) {
self.line = _line;
}
fn set_char_position_in_line(&mut self, _pos: i32) {
self.column = _pos;
}
fn set_channel(&mut self, _channel: i32) {
self.channel = _channel;
}
}
impl Default for &'_ CommonToken<'_> {
fn default() -> Self {
&INVALID_COMMON
}
}
impl From<&dyn Token> for CommonToken<'static> {
fn from(value: &dyn Token) -> Self {
CommonToken {
token_type: value.get_token_type(),
channel: value.get_channel(),
start: value.get_start_index(),
stop: value.get_stop_index(),
token_index: value.get_token_index(),
line: value.get_line(),
column: value.get_char_position_in_line(),
text: value.get_text().to_string().into(),
}
}
}