genitive

1.1.0 • Public • Published

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 succeeds
  • var?=value sets the value if unset, always succeeds
  • var!=value succeeds if unset or set to a different value
  • var<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

Readme

Keywords

none

Package Sidebar

Install

npm i genitive

Weekly Downloads

2

Version

1.1.0

License

ISC

Unpacked Size

5.66 kB

Total Files

4

Last publish

Collaborators

  • cie