Class: ParseState

Inherits:
Array
  • Object
show all
Defined in:
lib/parser/parse_state.rb

Overview

ParseState sub-classes Array to provide a stack for holding the current parse state.

Instance Method Summary collapse

Constructor Details

#initialize(state = nil, target = nil) ⇒ ParseState

Create the initial state. Optional arguments: * state, if it has a value, is pushed onto the state stack as the current state. * target, if it has a value, then it is the object we are using when in this state. It should be non-nil if state is not nil. e.g. GedcomParser.new calls ParseState.new( :transmission , @transmission) Pushes the transmission state onto the stack, and records the GedcomParser.transmission object as the current target object.



13
14
15
16
17
# File 'lib/parser/parse_state.rb', line 13

def initialize(state  = nil, target = nil)
  super(0)
  push state if state != nil
  @target = target #record this so that we can control the targets stack as we alter this one.
end

Instance Method Details

#dumpObject

dump() is a debugging aid to print out the current ParseState's stack.



45
46
47
# File 'lib/parser/parse_state.rb', line 45

def dump
  self.reverse.each { |the_state| p the_state }
end

#last_stateObject

last_state() is the previous state on the stack, so we know the state we came from.



30
31
32
# File 'lib/parser/parse_state.rb', line 30

def last_state
  self[-2]
end

#levelObject

level() is the depth of the stack, less 1, which gives us the GEDCOM level we are working with.



20
21
22
# File 'lib/parser/parse_state.rb', line 20

def level
  length - 1
end

#popObject

pop() reverts to the previous state.



35
36
37
38
39
40
41
42
# File 'lib/parser/parse_state.rb', line 35

def pop
  if level > 0
    super
    @target.pop if @target != nil #as we go up a level, we need to move up the target objects stack too.
  else
    raise "Empty Parse State Stack"
  end
end

#stateObject

state() is the last item in the Array, hence the top of the stack.



25
26
27
# File 'lib/parser/parse_state.rb', line 25

def state
  last
end