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
use cas_error::Error;
use crate::{
    parser::{
        ast::{
            expr::{Atom, Expr, Primary},
            helper::Surrounded,
            index::Index,
            literal::{Literal, LitSym},
        },
        error::{
            CompoundAssignmentInHeader,
            DefaultArgumentNotLast,
            ExpectedExpr,
            InvalidAssignmentLhs,
            InvalidCompoundAssignmentLhs,
        },
        fmt::Latex,
        garbage::Garbage,
        token::{op::AssignOp, Comma, OpenParen},
        Parse,
        Parser,
        ParseResult,
    },
    tokenizer::TokenKind,
};
use std::{fmt, ops::Range};

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

/// A parameter of a function declaration, such as `x` or `y = 1` in the declaration `f(x, y = 1) =
/// x^y`.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Param {
    /// A parameter with no default value, such as `x` in `f(x) = x^2`.
    Symbol(LitSym),

    /// A parameter with a default value, such as `y = 1` in `f(x, y = 1) = x^y`.
    Default(LitSym, Expr),
}

impl Param {
    /// Returns the span of the parameter.
    pub fn span(&self) -> Range<usize> {
        match self {
            Param::Symbol(symbol) => symbol.span.clone(),
            Param::Default(symbol, default) => symbol.span.start..default.span().end,
        }
    }

    /// Returns the symbol of the parameter.
    pub fn symbol(&self) -> &LitSym {
        match self {
            Param::Symbol(symbol) => symbol,
            Param::Default(symbol, _) => symbol,
        }
    }

    /// Returns true if the parameter has a default value.
    pub fn has_default(&self) -> bool {
        matches!(self, Param::Default(_, _))
    }
}

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

        if let Ok(assign) = input.try_parse::<AssignOp>().forward_errors(recoverable_errors) {
            if assign.is_compound() {
                recoverable_errors.push(Error::new(
                    vec![assign.span.clone()],
                    CompoundAssignmentInHeader,
                ));
            }
            let default = input.try_parse().forward_errors(recoverable_errors)?;
            Ok(Param::Default(symbol, default))
        } else {
            Ok(Param::Symbol(symbol))
        }
    }
}

impl std::fmt::Display for Param {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Param::Symbol(symbol) => write!(f, "{}", symbol),
            Param::Default(symbol, default) => write!(f, "{} = {}", symbol, default),
        }
    }
}

impl Latex for Param {
    fn fmt_latex(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Param::Symbol(symbol) => symbol.fmt_latex(f),
            Param::Default(symbol, default) => write!(f, "{} = {}", symbol.as_display(), default.as_display()),
        }
    }
}

/// A function header, **not including the body**. Functions can have multiple parameters with
/// optional default values, like in `f(x, y = 1)`. When a function with this header is called, the
/// default values are used (i.e. `y = 1`), unless the caller provides their own values (`f(2,
/// 3)`).
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct FuncHeader {
    /// The name of the function.
    pub name: LitSym,

    /// The parameters of the function.
    pub params: Vec<Param>,

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

impl FuncHeader {
    /// Returns the span of the function header.
    pub fn span(&self) -> Range<usize> {
        self.span.clone()
    }

    /// Attempts to parse a [`FuncHeader`], where the function name has already been parsed.
    fn parse_or_lower(
        input: &mut Parser,
        recoverable_errors: &mut Vec<Error>,
        name: LitSym,
    ) -> Result<Self, Vec<Error>> {
        /// Helper duplicate of the `Delimited` helper struct with additional state to ensure
        /// default parameters in the correct position.
        struct FuncHeaderInner {
            values: Vec<Param>,
        }

        impl<'source> Parse<'source> for FuncHeaderInner {
            fn std_parse(
                input: &mut Parser<'source>,
                recoverable_errors: &mut Vec<Error>
            ) -> Result<Self, Vec<Error>> {
                let mut bad_default_position = false;
                let mut default_params = Vec::new();
                let mut values = Vec::new();

                loop {
                    let Ok(value) = input.try_parse().forward_errors(recoverable_errors) else {
                        break;
                    };

                    // default parameters must be at the end of the list, i.e., no required
                    // parameters should come after
                    if !default_params.is_empty() && !bad_default_position {
                        if let Param::Symbol(_) = value {
                            bad_default_position = true;
                        }
                    }

                    if let Param::Default(_, _) = value {
                        default_params.push(value.span());
                    }

                    values.push(value);

                    if input.try_parse::<Comma>().forward_errors(recoverable_errors).is_err() {
                        break;
                    }
                }

                if bad_default_position {
                    recoverable_errors.push(Error::new(
                        default_params,
                        DefaultArgumentNotLast,
                    ));
                }

                Ok(Self { values })
            }
        }

        let surrounded = input.try_parse::<Surrounded<OpenParen, FuncHeaderInner>>()
            .forward_errors(recoverable_errors)?;

        let span = name.span.start..surrounded.close.span.end;
        Ok(Self { name, params: surrounded.value.values, span })
    }
}

impl std::fmt::Display for FuncHeader {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}(", self.name)?;
        if let Some((last, rest)) = self.params.split_last() {
            for param in rest {
                write!(f, "{}, ", param)?;
            }
            write!(f, "{}", last)?;
        }
        write!(f, ")")
    }
}

