gram/gram-parser
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2025-12-30 18:47:46 +01:00
..
dist chore: build parser to verify it generates the d.ts files 2025-12-30 18:47:46 +01:00
src refactor: First working TS refactoring 2025-12-30 18:34:28 +01:00
expected_JSON.json Initial release: Specification, Parser core, Playground and VSCode Extension 2025-12-28 21:28:39 +01:00
grammar.ohm Initial release: Specification, Parser core, Playground and VSCode Extension 2025-12-28 21:28:39 +01:00
package-lock.json refactor: First working TS refactoring 2025-12-30 18:34:28 +01:00
package.json refactor: First working TS refactoring 2025-12-30 18:34:28 +01:00
README.md docs: Updated docs to reflect TS refactoring 2025-12-30 18:40:44 +01:00
tsconfig.json fix: Updated compiler options 2025-12-30 18:40:33 +01:00

GRAM Parser

A robust, 3-stage parser for the GRAM recipe language.

Architecture

This parser extracts data from .gram files using a three-step pipeline:

  1. Parsing (OhmJS): Validates the source against the grammar.
  2. AST Generation: Converts the raw parse tree into a clean Abstract Syntax Tree.
  3. Compilation: Processes the AST to generate a registry-based JSON model, handling:
    • Linear scaling logic
    • Composite ingredient aggregation (MAX logic)
    • Ingredient & Cookware reference resolution
    • Shopping list generation

Usage

const { parse } = require('gram-parser');

const source = `
## My Recipe
Mix @flour{200%g} and @water{100%ml}.
`;

try {
    const result = parse(source);
    console.log(result);
} catch (e) {
    console.error("Parsing error:", e.message);
}

Building

To build the project from source (TypeScript):

npm install
npm run build

This will generate the compiled JavaScript in the dist/ directory.

Structure

  • src/index.ts: Entry point, orchestrates Parsing -> AST.
  • src/compiler/: Contains the logic for AST -> Schema JSON transformation.
  • grammar.ohm: The OhmJS grammar definition.
  • dist/: Compiled JavaScript output (generated).

Output Model

The output is a JSON object optimized for UI rendering, featuring:

  • registry: Deduplicated definitions of ingredients/cookware.
  • shopping_list: Aggregated quantities.
  • sections: Step-by-step instructions with references to the registry.

License

This project is licensed under the GPL-3.0 License - see the LICENSE file for details.