tokay 0.6.13

Tokay is a programming language designed for ad-hoc parsing.
Documentation
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
//! Contexts and stack frames for parselet calls.
use super::*;
use crate::reader::Offset;
use crate::value::{Dict, List, Object, Parselet, RefValue, Value};
use std::iter::FromIterator;

/** Representation of a stack-frame based on current context. */
#[derive(Debug, Clone, Copy)]
pub struct Frame {
    pub fuse: Option<usize>,  // optional fuse
    pub capture_start: usize, // capture start
    pub reader_start: Offset, // reader start
}

impl std::fmt::Display for Frame {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "capture: {}, reader: {}, fuse: {:?}",
            self.capture_start, self.reader_start.offset, self.fuse
        )
    }
}

/** Representatin of a loop-frame */
#[derive(Debug, Clone, Copy)]
pub struct Loop {
    pub frames: usize, // Number of frames at loop start
    pub start: usize,  // Start address of loop iteration
    pub end: usize,    // End address of loop
}

/** Contexts represent stack frames for parselet calls.

Within the context, most operations regarding capture storing and loading is performed. */
pub struct Context<'program, 'reader, 'thread, 'parselet> {
    // References
    pub thread: &'thread mut Thread<'program, 'reader>, // Current VM thread
    pub parselet: &'parselet Parselet,                  // Current parselet
    pub reader_start: Offset,                           // Overall reader start

    pub depth: usize, // Recursion depth
    pub debug: u8,    // Debug level

    // Virtual machine
    var: Capture,            // Context variable ($0)
    pub stack: Vec<Capture>, // Capture stack
    pub frames: Vec<Frame>,  // Frame stack
    pub frame: Frame,        // Current frame

    pub loops: Vec<Loop>, // Loop stack

    // Variables
    pub source_offset: Option<Offset>, // Tokay source offset needed for error reporting
}

impl<'program, 'reader, 'thread, 'parselet> Context<'program, 'reader, 'thread, 'parselet> {
    pub fn new(
        thread: &'thread mut Thread<'program, 'reader>,
        parselet: &'parselet Parselet,
        depth: usize,
        stack: Vec<Capture>,
    ) -> Self {
        let reader_start = thread.reader.tell();

        let frame = Frame {
            fuse: None,
            capture_start: stack.len(),
            reader_start: reader_start.clone(),
        };

        // Create Context
        Self {
            debug: thread.debug,
            thread,
            parselet,
            depth,
            var: Capture::Empty,
            stack,
            frames: Vec::new(),
            // Create context frame0
            frame,
            loops: Vec::new(),
            reader_start,
            source_offset: None,
        }
    }

