Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/bates64/nearley-make

Compile nearley grammars at run-time
https://github.com/bates64/nearley-make

Last synced: 14 days ago
JSON representation

Compile nearley grammars at run-time

Awesome Lists containing this project

README

        

# nearley-make
> Compile [nearley](https://github.com/Hardmath123/nearley/) grammars at run-time

`nearley-make` allows you to use [nearley](https://github.com/Hardmath123/nearley/) in Node.js, without requiring you to compile your grammar first.

Very useful for debug/development modes.

## Install
```sh
$ npm install nearley-make --save
```

## Usage
Examples speak 1000 words.

```js
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)
```

```python
# grammar.ne
main -> "the usual" {% d => output %} # output the exposed "output" variable
```