formula-lang 0.1.3

Spreadsheet formula language: tokenizer, Pratt parser, AST, evaluator core. Excel-compatible function semantics.
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
//! The Pratt parser — tokens in, [`Expr`] out.
//!
//! Precedence climbs through the classic Excel ladder (loosest first):
//! comparison → `&` concat → `+ -` → `* /` → `^` power → prefix `- +` →
//! postfix `%` → primaries. `^` is right-associative; everything else is
//! left-associative; and because prefix unary sits *above* power in the
//! binding ladder, `-2^2` parses as `(-2)^2` — the Excel quirk, faithfully
//! reproduced.
//!
//! The parser is stack-safe: recursion depth (nested parens, unary chains,
//! right-nested `^`, nested calls) is capped at [`MAX_DEPTH`] with a typed
//! [`FormulaError::RecursionLimit`], fuzz-verified. Left-associative chains
//! (`1+1+1+…`) iterate and grow the AST, not the stack; their depth is
//! caught at evaluation by the same cap.

use crate::ast::{col_number, BinaryOp, CellRef, Expr, UnaryOp, MAX_COL, MAX_ROW};
use crate::error::FormulaError;
use crate::lexer::tokenize;
use crate::token::{Token, TokenKind};
use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec::Vec;

/// Maximum parser/evaluator recursion depth. Excel itself caps nesting at
/// 64; 512 gives ample headroom while every recursion frame stays far below
/// any thread stack.
pub const MAX_DEPTH: usize = 512;

/// Parses formula text into an [`Expr`].
///
/// A single leading `=` (Excel's entry form) is accepted and skipped; both
/// `parse("1+1")` and `parse("=1+1")` work.
///
/// ```
/// use formula_lang::{parse, BinaryOp, Expr};
///
/// let e = parse("1+2*3").unwrap();
/// assert_eq!(e.to_string(), "1+2*3");
/// assert!(matches!(
///     e,
///     Expr::Binary { op: BinaryOp::Add, .. }
/// ));
/// ```
///
/// # Errors
/// [`FormulaError::Tokenize`] (from the lexer),
/// [`FormulaError::Parse`] for grammar violations, and
/// [`FormulaError::RecursionLimit`] when nesting exceeds [`MAX_DEPTH`].
pub fn parse(input: &str) -> Result<Expr, FormulaError> {
    let toks = tokenize(input)?;
    let mut p = Parser {
        toks: &toks,
        pos: 0,
        depth: 0,
    };
    // Optional leading `=` — Excel's formula entry form.
    if p.pos == 0 && matches!(p.peek().kind, TokenKind::Eq) {
        p.pos += 1;
    }
    let e = p.parse_expr(0)?;
    if p.peek().kind != TokenKind::Eof {
        return Err(p.err_here("end of formula"));
    }
    Ok(e)
}

struct Parser<'t, 'i> {
    toks: &'t [Token<'i>],
    pos: usize,
    depth: usize,
}

