Class: Stupidedi::Builder::AbstractState

Inherits:
Object
  • Object
show all
Includes:
Inspect
Defined in:
lib/stupidedi/builder/states/abstract_state.rb

Constructors collapse

SegmentVal Construction collapse

Instruction Generation collapse

Instance Method Summary collapse

Methods included from Inspect

#inspect

Class Method Details

.lsequence(loop_defs, offset = 0) ⇒ Array<Instruction>

Builds a sequence of Instruction values that corresponds to the given sequence of ‘loop_defs`

Returns:



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/stupidedi/builder/states/abstract_state.rb', line 230

def lsequence(loop_defs, offset = 0)
  instructions = []
  buffer       = []
  last         = nil

  loop_defs.each do |l|
    unless last.nil? or l.entry_segment_use.position == last.entry_segment_use.position
      drop_count =
        if buffer.length == 1 and not last.repeatable?
          offset
        else
          offset - buffer.length
        end

      buffer.each do |u|
        instructions << Instruction.new(nil, u, 0, drop_count, LoopState)
      end
      buffer.clear
    end

    buffer << l.entry_segment_use
    last    = l
    offset += 1
  end

  # Flush the buffer one last time
  unless buffer.nil?
    drop_count =
      if buffer.length == 1 and not last.repeatable?
        offset
      else
        offset - buffer.length
      end

    buffer.each do |u|
      instructions << Instruction.new(nil, u, 0, drop_count, LoopState)
    end
  end

  instructions
end

.mkcomposite(designator, composite_use, composite_tok) ⇒ Values::CompositeElementVal



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/stupidedi/builder/states/abstract_state.rb', line 118

def mkcomposite(designator, composite_use, composite_tok)
  composite_def  = composite_use.definition
  component_uses = composite_def.component_uses
  component_toks = composite_tok.component_toks

  position       = composite_tok.position
  component_vals = []
  component_idx  = "00"
  component_uses.zip(component_toks) do |component_use, component_tok|
    component_idx.succ!
    position = component_tok.position if component_tok

    component_vals <<
      if component_tok.nil?
        component_use.empty(position)
      else
        mksimple("#{designator}-#{component_idx}", component_use, component_tok)
      end
  end

  composite_use.value(component_vals, composite_tok.position)
end

.mkelement(designator, element_use, element_tok) ⇒ Values::SimpleElementVal, ...



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/stupidedi/builder/states/abstract_state.rb', line 85

def mkelement(designator, element_use, element_tok)
  if element_use.simple?
    if element_use.repeatable?
      element_toks = element_tok.element_toks
      element_vals = element_toks.map do |element_tok|
        mksimple(designator, element_use, element_tok)
      end

      mkrepeated(designator, element_use, element_vals)
    else
      mksimple(designator, element_use, element_tok)
    end
  else
    if element_use.repeatable?
      element_toks = element_tok.element_toks
      element_vals = element_toks.map do |element_tok|
        mkcomposite(designator, element_use, element_tok)
      end

      mkrepeated(designator, element_use, element_vals)
    else
      mkcomposite(designator, element_use, element_tok)
    end
  end
end

.mkrepeated(designator, element_use, element_vals) ⇒ Values::RepeatedElementVal



112
113
114
115
# File 'lib/stupidedi/builder/states/abstract_state.rb', line 112

def mkrepeated(designator, element_use, element_vals)
  # @todo: Position
  Values::RepeatedElementVal.build(element_vals, element_use)
end

.mksegment(segment_tok, segment_use) ⇒ Values::SegmentVal

Returns:



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
# File 'lib/stupidedi/builder/states/abstract_state.rb', line 57

def mksegment(segment_tok, segment_use)
  segment_def  = segment_use.definition
  element_uses = segment_def.element_uses
  element_toks = segment_tok.element_toks

  position     = segment_tok.position
  element_vals = []
  element_idx  = "00"
  element_uses.zip(element_toks) do |element_use, element_tok|
    element_idx.succ!
    position = element_tok.position if element_tok

    element_vals <<
      if element_tok.nil?
        if element_use.repeatable?
          Values::RepeatedElementVal.empty(element_use)
        else
          element_use.empty(position)
        end
      else
        mkelement("#{segment_def.id}#{element_idx}", element_use, element_tok)
      end
  end

  segment_use.value(element_vals, segment_tok.position)
end

.mksimple(designator, element_use, element_tok) ⇒ Values::SimpleElementVal



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/stupidedi/builder/states/abstract_state.rb', line 142

def mksimple(designator, element_use, element_tok)
  # We don't validate that element_tok is simple because the TokenReader
  # will always produce a SimpleElementTok given a SimpleElementUse from
  # the SegmentDef. On the other hand, the BuilderDsl API will throw an
  # exception if the programmer constructs the wrong kind of element
  # according to the SegmentDef.

  if element_tok.value == :default
    allowed_vals = element_use.allowed_values

    if element_use.requirement.forbidden?
      element_use.empty(element_tok.position)
    elsif allowed_vals.empty?
      element_use.empty(element_tok.position)
    elsif allowed_vals.size == 1
      element_use.value(allowed_vals.first, element_tok.position)
    else
      raise Exceptions::ParseError,
        "#{designator} cannot be inferred"
    end
  elsif element_tok.value == :not_used
    if element_use.requirement.forbidden?
      element_use.empty(element_tok.position)
    else
      raise Exceptions::ParseError,
        "#{designator} is not forbidden"
    end
  elsif element_tok.value == :blank
    element_use.empty(element_tok.position)
  else
    element_use.value(element_tok.value, element_tok.position)
  end
end

.pushZipper::AbstractCursor



51
# File 'lib/stupidedi/builder/states/abstract_state.rb', line 51

abstract :push, :args => %w(zipper parent segment_tok segment_use config)

.sequence(segment_uses, offset = 0) ⇒ Array<Instruction>

Builds a sequence of Instruction values that corresponds to the given sequence of ‘segment_uses`

