JFLib

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 ParseErrorKind why a formula did not parse

    • BadChar -- a character that starts nothing
    • UnexpectedEnd -- the formula ended in the middle of an expression
    • Unexpected -- a token where it does not fit
    • Unclosed -- a "(" or "[" without its closing bracket
    • UnclosedString -- a string without its closing quote
    • BadNumber -- a number that does not parse
    • BadReference -- a reference that does not parse (Sheet.x!, Geometry.X1 ...)
    • Trailing -- text after the end of the expression
    • TooComplex -- the formula nests too deep or is too long
  • struct ParseError a formula that did not parse, with the byte position where it stopped

    • pos: usize -- byte offset into the formula text
    • kind: ParseErrorKind -- what went wrong
  • enum BinOp binary operators

    • Add -- +
    • 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 know

  • fn name(self) -> Option<&'static str> the universal name (None for Unknown: Parsed::name has it)

  • enum SheetName the sheet part of a reference (in front of "!")

    • Local -- none: the sheet the formula belongs to
    • Call(u32) -- the sheet a function gives (CONTAINERSHEETREF(1,"Swimlane")!User.visHeadingText): the node of the call
    • Id(u32) -- Sheet.5!: the shape with this ID on the same page or in the same master
    • Named(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 sheet
    • Page(Box<str>, Option<u32>) -- Pages[name]! with an optional Sheet.N! after it
    • Master(Box<str>, Option<u32>) -- Masters[name]! (the master's page sheet) with an optional Sheet.N! after it
    • Style(Box<str>) -- Styles[name]!
  • enum CellPart the cell part of a reference

    • None -- no cell: the sheet itself (Sheet.5!SheetRef())
    • Single(Box<str>) -- a single cell: Width, PinX, EventXFMod, TheText
    • Section { 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.A1
    • Named { section: &'static str, row: Box<str>, cell: Box<str>, } -- a cell of a named row: User.Name, Prop.Name.Format, Controls.Row_1.Y
    • Unknown(Box<str>) -- a dotted name of a section the engine does not know, as written
  • struct CellRef a reference as written: sheet part and cell part

    • sheet: SheetName -- the sheet
    • cell: CellPart -- the cell
  • struct Parsed a parsed formula

impl Parsed

  • fn refs(&self) -> &[CellRef] the references in the order they are written

  • fn functions(&self) -> impl Iterator<Item = Func> + '_ the functions called, in the order they are written (with repeats)

  • fn func_name(&self, f: Func) -> &str the name of a function call (also of an unknown one)

  • fn unknown(&self) -> impl Iterator<Item = &str> + '_ the names of the unknown functions it calls

  • fn top_is(&self, f: Func) -> bool whether the whole formula is a call of f (GUARD(...), THEMEGUARD(...), SETATREF(...))

  • fn calls(&self, f: Func) -> bool whether the formula calls f anywhere

  • fn constant(&self) -> Option<f64> whether the formula is a plain number (with or without unit), and which

  • fn 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 form

  • fn unit_code(code: &str) -> Option<Unit> the unit of a code as written after a number ("MM", "mm", "Deg"); None: not a unit

  • fn parse(text: &str) -> Result<Parsed, ParseError> parse a formula in the universal syntax

Source: docs/API.md in the JFLib repository.