Skip to content

Formula

Formula is the root of every parse tree. It holds an ordered list of top-level nodes — typically one molecule, one reaction, or one cascade, but in general any sequence.

FieldTypeDescription
nodesArray of NodeOrdered list of top-level model nodes
parse(text) ──► Formula
├── nodes[0] (Molecule | Reaction | ...)
├── nodes[1] (Text | Molecule | ...)
└── ...
formula := spaces? nodes spaces?
nodes := node+
node := reaction_cascade | reaction | electron_config
| molecule | embedded_math | text_run

The formula trims leading/trailing whitespace, then captures one or more top-level nodes.

Formula exposes << for appending a node:

formula = AsciiChem::Model::Formula.new
formula << AsciiChem::Model::Atom.new(element: "H")

This is primarily useful in tests and direct model construction.

FormatterOutput
MathML<math xmlns="...">...</math> document
TextConcatenation of children’s text
HTMLConcatenation of children’s HTML
LaTeXChildren wrapped in \ce{...}
SVGLinear layout in <svg>

Even single-molecule parses wrap in a Formula. This normalises the contract: every formatter’s visit_formula is the entry point. Adding a top-level construct (e.g. a “section” or “footnote” type) means adding it to Formula#nodes; the formatters’ visit_formula already iterates.