    /// Print debug output with context depth indention
    #[inline]
    pub fn log(&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.thread.reader.tell().offset)
            } else {
                "".to_string()
            },
            msg
        );
    }

    /// Shortcut for an Ok(Accept::Push) with the given value.
    /// To push a value immediatelly, use context.thread.stack.push().
    #[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.stack.pop().unwrap();
        capture.extract(&mut self.thread.reader)
    }

    /// Peek top value of stack.
    #[inline]
    pub fn peek(&mut self) -> RefValue {
        // todo: check for context limitations on the stack?
        let capture = self.stack.last_mut().unwrap();
        capture.extract(&mut self.thread.reader)
    }

    // Push a value onto the stack
    #[inline]
    pub fn load(&mut self, index: usize) -> Result<Accept, Reject> {
        let capture = &mut self.stack[index];
        let value = capture.extract(&self.thread.reader);
        self.push(value)
    }

    // Reset context stack state
    #[inline]
    fn reset(&mut self, offset: Option<Offset>) {
        self.stack.truncate(self.frame.capture_start); // Truncate stack
        self.var = Capture::Empty; // Reset $0

        if let Some(offset) = offset {
            self.frame.reader_start = offset; // Set reader start to provided position
        }
    }

    /// Return top-level frame
    pub fn frame0(&self) -> &Frame {
        if self.frames.is_empty() {
            &self.frame
        } else {
            &self.frames[0]
        }
    }

    /// Return mutable top-level frame
    pub fn frame0_mut(&mut self) -> &mut Frame {
        if self.frames.is_empty() {
            &mut self.frame
        } else {
            &mut self.frames[0]
        }
    }

    /** Return a capture by index as RefValue. */
    pub fn get_capture(&mut self, pos: usize) -> Option<Capture> {
        let frame0 = self.frame0();

        if pos == 0 {
            // Capture 0 either returns an already set value or ...
            if let Capture::Value(value, ..) = &self.var {
                return Some(Capture::from(value.clone()));
            }

            // ...returns the current range read so far.
            return Some(Capture::Range(
                self.thread.reader.capture_from(&self.reader_start),
                None,
                self.parselet.severity,
            ));
        }

        let capture_start = frame0.capture_start;

        let pos = capture_start + pos - 1;

        if pos >= self.stack.len() {
            return None;
        }

        Some(self.stack[pos].clone())
    }

    /** Return a capture by name as RefValue. */
    pub fn get_capture_by_name(&mut self, name: &str) -> Option<Capture> {
        let capture_start = self.frame0().capture_start;
        let tos = self.stack.len();

        for i in (0..tos - capture_start).rev() {
            let capture = &self.stack[capture_start + i];

            if capture.alias(name) {
                return Some(capture.clone());
            }
        }

        None
    }

    /** Set a capture to a RefValue by index. */
    pub fn set_capture(&mut self, pos: usize, value: RefValue) {
        if pos == 0 {
            self.var = Capture::from(value);
            return;
        }

        let capture_start = self.frame0().capture_start;
        let pos = capture_start + pos - 1;

        if pos >= self.stack.len() {
            return;
        }

        let capture = &mut self.stack[pos];

        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,
        }
    }

    /** Set a capture to a RefValue by name. */
    pub fn set_capture_by_name(&mut self, name: &str, value: RefValue) {
        let capture_start = self.frame0().capture_start;
        let tos = self.stack.len();

        for i in (0..tos - capture_start).rev() {
            let capture = &mut self.stack[capture_start + i];

            if capture.alias(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;
            }
        }
    }

    /** Collect captures from a capture_start and turn them either into a dict or list object capture.

    Any items with a severity of at least 1 are being collected, but higher severities always win.

    - Results of a collection (either list or dict) inherit the highest collected severity
    - Token severity:
      - 0: Whitespace
      - 1: Touch
      - 5: Match, character-class, parselet
      - 10: Defined value

    This function is internally used for automatic AST construction and value inheriting.
    */
    pub fn collect(
        &mut self,
        capture_start: usize, // Stack offset to start from
        copy: bool,           // When true: Copy values instead of draining them from the stack
        sequence: bool, // Sequence mode; true: Determine dict, list or inherit type fitting best; false: Always dict
        debug: bool,    // Print debug information
    ) -> Capture {
        // Early abort when capture_start is behind stack len
        if capture_start > self.stack.len() {
            return Capture::Empty;
        }

        assert!(capture_start >= self.frame0().capture_start);

        // Eiter copy or drain captures from stack
        let captures: Vec<Capture> = if copy {
            // fixme: copy feature isn't used...
            Vec::from_iter(
                self.stack[capture_start..]
                    .iter()
                    .filter(|item| !(matches!(item, Capture::Empty)))
                    .cloned(),
            )
        } else {
            self.stack
                .drain(capture_start..)
                .filter(|item| !(matches!(item, Capture::Empty)))
                .collect()
        };

        if debug {
            self.log(&format!("collect captures = {}", captures.len()));

            for i in 0..captures.len() {
                self.log(&format!(" {}: {:?}", i, captures[i]));
            }
        }

        // Early abort when no valuable captures had been taken
        if captures.len() == 0 {
            return Capture::Empty;
        }

        // Capture inheritance is only possible when there is only one capture available
        let mut list = List::new(); // List collector
        let mut dict = Dict::new(); // Dict collector
        let mut max = self.parselet.severity; // Require at least parselet severity level
        let mut idx = 0; // Keep the order for dicts

        // Collect any significant captures and values
        // fixme: This part contains ugly and redundant code; must be reworked later.
        for capture in captures.into_iter() {
            match capture {
                Capture::Range(range, alias, severity) if severity >= max => {
                    // On higher severity, drop all results collected so far
                    if severity > max {
                        idx = 0;
                        max = severity;
                        list.clear();
                        dict.clear();
                    }

                    // fixme: This line is the only difference between the Capture::Range and Capture::Value branch.
                    //        This is totally ugly and should be reworked.
                    let value = RefValue::from(self.thread.reader.get(&range));

                    if let Some(alias) = alias {
                        // Move list items into dict when this is the first entry
                        if dict.is_empty() {
                            for (i, item) in list.drain(..).enumerate() {
                                dict.insert(RefValue::from(i), item);
                            }
                        }

                        dict.insert(RefValue::from(alias), value);
                    } else {
                        // Eiher collect into list, or insert into the dict
                        if dict.is_empty() && sequence {
                            list.push(value);
                        } else {
                            dict.insert(RefValue::from(idx), value);
                        }
                    }

                    idx += 1;
                }

                Capture::Value(value, alias, severity) if severity >= max => {
                    // On higher severity, drop all results collected so far
                    if severity > max {
                        idx = 0;
                        max = severity;
                        list.clear();
                        dict.clear();
                    }

                    if let Some(alias) = alias {
                        // Move list items into dict when this is the first entry
                        if dict.is_empty() {
                            for (i, item) in list.drain(..).enumerate() {
                                dict.insert(RefValue::from(i), item);
                            }
                        }

                        // A void value with an alias becomes null
                        dict.insert(
                            RefValue::from(alias),
                            if value.is_void() {
                                RefValue::from(Value::Null)
                            } else {
                                value
                            },
                        );
                    } else if !value.is_void() {
                        // Eiher collect into list, or insert into the dict
                        if dict.is_empty() && sequence {
                            list.push(value);
                        } else {
                            dict.insert(RefValue::from(idx), value);
                        }
                    }

                    idx += 1;
                }

                _ => {}
            };
        }

        if debug {
            self.log(&format!("list = {:?}", list));
            self.log(&format!("dict = {:?}", dict));
        }

        if dict.is_empty() && sequence {
            match list.len() {
                0 => Capture::Empty,
                1 => Capture::Value(list.pop().unwrap(), None, max),
                _ => Capture::Value(RefValue::from(list), None, max),
            }
        } else {
            Capture::Value(RefValue::from(dict), None, max)
        }
    }

    /// Drains n items off the stack into a vector of values
    pub fn drain(&mut self, n: usize) -> Vec<RefValue> {
        let tos = self.stack.len();
        assert!(n <= tos - self.frame0().capture_start);

        let captures: Vec<Capture> = self.stack.drain(tos - n..).collect();

        captures
            .into_iter()
            .map(|mut capture| capture.extract(&self.thread.reader))
            .collect()
    }

    // Execute VM opcodes in a context.
    // This function is a wrapper for Op::run() which post-processes the result.
    fn execute(&mut self, name: &str, ops: &[Op]) -> Result<Accept, Reject> {
        let mut state = Op::run(ops, self);

        match state {
            // In case state is Accept::Next, try to return a capture
            Ok(Accept::Next) => {
                // Either take $0 when set
                if let Capture::Value(value, ..) = &mut self.var {
                    state = Ok(Accept::Push(Capture::Value(
                        value.clone(),
                        None,
                        self.parselet.severity,
                    )));
                // Otherwise, push last value
                } else if self.stack.len() > self.frame.capture_start {
                    state =
                        Ok(Accept::Push(self.stack.pop().unwrap())
                            .into_push(self.parselet.severity));
                }
            }

            // Patch context source position on error, if no other position already set
            Err(Reject::Error(ref mut err)) => {
                if let Some(source_offset) = self.source_offset {
                    err.patch_offset(source_offset);
                }
            }

            _ => {}
        }

        if self.thread.debug > 3 {
            self.log(&format!("{} final state = {:?}", name, state));
        }

        state
    }

    /// Run the current context with the associated parselet
    pub fn run(&mut self, main: bool) -> Result<Accept, Reject> {
        // Debugging
        if self.debug < 3 {
            //println!("{:?}", self.parselet.name);
            if let Ok(inspect) = std::env::var("TOKAY_INSPECT") {
                for name in inspect.split(" ") {
                    if self.parselet.name.starts_with(name) {
                        self.debug = 6;
                        break;
                    }
                }
            }
        }

        if main {
            return self.run_as_main();
        }

        // Begin
        let mut ret = match self.execute("begin", &self.parselet.begin) {
            Ok(Accept::Next) | Err(Reject::Skip) => Capture::Empty,
            Ok(Accept::Push(capture)) => {
                self.reset(Some(self.thread.reader.tell()));
                capture
            }
            Ok(Accept::Repeat) => {
                self.reset(Some(self.thread.reader.tell()));
                Capture::Empty
            }
            Ok(accept) => return Ok(accept.into_push(self.parselet.severity)),
            other => return other,
        };

        // Body
        let mut first = true;
        ret = loop {
            match self.execute("body", &self.parselet.body) {
                Err(Reject::Skip) | Ok(Accept::Repeat) => {}
                Ok(Accept::Next) => break ret,
                Ok(Accept::Push(capture)) => break capture,
                Ok(accept) => return Ok(accept.into_push(self.parselet.severity)),
                Err(Reject::Next) if !first && !self.parselet.end.is_empty() => {
                    break Capture::Empty;
                }
                other => return other,
            }

            // Reset capture stack for loop repeat
            self.reset(Some(self.thread.reader.tell()));
            first = false;
        };

        // End
        ret = match self.execute("end", &self.parselet.end) {
            Ok(Accept::Next) | Err(Reject::Skip) | Ok(Accept::Repeat) => ret,
            Ok(Accept::Push(capture)) => capture,
            Ok(accept) => return Ok(accept.into_push(self.parselet.severity)),
            other => return other,
        };

        let ret = Accept::Push(ret).into_push(self.parselet.severity);

        if self.thread.debug > 3 {
            self.log(&format!("ret = {:?}", ret));
        }

        Ok(ret)
    }

    /** Run the current context as a main parselet.

    __main__-parselets are executed differently, as they handle unrecognized input as whitespace or gap,
    by skipping over it. __main__ parselets do also operate on multiple input Readers by sequence inside
    of the Context's thread.
    */
    fn run_as_main(&mut self) -> Result<Accept, Reject> {
        // collected results
        let mut results = List::new();

        // Begin
        match self.execute("main begin", &self.parselet.begin) {
            Ok(Accept::Next) | Err(Reject::Skip) | Ok(Accept::Push(Capture::Empty)) => {}
            Ok(Accept::Push(mut capture)) => {
                let res = capture.extract(&self.thread.reader);
                if !res.is_void() {
                    results.push(res);
                }
            }
            Ok(Accept::Repeat) => {}
            Ok(accept) => return Ok(accept.into_push(self.parselet.severity)),
            other => return other,
        };

        loop {
            self.reset(Some(self.thread.reader.tell()));

            // Body
            loop {
                match self.execute("main body", &self.parselet.body) {
                    Err(Reject::Next)
                    | Err(Reject::Skip)
                    | Ok(Accept::Next)
                    | Ok(Accept::Push(Capture::Empty)) => {}
                    Ok(Accept::Push(mut capture)) => {
                        let res = capture.extract(&self.thread.reader);
                        if !res.is_void() {
                            results.push(res);
                        }
                    }
                    Ok(Accept::Repeat) => {}
                    Ok(accept) => return Ok(accept.into_push(self.parselet.severity)),
                    other => return other,
                }

                if self.frame.reader_start == self.thread.reader.tell() {
                    // Skip one character if nothing was consumed
                    self.thread.reader.next();

                    // Drop all memoizations
                    self.thread.memo.clear();
                }

                // Reset capture stack for loop repeat
                self.reset(Some(self.thread.reader.tell()));

                // Break on EOF
                if self.thread.reader.eof() {
                    break;
                }
            }

            if self.thread.readers.is_empty() {
                break;
            }

            // Change reader within thread, and continue
            self.thread.reader = self.thread.readers.remove(0);

            // Drop all memoizations
            self.thread.memo.clear();
        }

        // End
        match self.execute("main end", &self.parselet.end) {
            Ok(Accept::Next) | Err(Reject::Skip) | Ok(Accept::Push(Capture::Empty)) => {}
            Ok(Accept::Push(mut capture)) => {
                let res = capture.extract(&self.thread.reader);
                if !res.is_void() {
                    results.push(res);
                }
            }
            Ok(Accept::Repeat) => {}
            Ok(accept) => return Ok(accept.into_push(self.parselet.severity)),
            other => return other,
        };

        // results has higher priority than ret
        if !results.is_empty() {
            Ok(Accept::Push(Capture::Value(
                if results.len() > 1 {
                    RefValue::from(results)
                } else {
                    results.pop().unwrap()
                },
                None,
                self.parselet.severity,
            )))
        } else {
            Ok(Accept::Push(Capture::Empty))
        }
    }
}