wlambda 0.8.1

WLambda is an embeddable scripting language for Rust
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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
// Copyright (c) 2020-2022 Weird Constructor <weirdconstructor@gmail.com>
// This is a part of WLambda. See README.md and COPYING for details.

use crate::vval::VVal;
use crate::vval::Syntax;
use crate::vval::SynPos;
use crate::vval::FileRef;
use std::fmt;

/// This is the parser state data structure. It holds the to be read source
/// code and keeps track of the parser head position.
///
/// Can be created using `parser::State::new`:
///
/// ```rust
/// use wlambda::parser::State;
///
/// let code    = "{ 123 }";
/// let mut ps  = State::new(code, "filenamehere");
///
/// // ...
/// ```
#[allow(dead_code)]
pub struct State {
    input:          Vec<char>,
    ch_ptr:         usize,
    line_no:        u32,
    col_no:         u32,
    indent:         Option<u32>,
    line_indent:    u32,
    last_tok_char:  char,
    file:           FileRef,
    ident_mode:     IdentMode,
}

/// This is a special mode for selector parsing.
/// It differenciates between normal (selector)
/// identifiers and direct pattern identifiers.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum IdentMode {
    IdentSelector,
    IdentPattern,
}

#[derive(Copy, Clone, Debug)]
pub struct IndentPos {
    line_no:        u32,
    indent:         Option<u32>,
    line_indent:    u32,
}

impl IndentPos {
    pub fn belongs_to(&self, call_ip: &IndentPos) -> bool {
        self.line_no == call_ip.line_no
        || (if let Some(i) = self.indent {
                if let Some(i2) = call_ip.indent { i > i2 }
                else { i > call_ip.line_indent } }
            else { true })
    }
}

/// The possible errors the parser can detect while reading code.
#[derive(Debug, PartialEq)]
pub enum ParseErrorKind {
    UnexpectedToken(char, &'static str),
    ExpectedToken(char, &'static str),
    BadEscape(&'static str),
    BadIndent(&'static str),
    BadValue(ParseValueError),
    BadPack(String),
    BadPattern(String),
    BadFormat(String),
    BadKeyword(String, &'static str),
    BadNumber(ParseNumberError),
    //BadCall(&'static str),
    EOF(&'static str),
}
impl fmt::Display for ParseErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use ParseErrorKind::*;
        match self {
            UnexpectedToken(c, s) => write!(f, "Unexpected token '{}'. At {}", c, s),
            ExpectedToken(c, s)   => write!(f, "Expected token '{}'. At {}", c, s),
            BadEscape(s)          => write!(f, "{}", s),
            BadIndent(s)          => write!(f, "{}", s),
            BadValue(s)           => write!(f, "{}", s),
            BadPack(s)            => write!(f, "{}", s),
            BadPattern(s)         => write!(f, "{}", s),
            BadFormat(s)          => write!(f, "{}", s),
            BadKeyword(kw, s)     => write!(f, "Got '{}', expected {}", kw, s),
            BadNumber(s)          => write!(f, "{}", s),
            //BadCall(s)            => write!(f, "{}", s),
            EOF(s)                => write!(f, "EOF while parsing: {}", s),
        }
    }
}

#[derive(Debug, PartialEq)]
pub enum ParseValueError {
    Expected(&'static str),
    UnknownSpecialIdentifier(char),
    VectorLength,
    ExpectedAccumulator,
    ExpectedMaxArity,
    ExpectedMinArity,
}
impl fmt::Display for ParseValueError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use ParseValueError::*;
        match self {
            UnknownSpecialIdentifier(c) => write!(
                f,
                "Expected special value, unknown special value identifier '{}'",
                c
            ),
            Expected(s) => write!(
                f,
                "Expected {}",
                s
            ),
            VectorLength        => write!(f, "Numerical Vectors must have 2 to 4 values, inclusive!"),
            ExpectedAccumulator => write!(f, "Expected accumulator value"),
            ExpectedMaxArity    => write!(f, "Expected integer value for min arity"),
            ExpectedMinArity    => write!(f, "Expected integer value for max arity"),
        }
    }
}

impl From<ParseValueError> for ParseErrorKind {
    fn from(p: ParseValueError) -> ParseErrorKind {
        ParseErrorKind::BadValue(p)
    }
}

#[derive(Debug, PartialEq)]
pub enum ParseNumberError {
    InvalidIndexDigits(String),
    InvalidFractionalDigits(String),
    UnsupportedRadix(u8),
    UnsupportedRadixPrefix(char, String),
    InvalidRadix(String, u8, std::num::ParseIntError),
}
impl fmt::Display for ParseNumberError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use ParseNumberError::*;
        match self {
            InvalidIndexDigits(r)      => write!(f, "Invalid radix: {}", r),
            InvalidFractionalDigits(s) => write!(f, "Invalid fractional digits: {}", s),
            UnsupportedRadix(r)        => write!(f, "Unsupported radix: {}", r),
            UnsupportedRadixPrefix(body, found) => write!(
                f,
                "Unsupported radix prefix. Must be '0{0}'. Found '{1}{0}'",
                body,
                found
            ),
            InvalidRadix(num, r, e) => write!(
                f,
                "'{}' can't be parsed with radix '{}': {}",
                num, r, e
            ),
        }
    }
}

impl From<ParseNumberError> for ParseErrorKind {
    fn from(p: ParseNumberError) -> ParseErrorKind {
        ParseErrorKind::BadNumber(p)
    }
}

#[derive(Debug, PartialEq)]
pub struct ParseError {
    kind: ParseErrorKind,
    /// A snip of the code that caused this error.
    snip: String,
    line: u32,
    col:  u32,
    file: FileRef
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(f, "{}:{}:{} {}",
            self.file.s(),
            self.line,
            self.col,
            self.kind)?;

