Class: RLTK::CFG::Item

Inherits:
Production show all
Defined in:
lib/rltk/cfg.rb

Overview

The Item class represents a CFG production with dot in it.

Instance Attribute Summary collapse

Attributes inherited from Production

#id, #lhs, #rhs

Instance Method Summary collapse

Methods inherited from Production

#last_terminal, #to_item

Constructor Details

#initialize(dot, *args) ⇒ Item

Instantiates a new Item object with a dot located before the symbol at index dot of the right-hand side. The remaining arguments (args) should be as specified by Production#initialize.

Parameters:

  • dot (Integer)

    Location of the dot in this Item.

  • args (Array<Object>)


631
632
633
634
635
636
# File 'lib/rltk/cfg.rb', line 631

def initialize(dot, *args)
	super(*args)

	# The Dot indicates the NEXT symbol to be read.
	@dot = dot
end

Instance Attribute Details

#dotInteger (readonly)

Returns Index of the next symbol in this item.

Returns:

  • (Integer)

    Index of the next symbol in this item.



622
623
624
# File 'lib/rltk/cfg.rb', line 622

def dot
  @dot
end

Instance Method Details

#==(other) ⇒ Boolean

Compares two items.

Parameters:

  • other (Item)

    Another item to compare to.

Returns:

  • (Boolean)


643
644
645
# File 'lib/rltk/cfg.rb', line 643

def ==(other)
	self.dot == other.dot and self.lhs == other.lhs and self.rhs == other.rhs
end

#advanceInteger?

Moves the items dot forward by one if the end of the right-hand side hasn’t already been reached.

Returns:

  • (Integer, nil)


651
652
653
654
655
# File 'lib/rltk/cfg.rb', line 651

def advance
	if @dot < @rhs.length
		@dot += 1
	end
end

#at_end?Boolean

Tests to see if the dot is at the end of the right-hand side.

Returns:

  • (Boolean)


660
661
662
# File 'lib/rltk/cfg.rb', line 660

def at_end?
	@dot == @rhs.length
end

#copyItem

Returns A new copy of this item.

Returns:

  • (Item)

    A new copy of this item.



665
666
667
# File 'lib/rltk/cfg.rb', line 665

def copy
	Item.new(@dot, @id, @lhs, @rhs.clone)
end

#next_symbolSymbol

Returns the symbol located after the dot.

Returns:

  • (Symbol)

    Symbol located after the dot (at the index indicated by the #dot attribute).



672
673
674
# File 'lib/rltk/cfg.rb', line 672

def next_symbol
	@rhs[@dot]
end

#to_s(padding = 0) ⇒ String

Returns a string representation of this item.

Parameters:

  • padding (Integer) (defaults to: 0)

    The ammount of padding spaces to add to the beginning of the string.

Returns:

  • (String)


681
682
683
# File 'lib/rltk/cfg.rb', line 681

def to_s(padding = 0)
	"#{format("%-#{padding}s", @lhs)} -> #{@rhs.map { |s| s.to_s }.insert(@dot, '·').join(' ') }"
end