nearley-make
v1.0.1
Published
Compile nearley grammars at run-time
Downloads
5
Maintainers
Readme
nearley-make
Compile nearley grammars at run-time
nearley-make
allows you to use nearley in Node.js, without requiring you to compile your grammar first.
Very useful for debug/development modes.
Install
$ npm install nearley-make --save
Usage
Examples speak 1000 words.
const make = require('nearley-make')
const fs = require('fs')
const grammar = fs.readFileSync('grammar.ne', 'utf-8') // note that this is a *nearley* file
const parser = make(grammar, {
// anything you want to expose to the grammar as variables
// for example builtins or flavour settings
output: 'Hello, World!',
// if you want to use `require` in your grammar, make sure you do the following:
require: require
})
const trees = parser.feed('the usual').results
const tree = trees[0]
// logs "Hello, World!"
console.log(tree)
# grammar.ne
main -> "the usual" {% d => output %} # output the exposed "output" variable