Class: Grammar
- Inherits:
-
Object
- Object
- Grammar
- Defined in:
- lib/tracery.rb
Instance Attribute Summary collapse
-
#distribution ⇒ Object
Returns the value of attribute distribution.
-
#modifiers ⇒ Object
Returns the value of attribute modifiers.
Instance Method Summary collapse
- #addModifiers(mods) ⇒ Object
- #clearState ⇒ Object
- #createRoot(rule) ⇒ Object
- #expand(rule, allowEscapeChars = false) ⇒ Object
- #flatten(rule, allowEscapeChars = false) ⇒ Object
-
#initialize(raw) ⇒ Grammar
constructor
, settings.
- #loadFromRawObj(raw) ⇒ Object
- #popRules(key) ⇒ Object
- #pushRules(key, rawRules, sourceAction) ⇒ Object
- #selectRule(key, node, errors) ⇒ Object
- #toJSON ⇒ Object
Constructor Details
#initialize(raw) ⇒ Grammar
, settings
529 530 531 532 |
# File 'lib/tracery.rb', line 529 def initialize(raw) #, settings @modifiers = {} loadFromRawObj(raw) end |
Instance Attribute Details
#distribution ⇒ Object
Returns the value of attribute distribution.
527 528 529 |
# File 'lib/tracery.rb', line 527 def distribution @distribution end |
#modifiers ⇒ Object
Returns the value of attribute modifiers.
527 528 529 |
# File 'lib/tracery.rb', line 527 def modifiers @modifiers end |
Instance Method Details
#addModifiers(mods) ⇒ Object
538 539 540 541 |
# File 'lib/tracery.rb', line 538 def addModifiers(mods) # copy over the base modifiers mods.each{|k,v| @modifiers[k] = v} end |
#clearState ⇒ Object
534 535 536 |
# File 'lib/tracery.rb', line 534 def clearState @symbols.each{|k,v| v.clearState} # TODO_ check for nil keys end |
#createRoot(rule) ⇒ Object
554 555 556 557 558 559 560 561 |
# 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) ⇒ Object
563 564 565 566 567 568 |
# File 'lib/tracery.rb', line 563 def (rule, allowEscapeChars = false) root = createRoot(rule) root. root.clearEscapeCharacters if(!allowEscapeChars) return root end |
#flatten(rule, allowEscapeChars = false) ⇒ Object
570 571 572 |
# File 'lib/tracery.rb', line 570 def flatten(rule, allowEscapeChars = false) return (rule, allowEscapeChars).finishedText end |
#loadFromRawObj(raw) ⇒ Object
543 544 545 546 547 548 549 550 551 552 |
# 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) ⇒ Object
584 585 586 587 |
# 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) ⇒ Object
574 575 576 577 578 579 580 581 582 |
# 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) ⇒ Object
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 |
# 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 ⇒ Object
606 607 608 609 |
# File 'lib/tracery.rb', line 606 def toJSON symbols = @symbols.each.collect{|symkey, symval| "\"#{symkey}\": #{symval.rulesToJSON}"} return "{\n#{symbols.join("\n")}\n}" end |