genitive
v1.1.0
Published
A DSL for defining grammars for generating strings.
Downloads
3
Readme
Genitive
A DSL for defining grammars for generating strings.
npm install genitive
Call the default export with a grammar that defines productions as nonterminals mapped to an (array of) expansion.
const genitive = require('genitive')
const g = genitive({
greeting: ['Hello, <who>!', 'Hi, <who>!'],
who: 'World'
})
console.log([...g('<greeting>')])
// [ 'Hello, World!', 'Hi, World!' ]
The following examples are in YAML, and always the first production is evaluated.
You can build filters from variables, whose effect is only active within the block given to them - every assignment of a variable is undone at backtracking.
greeting:
style=formal: Good morning <title>!
style=informal:
- Hi <title>!
- Hey <title>!
title:
style=formal: [Sir, Madam]
style=informal: Dude
Good morning Sir!
Good morning Madam!
Hi Dude!
Hey Dude!
There are different kinds of filters:
var=value
if unset, sets it. if set, succeeds if has the same value (using ==)var:=value
sets the value regardless of whether it is set, always succeedsvar?=value
sets the value if unset, always succeedsvar!=value
succeeds if unset or set to a different valuevar<value
var<=value
var>value
var>=value
error if unset, succeeds if the mathematical relation holds
You can specify a disjunction of filters with |
:
greeting:
style=formal|style=informal: Hello, <title>!
You can also specify filters in the substitution placeholders:
greeting:
- Good morning, <style=formal:title>!
- Hi, <style=informal:title>!
- Hey, <style=informal:title>!
title:
style=formal: [Sir, Madam]
style=informal: Dude