Skip to main content

sheetkit_core/formula/
ast.rs

1//! AST types for parsed Excel formulas.
2
3/// A parsed Excel formula expression.
4#[derive(Debug, Clone, PartialEq)]
5pub enum Expr {
6    /// Numeric literal (e.g., 42, 3.14)
7    Number(f64),
8    /// String literal (e.g., "hello")
9    String(String),
10    /// Boolean literal (TRUE/FALSE)
11    Bool(bool),
12    /// Error literal (#N/A, #VALUE!, #REF!, #DIV/0!, #NUM!, #NAME?, #NULL!)
13    Error(String),
14    /// Cell reference (e.g., A1, $B$2)
15    CellRef(CellReference),
16    /// Range reference (e.g., A1:B10)
17    Range {
18        start: CellReference,
19        end: CellReference,
20    },
21    /// Function call (e.g., SUM(A1:A10))
22    Function { name: String, args: Vec<Expr> },
23    /// Binary operation (e.g., A1 + B1)
24    BinaryOp {
25        op: BinaryOperator,
26        left: Box<Expr>,
27        right: Box<Expr>,
28    },
29    /// Unary operation (e.g., -A1, +5)
30    UnaryOp {
31        op: UnaryOperator,
32        operand: Box<Expr>,
33    },
34    /// Parenthesized expression
35    Paren(Box<Expr>),
36}
37
38/// A cell reference with optional absolute markers.
39#[derive(Debug, Clone, PartialEq)]
40pub struct CellReference {
41    /// Column name (e.g., "A", "AB")
42    pub col: String,
43    /// Row number (1-based)
44    pub row: u32,
45    /// True if column is absolute ($A)
46    pub abs_col: bool,
47    /// True if row is absolute ($1)
48    pub abs_row: bool,
49    /// Optional sheet name (e.g., Sheet1!A1)
50    pub sheet: Option<String>,
51}
52
53/// Binary operators.
54#[derive(Debug, Clone, Copy, PartialEq, Eq)]
55pub enum BinaryOperator {
56    /// Addition (+)
57    Add,
58    /// Subtraction (-)
59    Sub,
60    /// Multiplication (*)
61    Mul,
62    /// Division (/)
63    Div,
64    /// Exponentiation (^)
65    Pow,
66    /// Concatenation (&)
67    Concat,
68    /// Equal (=)
69    Eq,
70    /// Not equal (<>)
71    Ne,
72    /// Less than (<)
73    Lt,
74    /// Less than or equal (<=)
75    Le,
76    /// Greater than (>)
77    Gt,
78    /// Greater than or equal (>=)
79    Ge,
80}
81
82/// Unary operators.
83#[derive(Debug, Clone, Copy, PartialEq, Eq)]
84pub enum UnaryOperator {
85    /// Negation (-)
86    Neg,
87    /// Positive (+)
88    Pos,
89    /// Percent (%)
90    Percent,
91}