API reference
Gram is not just a file format — it is a pipeline of small, composable libraries. Each package does one dedicated job and hands a deterministic JSON object to the next. You can use the full pipeline end-to-end, or pick out just the module you need (e.g. only the parser, to build a custom linter).
Installation
Section titled “Installation”All packages are ESM-only, side-effect free, and run in any JavaScript runtime — Node.js, Deno, Bun, edge runtimes, or directly in the browser (see the Playground for a fully client-side demonstration).
Package matrix
Section titled “Package matrix”| Package | Role | Main entry point |
|---|---|---|
@gram-lang/parser | Turns .gram source text into an Abstract Syntax Tree (AST) | getAST(source) |
@gram-lang/modules | Resolves @use imports, tracks dependencies, scales bases by yield, and composes a unified AST | loadModuleGraph(entry, host), composeRecipe(graph, opts) |
@gram-lang/kitchen | Compiles the AST into a structured, render-ready payload (shopping list, task graph, default schedule, cookware registry) | compile(ast, options?) |
@gram-lang/scheduler | Computes theoretical ALAP scheduling and projects recipes onto a real-world calendar based on target serving time and availability | layout(graph, options?), project(recipes, context), runSheet(plan), findServeTimes(), earliestServe() |
@gram-lang/analyzer | Enriches a compiled recipe with physical properties (mass, yield, nutrition, baker’s percentages) using an ingredient database | analyze(compiled, database, options?) |
@gram-lang/renderer | Renders a (compiled or analyzed) recipe to Markdown, HTML, or plain text, and the run sheet of a schedule | toMarkdown, toHTML, toPrintHTML, toText, runSheetTo* |
@gram-lang/format | Canonical code formatter for .gram files (13 unified formatting rules) | formatGram(source) |
@gram-lang/i18n | Shared unit/time normalization and UI dictionaries used internally by the pipeline packages | normalizeUnit, getDictionary |
Analysis is optional: compile() alone provides a complete, renderable recipe (with raw, un-standardized quantities). You only need @gram-lang/analyzer when you require volume-to-mass conversion, nutrition estimation, or baker’s math, which rely on an ingredient database.
Optional: Node-only shortcut (@gram-lang/cli)
Section titled “Optional: Node-only shortcut (@gram-lang/cli)”If you are running in Node.js and do not require stage-by-stage control, @gram-lang/cli — the same package that powers the gram CLI binary — also exports a concise programmatic helper:
runPipeline(filePath, options?) reads the file from disk, then executes getAST → compile → (only if db is supplied) analyze, returning { content, compiled, analyzed }. It throws a GramCLIError (an Error subclass carrying an .exitCode property aligned with the exported ExitCode enum) instead of a generic error — making it easy to map failures to custom process exit codes. This shortcut is Node-only (reads via node:fs); for browsers or edge runtimes, compose the individual packages as shown below.
The full pipeline
Section titled “The full pipeline”Modular recipes with @gram-lang/modules
Section titled “Modular recipes with @gram-lang/modules”When working with recipes that contain @use import directives across multiple files, insert @gram-lang/modules between the parser and the compiler:
Handling parse errors and warnings
Section titled “Handling parse errors and warnings”getAST() is the only function in the pipeline that throws: a syntax error means there is no AST to work with. Every later stage instead collects warnings — a recipe with non-fatal issues still compiles, allowing your application to render the preview and surface diagnostics to the user.
GramParseError(thrown bygetAST): carries.message(human-readable diagnostic from Ohm.js, source excerpt included),.offset(character offset into the input string), and.expected(grammar rule or token expected).CompilationResult.warnings/AnalyzedCompilationResult.warnings(returned bycompile()and carried throughanalyze()): an array of structuredWarningobjects. See the Warnings reference for the complete list of error codes and instructions for enforcing--strict-style gates.