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