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)



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
99
100
101
102
# File 'lib/stupidedi/builder/generation.rb', line 28

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>)



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/stupidedi/builder/generation.rb', line 11

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

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

      reader.read_segment
    end
  end

  return machine, reader
end

#removeStateMachine

Returns:



110
111
112
# File 'lib/stupidedi/builder/generation.rb', line 110

def remove
  # @todo
end

#replace(segment_tok, reader) ⇒ StateMachine

Returns:



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

def replace(segment_tok, reader)
  # @todo
end