Use the Generate function to compile your grammar source into JavaScript or TypeScript code. This code defines a parser that you can use in your application.

import { Generate } from 'grammar-well/generator';
const ts = await Generate(source, {
    output: {
        name: 'grammar',
        format: 'typescript',
        artifacts: {
            lexer: true,
            grammar: true
        }
    }
});

Generate Function Options

The Generate function accepts an options object that controls how your grammar is compiled. Below is a description of the main options you can use:

GenerateOptions

PropertyTypeDescription
basedirstringThe base directory for resolving imports. (optional)
resolverfunctionA custom import resolver or constructor. (optional)
outputobjectSpecifies how the generated parser should be produced. See below for details. (optional)

GeneratorOutputOptions (output property)

PropertyTypeDescription
artifactsobjectWhich artifacts to generate.
- lexer: Set to true to generate a lexer.
- grammar: Set to true to generate grammar code. (optional)
formatstringThe output format. Supported values:
'object', 'json', 'cjs', 'commonjs', 'js', 'javascript', 'module', 'esmodule', 'esm', 'ts', 'typescript'. (optional)
noscriptbooleanIf true, strips JavaScript embedded in the grammars from the output. (optional)
namestringThe export name for the generated JavaScript/TypeScript. (optional)