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
Property | Type | Description |
---|---|---|
basedir | string | The base directory for resolving imports. (optional) |
resolver | function | A custom import resolver or constructor. (optional) |
output | object | Specifies how the generated parser should be produced. See below for details. (optional) |
GeneratorOutputOptions (output property)
Property | Type | Description |
---|---|---|
artifacts | object | Which artifacts to generate. - lexer : Set to true to generate a lexer.- grammar : Set to true to generate grammar code. (optional) |
format | string | The output format. Supported values: 'object' , 'json' , 'cjs' , 'commonjs' , 'js' , 'javascript' , 'module' , 'esmodule' , 'esm' , 'ts' , 'typescript' . (optional) |
noscript | boolean | If true , strips JavaScript embedded in the grammars from the output. (optional) |
name | string | The export name for the generated JavaScript/TypeScript. (optional) |