https://github.com/dmbaturin/bnfgen-js
JavaScript bindings for the BNFGen library
https://github.com/dmbaturin/bnfgen-js
Last synced: about 1 month ago
JSON representation
JavaScript bindings for the BNFGen library
- Host: GitHub
- URL: https://github.com/dmbaturin/bnfgen-js
- Owner: dmbaturin
- License: mit
- Created: 2021-02-15T14:05:35.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-02-17T14:40:26.000Z (almost 5 years ago)
- Last Synced: 2024-10-17T02:45:13.827Z (about 1 year ago)
- Language: OCaml
- Size: 39.1 KB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bnfgen-js
JavaScript bindings for the [BNFGen](https://baturin.org/tools/bnfgen) library.
Features:
* Familiar BNF-like syntax for grammar descriptions.
* Control over the generation process.
* Debugging/tracing options.
Grammar example:
```
# You can add "weights" to your rules.
# The recursive " " alternative will be taken 10 times more often
# to produce longer sequences.
::= | 10 ;
# Deterministic repetition: up to 10 of "foo" or exactly two of "baz".
::= {1,10} | "baz"{2} ;
::= "foo" | "bar"
```
## Install
```
$ npm install bnfgen
```
## Use in a web browser
The `bnfgen_browser.js` file is a browser library that "exports" a `window.bnfgen` object.
## Usage
```javascript
var bnfgen = require('bnfgen');
// Parse and load a grammar
bnfgen.loadGrammar(' ::= "world"; = "hello" | "hi"');
// Set symbol separator to space (default is '')
bnfgen.symbolSeparator = ' ';
// bnfgen.generate function requires a start symbol
bnfgen.generate('start') // generates "hello world" or "hi world"
bnfgen.generate('greeting') // generates "hello" or "hi"
//// Limits
// Maximum number of reductions you allow to perform
// (bnfgen will raise an error if it's exceeded)
// 0 means no limit
bnfgen.maxReductions = 0
// Maximum number of reductions that don't produce any terminals
maxNonproductiveReductions = 0
//// Debug options
// Log symbol reductions
bnfgen.debug = false
// Also log current symbol stack at every
bnfgen.dumpStack = false
// You can supply a custom logging function instead of the default console.log
bnfgen.debugFunction = console.log
```
## Building the JS files from the OCaml source
```
opam install ocamlfind bnfgen js_of_ocaml js_of_ocaml-ppx
make all
```