Class: Grammar

Inherits:
Object
  • Object
show all
Defined in:
lib/tracery.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Grammar

, settings


515
516
517
518
# File 'lib/tracery.rb', line 515

def initialize(raw) #, settings
    @modifiers = {}
    loadFromRawObj(raw)
end

Instance Attribute Details

#distributionObject

Returns the value of attribute distribution


513
514
515
# File 'lib/tracery.rb', line 513

def distribution
  @distribution
end

#modifiersObject

Returns the value of attribute modifiers


513
514
515
# File 'lib/tracery.rb', line 513

def modifiers
  @modifiers
end

Instance Method Details

#addModifiers(mods) ⇒ Object


524
525
526
527
# File 'lib/tracery.rb', line 524

def addModifiers(mods)
    # copy over the base modifiers
    mods.each{|k,v| @modifiers[k] = v}
end

#clearStateObject


520
521
522
# File 'lib/tracery.rb', line 520

def clearState
    @symbols.each{|k,v| v.clearState} # TODO_ check for nil keys
end

#createRoot(rule) ⇒ Object


539
540
541
542
543
544
545
546
# File 'lib/tracery.rb', line 539

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


548
549
550
551
552
553
# File 'lib/tracery.rb', line 548

def expand(rule, allowEscapeChars = false)
    root = createRoot(rule)
    root.expand
    root.clearEscapeCharacters if(!allowEscapeChars)
    return root
end

#flatten(rule, allowEscapeChars = false) ⇒ Object


555
556
557
# File 'lib/tracery.rb', line 555

def flatten(rule, allowEscapeChars = false)
    return expand(rule, allowEscapeChars).finishedText
end

#loadFromRawObj(raw) ⇒ Object


529
530
531
532
533
534
535
536
537
# File 'lib/tracery.rb', line 529

def loadFromRawObj(raw)
    @raw = raw
    @symbols = {}
    @subgrammars = []
    return if(@raw.nil?)
    @raw.each{|k,v|
        @symbols[k] = TracerySymbol.new(self, k, v)
    }
end

#popRules(key) ⇒ Object


569
570
571
572
# File 'lib/tracery.rb', line 569

def popRules(key)
    errors << "No symbol for key #{key}" if(@symbols[key].nil?)
    @symbols[key].popRules
end

#pushRules(key, rawRules, sourceAction) ⇒ Object


559
560
561
562
563
564
565
566
567
# File 'lib/tracery.rb', line 559

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


574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# File 'lib/tracery.rb', line 574

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

#toJSONObject


591
592
593
594
# File 'lib/tracery.rb', line 591

def toJSON
    symbols = @symbols.each.collect{|symkey, symval| "\"#{symkey}\": #{symval.rulesToJSON}"}
    return "{\n#{symbols.join("\n")}\n}"
end