Returns:



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/stupidedi/builder/states/abstract_state.rb', line 183

def sequence(segment_uses, offset = 0)
  instructions = []
  buffer       = []
  last         = nil

  segment_uses.each do |s|
    unless last.nil? or s.position == last.position
      drop_count =
        if buffer.length == 1 and not last.repeatable?
          offset
        else
          offset - buffer.length
        end

      buffer.each do |u|
        instructions << Instruction.new(nil, u, 0, drop_count, nil)
      end

      buffer.clear
    end

    buffer << s
    last    = s
    offset += 1
  end

  # Flush the buffer one last time
  unless buffer.empty?
    drop_count =
      if buffer.length == 1 and not last.repeatable?
        offset
      else
        offset - buffer.length
      end

    buffer.each do |u|
      instructions << Instruction.new(nil, u, 0, drop_count, nil)
    end
  end

  instructions
end

.tsequence(table_defs, offset = 0) ⇒ Array<Instruction>

Builds a sequence of Instruction values that corresponds to the given sequence of ‘table_defs`

Returns:



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/stupidedi/builder/states/abstract_state.rb', line 276

def tsequence(table_defs, offset = 0)
  instructions = []
  buffer       = []
  last         = nil

  table_defs.each do |t|
    unless last.nil? or t.position == last.position
      drop_count =
        if buffer.length == 1 and not last.repeatable?
          offset
        else
          offset - buffer.inject(0){|n,b| n + b.entry_segment_uses.length }
        end

      buffer.each do |b|
        if b.repeatable? and b.entry_segment_uses.length > 1
          raise Exceptions::InvalidSchemaError,
            "@todo"
        end

        b.entry_segment_uses.each do |u|
          instructions << Instruction.new(nil, u, 0, drop_count, TableState)
        end
      end

      buffer.clear
    end

    last    = t
    offset += t.entry_segment_uses.length
    buffer << t
  end

  unless buffer.empty?
    drop_count =
      if buffer.length == 1 and not last.repeatable?
        offset
      else
        offset - buffer.inject(0){|n,b| n + b.entry_segment_uses.length }
      end

    buffer.each do |b|
      b.entry_segment_uses.each do |u|
        instructions << Instruction.new(nil, u, 0, drop_count, TableState)
      end
    end
  end

  instructions
end

Instance Method Details

#childrenArray<AbstractState>

Returns:



17
# File 'lib/stupidedi/builder/states/abstract_state.rb', line 17

abstract :children

#instructionsInstructionTable

Returns:



20
# File 'lib/stupidedi/builder/states/abstract_state.rb', line 20

abstract :instructions

#leaf?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/stupidedi/builder/states/abstract_state.rb', line 22

def leaf?
  false
end

#pretty_print(q) ⇒ void

This method returns an undefined value.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/stupidedi/builder/states/abstract_state.rb', line 27

def pretty_print(q)
  q.text self.class.name.split('::').last
# q.text "[#{zipper.node.class.name.split('::').last}]"
# q.text "[#{zipper.node.definition.id}]"

  q.group(2, "(", ")") do
    q.breakable ""

    children.each do |e|
      unless q.current_group.first?
        q.text ","
        q.breakable
      end
      q.pp e
    end
  end
end

#segment_dictReader::SegmentDict

Returns:



11
# File 'lib/stupidedi/builder/states/abstract_state.rb', line 11

abstract :segment_dict

#separatorsReader::Separators

Returns:



8
# File 'lib/stupidedi/builder/states/abstract_state.rb', line 8

abstract :separators

#zipperZipper::AbstractCursor



14
# File 'lib/stupidedi/builder/states/abstract_state.rb', line 14

abstract :zipper