gram/packages/parser/README.md
abiwab a68a18c3c4
Some checks failed
CI / Build & Test (push) Has been cancelled
docs: rewrite root and package READMEs for v1.6
2026-11-29 22:35:50 +01:00

1.5 KiB

@gram-lang/parser

npm License: GPL v3

Part of the Gram monorepo · Documentation

Ohm.js parser for the Gram recipe DSL. Strictly limited to converting .gram source text into a typed Recipe AST.

Installation

npm install @gram-lang/parser
# or
bun add @gram-lang/parser

Usage

The package exports getAST() to convert Gram source strings into a typed Recipe AST.

import { getAST } from '@gram-lang/parser';

const source = `
## My Recipe

Mix @flour{200g} and @water{100ml}.
`;

try {
  const ast = getAST(source);
  console.log(JSON.stringify(ast, null, 2));
} catch (e) {
  console.error('Parsing error:', e.message);
}

Structure

  • src/index.ts: orchestrates parsing and Ohm semantics to AST conversions (getAST, GramParseError).
  • src/types.ts: typed AST node definitions.
  • src/guards.ts: type guards for AST nodes.
  • src/numbers.ts: number, mixed fraction, and decimal comma/point parsers.
  • src/schemas.ts: Zod schemas for AST structures.
  • grammar.ohm: Ohm.js grammar defining the Gram language syntax.

License

Licensed under the GPL-3.0 license.