impl Latex for FuncHeader {
    fn fmt_latex(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "\\mathrm{{ {} }} \\left(", self.name.as_display())?;
        if let Some((last, rest)) = self.params.split_last() {
            for param in rest {
                param.fmt_latex(f)?;
                write!(f, ", ")?;
            }
            last.fmt_latex(f)?;
        }
        write!(f, "\\right)")
    }
}

/// An assignment target, such as `x`, `list[0]`, or `f(x)`.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum AssignTarget {
    /// A symbol, such as `x`.
    Symbol(LitSym),

    /// A list index, such as `list[0]`.
    Index(Index),

    /// A function header, such as `f(x)`.
    Func(FuncHeader),
}

impl AssignTarget {
    /// Returns the span of the assignment target.
    pub fn span(&self) -> Range<usize> {
        match self {
            AssignTarget::Symbol(symbol) => symbol.span.clone(),
            AssignTarget::Index(index) => index.span(),
            AssignTarget::Func(func) => func.span(),
        }
    }

    /// Returns true if the assignment target is a function.
    pub fn is_func(&self) -> bool {
        matches!(self, AssignTarget::Func(_))
    }

    /// Tries to convert a general [`Expr`] into an [`AssignTarget`]. This is used when parsing
    /// assignment expressions, such as `x = 1` or `f(x) = x^2`.
    pub fn try_from_with_op(expr: Expr, op: &AssignOp) -> ParseResult<Self> {
        let op_span = op.span.clone();
        match expr {
            Expr::Literal(Literal::Symbol(symbol)) => ParseResult::Ok(AssignTarget::Symbol(symbol)),
            Expr::Index(index) => ParseResult::Ok(AssignTarget::Index(index)),
            Expr::Call(call) => {
                let spans = vec![call.span.clone(), op_span.clone()];
                let error = if op.is_compound() {
                    Error::new(spans, InvalidCompoundAssignmentLhs)
                } else {
                    Error::new(spans, InvalidAssignmentLhs { is_call: true })
                };

                ParseResult::Recoverable(Garbage::garbage(), vec![error])
            },
            expr => {
                let spans = vec![expr.span(), op_span.clone()];
                let error = if op.is_compound() {
                    Error::new(spans, InvalidCompoundAssignmentLhs)
                } else {
                    Error::new(spans, InvalidAssignmentLhs { is_call: false })
                };

                ParseResult::Recoverable(
                    Garbage::garbage(),
                    vec![error]
                )
            },
        }
    }
}

impl From<LitSym> for AssignTarget {
    fn from(symbol: LitSym) -> Self {
        AssignTarget::Symbol(symbol)
    }
}

impl From<Index> for AssignTarget {
    fn from(index: Index) -> Self {
        AssignTarget::Index(index)
    }
}

impl From<FuncHeader> for AssignTarget {
    fn from(func: FuncHeader) -> Self {
        AssignTarget::Func(func)
    }
}

