https://github.com/bates64/nearley-moo
ultra-fast tokenizer plugin for nearley :cow2:
https://github.com/bates64/nearley-moo
Last synced: 11 months ago
JSON representation
ultra-fast tokenizer plugin for nearley :cow2:
- Host: GitHub
- URL: https://github.com/bates64/nearley-moo
- Owner: bates64
- Created: 2017-03-19T19:41:45.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-07-11T15:17:45.000Z (almost 9 years ago)
- Last Synced: 2025-04-03T07:46:02.375Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 2.93 KB
- Stars: 5
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
## nearley-moo
#### [moo (ultra-fast tokenizer)](https://github.com/tjvr/moo) plugin for [nearley](https://github.com/Hardmath123/nearley) :cow2:
[](https://npmjs.com/package/nearley-moo)
## install
**with npm**
```sh
npm install nearley-moo --save
```
**with yarn**
```sh
yarn add nearley-moo
```
## usage
**index.js**
```js
const moo = require('moo')
const nearley = require('nearley')
const grammar = require('./grammar.js') // compiled from grammar.ne
const tokens = require('./tokens.js')
const nm = require('nearley-moo').parser(nearley, grammar) // curried
let parser = nm(moo.compile(tokens))
// ignored tokens will not be passed to nearley
// helpful for whitespace and/or comments
parser.ignore('whitespace') // may be Array or String
// feed your lexer+parser combo as normal
parser.feed('if true then moomoomoo else cows')
console.log(parser.results) // just like nearley
```
**tokens.js**
```js
module.exports = {
whitespace: /[ \t]+/,
moo: /(moo)+/,
cows: /cows/,
boolean: [ 'true', 'false' ],
keyword: [ 'if', 'then', 'else'],
}
```
**grammar.ne**
```ne
@{%
const nm = require('nearley-moo')
const tokens = require('./tokens.js')
nm(tokens)
%}
main -> %keyword_if expression %keyword_then expression %keyword_else expression
expression -> boolean
| %moo
| %cows
boolean -> %boolean_true {% d => true %}
| %boolean_false {% d => false %}
```