@gram-lang/parser
Turns .gram source text into a plain-object Abstract Syntax Tree (AST). This is the only stage of the pipeline that can fail on malformed input — every other package trusts that if it received an AST, it’s structurally valid.
getAST
Section titled “getAST”Parses a .gram source string and returns the root RecipeAST node. Throws a GramParseError on invalid syntax.
GramParseError
Section titled “GramParseError”Thrown by getAST on a syntax error.
| Field | Description |
|---|---|
message | ohm-js’s human-readable prose (source excerpt included) — safe to display directly. |
offset | Plain character offset into input where the failure occurred. |
expected | Description of what the parser expected at that offset. |
offset and expected are the portable, structured parts of the failure — useful for editor integrations (squiggly underlines, quick-fixes) that don’t want to parse ohm’s prose.
AST node types
Section titled “AST node types”Every node has a type: ASTNodeType discriminant and an optional loc: { start, end } (character offsets into the source, present on most but not all node types — see the interfaces below).
| Value |
|---|
Recipe |
Section |
Step |
Comment |
Text |
IntermediateDecl |
RelativeQuantity |
TextQuantity |
Quantity |
Ingredient |
Composite |
Cookware |
Reference |
Timer |
Temperature |
Alternative |
ImportDecl |
Key interfaces
Section titled “Key interfaces”See Module Imports for the @use syntax itself, and @gram-lang/modules for resolving and composing an import graph into a single AST.
For ## Puff Pastry ~{-2h}, retroPlanning is { raw: "-2h", sign: -1, value: 2, unit: "h" }. Free text like ~{the day before} still parses (the parser never throws on this — see Times & Scheduling for why), but produces { raw: "the day before", sign: 1, value: null, unit: null }; it’s @gram-lang/kitchen that flags this as invalid (MISSING_UNIT) when compiling.
The remaining node interfaces (CookwareAST, ReferenceAST, TimerAST, TemperatureAST, CommentAST, AlternativeAST, IntermediateDecl, TextQuantityAST) follow the same pattern — see packages/parser/src/types.ts for the exhaustive list.
Type guards
Section titled “Type guards”13 type guards are exported for safely narrowing ASTNode | StepAST | ... | null | undefined inputs without manual .type checks:
isIngredient, isCookware, isTimer, isTemperature, isReference, isIntermediateDecl, isAlternative, isComment, isStep, isSection, isQuantity, isTextQuantity, isRelativeQuantity.
Syntax highlighting: @gram-lang/parser/textmate
Section titled “Syntax highlighting: @gram-lang/parser/textmate”A subpath export ships the TextMate grammar used by the VS Code extension and the docs’ own Shiki-powered code blocks:
It resolves to a .tmLanguage.json file — pass it straight into any TextMate-grammar-compatible highlighter (Shiki, Monaco, VS Code).