impl<'source> Parse<'source> for AssignTarget {
    fn std_parse(
        input: &mut Parser<'source>,
        recoverable_errors: &mut Vec<Error>
    ) -> Result<Self, Vec<Error>> {
        // this uses a similar approach to Primary::parse, where we try to parse an Atom first
        // and then check if it's followed by an open parenthesis or open square bracket
        // to determine if the target is a function or index
        let atom = input.try_parse::<Atom>().forward_errors(recoverable_errors)?;

        let mut fork = input.clone();
        match fork.next_token() {
            Ok(next) if next.kind == TokenKind::OpenParen => {
                if let Atom::Literal(Literal::Symbol(symbol)) = atom {
                    Ok(FuncHeader::parse_or_lower(input, recoverable_errors, symbol)
                        .map(Into::into)?)
                } else {
                    Err(vec![input.error(ExpectedExpr { expected: "a symbol" })])
                }
            },
            Ok(next) if next.kind == TokenKind::OpenSquare => {
                match Index::parse_or_lower(input, recoverable_errors, atom.into()) {
                    (new_primary, true) => match new_primary {
                        Primary::Index(index) => Ok(AssignTarget::Index(index)),
                        _ => unreachable!(),
                    },
                    (unchanged_primary, false) => match unchanged_primary {
                        Primary::Literal(Literal::Symbol(symbol)) => Ok(AssignTarget::Symbol(symbol)),
                        _ => unreachable!(),
                    },
                }
            },
            _ => if let Atom::Literal(Literal::Symbol(symbol)) = atom {
                Ok(AssignTarget::Symbol(symbol))
            } else {
                Err(vec![input.error(ExpectedExpr { expected: "a symbol" })])
            },
        }
    }
}

impl std::fmt::Display for AssignTarget {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            AssignTarget::Symbol(symbol) => write!(f, "{}", symbol),
            AssignTarget::Index(index) => write!(f, "{}", index),
            AssignTarget::Func(func) => write!(f, "{}", func),
        }
    }
}

impl Latex for AssignTarget {
    fn fmt_latex(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            AssignTarget::Symbol(symbol) => symbol.fmt_latex(f),
            AssignTarget::Index(index) => index.fmt_latex(f),
            AssignTarget::Func(func) => func.fmt_latex(f),
        }
    }
}

/// An assignment of a variable or function, such as `x = 1` or `f(x) = x^2`.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Assign {
    /// The target to assign to.
    pub target: AssignTarget,

    /// The operator used to assign to the target.
    pub op: AssignOp,

    /// The expression to assign to the target.
    pub value: Box<Expr>,

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

impl Assign {
    /// Returns the span of the assignment expression.
    pub fn span(&self) -> Range<usize> {
        self.span.clone()
    }
}

impl<'source> Parse<'source> for Assign {
    fn std_parse(
        input: &mut Parser<'source>,
        recoverable_errors: &mut Vec<Error>
    ) -> Result<Self, Vec<Error>> {
        let target = input.try_parse().forward_errors(recoverable_errors)?;
        let op = input.try_parse::<AssignOp>().forward_errors(recoverable_errors)?;

        let value = if matches!(target, AssignTarget::Func(_)) {
            if op.is_compound() {
                // can't compound assignment to function, for example:
                //
                // f(x) += 5
                //      ^^
                recoverable_errors.push(Error::new(
                    vec![target.span(), op.span.clone()],
                    InvalidCompoundAssignmentLhs,
                ));
            }

            input.try_parse_with_state::<_, Expr>(|state| {
                // loop control not allowed inside a function definition inside a loop, for example:
                //
                // loop {
                //     f(x) = break x <-- illegal break
                //     f(5)
                // }
                state.allow_loop_control = false;
                state.allow_return = true;
            }).forward_errors(recoverable_errors)?
        } else {
            input.try_parse::<Expr>().forward_errors(recoverable_errors)?
        };

        let span = target.span().start..value.span().end;
        Ok(Self {
            target,
            op,
            value: Box::new(value),
            span,
        })
    }
}

impl std::fmt::Display for Assign {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{} {} {}",
            self.target,
            self.op,
            self.value,
        )
    }
}

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