Recipe
The root node containing frontmatter metadata (meta) and child Section nodes.
The @gram-lang/parser package is the foundation of the Gram ecosystem. Its sole responsibility is to read raw .gram text and convert it into an Abstract Syntax Tree (AST).
flowchart LR
A["Raw .gram text"] --> B["Ohm.js PEG<br/>(grammar.ohm)"]
B --> C["Grammar semantics<br/>(semantics.ts)"]
C --> D["RecipeAST"]
D --> E["@gram-lang/kitchen<br/>(Compiler)"]
D --> F["Language Server<br/>(LSP & Tooling)"]
Gram’s syntax rules are defined using Ohm.js, an object-oriented parsing toolkit built around Parsing Expression Grammars (PEGs).
Ohm makes it straightforward to build modular grammars. Because of this, @gram-lang/parser is fast and strictly enforces the structural rules of the language. For example, whitespace is forbidden immediately around the composite marker <@ (invalidComposite in the grammar), so pastry < @dough fails to parse while <@dough succeeds.
When parsing succeeds, the parser outputs an AST. This is a tree of JavaScript objects representing every semantic token in the recipe.
In this example, the step is parsed into a Step node containing a leading Text node ("Add the "), an Ingredient node, and a trailing Text node (".").
Note that quantity.value is always a QuantityValueAST object, never a bare number — this allows the parser to preserve fractions (1/2) and ranges (2-3) alongside the author’s original text representation.
The parser exposes specific node types for every construct in the Gram language (ASTNodeType in src/types.ts):
Recipe
The root node containing frontmatter metadata (meta) and child Section nodes.
Section
A grouping of Step and top-level Comment nodes. Can carry an optional retro-planning anchor (~{...}) and an intermediate declaration.
Step
A single paragraph containing text and semantic inline tokens. Supports an optional leading action tag (e.g. [Preheat]).
Ingredient & Cookware
Represent ingredients (@flour) and tools (#whisk). Can be wrapped in an Alternative node when combined with the | operator.
Quantity
Represents amounts in three forms: standard numeric/fraction/range (Quantity), relative baker’s percentage (RelativeQuantity), or raw text (TextQuantity).
Composite & Reference
Composite references (<@lemon) aggregate components into a whole item. Reference nodes consume intermediate variables (&dough).
@gram-lang/parser performs strictly syntactic parsing without semantic domain evaluation:
&dough was declared previously.ingredients.yaml database.It does, however, perform a few localized structural validations during tree construction: it validates recipe frontmatter against a schema (discarding invalid fields), computes the midpoint for range quantities (2-3 provides an average of 2.5) for downstream convenience, and emits clear syntax errors for malformed composite references.
Semantic resolution, timeline generation, and reference validation take place in the next pipeline stage: @gram-lang/kitchen.