0.17.0 • Published 2 years ago

calyx v0.17.0

Weekly downloads
179
License
MIT
Repository
github
Last release
2 years ago

Calyx

A JavaScript port of Calyx, a Ruby based generative grammar tool.

Status

npm github

Install

npm install calyx

Usage

Most of the existing documentation is for the Ruby version, but some brief usage notes are included below.

ESM

import calyx from "calyx"

const hello = calyx.grammar({
  "start": "Hello world."
})

CommonJS

const calyx = require("calyx")

const hello = calyx.grammar({
  "start": "Hello world."
})

Generator API

Call generate on a grammar instance to generate a random text based on your configured rules.

const result = hello.generate()

generate returns a Result type which allows you to access the generated text as a string or an s-expression tree of arrays.

result.text
result.tree

Example Usage

Every grammar feature documented for the Ruby version should work here (if not, please raise a bug report).

The main difference between the Ruby and JavaScript implementations is the class-based Ruby DSL for defining grammar rules. This doesn’t exist in the JavaScript version which only supports definitions in the object-literal format with string keys for rule names.

import { grammar } from "calyx"

const companyNames = grammar({
  "start": ["{web_two_punkt_oh} {ext}", "{acme_variations} {ext}"],
  "ext": ["LLC", "Ltd", "Limited", "Pty Ltd"],
  "web_two_punkt_oh": ["{punktrs}r", "{punktrs}zy"],
  "punktrs": ["Lick", "Stick", "Brick", "Syntax", "Relax", "Back"],
  "acme_variations": "Acme {acme_noun}",
  "acme_noun": ["Products", "Industries", "Goods", "Holdings", "Services"]
})

const company = companyNames.generate()

console.log(company.text)

Syntax

See more details here.

License

This package is copyright 2017-2022 Mark Rickerby and distributed freely under the terms of the MIT License. See the LICENSE file packaged with this software distribution.

0.17.0

2 years ago

0.16.0

2 years ago

0.14.0

2 years ago

0.15.0

2 years ago

0.13.0

3 years ago

0.12.0

3 years ago

0.10.0

4 years ago

0.9.0

6 years ago

0.8.2

6 years ago

0.8.1

6 years ago

0.8.0

6 years ago

0.7.0

6 years ago

0.6.0

6 years ago

0.5.0

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago