gram/packages/parser
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-01-02 20:16:03 +01:00
..
dist refactor: Moved source code to new packages folder 2026-01-02 20:16:03 +01:00
src refactor: Moved source code to new packages folder 2026-01-02 20:16:03 +01:00
expected_JSON.json refactor: Moved source code to new packages folder 2026-01-02 20:16:03 +01:00
grammar.ohm refactor: Moved source code to new packages folder 2026-01-02 20:16:03 +01:00
package-lock.json refactor: Moved source code to new packages folder 2026-01-02 20:16:03 +01:00
package.json refactor: Moved source code to new packages folder 2026-01-02 20:16:03 +01:00
README.md refactor: Moved source code to new packages folder 2026-01-02 20:16:03 +01:00
tsconfig.json refactor: Moved source code to new packages folder 2026-01-02 20:16:03 +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.