class Grammar
Attributes
distribution[RW]
modifiers[RW]
Public Class Methods
new(raw)
click to toggle source
# File lib/tracery.rb, line 529 def initialize(raw) #, settings @modifiers = {} loadFromRawObj(raw) end
Public Instance Methods
addModifiers(mods)
click to toggle source
# File lib/tracery.rb, line 538 def addModifiers(mods) # copy over the base modifiers mods.each{|k,v| @modifiers[k] = v} end
clearState()
click to toggle source
# File lib/tracery.rb, line 534 def clearState @symbols.each{|k,v| v.clearState} # TODO_ check for nil keys end
createRoot(rule)
click to toggle source
# File lib/tracery.rb, line 554 def createRoot(rule) # Create a node and subnodes root = TraceryNode.new(self, 0, { type: -1, raw: rule }) return root end
expand(rule, allowEscapeChars = false)
click to toggle source
# File lib/tracery.rb, line 563 def expand(rule, allowEscapeChars = false) root = createRoot(rule) root.expand root.clearEscapeCharacters if(!allowEscapeChars) return root end
flatten(rule, allowEscapeChars = false)
click to toggle source
# File lib/tracery.rb, line 570 def flatten(rule, allowEscapeChars = false) return expand(rule, allowEscapeChars).finishedText end
loadFromRawObj(raw)
click to toggle source
# File lib/tracery.rb, line 543 def loadFromRawObj(raw) raw = Hash[raw.collect{|k,v| [k.to_s, v]}] @raw = raw @symbols = {} @subgrammars = [] return if(@raw.nil?) @raw.each{|k,v| @symbols[k] = TracerySymbol.new(self, k, v) } end
popRules(key)
click to toggle source
# File lib/tracery.rb, line 584 def popRules(key) errors << "No symbol for key #{key}" if(@symbols[key].nil?) @symbols[key].popRules end
pushRules(key, rawRules, sourceAction)
click to toggle source
# File lib/tracery.rb, line 574 def pushRules(key, rawRules, sourceAction) # Create or push rules if(@symbols[key].nil?) then @symbols[key] = TracerySymbol.new(self, key, rawRules) @symbols[key].isDynamic = true if(sourceAction) else @symbols[key].pushRules(rawRules) end end
selectRule(key, node, errors)
click to toggle source
# File lib/tracery.rb, line 589 def selectRule(key, node, errors) if(@symbols.has_key? key) then return @symbols[key].selectRule(node, errors) end # Failover to alternative subgrammars @subgrammars.each do |subgrammar| if(subgrammar.symbols.has_key? key) then return subgrammar.symbols[key].selectRule end end # No symbol? errors << "No symbol for '#{key}'" return "((#{key}))" end
toJSON()
click to toggle source
# File lib/tracery.rb, line 606 def toJSON symbols = @symbols.each.collect{|symkey, symval| "\"#{symkey}\": #{symval.rulesToJSON}"} return "{\n#{symbols.join("\n")}\n}" end