Class: Calyx::Syntax::Choices
- Inherits:
-
Object
- Object
- Calyx::Syntax::Choices
- Defined in:
- lib/calyx/syntax/choices.rb
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
-
#evaluate(options) ⇒ Array
Evaluate the choice by randomly picking one of its possible options.
-
#initialize(collection) ⇒ Choices
constructor
Initialize a new choice with a list of child nodes.
-
#size ⇒ Integer
The number of possible choices available for this rule.
Constructor Details
#initialize(collection) ⇒ Choices
Initialize a new choice with a list of child nodes.
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.
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.
48 49 50 |
# File 'lib/calyx/syntax/choices.rb', line 48 def evaluate() [:choice, @collection.sample(random: .rng).evaluate()] end |
#size ⇒ Integer
The number of possible choices available for this rule.
40 41 42 |
# File 'lib/calyx/syntax/choices.rb', line 40 def size @collection.size end |