        writeln!(f, "at code:")?;
        for (i, line) in self.snip.split('\n').enumerate() {
            writeln!(f, "{:<4}| {}", self.line as usize + i, line)?
        }

        Ok(())
    }
}

#[derive(Debug, Clone, Copy)]
pub struct StrPart<'a> {
    slice: &'a [char],
}

impl<'a, 'b> PartialEq<&'a str> for StrPart<'a> {
    fn eq(&self, other: &&'a str) -> bool {
        if other.len() != self.slice.len() { return false; }

        let mut sc = other.chars();
        for c in self.slice.iter() {
            if let Some(oc) = sc.next() {
                if *c != oc { return false; }
            } else {
                return false;
            }
        }

        true
    }
}

#[allow(clippy::inherent_to_string)]
impl<'a> StrPart<'a> {
    pub fn to_string(&self) -> String { self.slice.iter().collect() }
    pub fn is_empty(&self) -> bool { self.slice.is_empty() }
    pub fn len(&self) -> usize { self.slice.len() }
    pub fn at(&self, i: usize) -> char { self.slice[i] }
}

#[allow(dead_code)]
impl State {
    pub fn err<E: Into<ParseErrorKind>>(&self, kind: E) -> ParseError {
        ParseError {
            kind: kind.into(),
            snip: self.rest().to_string(),
            line: self.line_no,
            col:  self.col_no,
            file: self.file.clone(),
        }
    }

    /// Creates a `SynPos` annotated with the current parse head position.
    pub fn syn_pos(&self, s: Syntax) -> SynPos {
        SynPos::new(s, self.line_no, self.col_no, self.file.clone())
    }

    /// Creates a `VVal::Syn` annotated with the current parse head position.
    pub fn syn_raw(&self, s: Syntax) -> VVal {
        VVal::Syn(self.syn_pos(s))
    }

    /// Creates an syntactic AST node.
    pub fn syn(&self, s: Syntax) -> VVal {
        let vec = VVal::vec();
        vec.push(self.syn_raw(s));
        vec
    }

    /// Returns the next character under the parse head.
    /// Returns `None` when the parse head is at EOF.
    pub fn peek(&self) -> Option<char> {
        if self.at_end() { None } else { Some(self.input[self.ch_ptr]) }
    }

    /// Returns if the end of the input was reached.
    #[inline]
    pub fn at_end(&self) -> bool { self.ch_ptr >= self.input.len() }

    /// Returns the remaining count of characters in the buffer.
    pub fn rest_len(&self) -> usize { self.input.len() - self.ch_ptr }

    /// Generates a StrPart slice:
    fn spart(&self, a: usize, b: usize) -> StrPart {
        StrPart { slice: &self.input[a..b] }
    }

    /// Generates a StrPart slice:
    fn spart_ptr(&self, offs: usize) -> StrPart {
        StrPart { slice: &self.input[self.ch_ptr..self.ch_ptr + offs] }
    }

    /// Returns the next 2 characters or `None` if EOF.
    pub fn peek2(&self) -> Option<StrPart> {
        if self.rest_len() > 1 {
            Some(self.spart_ptr(2))
        } else {
            None
        }
    }

    /// Returns the next 3 characters or `None` if EOF.
    pub fn peek3(&self) -> Option<StrPart> {
        if self.rest_len() > 2 {
            Some(self.spart_ptr(3))
        } else {
            None
        }
    }

    /// Returns the next 4 characters or `None` if EOF.
    pub fn peek4(&self) -> Option<StrPart> {
        if self.rest_len() > 3 {
            Some(self.spart_ptr(4))
        } else {
            None
        }
    }

