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:



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

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



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

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, ...



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

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_tok1|
        mksimple(designator, element_use, element_tok1)
      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_tok1|
        mkcomposite(designator, element_use, element_tok1)
      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



114
115
116
117
# File 'lib/stupidedi/builder/states/abstract_state.rb', line 114

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:



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

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



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

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



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

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:



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

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:



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

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:



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

abstract :children

#instructionsInstructionTable

Returns:



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

abstract :instructions

#leaf?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/stupidedi/builder/states/abstract_state.rb', line 24

def leaf?
  false
end

#pretty_print(q)

This method returns an undefined value.



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

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:



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

abstract :segment_dict

#separatorsReader::Separators

Returns:



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

abstract :separators

#zipperZipper::AbstractCursor



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

abstract :zipper