Skip to content
/ kbnf Public

Context-free-grammar parser to generate random items and text

Notifications You must be signed in to change notification settings

coder13/kbnf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

KBNF

Installation

npm install kbnf

Usage

const { Parser, Generator } = require('kbnf');

const parser = new Parser();

const grammar = parser.parse(`
# this is a comment
# the lhs of each rule denotes the rule's name
# the right hand side is a "|" separated list of values to be randomly returned
# numbers and letters are currently supported for generating random values
<num> = 1 | 2 | 3 | 4 | 5
<letter> = "a" | "b" | "c"
# rules can be referenced to create more complex generators
<numOrLetter> = <num> | <letter>
# values can consist of multiple terms that optionally are separated with whitespace
<nums> = <num> | <num> <nums>
<addition> = <nums>"+"<nums>
`);

const generator = new Generator(grammar);

// generator.generate will return an array for each term specified in the rule.
generator.generate('num'); // returns an array where the only element is one of 1, 2, 3, 4 or 5.

generator.generate('letter'); // array with only element being one of a, b, or c
generator.generate('numOrLetter'); // array with only element being one of 1-5 or a-c

// passing in true as the 2nd argument will stringify all the output rather than returning a nested set of arrays.
generator.generate('nums', true); // any digit length

Examples

About

Context-free-grammar parser to generate random items and text

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published