cas-parser 0.2.0

Parser for the CalcScript language
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
use cas_error::Error;
use crate::{
    parser::{
        ast::{expr::Expr, helper::{SquareDelimited, Surrounded}},
        error::{EmptyRadixLiteral, InvalidRadixBase, InvalidRadixDigit, UnexpectedToken},
        fmt::Latex,
        token::{
            Boolean,
            CloseParen,
            Name,
            Int,
            OpenParen,
            OpenSquare,
            Semicolon,
            Quote,
        },
        Parse,
        Parser,
        ParseResult,
    },
    tokenizer::TokenKind,
    return_if_ok,
};
use std::{collections::HashSet, fmt, ops::Range};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// An integer literal, representing as a [`String`].
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct LitInt {
    /// The value of the integer literal as a string.
    pub value: String,

    /// The region of the source code that this literal was parsed from.
    pub span: Range<usize>,
}

impl std::fmt::Display for LitInt {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.value)
    }
}

impl Latex for LitInt {
    fn fmt_latex(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.value)
    }
}

/// A floating-point literal, represented as a [`String`].
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct LitFloat {
    /// The value of the floating-point literal as a string.
    pub value: String,

    /// The region of the source code that this literal was parsed from.
    pub span: Range<usize>,
}

impl std::fmt::Display for LitFloat {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.value)
    }
}

impl Latex for LitFloat {
    fn fmt_latex(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.value)
    }
}

/// Parse either an integer or a floating-point number.
fn parse_ascii_number(input: &mut Parser) -> Result<Literal, Vec<Error>> {
    struct Num {
        value: String,
        span: Range<usize>,
        has_digits: bool,
        has_decimal: bool,
    }

    let mut result: Option<Num> = None;
    input.advance_past_whitespace();

    while let Ok(token) = input.next_token_raw() {
        match token.kind {
            TokenKind::Int => {
                let mut has_decimal = false;
                result = result.map_or_else(|| {
                    Some(Num {
                        value: token.lexeme.to_owned(),
                        span: token.span.clone(),
                        has_digits: true,
                        has_decimal: false,
                    })
                }, |mut num| {
                    num.value.push_str(token.lexeme);
                    num.span.end = token.span.end;
                    num.has_digits = true;
                    has_decimal = num.has_decimal;
                    Some(num)
                });

                if has_decimal {
                    break;
                }
            },
            TokenKind::Dot => {
                result = result.map_or_else(|| {
                    Some(Num {
                        value: ".".to_owned(),
                        span: token.span.clone(),
                        has_digits: false,
                        has_decimal: true,
                    })
                }, |mut num| {
                    num.value.push('.');
                    num.span.end = token.span.end;
                    num.has_decimal = true;
                    Some(num)
                });
            },
            _ => {
                input.prev();
                break;
            },
        }
    }

    let num = result.ok_or_else(|| {
        // clone to emulate peeking
        let mut input_ahead = input.clone();
        match input_ahead.next_token() {
            Ok(token) => vec![Error::new(vec![token.span], UnexpectedToken {
                expected: &[TokenKind::Int],
                found: token.kind,
            })],
            Err(e) => vec![e],
        }
    })?;

    if !num.has_digits {
        // could have only encountered a single dot for `num` to be `Some`, yet have no digits
        return Err(vec![Error::new(vec![num.span.clone()], UnexpectedToken {
            expected: &[TokenKind::Int],
            found: TokenKind::Dot,
        })]);
    }

    if num.has_decimal {
        Ok(Literal::Float(LitFloat {
            value: num.value,
            span: num.span,
        }))
    } else {
        Ok(Literal::Integer(LitInt {
            value: num.value,
            span: num.span,
        }))
    }
}

/// The digits in base 64, in order of increasing value.
pub const DIGITS: [char; 64] = [
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
    '+', '/',
];

/// Helper struct to parse the digits used in various bases.
#[derive(Debug, Clone, PartialEq, Eq)]
struct RadixWord {
    /// The parsed digits.
    pub value: String,

    /// The region of the source code that this literal was parsed from.
    pub span: Range<usize>,
}

impl RadixWord {
    fn parse(input: &mut Parser) -> Self {
        let mut value = String::new();
        let mut span = 0..0;
        while let Ok(token) = input.next_token_raw() {
            match token.kind {
                TokenKind::Add
                    | TokenKind::Name
                    | TokenKind::Int
                    | TokenKind::Div => value.push_str(token.lexeme),
                _ => {
                    input.prev();
                    break;
                },
            }

            if span.start == 0 {
                span.start = token.span.start;
            }
            span.end = token.span.end;
        }

        Self {
            value,
            span,
        }
    }
}

