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:



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
273
274
275
276
# File 'lib/stupidedi/builder/states/abstract_state.rb', line 236

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



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

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!
    component_idx = 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, ...



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

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



117
118
119
120
# File 'lib/stupidedi/builder/states/abstract_state.rb', line 117

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:



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

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!
    element_idx = 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



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
177
178
179
180
# File 'lib/stupidedi/builder/states/abstract_state.rb', line 148

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



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

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:



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
227
228
229
230
# File 'lib/stupidedi/builder/states/abstract_state.rb', line 189

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:



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
328
329
330
331
# File 'lib/stupidedi/builder/states/abstract_state.rb', line 282

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:



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

abstract :children

#instructionsInstructionTable

Returns:



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

abstract :instructions

#leaf?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/stupidedi/builder/states/abstract_state.rb', line 26

def leaf?
  false
end

#pretty_print(q)

This method returns an undefined value.



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

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:



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

abstract :segment_dict

#separatorsReader::Separators

Returns:



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

abstract :separators

#zipperZipper::AbstractCursor



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

abstract :zipper