impl<'i> Parser<'_, 'i> {
    fn peek(&self) -> Token<'i> {
        // `tokenize` always appends `Eof`, so the slice is never empty and
        // the index stays in bounds (self.pos only advances onto Eof).
        *self
            .toks
            .get(self.pos)
            .unwrap_or(self.toks.last().unwrap_or(&Token {
                kind: TokenKind::Eof,
                pos: 0,
            }))
    }

    fn err_here(&self, expected: &'static str) -> FormulaError {
        let tok = self.peek();
        FormulaError::Parse {
            pos: tok.pos,
            expected,
            got: tok.kind.describe(),
        }
    }

    fn enter(&mut self) -> Result<(), FormulaError> {
        self.depth += 1;
        if self.depth > MAX_DEPTH {
            return Err(FormulaError::RecursionLimit);
        }
        Ok(())
    }

    fn leave(&mut self) {
        self.depth -= 1;
    }

    /// `expr := comparison` — the Pratt loop over the infix ladder.
    fn parse_expr(&mut self, min_bp: usize) -> Result<Expr, FormulaError> {
        self.enter()?;
        let mut lhs = self.parse_unary()?;
        while let Some((lbp, rbp, op)) = self.peek_infix() {
            if lbp < min_bp {
                break;
            }
            self.pos += 1; // consume the operator token
            let rhs = self.parse_expr(rbp)?;
            lhs = Expr::Binary {
                op,
                left: Box::new(lhs),
                right: Box::new(rhs),
            };
        }
        self.leave();
        Ok(lhs)
    }

    fn peek_infix(&self) -> Option<(usize, usize, BinaryOp)> {
        let op = match self.peek().kind {
            TokenKind::Plus => BinaryOp::Add,
            TokenKind::Minus => BinaryOp::Sub,
            TokenKind::Star => BinaryOp::Mul,
            TokenKind::Slash => BinaryOp::Div,
            TokenKind::Caret => BinaryOp::Pow,
            TokenKind::Amp => BinaryOp::Concat,
            TokenKind::Eq => BinaryOp::Eq,
            TokenKind::Ne => BinaryOp::Ne,
            TokenKind::Lt => BinaryOp::Lt,
            TokenKind::Gt => BinaryOp::Gt,
            TokenKind::Le => BinaryOp::Le,
            TokenKind::Ge => BinaryOp::Ge,
            _ => return None,
        };
        let (lbp, rbp) = op.binding_power();
        Some((lbp, rbp, op))
    }

    /// `unary := ('-' | '+') unary | postfix` — prefix level, binding
    /// tighter than `^`.
    fn parse_unary(&mut self) -> Result<Expr, FormulaError> {
        self.enter()?;
        let e = match self.peek().kind {
            TokenKind::Minus => {
                self.pos += 1;
                Expr::Unary {
                    op: UnaryOp::Neg,
                    expr: Box::new(self.parse_unary()?),
                }
            }
            TokenKind::Plus => {
                self.pos += 1;
                Expr::Unary {
                    op: UnaryOp::Pos,
                    expr: Box::new(self.parse_unary()?),
                }
            }
            _ => self.parse_postfix()?,
        };
        self.leave();
        Ok(e)
    }

    /// `postfix := primary ('%')*`
    fn parse_postfix(&mut self) -> Result<Expr, FormulaError> {
        let mut e = self.parse_primary()?;
        while self.peek().kind == TokenKind::Percent {
            self.pos += 1;
            e = Expr::Unary {
                op: UnaryOp::Percent,
                expr: Box::new(e),
            };
        }
        Ok(e)
    }

    /// `primary := NUMBER | STRING | BOOLEAN | ERROR | cell_ref | range |
    ///              function_call | '(' expr ')'`
    fn parse_primary(&mut self) -> Result<Expr, FormulaError> {
        let tok = self.peek();
        match tok.kind {
            TokenKind::Number(n) => {
                self.pos += 1;
                Ok(Expr::Number(n))
            }
            TokenKind::Str(raw) => {
                self.pos += 1;
                Ok(Expr::Text(unescape(raw)))
            }
            TokenKind::ErrLit(e) => {
                self.pos += 1;
                Ok(Expr::Error(e))
            }
            TokenKind::Cell { .. } => {
                self.pos += 1;
                let r = self.finish_celllike(tok, None)?;
                self.maybe_range(r)
            }
            TokenKind::Ident(word) => {
                self.pos += 1;
                // Function call: `IDENT '(' args ')'` — checked first, so
                // ref-shaped function names (`LOG10`) win over references.
                if self.peek().kind == TokenKind::LParen {
                    self.pos += 1;
                    let args = self.parse_args()?;
                    return Ok(Expr::Function {
                        name: word.to_ascii_uppercase(),
                        args,
                    });
                }
                if word.eq_ignore_ascii_case("TRUE") {
                    return Ok(Expr::Boolean(true));
                }
                if word.eq_ignore_ascii_case("FALSE") {
                    return Ok(Expr::Boolean(false));
                }
                // Ref-shaped bare word (`A1`, `ab12`, `XFD1048576`)?
                let cr = match self.bare_word_ref(word) {
                    Ok(cr) => cr,
                    Err(expected) => return Err(self.err_here(expected)),
                };
                let r = self.finish_celllike(tok, Some(cr))?;
                self.maybe_range(r)
            }
            TokenKind::LParen => {
                self.pos += 1;
                self.enter()?;
                let e = self.parse_expr(0)?;
                if self.peek().kind != TokenKind::RParen {
                    return Err(self.err_here("')'"));
                }
                self.pos += 1;
                self.leave();
                Ok(e)
            }
            _ => Err(self.err_here("expression")),
        }
    }

    /// Builds a `CellRef` from a token, applying grid limits. `word` is the
    /// already-classified 1-based (col, row) for `Ident` tokens; `Cell`
    /// tokens carry it in the token itself.
    fn finish_celllike(
        &mut self,
        tok: Token<'_>,
        word: Option<(u32, u32)>,
    ) -> Result<CellRef, FormulaError> {
        let (col, row, col_abs, row_abs) = match tok.kind {
            TokenKind::Cell {
                col,
                row,
                col_abs,
                row_abs,
            } => (col, row, col_abs, row_abs),
            TokenKind::Ident(_) => {
                let (col, row) = word.unwrap_or((0, 0));
                (col, row, false, false)
            }
            _ => (0, 0, false, false),
        };
        if col == 0 || col > MAX_COL || row == 0 || row > MAX_ROW {
            return Err(FormulaError::Parse {
                pos: tok.pos,
                expected: "cell reference within A1:XFD1048576",
                got: tok.kind.describe(),
            });
        }
        Ok(CellRef {
            col,
            row,
            col_abs,
            row_abs,
        })
    }

    /// Classifies a bare word as an unanchored (col, row). `Err(expected)`
    /// distinguishes "not ref-shaped at all" (names are not in the grammar)
    /// from "ref-shaped but outside the grid" (both are parse errors, but
    /// the messages differ).
    fn bare_word_ref(&self, word: &str) -> Result<(u32, u32), &'static str> {
        let b = word.as_bytes();
        let letters = b.iter().take_while(|c| c.is_ascii_alphabetic()).count();
        let digits = b
            .get(letters..)
            .map_or(0, |rest| rest.iter().filter(|c| c.is_ascii_digit()).count());
        let ref_shaped =
            letters > 0 && letters <= 3 && digits > 0 && letters + digits == b.len() && digits <= 7;
        if !ref_shaped {
            return Err("cell reference, TRUE/FALSE, or function call");
        }
        let col = col_number(&word[..letters]).ok_or("cell reference within A1:XFD1048576")?;
        let row: u32 = word[letters..]
            .parse()
            .map_err(|_| "cell reference within A1:XFD1048576")?;
        if row > MAX_ROW {
            return Err("cell reference within A1:XFD1048576");
        }
        Ok((col, row))
    }

    /// After a cell reference, consume `':' ref` if present — the range
    /// production. The second corner must be a plain reference.
    fn maybe_range(&mut self, start: CellRef) -> Result<Expr, FormulaError> {
        if self.peek().kind != TokenKind::Colon {
            return Ok(Expr::CellRef {
                col: start.col,
                row: start.row,
                col_abs: start.col_abs,
                row_abs: start.row_abs,
            });
        }
        self.pos += 1;
        let tok = self.peek();
        let end = match tok.kind {
            TokenKind::Cell { .. } => {
                self.pos += 1;
                self.finish_celllike(tok, None)?
            }
            TokenKind::Ident(word) => {
                self.pos += 1;
                let (c, r) = self.bare_word_ref(word).map_err(|_| FormulaError::Parse {
                    pos: tok.pos,
                    expected: "cell reference after ':'",
                    got: tok.kind.describe(),
                })?;
                self.finish_celllike(tok, Some((c, r)))?
            }
            _ => return Err(self.err_here("cell reference after ':'")),
        };
        if self.peek().kind == TokenKind::Colon {
            return Err(self.err_here("end of range (single ':' only)"));
        }
        Ok(Expr::Range { start, end })
    }

    /// `args := [arg (',' arg)*]` with empty slots (`F(,2)`, `F(1,)`)
    /// represented by the omitted-argument marker
    /// ([`is_omitted_arg`]). `F()` parses to zero arguments.
    fn parse_args(&mut self) -> Result<Vec<Expr>, FormulaError> {
        self.enter()?;
        let mut args = Vec::new();
        if self.peek().kind == TokenKind::RParen {
            self.pos += 1;
            self.leave();
            return Ok(args);
        }
        loop {
            // One argument slot: empty (marker) or a full expression.
            if matches!(self.peek().kind, TokenKind::Comma | TokenKind::RParen) {
                args.push(Expr::Error(crate::error::ExcelError::Null));
            } else {
                args.push(self.parse_expr(0)?);
            }
            match self.peek().kind {
                TokenKind::Comma => self.pos += 1, // another slot follows
                TokenKind::RParen => {
                    self.pos += 1;
                    break;
                }
                _ => return Err(self.err_here("',' or ')'")),
            }
        }
        self.leave();
        Ok(args)
    }
}

