class Calyx::Syntax::Choices

Public Class Methods

new(collection) click to toggle source

Initialize a new choice with a list of child nodes.

@param [Array] collection

# File lib/calyx/syntax/choices.rb, line 33
def initialize(collection)
  @collection = collection
end
parse(productions, registry) click to toggle source

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

@param [Array] productions @param [Calyx::Registry] registry

# 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

Public Instance Methods

evaluate(options) click to toggle source

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

@param [Calyx::Options] options @return [Array]

# File lib/calyx/syntax/choices.rb, line 48
def evaluate(options)
  [:choice, @collection.sample(random: options.rng).evaluate(options)]
end
size() click to toggle source

The number of possible choices available for this rule.

@return [Integer]

# File lib/calyx/syntax/choices.rb, line 40
def size
  @collection.size
end