Install the CLI (includes REPL) using:
$ npm i -g cheddar-parser
Install for your project using:
$ npm i --save cheddar-parser
Simply running cheddar-parser
will open up a REPL. Otherwise, if not a TTY, STDIN will be used as input to STDOUT. Various output formats exist, through flags.
--ast
: Outputs an AST. Best for manually reading the parse tree
--obj
: Outputs an object. Provides in-depth data such as index & state
--json
: Outputs a JSON. Best for processing through another program
--pretty
: Pretty prints outputs. This includes syntax highlighting result & indenting JSON. Automatically assumed on the REPL.
One installed simply access the given parser using:
require('cheddar-parser/dist/path/to/item')
Alternatively, most common parsers are available through simply the module itself. example:
require('cheddar-parser').Expression
The full list of parsers is here.
To use a parser simply do:
let parserInstance = new parser(code, startIndex);
let result = parserInstance.exec();
result
may be an instanceof CheddarLexer
(ches.Lexer
), in which case it succesfully parsed, a string, in which it errored, the string is the error description, or a symbol, in which it is a symbol which represents the given error.
An example, of the most common way a cheddar lexer is used:
let run = new parser(code, 0);
let res = run.exec();
if (!(res instanceof ches.Lexer)) {
// Error, propogate the error
return `Syntax Error: ${res} at ${run.Index}`;
} else {
}
You can also use the ches.Helper.Locate(code, index)
to retrieve an array of [row, col, index]
of the index in the code.