    /// Tries to peek for an WLambda operator followed
    /// by the given look-ahead string.
    pub fn peek_op_ws_la(&self, la: &str) -> Option<StrPart> {
        let op = self.peek_op();
        if let Some(op) = op {
            if self.rest_len() >= (op.len() + la.len()) {
                let mut inp_ws_offs = 0;
                for (i, c) in la.chars().enumerate() {
                    let mut inp = self.input[self.ch_ptr + op.len() + i];
                    while inp.is_whitespace() {
                        inp_ws_offs += 1;
                        inp = self.input[self.ch_ptr + op.len() + inp_ws_offs + i];
                    }
                    if c != inp {
                        return None;
                    }
                }
                return Some(op);
            } else {
                return None;
            }
        }

        op
    }

    /// Tries to peek for an WLambda operator. Returns `None`
    /// either at EOF or if no operator could be found.
    pub fn peek_op(&self) -> Option<StrPart> {
        if self.at_end() { return None; }
        let ch = self.input[self.ch_ptr];
        match ch {
            '+' | '-'
                => {
                    if let Some(s) = self.peek2() {
                        if self.rest_len() > 1
                           && self.input[self.ch_ptr + 1].is_digit(10)
                        {
                            return None;
                        } else if s == "+>" {
                            return Some(s);
                        }
                    }
                    Some(self.spart_ptr(1))
                },
            '*' | '/' | '%' | '^'
                => {
                    if let Some(s) = self.peek3() {
                        if    s == "/$e"
                           || s == "/$n"
                           || s == "/$o" { return Some(s); }
                    }
                    if let Some(s) = self.peek2() {
                        if    s == "%>"
                           || s == "//"
                           || s == "/?"
                        {
                            Some(s)
                        } else {
                            Some(self.spart_ptr(1))
                        }
                    } else {
                        Some(self.spart_ptr(1))
                    }
                },
            '<' | '>' | '!' | '=' | '&' => {
                if let Some(s) = self.peek4() {
                    if s == "&and" { return Some(s); }
                }
                if let Some(s) = self.peek3() {
                    if    s == "&or"
                       || s == "&@>"
                       || s == "<@&"
                       { return Some(s); }
                }
                if let Some(s) = self.peek2() {
                    if   s == "<="
                      || s == ">="
                      || s == "!="
                      || s == "=="
                      || s == "<<"
                      || s == ">>"
                      || s == "&|"
                      || s == "&^"
                      || s == "=>"
                      || s == "<&"
                      || s == "&>"
                      || s == "<+"
                      || s == "<%"
                    { return Some(s); }
                }

                if ch != '=' && ch != '!' {
                    Some(self.spart_ptr(1))
                } else {
                    None
                }
            },
            _ => { None }
        }
    }

    /// Returns the rest of the code after the parse head,
    /// including the current character under the parse head.
    pub fn rest(&self) -> StrPart {
        let len = if self.rest_len() > 50 { 50 } else { self.rest_len() };
        self.spart_ptr(len)
    }

    /// Consumes characters while `pred` returns a true value.
    /// Returns `true` if it matched and consumed at least once.
    pub fn consume_while<F>(&mut self, pred: F) -> bool
        where F: Fn(char) -> bool {

        let mut did_match_once = false;
        while let Some(c) = self.peek() {
            if pred(c) { self.consume(); did_match_once = true; }
            else { break; }
        }
        did_match_once
    }

    /// Consumes the `expected_char` and possibly following
    /// white space and comments following it. Returns true if
    /// `expected_char` was found.
    pub fn consume_if_eq_wsc(&mut self, expected_char: char) -> bool {
        let res = self.consume_if_eq(expected_char);
        self.skip_ws_and_comments();
        res
    }

    /// Consumes the `expected_char` and possibly following
    /// white space following it. Returns true if
    /// `expected_char` was found.
    pub fn consume_if_eq_ws(&mut self, expected_char: char) -> bool {
        let res = self.consume_if_eq(expected_char);
        self.skip_ws();
        res
    }

    pub fn consume_if_eq(&mut self, expected_char: char) -> bool {
        if let Some(c) = self.peek() {
            if c == expected_char {
                self.consume();
                return true;
            }
        }
        false
    }

    pub fn take_while_wsc<F>(&mut self, pred: F) -> StrPart
        where F: Fn(char) -> bool {
        let start = self.remember();
        self.take_while(pred);
        let end = self.remember();
        self.skip_ws_and_comments();
        self.spart(start, end)
    }

    pub fn take_while<F>(&mut self, pred: F) -> StrPart
        where F: Fn(char) -> bool {

        let start = self.ch_ptr;
        while let Some(c) = self.peek() {
            if !pred(c) { break; }
            self.consume();
        }
        self.spart(start, self.ch_ptr)
    }

    pub fn indent_pos(&self) -> IndentPos {
        IndentPos {
            line_no:     self.line_no,
            indent:      self.indent,
            line_indent: self.line_indent,
        }
    }

