Class: Calyx::Syntax::Choices

Inherits:
Object
  • Object
show all
Defined in:
lib/calyx/syntax/choices.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ Choices

Initialize a new choice with a list of child nodes.

Parameters:

  • collection (Array)


33
34
35
# File 'lib/calyx/syntax/choices.rb', line 33

def initialize(collection)
  @collection = collection
end

Class Method Details

.parse(productions, registry) ⇒ Object

Parse a list of productions and return a choice node which is the head of a syntax tree of child nodes.

Parameters:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/calyx/syntax/choices.rb', line 11

def self.parse(productions, registry)
  choices = productions.map do |choice|
    if choice.is_a?(String)
      Concat.parse(choice, registry)
    elsif choice.is_a?(Integer)
      Terminal.new(choice.to_s)
    elsif choice.is_a?(Symbol)
      if choice[0] == Memo::SIGIL
        Memo.new(choice, registry)
      elsif choice[0] == Unique::SIGIL
        Unique.new(choice, registry)
      else
        NonTerminal.new(choice, registry)
      end
    end
  end
  self.new(choices)
end

Instance Method Details

#evaluate(options) ⇒ Array

Evaluate the choice by randomly picking one of its possible options.

Parameters:

Returns:

  • (Array)


48
49
50
# File 'lib/calyx/syntax/choices.rb', line 48

def evaluate(options)
  [:choice, @collection.sample(random: options.rng).evaluate(options)]
end

#sizeInteger

The number of possible choices available for this rule.

Returns:

  • (Integer)


40
41
42
# File 'lib/calyx/syntax/choices.rb', line 40

def size
  @collection.size
end