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
//! Spreadsheet formula language — tokenizer, Pratt parser, AST, evaluator
//! core. Excel-compatible function semantics.
//!
//! `formula-lang` is the estate's **L0 leaf** for spreadsheet work: a pure,
//! total formula engine with no I/O and no cell storage. Host engines
//! implement the [`CellResolver`] trait (a sheet, a sparse map, a replay
//! fixture); the crate turns formula text into typed results.
//!
//! ```
//! use formula_lang::{parse, evaluate, MapResolver, Value};
//!
//! // A tiny sheet: A1=2, B1=3, C1 text.
//! let mut sheet = MapResolver::new();
//! sheet.set_num(1, 1, 2.0);
//! sheet.set_num(2, 1, 3.0);
//! sheet.set_text(3, 1, "unit");
//!
//! let e = parse("=SUM(A1:B1) * 10 & \" \" & C1").unwrap();
//! assert_eq!(
//! evaluate(&e, &sheet).unwrap(),
//! Value::Text("50 unit".to_string())
//! );
//! ```
//!
//! # The pipeline
//!
//! 1. [`tokenize`] — `&str` in, zero-copy [`Token`]s out (`&[u8]`-backed
//! slices of the input; the one allocation is the token vector).
//! 2. [`parse`] — a Pratt parser over the Excel precedence ladder
//! (comparison < concat < add < mul < **right-assoc** power < unary <
//! postfix `%`), producing the [`Expr`] AST.
//! 3. [`evaluate`] — a depth-capped, allocation-light evaluator over a
//! [`CellResolver`]; [`evaluate_with_clock`] pins "now" for the
//! volatile functions.
//! 4. [`Display for Expr`](Expr#impl-Display) / [`to_formula`] —
//! precedence-correct serialization (round-trip property-tested).
//!
//! # Excel compatibility, stated precisely
//!
//! - **Precedence & the unary quirk**: `-2^2` is `(-2)^2` = `4` — prefix
//! unary binds *tighter* than power, as Excel does (and unlike school
//! math). `2^-3` still parses; `^` is right-associative (`2^3^2` =
//! `512`).
//! - **Coercions**: arithmetic coerces bool→1/0, numeric text→number,
//! empty→0; comparisons rank Number < Text < Boolean and never
//! cross-coerce (`"1" = 1` is FALSE); text compares case-insensitively;
//! empty substitutes the other side's zero.
//! - **Errors**: the seven `#…!` values propagate as data;
//! `IFERROR`/`IFNA`/`ISERROR`/`ISNA` catch them. Two documented
//! divergences: *unknown function names* are a hard
//! [`FormulaError::UnknownFunction`] rather than `#NAME?` (typed over
//! forgiving), and a bare range used as a scalar (`=A1:B2`) is
//! `#VALUE!` rather than a spill (scalar core).
//! - **Rounding**: `ROUND` is half-away-from-zero; `INT` floors (−∞);
//! `TRUNC`/`ROUNDDOWN` truncate; `MOD` takes the divisor's sign;
//! `CEILING`/`FLOOR` carry Excel's significance-sign rules.
//! - **Text**: UTF-16 code-unit indexing (`LEN("𐍈")` = 2); `TRIM` only
//! ever touches U+0020; `SEARCH` honors wildcards, `FIND` does not;
//! cell text caps at 32767 units (`REPT` overflows to `#VALUE!`).
//! - **Dates**: the Lotus leap bug is preserved — serial 60 is the
//! phantom 1900-02-29; 1970-01-01 is serial 25569.
//! - **Volatile functions**: `TODAY`, `NOW`, `OFFSET` — reported by
//! [`is_volatile`], clocked by [`evaluate_with_clock`].
//!
//! # The omitted-argument marker
//!
//! The public AST has no "omitted" variant, so empty argument slots
//! (`IF(A1,,5)`, `SUM(1,)`) are stored as
//! [`Expr::Error`]`(`[`ExcelError::Null`]`)` and detected with
//! [`is_omitted_arg`]. Two documented trade-offs: a literal `#NULL!` in a
//! direct argument position is treated as omitted, and a call whose
//! arguments are a *single* omitted slot renders back as `F()` (the
//! grammar cannot express one empty slot — `F(,)` parses as two). Outside
//! argument positions the marker is inert.
//!
//! # Safety & totality
//!
//! `#![no_std]` + `alloc`; `unsafe_code = deny`; `unwrap`/`expect`/
//! `panic`/indexing deny-linted on the lib target. Parsing and evaluation
//! are **total**: hostile input (fuzz-verified) yields typed errors —
//! including [`FormulaError::RecursionLimit`] past [`MAX_DEPTH`] nesting —
//! never a panic, never a stack overflow.
//!
//! # Allocation discipline
//!
//! The numeric scalar path (operators, coercions, all arithmetic and
//! comparison functions) allocates nothing — coercion happens through
//! `f64` locals, and results are returned unboxed. The allocating edges
//! are exactly where ownership forces them: text results (owned
//! `String`s), aggregate operand vectors, range materialization (the
//! resolver's policy — see [`CellResolver::get_range`]), and the
//! parse-path's token vector and AST nodes.
extern crate alloc;
extern crate std;
pub use crate;
pub use crate;
pub use crate;
pub use crate;
pub use cratebuiltin_functions;
pub use cratetokenize;
pub use crateparse;
pub use crateMAX_DEPTH;
pub use crate;
pub use crate;
pub use crateValue;
/// Crate version, for host diagnostics.
pub const VERSION: &str = env!;