/// Helper function to ensure the given string represents a valid base for radix notation.
fn validate_radix_base(num: &Int) -> ParseResult<u8> {
    match num.lexeme.parse() {
        Ok(base) if (2..=64).contains(&base) => ParseResult::Ok(base),
        Ok(base) if base < 2 => ParseResult::Recoverable(
            64, // use base 64 to limit invalid radix digit errors
            vec![Error::new(vec![num.span.clone()], InvalidRadixBase { too_large: false })],
        ),
        _ => ParseResult::Recoverable(
            64,
            vec![Error::new(vec![num.span.clone()], InvalidRadixBase { too_large: true })],
        ),
    }
}

/// A number written in radix notation. Radix notation allows users to express integers in a base
/// other than base 10.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct LitRadix {
    /// The radix of the literal. This value must be between 2 and 64, inclusive.
    pub base: u8,

    /// The number, expressed in the given radix.
    pub value: String,

    /// The region of the source code that this literal was parsed from.
    pub span: Range<usize>,
}

impl<'source> Parse<'source> for LitRadix {
    fn std_parse(
        input: &mut Parser<'source>,
        recoverable_errors: &mut Vec<Error>
    ) -> Result<Self, Vec<Error>> {
        let num = input.try_parse().forward_errors(recoverable_errors)?;
        let quote = input.try_parse::<Quote>().forward_errors(recoverable_errors)?;

        let base = validate_radix_base(&num).forward_errors(recoverable_errors)?;
        let word = RadixWord::parse(input);
        if word.value.is_empty() {
            recoverable_errors.push(Error::new(vec![quote.span], EmptyRadixLiteral {
                radix: base,
                allowed: &DIGITS[..base as usize],
            }));
        }

        // ensure that the number is valid for this radix
        let allowed_digits = &DIGITS[..base as usize];
        let mut bad_digits = HashSet::new();
        let mut bad_digit_spans: Vec<Range<usize>> = Vec::new();
        for (i, c) in word.value.chars().enumerate() {
            // if we find a digit that isn't allowed, that is fatal
            // but continue to find all the bad digits so we can report them all at once
            if !allowed_digits.contains(&c) {
                let char_start = word.span.start + i;
                if let Some(last_span) = bad_digit_spans.last_mut() {
                    // merge adjacent spans
                    if last_span.end == char_start {
                        last_span.end += 1;
                    } else {
                        bad_digit_spans.push(char_start..char_start + 1);
                    }
                } else {
                    bad_digit_spans.push(char_start..char_start + 1);
                }

                bad_digits.insert(c);
                continue;
            }
        }

        if !bad_digit_spans.is_empty() {
            recoverable_errors.push(Error::new(bad_digit_spans, InvalidRadixDigit {
                radix: base,
                allowed: allowed_digits,
                digits: bad_digits,
                last_op_digit: {
                    if let Some(ch) = word.value.chars().last() {
                        ['+', '/'].into_iter()
                            .find(|&op| op == ch)
                            .map(|op| (op, word.span.end - 1..word.span.end))
                    } else {
                        None
                    }
                },
            }));
        }

        Ok(Self {
            base,
            value: word.value,
            span: num.span.start..word.span.end,
        })
    }
}

impl std::fmt::Display for LitRadix {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}'{}", self.base, self.value)
    }
}

impl Latex for LitRadix {
    fn fmt_latex(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}'{}", self.base, self.value)
    }
}

/// A boolean literal, either `true` or `false`.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct LitBool {
    /// The value of the boolean literal.
    pub value: bool,

    /// The region of the source code that this literal was parsed from.
    pub span: Range<usize>,
}

impl<'source> Parse<'source> for LitBool {
    fn std_parse(
        input: &mut Parser<'source>,
        recoverable_errors: &mut Vec<Error>
    ) -> Result<Self, Vec<Error>> {
        input.try_parse::<Boolean>()
            .map(|boolean| Self {
                value: match boolean.lexeme {
                    "true" => true,
                    "false" => false,
                    _ => unreachable!(),
                },
                span: boolean.span,
            })
            .forward_errors(recoverable_errors)
    }
}

impl std::fmt::Display for LitBool {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.value)
    }
}

impl Latex for LitBool {
    fn fmt_latex(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.value)
    }
}

