Once you have generated the parser, you can use it to parse text by combining it with the Parse function.
Example:
import { Parse } from 'grammar-well/parse';
import grammar from './my-grammar.js';
const output = Parse(
new grammar(),
body,
{
algorithm: 'earley',
parserOptions: {}
},
'first'
);
Function signature:
export function Parse(
language: InstanceType<RuntimeParserClass>,
input: string,
options: {
algorithm: 'earley',
parserOptions: {}
},
results: 'full' | 'first' = 'first'
)
Below are the arguments the Parse function accepts:
| # | Name | Type | Description |
|---|---|---|---|
| 1 | language | Instance of generated parser class | An instance of the generated parser class (e.g., new grammar()). |
| 2 | input | string | The input string to be parsed. |
| 3 | options | object (optional) | Parser options: - algorithm: Parsing algorithm to use ('earley' by default).- parserOptions: Additional parser options. |
| 4 | results | 'first' | 'full' (optional, default: 'first') | Determines the type of result returned: - 'first': Returns the first parse result.- 'full': Returns all possible parse results. |