This is an implementation of Kate Compton's Tracery language in pure lua. The library takes a recursive approach to managing generative state, and returns any un-popped rules that have been pushed to the stack during the course of generation.
local tracery = require "tracery"
local testGrammar = {
origin = "#group.a# of #animal.s# #number#",
animal = {"zebra", "osprey", "rhinoceros", "cassowary", "donkey", "eel part"},
group = {"group", "herd", "flock", "murder"},
number = function() return 10 end,
}
local grammar = tracery.createGrammar(testGrammar)
grammer:addModifiers(tracery.baseEngModifiers)
local text, data = grammar:flatten("You see #origin#")
Creates a [Grammar] object, using the optional tbl
as the base rule set. This rule set is immutable.
The following are the methods exposed by a Grammar object.
Resolves any rules in a piece of text and returns any pushed rules from the process in a table.
A shorthand for grammar:flatten("#symbol#")
. Useful if you want to generate from a single symbol and not a more complex piece of text.
Pushes a rule onto the context of the grammar (destructive push).
Removes a rule from the context.
Pushes a new context table onto the grammar stack (non destructive push).
Pops off a context stack and restores the previous context stack.
Adds the given modifiers table to the gramar. Modifier tables are in the form {modifier_name = function(text)}
.
Deletes the un-popped rules from the grammar's last generation.
The same modifiers from Kate Compton's original tracery are accessible as tracery.baseEngModifiers
.
- (See issue #1) POP's inside of rules don't affect higher pushes.
Please raise an issue if you find any more!
Grammars can contain functions as the value for a symbol. This function will be called with no arguments, and will have their return values inserted into the output text.
>>> grammar = createGrammar({
origin = "Your lucky number is: #number#",
number = function()
return math.random(42)
end
})
>>> text, _ = grammar:generate("origin")
>>> print(text)
Your lucky number is: 34
- Allow repetition of rules by a given number
- Allow custom weighting rules for array rules
- lua-tracery is Copyright Aldous Rice-Leech 2020
- Tracery is Copyright Kate Compton 2015
lua-tracery inherits the Apache Public License from Tracery See LICENSE.md for more details