/// A symbol / identifier literal. Symbols are used to represent variables and functions.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct LitSym {
    /// The name of the symbol.
    pub name: String,

    /// The region of the source code that this literal was parsed from.
    pub span: Range<usize>,
}

impl<'source> Parse<'source> for LitSym {
    fn std_parse(
        input: &mut Parser<'source>,
        recoverable_errors: &mut Vec<Error>
    ) -> Result<Self, Vec<Error>> {
        // TODO: it would be nice if we could report an error if the symbol is a keyword
        //
        // for example:
        // break(x) = x
        // ^^^^^ error: `break` is a keyword and cannot be used as a symbol
        //
        // unfortunately this is hard since CalcScript is context-sensitive and we would have to
        // to parse further ahead to determine if this error should be reported
        // maybe we should require a `let` keyword to declare variables?
        input.try_parse::<Name>()
            .map(|name| Self {
                name: name.lexeme.to_owned(),
                span: name.span,
            })
            .forward_errors(recoverable_errors)
    }
}

impl std::fmt::Display for LitSym {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.name)
    }
}

impl Latex for LitSym {
    fn fmt_latex(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.name.as_str() {
            "tau" | "pi" | "phi" | "theta" => write!(f, "\\{} ", self.name),
            _ => write!(f, "{}", self.name),
        }
    }
}

/// The unit type, written as `()`. The unit type is by-default returned by functions that do not
/// return a value.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct LitUnit {
    /// The region of the source code that this literal was parsed from.
    pub span: Range<usize>,
}

impl<'source> Parse<'source> for LitUnit {
    fn std_parse(
        input: &mut Parser<'source>,
        recoverable_errors: &mut Vec<Error>
    ) -> Result<Self, Vec<Error>> {
        let open = input.try_parse::<OpenParen>().forward_errors(recoverable_errors)?;
        let close = input.try_parse::<CloseParen>().forward_errors(recoverable_errors)?;
        Ok(Self {
            span: open.span.start..close.span.end,
        })
    }
}

impl std::fmt::Display for LitUnit {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "()")
    }
}

impl Latex for LitUnit {
    fn fmt_latex(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "()")
    }
}

/// The list type, consisting of a list of expressions surrounded by square brackets and delimited by
/// commas: `[expr1, expr2, ...]`.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct LitList {
    /// The list of expressions.
    pub values: Vec<Expr>,

    /// The region of the source code that this literal was parsed from.
    pub span: Range<usize>,
}

impl<'source> Parse<'source> for LitList {
    fn std_parse(
        input: &mut Parser<'source>,
        recoverable_errors: &mut Vec<Error>
    ) -> Result<Self, Vec<Error>> {
        let surrounded = input.try_parse::<SquareDelimited<_>>().forward_errors(recoverable_errors)?;

        Ok(Self {
            values: surrounded.value.values,
            span: surrounded.open.span.start..surrounded.close.span.end,
        })
    }
}

impl std::fmt::Display for LitList {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "[")?;
        for (i, value) in self.values.iter().enumerate() {
            if i > 0 {
                write!(f, ", ")?;
            }
            write!(f, "{}", value)?;
        }
        write!(f, "]")
    }
}

impl Latex for LitList {
    fn fmt_latex(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "[")?;
        for (i, value) in self.values.iter().enumerate() {
            if i > 0 {
                write!(f, ", ")?;
            }
            value.fmt_latex(f)?;
        }
        write!(f, "]")
    }
}

/// The list type, formed by repeating the given expression `n` times: `[expr; n]`.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct LitListRepeat {
    /// The expression to repeat.
    pub value: Box<Expr>,

    /// The number of times to repeat the expression.
    pub count: Box<Expr>,

    /// The region of the source code that this literal was parsed from.
    pub span: Range<usize>,
}

impl<'source> Parse<'source> for LitListRepeat {
    fn std_parse(
        input: &mut Parser<'source>,
        recoverable_errors: &mut Vec<Error>
    ) -> Result<Self, Vec<Error>> {
        /// Inner struct representing the contents of a repeated list so that we can use the
        /// [`Surrounded`] helper with it.
        #[derive(Debug)]
        struct LitListRepeatInner {
            /// The expression to repeat.
            value: Expr,

            /// The number of times to repeat the expression.
            count: Expr,
        }

        impl<'source> Parse<'source> for LitListRepeatInner {
            fn std_parse(
                input: &mut Parser<'source>,
                recoverable_errors: &mut Vec<Error>
            ) -> Result<Self, Vec<Error>> {
                let value = input.try_parse().forward_errors(recoverable_errors)?;
                input.try_parse::<Semicolon>().forward_errors(recoverable_errors)?;
                let count = input.try_parse().forward_errors(recoverable_errors)?;
                Ok(Self { value, count })
            }
        }

        let inner = input.try_parse::<Surrounded<OpenSquare, LitListRepeatInner>>().forward_errors(recoverable_errors)?;

        Ok(Self {
            value: Box::new(inner.value.value),
            count: Box::new(inner.value.count),
            span: inner.open.span.start..inner.close.span.end,
        })
    }
}