/// Unescapes a string literal's inner span: `""` → `"`. Allocation is one
/// `String` per literal — the parser path, not the eval path.
fn unescape(raw: &str) -> String {
    if !raw.contains('"') {
        return String::from(raw);
    }
    let mut out = String::with_capacity(raw.len());
    let mut chars = raw.chars();
    while let Some(c) = chars.next() {
        if c == '"' {
            // Doubled quote: consume the partner.
            if chars.next() == Some('"') {
                out.push('"');
            }
        } else {
            out.push(c);
        }
    }
    out
}

#[cfg(test)]
mod tests {
    #![allow(
        clippy::unwrap_used,
        clippy::expect_used,
        clippy::panic,
        clippy::indexing_slicing
    )]

    use super::*;
    use crate::token::Token;
    use alloc::format;
    use alloc::vec;
    use alloc::vec::Vec;

    fn toks(input: &str) -> Vec<Token<'_>> {
        tokenize(input).unwrap()
    }

    #[test]
    fn leading_eq_positions() {
        // `=` consumed only as the very first token.
        assert_eq!(parse("=1+1"), parse("1+1"));
        // Second `=` is the comparison operator.
        assert!(parse("1=1").is_ok());
        assert!(parse("==1").is_err()); // `=` `=1` → missing lhs
    }

    #[test]
    fn eof_sentinel_keeps_peek_in_bounds() {
        let t = toks("1");
        assert!(matches!(t.last().unwrap().kind, TokenKind::Eof));
    }

    #[test]
    fn unclosed_function_call() {
        assert!(matches!(
            parse("SUM(1,2"),
            Err(FormulaError::Parse { expected: "',' or ')'", got, .. }) if got == "end of formula"
        ));
    }

    #[test]
    fn empty_vs_omitted_args() {
        // F() → zero args.
        let e = parse("F()").unwrap();
        let Expr::Function { args, .. } = &e else {
            panic!()
        };
        assert_eq!(args.len(), 0);
        // F(1) → one.
        let e = parse("F(1)").unwrap();
        let Expr::Function { args, .. } = &e else {
            panic!()
        };
        assert_eq!(args.len(), 1);
        // F(,) → two omitted.
        let e = parse("F(,)").unwrap();
        let Expr::Function { args, .. } = &e else {
            panic!()
        };
        assert_eq!(args.len(), 2);
    }

    #[test]
    fn function_names_normalize_and_reject_bad_words() {
        assert_eq!(
            parse("min(1)").unwrap(),
            Expr::Function {
                name: "MIN".into(),
                args: vec![Expr::Number(1.0)]
            }
        );
        // `_xlfn.SUM(1)` — dotted names parse as calls.
        assert!(matches!(
            parse("_xlfn.SUM(1)").unwrap(),
            Expr::Function { ref name, .. } if name == "_XLFN.SUM"
        ));
    }

    #[test]
    fn mixed_case_refs_and_ranges() {
        assert_eq!(parse("Ab12").unwrap(), parse("aB12").unwrap());
        assert_eq!(parse("ab12:CD34").unwrap(), parse("AB12:cd34").unwrap());
    }

    #[test]
    fn unary_percent_chains() {
        // Deep unary chains cap at MAX_DEPTH.
        let deep = format!("{}1", "+".repeat(MAX_DEPTH + 5));
        assert_eq!(parse(&deep), Err(FormulaError::RecursionLimit));
        // Just under parses.
        let near = format!("{}1", "+".repeat(MAX_DEPTH - 2));
        assert!(parse(&near).is_ok());
    }

    #[test]
    fn unescape_doubled_quotes() {
        assert_eq!(unescape("plain"), "plain");
        assert_eq!(unescape("a\"\"b"), "a\"b");
        assert_eq!(unescape("\"\"\"\""), "\"\"");
        assert_eq!(unescape(""), "");
    }

    #[test]
    fn trailing_percent_after_ref() {
        assert!(parse("A1%").is_ok());
        assert!(parse("SUM(1)%").is_ok());
        assert!(parse("A1:B2%").is_ok());
    }

    #[test]
    fn parens_do_not_make_calls() {
        // `(A1)(B1)` — juxtaposition → parse error at second `(`.
        assert!(matches!(
            parse("(A1)(B1)"),
            Err(FormulaError::Parse {
                expected: "end of formula",
                ..
            })
        ));
    }
}