    pub fn last_token_char(&self) -> char {
        self.last_tok_char
    }

    pub fn find_char(&self, c: char) -> Option<usize> {
        let len = self.input.len();
        for i in self.ch_ptr..len {
            if self.input[i] == c {
                return Some(i);
            }
        }

        None
    }

    pub fn find_char_not_of(&self, c: char, not_of: &str) -> Option<usize> {
        let len = self.input.len();
        for i in self.ch_ptr..len {
            for nc in not_of.chars() {
                if self.input[i] == nc {
                    return None;
                }
            }
            if self.input[i] == c {
                return Some(i);
            }
        }

        None
    }

    pub fn consume_lookahead(&mut self, s: &str) -> bool {
        if self.lookahead(s) {
            self.ch_ptr += s.len();
            return true;
        }
        false
    }

    pub fn lookahead_one_of(&self, s: &str) -> bool {
        if self.at_end() { return false; }

        let pc = self.peek().unwrap();
        for c in s.chars() {
            if pc == c { return true; }
        }
        false
    }

    pub fn lookahead(&mut self, s: &str) -> bool {
        if self.rest_len() < s.len() {
            return false;
        }

        for (i, c) in s.chars().enumerate() {
            if self.input[self.ch_ptr + i] != c {
                return false;
            }
        }

        true
    }

    pub fn consume_wsc_n(&mut self, n: usize) {
        for _i in 0..n {
            self.consume();
        }
        self.skip_ws_and_comments();
    }

    pub fn consume_wsc(&mut self) {
        self.consume();
        self.skip_ws_and_comments();
    }

    pub fn consume_ws(&mut self) {
        self.consume();
        self.skip_ws();
    }

    pub fn consume(&mut self) {
        if self.at_end() { return }

        let c = self.peek().unwrap();
        self.col_no = self.col_no.wrapping_add(1);
        if c == '\n' {
            self.line_no += 1;
            self.indent      = Some(0);
            self.line_indent = 0;
            self.col_no      = 1;
        } else if c.is_whitespace() {
            if let Some(i) = self.indent {
                self.line_indent += 1;
                self.indent = Some(i + 1);
            }
        } else {
            self.indent = None;
            self.last_tok_char = c;
        }

        self.ch_ptr += 1;
    }

    pub fn skip_ws(&mut self) {
        self.consume_while(char::is_whitespace);
    }

    pub fn skip_ws_and_comments(&mut self) {
        self.skip_ws();
        while let Some(c) = self.peek() {
            if c == '#' {
                self.consume_while(|c| c != '\n');
                if !self.consume_if_eq('\n') {
                    return;
                }
                self.skip_ws();
            } else {
                break;
            }
        }
    }

    /// The constructor for the `parser::State`.
    ///
    /// If you need to have a parser state for a syntax that
    /// with different white space and comment rules as WLambda
    /// please use [State::new_verbatim].
    ///
    /// ```rust
    /// use wlambda::parser::State;
    ///
    /// let code    = "{ 123 }";
    /// let mut ps  = State::new(code, "filenamehere");
    /// // ...
    /// wlambda::parser::parse_block(&mut ps, true, true, true);
    /// // ...
    /// ```
    pub fn new(code: &str, filename: &str) -> State {
        let mut ps = Self::new_verbatim(code, filename);
        ps.skip_ws_and_comments();
        ps
    }

    /// A constructor for the `parser::State` that does not
    /// imply white space and comment rules of the WLambda syntax.
    ///
    /// ```rust
    /// use wlambda::parser::State;
    ///
    /// let code    = "  { 123 }";
    /// let mut ps  = State::new_verbatim(code, "filenamehere");
    /// // ...
    /// assert_eq!(ps.peek().unwrap(), ' ');
    /// ```
    pub fn new_verbatim(code: &str, filename: &str) -> State {
        State {
            input:          code.chars().collect(),
            ch_ptr:         0,
            line_no:        1,
            col_no:         1,
            indent:         Some(0),
            line_indent:    0,
            last_tok_char:  ' ',
            file:           FileRef::new(filename),
            ident_mode:     IdentMode::IdentSelector,
        }
    }

    pub fn is_pattern_ident_mode(&self) -> bool {
        self.ident_mode == IdentMode::IdentPattern
    }

    pub fn set_pattern_ident_mode(&mut self) {
        self.ident_mode = IdentMode::IdentPattern;
    }

    pub fn expect_some<T>(&self, o: Option<T>) -> Result<T, ParseError> {
        match o {
            None => Err(self.err(ParseErrorKind::EOF("Unexpected EOF"))),
            Some(r) => Ok(r)
        }
    }

    pub fn remember(&mut self) -> usize {
        self.ch_ptr
    }

    pub fn collect(&mut self, start: usize, end: usize) -> StrPart {
        self.spart(start, end)
    }
}