impl std::fmt::Display for LitListRepeat {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "[{}; {}]", self.value, self.count)
    }
}

impl Latex for LitListRepeat {
    fn fmt_latex(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "[{}; {}]", self.value, self.count)
    }
}

/// Represents a literal value in CalcScript.
///
/// A literal is any value that can is written directly into the source code. For example, the
/// number `1` is a literal (it is currently the only literal type supported by CalcScript).
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Literal {
    /// An integer literal.
    Integer(LitInt),

    /// A floating-point literal.
    Float(LitFloat),

    /// A number written in radix notation. Radix notation allows users to express integers in a
    /// base other than base 10.
    Radix(LitRadix),

    /// A boolean literal, either `true` or `false`.
    Boolean(LitBool),

    /// A symbol / identifier literal. Symbols are used to represent variables and functions.
    Symbol(LitSym),

    /// The unit type, written as `()`. The unit type is by-default returned by functions that do
    /// not return a value.
    Unit(LitUnit),

    /// The list type, consisting of a list of expressions surrounded by square brackets and
    /// delimited by commas: `[expr1, expr2, ...]`.
    List(LitList),

    /// The list type, formed by repeating the given expression `n` times: `[expr; n]`.
    ListRepeat(LitListRepeat),
}

impl Literal {
    /// Returns the span of the literal.
    pub fn span(&self) -> Range<usize> {
        match self {
            Literal::Integer(int) => int.span.clone(),
            Literal::Float(float) => float.span.clone(),
            Literal::Radix(radix) => radix.span.clone(),
            Literal::Boolean(boolean) => boolean.span.clone(),
            Literal::Symbol(name) => name.span.clone(),
            Literal::Unit(unit) => unit.span.clone(),
            Literal::List(list) => list.span.clone(),
            Literal::ListRepeat(repeat) => repeat.span.clone(),
        }
    }
}

impl<'source> Parse<'source> for Literal {
    fn std_parse(
        input: &mut Parser<'source>,
        recoverable_errors: &mut Vec<Error>
    ) -> Result<Self, Vec<Error>> {
        let _ = return_if_ok!(input.try_parse().map(Literal::Boolean).forward_errors(recoverable_errors));
        let _ = return_if_ok!(input.try_parse().map(Literal::Radix).forward_errors(recoverable_errors));
        let _ = return_if_ok!(parse_ascii_number(input));
        let _ = return_if_ok!(input.try_parse().map(Literal::Symbol).forward_errors(recoverable_errors));
        let _ = return_if_ok!(input.try_parse().map(Literal::Unit).forward_errors(recoverable_errors));
        let _ = return_if_ok!(input.try_parse().map(Literal::List).forward_errors(recoverable_errors));
        input.try_parse().map(Literal::ListRepeat).forward_errors(recoverable_errors)
    }
}

impl std::fmt::Display for Literal {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Literal::Integer(int) => int.fmt(f),
            Literal::Float(float) => float.fmt(f),
            Literal::Radix(radix) => radix.fmt(f),
            Literal::Boolean(boolean) => boolean.fmt(f),
            Literal::Symbol(name) => name.fmt(f),
            Literal::Unit(unit) => unit.fmt(f),
            Literal::List(list) => list.fmt(f),
            Literal::ListRepeat(repeat) => repeat.fmt(f),
        }
    }
}

impl Latex for Literal {
    fn fmt_latex(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Literal::Integer(int) => int.fmt_latex(f),
            Literal::Float(float) => float.fmt_latex(f),
            Literal::Radix(radix) => radix.fmt_latex(f),
            Literal::Boolean(boolean) => boolean.fmt_latex(f),
            Literal::Symbol(name) => name.fmt_latex(f),
            Literal::Unit(unit) => unit.fmt_latex(f),
            Literal::List(list) => list.fmt_latex(f),
            Literal::ListRepeat(repeat) => repeat.fmt_latex(f),
        }
    }
}