Module: Stupidedi::Builder::Generation

Included in:
StateMachine
Defined in:
lib/stupidedi/builder/generation.rb

Instance Method Summary collapse

Instance Method Details

#insert(segment_tok, reader) ⇒ (StateMachine, Reader::TokenReader)



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/stupidedi/builder/generation.rb', line 24

def insert(segment_tok, reader)
  active = []

  @active.each do |zipper|
    state        = zipper.node
    instructions = state.instructions.matches(segment_tok)

    if instructions.empty?
      active << zipper.append(FailureState.mksegment(segment_tok, state))
      next
    end

    instructions.each do |op|
      if op.push.nil?
        table = zipper.node.instructions
        value = zipper.node.zipper
        state = zipper

        op.pop_count.times do
          value = value.up
          state = state.up
        end

        # Create a new AbstractState node that has a new InstructionTable
        # and also points to a new AbstractVal tree (with the new segment)
        segment   = AbstractState.mksegment(segment_tok, op.segment_use)
        successor = state.append(state.node.copy(
          :zipper       => value.append(segment),
          :instructions => table.pop(op.pop_count).drop(op.drop_count)))

        unless op.pop_count.zero? or reader.stream?
          # More general than checking if segment_tok is an ISE/GE segment
          unless reader.separators.eql?(successor.node.separators) \
            and reader.segment_dict.eql?(successor.node.segment_dict)
            reader = reader.copy \
              :separators   => successor.node.separators,
              :segment_dict => successor.node.segment_dict
          end
        end
      else
        table = zipper.node.instructions
        value = zipper.node.zipper
        state = zipper

        op.pop_count.times do
          value = value.up
          state = state.up
        end

        parent = state.node.copy \
          :zipper       => value,
          :children     => [],
          :separators   => reader.separators,
          :segment_dict => reader.segment_dict,
          :instructions => table.pop(op.pop_count).drop(op.drop_count)

        state = state.append(parent) unless state.root?

        successor = op.push.push(state, parent, segment_tok, op.segment_use, @config)

        # More general than checking if segment_tok is an ISA/GS segment
        unless reader.separators.eql?(successor.node.separators) \
          and reader.segment_dict.eql?(successor.node.segment_dict)
          reader = reader.copy \
            :separators   => successor.node.separators,
            :segment_dict => successor.node.segment_dict
        end
      end

      active << successor
    end
  end

  return StateMachine.new(@config, active), reader
end

#read(reader) ⇒ (StateMachine, Either<Reader::TokenReader>)



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/stupidedi/builder/generation.rb', line 7

def read(reader)
  machine = self
  reader  = reader.read_segment

  while reader.defined?
    reader = reader.flatmap do |segment_tok, reader|
      machine, reader =
        machine.insert(segment_tok, reader)

      reader.read_segment
    end
  end

  return machine, reader
end

#removeStateMachine

Returns:



106
107
108
# File 'lib/stupidedi/builder/generation.rb', line 106

def remove
  # @todo
end

#replace(segment_tok, reader) ⇒ StateMachine

Returns:



101
102
103
# File 'lib/stupidedi/builder/generation.rb', line 101

def replace(segment_tok, reader)
  # @todo
end