diagram_core/src/formula/parse.rs#
Source: lib/diagram_core/src/formula/parse.rs
parse -- the universal formula syntax of a ShapeSheet cell into a small tree (FX-01)
parse("IF(Width>10 mm,Height*0.5,1 in)") -> Ok(Parsed) / Err(ParseError { pos, kind })
p.refs() the cell references as written (CellRef: sheet part + cell part)
p.functions() the functions it calls (Func; Func::Unknown keeps the name)
What the syntax has ([MS-VSDX] universal syntax, as the files write it):
* numbers with an optional unit right after them ("180DEG", "0.25DL", "10 mm", "1.5E-3IN");
the value is kept in internal units (inches, radians, days), so "10 mm" is 0.3937...;
"60%" is 0.6 (% is a postfix operator, also after a bracket)
* strings in double quotes, "" inside is one quote; TRUE and FALSE; error values (#REF! ...)
* operators, loosest first ([MS-VSDX] 2.5.2): = <>, then < <= > >=, & (text), + -, * /, ^,
unary - and + (so -2^2 is 4); all left to right; % after a number ("60%" is 0.6; the
parser also takes it after a bracket)
* references: Width, Geometry1.X2, Geometry1.NoFill, Char.Size, Char.Size[2], Para.HorzAlign,
Prop.Name, Prop.Name.Format, User.Name, User.Name.Prompt, Scratch.X1, Controls.Row_1,
Controls.Row_1.Y, Connections.X1, Connections.Name.Y, Actions.Row_1.Action; with a sheet in
front: Sheet.5!Width, ThePage!PageScale, TheDoc!DocLangID, Pages[Page-1]!Sheet.3!Width,
Masters[Name]!Width, Masters[Name]!Sheet.5!Width, Styles[Normal]!LineColor,
'Some name'!Width and Name.7!Width (a shape by its NameU)
* function calls, also with a sheet in front (Sheet.5!SheetRef()); an empty argument is
allowed (SETATREF(User.X,,1)); unknown names parse as Func::Unknown
Errors carry the byte position where the parser stopped. Nothing here needs a document: the
engine binds the names to a document's string table once per formula text.
enum ParseErrorKindwhy a formula did not parseBadChar-- a character that starts nothingUnexpectedEnd-- the formula ended in the middle of an expressionUnexpected-- a token where it does not fitUnclosed-- a "(" or "[" without its closing bracketUnclosedString-- a string without its closing quoteBadNumber-- a number that does not parseBadReference-- a reference that does not parse (Sheet.x!, Geometry.X1 ...)Trailing-- text after the end of the expressionTooComplex-- the formula nests too deep or is too long
struct ParseErrora formula that did not parse, with the byte position where it stoppedpos: usize-- byte offset into the formula textkind: ParseErrorKind-- what went wrong
enum BinOpbinary operatorsAdd-- +Sub-- -Mul-- *Div-- /Pow-- ^Concat-- & (text concatenation)Eq-- =Ne-- <>Lt-- <Gt-- >Le-- <=Ge-- >=
impl Func
fn by_name(name: &str) -> Option<Func>the function of a name (any case); None for names the engine does not knowfn name(self) -> Option<&'static str>the universal name (None for Unknown: Parsed::name has it)enum SheetNamethe sheet part of a reference (in front of "!")Local-- none: the sheet the formula belongs toCall(u32)-- the sheet a function gives (CONTAINERSHEETREF(1,"Swimlane")!User.visHeadingText): the node of the callId(u32)-- Sheet.5!: the shape with this ID on the same page or in the same masterNamed(Box<str>)-- a shape by its NameU ("Name.7!", "'My name'!")ThePage-- ThePage!: the page sheet (in a master: the master's page sheet)TheDoc-- TheDoc!: the document sheetPage(Box<str>, Option<u32>)-- Pages[name]! with an optional Sheet.N! after itMaster(Box<str>, Option<u32>)-- Masters[name]! (the master's page sheet) with an optional Sheet.N! after itStyle(Box<str>)-- Styles[name]!
enum CellPartthe cell part of a referenceNone-- no cell: the sheet itself (Sheet.5!SheetRef())Single(Box<str>)-- a single cell: Width, PinX, EventXFMod, TheTextSection { section: &'static str, index: u32, cell: Box<str>, }-- a cell of a section itself: Geometry1.NoFill (section index 0-based)Indexed { section: &'static str, index: u32, row: u32, cell: Box<str>, }-- a cell of an indexed row: Geometry1.X2, Connections.X1, Char.Size, Scratch.A1Named { section: &'static str, row: Box<str>, cell: Box<str>, }-- a cell of a named row: User.Name, Prop.Name.Format, Controls.Row_1.YUnknown(Box<str>)-- a dotted name of a section the engine does not know, as written
struct CellRefa reference as written: sheet part and cell partsheet: SheetName-- the sheetcell: CellPart-- the cell
struct Parseda parsed formula
impl Parsed
fn refs(&self) -> &[CellRef]the references in the order they are writtenfn functions(&self) -> impl Iterator<Item = Func> + '_the functions called, in the order they are written (with repeats)fn func_name(&self, f: Func) -> &strthe name of a function call (also of an unknown one)fn unknown(&self) -> impl Iterator<Item = &str> + '_the names of the unknown functions it callsfn top_is(&self, f: Func) -> boolwhether the whole formula is a call off(GUARD(...), THEMEGUARD(...), SETATREF(...))fn calls(&self, f: Func) -> boolwhether the formula callsfanywherefn constant(&self) -> Option<f64>whether the formula is a plain number (with or without unit), and whichfn cell_part(name: &str, bracket: Option<u32>) -> Option<CellPart>the cell part of a dotted name ("Geometry1.X2", "User.Name.Prompt", "Width") with an optional [n] after it; None when the name does not fit a section's formfn unit_code(code: &str) -> Option<Unit>the unit of a code as written after a number ("MM", "mm", "Deg"); None: not a unitfn parse(text: &str) -> Result<Parsed, ParseError>parse a formula in the universal syntax