Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bates64/nearley-moo
ultra-fast tokenizer plugin for nearley :cow2:
https://github.com/bates64/nearley-moo
Last synced: about 1 month 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 (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-07-11T15:17:45.000Z (over 7 years ago)
- Last Synced: 2024-11-28T18:11:51.387Z (about 1 month ago)
- Language: JavaScript
- Homepage:
- Size: 2.93 KB
- Stars: 4
- 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:[![npm](https://img.shields.io/npm/v/nearley-moo.svg)](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
| %cowsboolean -> %boolean_true {% d => true %}
| %boolean_false {% d => false %}
```