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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
//! Contexts represent stack frames for parselet calls.
use std::iter::FromIterator;
use super::*;
use crate::reader::Offset;
use crate::value::{Dict, List, Object, Parselet, RefValue};
/** Contexts represent stack frames for parselet calls.
Via the context, most operations regarding capture storing and loading is performed. */
pub struct Context<'runtime, 'program, 'reader, 'parselet> {
pub(crate) runtime: &'runtime mut Runtime<'program, 'reader>, // Overall runtime
pub(crate) parselet: &'parselet Parselet, // Current parselet that is executed
pub(crate) stack_start: usize, // Stack start (including locals and parameters)
pub(crate) capture_start: usize, // Stack capturing start
pub(crate) reader_start: Offset, // Current reader offset
pub(crate) source_offset: Option<Offset>, // Tokay source offset
hold: usize, // Defines number of stack items to hold on context drop
pub(crate) depth: usize, // Recursion depth
}
impl<'runtime, 'program, 'reader, 'parselet> Context<'runtime, 'program, 'reader, 'parselet> {
pub fn new(
runtime: &'runtime mut Runtime<'program, 'reader>,
parselet: &'parselet Parselet,
locals: usize,
take: usize,
hold: usize,
depth: usize,
) -> Self {
let stack_start = runtime.stack.len() - take;
/*
println!("--- {:?} ---", parselet.name);
println!("stack = {:#?}", runtime.stack);
println!("stack = {:?}", runtime.stack.len());
println!("start = {:?}", stack_start);
println!("resize = {:?}", stack_start + locals + 1);
*/
runtime
.stack
.resize(stack_start + locals + 1, Capture::Empty);
Self {
stack_start,
capture_start: stack_start + locals + 1,
reader_start: runtime.reader.tell(),
runtime,
parselet,
source_offset: None,
hold,
depth,
}
}
/// Print debug output with context depth indention
#[inline]
pub fn debug(&self, msg: &str) {
println!(
"{}{}{:5} {}",
".".repeat(self.depth),
//self.parselet.name.as_deref().unwrap_or("(unnamed)"), // fixme: TEMPORAL!
self.parselet.name,
//if self.parselet.consuming.is_some() {
if self.parselet.consuming.is_some() {
format!("@{: <4}", self.runtime.reader.tell().offset)
} else {
"".to_string()
},
msg
);
}
/// Shortcut for an Ok(Accept::Push) with the given value.
/// To push a value immediatelly, use context.runtime.stack.push().
// fixme: Replace later by real push, when recursive VM is removed.
#[inline]
pub fn push(&self, value: RefValue) -> Result<Accept, Reject> {
Ok(Accept::Push(Capture::Value(value, None, 10)))
}
/// Pop value off the stack.
#[inline]
pub fn pop(&mut self) -> RefValue {
// todo: check for context limitations on the stack?
let mut capture = self.runtime.stack.pop().unwrap();
capture.extract(self.runtime.reader)
}
/// Peek top value of stack.
#[inline]
pub fn peek(&mut self) -> RefValue {
// todo: check for context limitations on the stack?
let capture = self.runtime.stack.last_mut().unwrap();
capture.extract(self.runtime.reader)
}
// Push a value onto the stack
#[inline]
pub fn load(&mut self, index: usize) -> Result<Accept, Reject> {
let capture = &mut self.runtime.stack[index];
let value = capture.extract(self.runtime.reader);
self.push(value)
}
/** Return a capture by index as RefValue. */
pub fn get_capture(&mut self, pos: usize) -> Option<RefValue> {
let pos = self.capture_start + pos - 1;
if pos >= self.runtime.stack.len() {
None
}
// This is $0?
else if pos < self.capture_start {
// Capture 0 either returns an already set value or ...
if let Capture::Value(value, ..) = &self.runtime.stack[pos] {
return Some(value.clone());
}
// ...returns the current range read so far.
Some(RefValue::from(
self.runtime
.reader
.get(&self.runtime.reader.capture_from(&self.reader_start)),
))
// Any other index.
} else {
self.runtime.stack[pos].degrade();
Some(self.runtime.stack[pos].extract(&self.runtime.reader))
}
}
/** Return a capture by name as RefValue. */
pub fn get_capture_by_name(&mut self, name: &str) -> Option<RefValue> {
let tos = self.runtime.stack.len();
for i in (0..tos - self.capture_start).rev() {
let capture = &mut self.runtime.stack[self.capture_start + i];
match capture {
Capture::Range(_, alias, ..) | Capture::Value(_, alias, ..) if alias.is_some() => {
if alias.as_ref().unwrap() == name {
capture.degrade();
return Some(capture.extract(self.runtime.reader));
}
}
_ => {}
}
}
None
}
/** Set a capture to a RefValue by index. */
pub fn set_capture(&mut self, pos: usize, value: RefValue) {
let pos = self.capture_start + pos - 1;
if pos >= self.runtime.stack.len() {
return;
}
let capture = &mut self.runtime.stack[pos];
// $0 gets a higher severity than normal captures.
let severity = if pos < self.capture_start { 10 } else { 5 };
match capture {
Capture::Empty => *capture = Capture::Value(value, None, severity),
Capture::Range(_, alias, _) => {
*capture = Capture::Value(value, alias.clone(), severity)
}
Capture::Value(capture_value, ..) => {
*capture_value = value;
}
}
}
/** Set a capture to a RefValue by name. */
pub fn set_capture_by_name(&mut self, name: &str, value: RefValue) {
let tos = self.runtime.stack.len();
for i in (0..tos - self.capture_start).rev() {
let capture = &mut self.runtime.stack[self.capture_start + i];
match capture {
Capture::Range(_, alias, ..) | Capture::Value(_, alias, ..) if alias.is_some() => {
if alias.as_ref().unwrap() == name {
match capture {
Capture::Empty => *capture = Capture::Value(value, None, 5),
Capture::Range(_, alias, _) => {
*capture = Capture::Value(value, alias.clone(), 5)
}
Capture::Value(capture_value, ..) => {
*capture_value = value;
}
}
break;
}
}
_ => {}
}
}
}
/** Helper function to collect captures from a capture_start and turn
them either into a dict or list object capture or take them as is.
This function is internally used for automatic AST construction and value
inheriting.
*/
pub(crate) fn collect(
&mut self,
capture_start: usize,
copy: bool,
single: bool,
mut inherit: bool,
severity: u8,
) -> Result<Option<RefValue>, Capture> {
if capture_start > self.runtime.stack.len() {
return Ok(None);
}
// Eiter copy or drain captures from stack
let captures: Vec<Capture> = if copy {
Vec::from_iter(
self.runtime.stack[capture_start..]
.iter()
.filter(|item| !(matches!(item, Capture::Empty)))
.cloned(),
)
} else {
self.runtime
.stack
.drain(capture_start..)
.filter(|item| !(matches!(item, Capture::Empty)))
.collect()
};
if self.runtime.debug > 5 {
self.debug(&format!(
"collect captures = {} single = {}, severity = {}",
captures.len(),
single,
severity
));
for i in 0..captures.len() {
self.debug(&format!(" {}: {:?}", i, captures[i]));
}
}
// No captures, then just stop!
if captures.len() == 0 {
return Ok(None);
}
let mut list = List::new();
let mut dict = Dict::new();
let mut max = severity;
// Capture inheritance is only possible when there is only one capture
if inherit && captures.len() > 1 {
inherit = false;
}
// Collect any significant captures and values
for capture in captures.into_iter() {
match capture {
Capture::Range(range, alias, severity) if severity >= max => {
if severity > max {
max = severity;
list.clear();
dict.clear();
}
let value = RefValue::from(self.runtime.reader.get(&range));
if let Some(alias) = alias {
dict.insert(alias, value);
} else if inherit {
return Err(Capture::Range(range, alias, severity));
} else {
list.push(value);
}
}
Capture::Value(value, alias, severity) if severity >= max => {
if severity > max {
max = severity;
list.clear();
dict.clear();
}
if !value.is_void() {
if let Some(alias) = alias {
dict.insert(alias, value);
} else if inherit {
return Err(Capture::Value(value, alias, severity));
} else {
list.push(value);
}
}
}
_ => {}
};
}
if self.runtime.debug > 6 {
println!("list = {:?}", list);
println!("dict = {:?}", dict);
}
if dict.len() == 0 {
if list.len() > 1 || (list.len() > 0 && !single) {
Ok(Some(RefValue::from(list)))
} else if list.len() == 1 {
Ok(Some(list.pop().unwrap()))
} else {
Ok(None)
}
} else {
// Store list-items additionally when there is a dict?
// This is currently under further consideration and not finished.
let mut idx = 0;
for item in list.into_iter() {
loop {
let key = format!("#{}", idx);
if let None = dict.get(&key) {
dict.insert(key, item);
break;
}
idx += 1;
}
idx += 1;
}
Ok(Some(RefValue::from(dict)))
}
}
/// Drains n items off the stack into a vector of values
pub(crate) fn drain(&mut self, n: usize) -> Vec<RefValue> {
let tos = self.runtime.stack.len();
assert!(n <= tos - self.capture_start);
let captures: Vec<Capture> = self
.runtime
.stack
.drain(tos - n..)
.filter(|capture| !matches!(capture, Capture::Empty))
.collect();
captures
.into_iter()
.map(|mut capture| capture.extract(self.runtime.reader))
.collect()
}
}
impl<'runtime, 'program, 'reader, 'parselet> Drop
for Context<'runtime, 'program, 'reader, 'parselet>
{
fn drop(&mut self) {
self.runtime.stack.truncate(self.stack_start + self.hold);
}
}