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
pub mod subtree;
pub mod lrk;
pub mod rnglr;
use super::lexers::ContextProvider;
use super::symbols::Symbol;
use super::utils::bin::*;
pub const MAX_ERROR_COUNT: usize = 100;
pub type TreeAction = u16;
pub const TREE_ACTION_NONE: TreeAction = 0;
pub const TREE_ACTION_REPLACE_BY_CHILDREN: TreeAction = 1;
pub const TREE_ACTION_DROP: TreeAction = 2;
pub const TREE_ACTION_PROMOTE: TreeAction = 3;
pub const TREE_ACTION_REPLACE_BY_EPSILON: TreeAction = 4;
pub type LROpCode = u16;
pub const LR_OP_CODE_BASE_POP_STACK: LROpCode = 0;
pub const LR_OP_CODE_BASE_ADD_VIRTUAL: LROpCode = 1 << 2;
pub const LR_OP_CODE_BASE_SEMANTIC_ACTION: LROpCode = 1 << 3;
pub const LR_OP_CODE_BASE_ADD_NULLABLE_VARIABLE: LROpCode = 1 << 4;
pub fn get_op_code_base(op_code: LROpCode) -> LROpCode {
op_code & 0xFFFC
}
pub fn get_op_code_tree_action(op_code: LROpCode) -> TreeAction {
op_code & 0x0003
}
pub type LRActionCode = u16;
pub const LR_ACTION_CODE_NONE: LRActionCode = 0;
pub const LR_ACTION_CODE_REDUCE: LRActionCode = 1;
pub const LR_ACTION_CODE_SHIFT: LRActionCode = 2;
pub const LR_ACTION_CODE_ACCEPT: LRActionCode = 3;
#[derive(Copy, Clone)]
struct LRColumnMapCell {
identifier: u16,
column: u16
}
struct LRColumnMap {
cache: [u16; 512],
others: Vec<LRColumnMapCell>
}
impl LRColumnMap {
pub fn new(data: &[u8], offset: usize, column_count: usize) -> LRColumnMap {
let mut result = LRColumnMap {
cache: [0; 512],
others: Vec::<LRColumnMapCell>::new()
};
for i in 0..column_count {
let index = offset + i * 2;
let identifier = read_u16(data, index);
result.add(i as u16, identifier);
}
result
}
fn add(&mut self, column: u16, identifier: u16) {
if identifier <= 0x1FF {
self.cache[identifier as usize] = column;
} else {
self.others.push(LRColumnMapCell { identifier, column });
}
}
pub fn get(&self, identifier: u32) -> u16 {
if identifier <= 0x01FF {
self.cache[identifier as usize]
} else {
for i in 0..self.others.len() {
if self.others[i].identifier as u32 == identifier {
return self.others[i].column;
}
}
return 0;
}
}
}
#[derive(Copy, Clone)]
struct LRContextOpening {
identifier: u16,
context: u16
}
pub struct LRContexts {
openings: Option<Vec<LRContextOpening>>
}
impl LRContexts {
pub fn new() -> LRContexts {
LRContexts { openings: None }
}
pub fn add(&mut self, identifier: u16, context: u16) {
match self.openings {
None => {
let mut content = Vec::<LRContextOpening>::new();
content.push(LRContextOpening {
identifier,
context
});
self.openings = Some(content);
}
Some(ref mut data) => {
data.push(LRContextOpening {
identifier,
context
});
}
}
}
pub fn opens(&self, terminal_id: u32, context: u16) -> bool {
match self.openings {
None => false,
Some(ref data) => {
for x in data.iter() {
if x.identifier as u32 == terminal_id && x.context == context {
return true;
}
}
false
}
}
}
}
#[derive(Copy, Clone)]
pub struct LRAction<'a> {
table: &'a [u16],
offset: usize
}
impl<'a> LRAction<'a> {
pub fn get_code(&self) -> LRActionCode {
self.table[self.offset]
}
pub fn get_data(&self) -> u16 {
self.table[self.offset + 1]
}
}
pub struct LRProduction {
pub head: usize,
pub head_action: TreeAction,
pub reduction_length: usize,
pub bytecode: Vec<LROpCode>
}
impl LRProduction {
pub fn new(data: &[u8], index: &mut usize) -> LRProduction {
let head = read_u16(data, *index) as usize;
*index += 2;
let head_action = data[*index] as TreeAction;
*index += 1;
let reduction_length = data[*index] as usize;
*index += 1;
let bytecode_length = data[*index] as usize;
*index += 1;
let mut bytecode = Vec::<LROpCode>::with_capacity(bytecode_length);
for i in 0..bytecode_length {
bytecode.push(read_u16(data, *index + i * 2));
}
*index += bytecode_length * 2;
LRProduction {
head,
head_action,
reduction_length,
bytecode
}
}
}
pub struct LRExpected {
pub shifts: Vec<Symbol>,
pub reductions: Vec<Symbol>
}
impl LRExpected {
pub fn new() -> LRExpected {
LRExpected {
shifts: Vec::<Symbol>::new(),
reductions: Vec::<Symbol>::new()
}
}
pub fn add_unique_shift(&mut self, terminal: Symbol) {
let position = self.reductions.iter().position(|x| *x == terminal);
if position.is_some() {
self.reductions.remove(position.unwrap());
}
if !self.shifts.contains(&terminal) {
self.shifts.push(terminal);
}
}
pub fn add_unique_reduction(&mut self, terminal: Symbol) {
if !self.shifts.contains(&terminal) && !self.reductions.contains(&terminal) {
self.reductions.push(terminal);
}
}
}
pub trait Parser {
fn parse(&mut self);
}