BGEX Compiler
raw JSON → 0.0.26 verified Fri May 01 auth: no javascript
BGEX (Better Grammar EXtractor) is a language and compiler for defining and generating context-free grammar parsers. v0.0.26 compiles BGEX source into TypeScript parsers that export typed AST nodes. It relies on TypeScript >=5.0 as a peer dependency. Differentiators include native ESM, full TypeScript support, and lightweight output with no runtime dependencies.
Common errors
error TypeError: compile is not a function ↓
cause Importing default export instead of named export.
fix
Use import { compile } from 'bgex-compiler' instead of import compile from 'bgex-compiler'.
error Error: Invalid grammar: unexpected token at line 1 ↓
cause Grammar string contains trailing spaces or CRLF line endings.
fix
Trim input and replace \r\n with \n before passing to compile().
error Cannot find module 'bgex-compiler' or its corresponding type declarations. ↓
cause TypeScript project not configured for ESM or missing module resolution.
fix
Ensure tsconfig.json has 'moduleResolution': 'node16' or 'bundler' and 'module': 'ESNext'.
error SyntaxError: Named export 'compile' not found. The requested module 'bgex-compiler' is a CommonJS module. ↓
cause Running in CJS context; bgex-compiler is ESM-only.
fix
Set package.json type: 'module' or use dynamic import().
Warnings
breaking API changed in v0.0.20: compile() now returns an object with parse() instead of direct function. ↓
fix Update to use const parser = compile(grammar); const ast = parser.parse(input);
gotcha Grammar line endings must be Unix-style LF; Windows CRLF causes parse errors. ↓
fix Convert input to LF before passing to compile().
deprecated The 'grammar' option in compile() is deprecated; pass grammar string directly. ↓
fix Use compile(grammar) instead of compile({ grammar }).
gotcha Compiled parsers are not serializable; cannot be passed across worker threads. ↓
fix Recompile grammar in each worker.
Install
npm install bgex-compiler yarn add bgex-compiler pnpm add bgex-compiler Imports
- compile wrong
const compile = require('bgex-compiler')correctimport { compile } from 'bgex-compiler' - type ASTNode
import type { ASTNode } from 'bgex-compiler' - parse
import { parse } from 'bgex-compiler'
Quickstart
import { compile, parse } from 'bgex-compiler';
const grammar = `
expr = term ('+' term)*
term = factor ('*' factor)*
factor = '(' expr ')' | NUMBER
NUMBER = [0-9]+
`;
const parser = compile(grammar);
const ast = parser.parse('3+4*5');
console.log(ast);
// Example output: { type: 'expr', left: { type: 'term', ... }, right: ... }