Class: Dendroid::GrmAnalysis::AlternativeItem

Inherits:
DottedItem
  • Object
show all
Defined in:
lib/dendroid/grm_analysis/alternative_item.rb

Overview

A specialization of DottedItem specific for Choice (rule)

Instance Attribute Summary collapse

Attributes inherited from DottedItem

#position, #rule

Instance Method Summary collapse

Methods inherited from DottedItem

#expecting?, #initial_pos?, #intermediate_pos?, #pre_scan?, #state

Constructor Details

#initialize(aChoice, aPosition, index) ⇒ AlternativeItem

Constructor.

Parameters:

  • aChoice (Dendroid::Syntax::Choice)
  • aPosition (Integer)

    Position of the dot in rhs of production.

  • index (Integer)

    the rank of the alternative at hand



16
17
18
19
# File 'lib/dendroid/grm_analysis/alternative_item.rb', line 16

def initialize(aChoice, aPosition, index)
  @alt_index = index
  super(aChoice, aPosition)
end

Instance Attribute Details

#alt_indexInteger (readonly)

Returns the alternative number.

Returns:

  • (Integer)

    the alternative number



10
11
12
# File 'lib/dendroid/grm_analysis/alternative_item.rb', line 10

def alt_index
  @alt_index
end

Instance Method Details

#==(other) ⇒ Boolean

Test for equality with another dotted item. Two dotted items are equal if they refer to the same rule and have both the same rhs and dot positions.

Returns:

  • (Boolean)


55
56
57
58
59
# File 'lib/dendroid/grm_analysis/alternative_item.rb', line 55

def ==(other)
  return true if eql?(other)

  (position == other.position) && rule.eql?(other.rule) && (alt_index == other.alt_index)
end

#empty?Boolean

Indicate whether the rhs of the alternative is empty

Returns:

  • (Boolean)


31
32
33
# File 'lib/dendroid/grm_analysis/alternative_item.rb', line 31

def empty?
  rule.alternatives[alt_index].empty?
end

#final_pos?Boolean Also known as: completed?

Indicate whether the dot is at the start of rhs

Returns:

  • (Boolean)


37
38
39
# File 'lib/dendroid/grm_analysis/alternative_item.rb', line 37

def final_pos?
  empty? || position == rule.alternatives[alt_index].size
end

#next_symbolDendroid::Syntax::GrmSymbol, NilClass

Return the symbol right after the dot (if any)

Returns:



45
46
47
48
49
# File 'lib/dendroid/grm_analysis/alternative_item.rb', line 45

def next_symbol
  return nil if empty? || completed?

  rule.alternatives[alt_index].members[position]
end

#to_sString

Return a String representation of the alternative item.

Returns:

  • (String)


23
24
25
26
27
# File 'lib/dendroid/grm_analysis/alternative_item.rb', line 23

def to_s
  rhs_names = rule.alternatives[alt_index].members.map(&:to_s)
  dotted_rhs = rhs_names.insert(position, '.')
  "#{rule.head} => #{dotted_rhs.join(' ')}"
end