Class: Stupidedi::Parser::AbstractState

Inherits:
Object
  • Object
show all
Includes:
Inspect
Defined in:
lib/stupidedi/parser/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:



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

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



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

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



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

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



111
112
113
114
# File 'lib/stupidedi/parser/states/abstract_state.rb', line 111

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:



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

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



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

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

.push(zipper, parent, segment_tok, segment_use, config) ⇒ Zipper::AbstractCursor



50
# File 'lib/stupidedi/parser/states/abstract_state.rb', line 50

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:



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

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:



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

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|
        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/parser/states/abstract_state.rb', line 19

abstract :children

#instructionsInstructionTable

Returns:



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

abstract :instructions

#leaf?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/stupidedi/parser/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
# File 'lib/stupidedi/parser/states/abstract_state.rb', line 29

def pretty_print(q)
  q.text self.class.name.split("::").last
  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/parser/states/abstract_state.rb', line 13

abstract :segment_dict

#separatorsReader::Separators

Returns:



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

abstract :separators

#zipperZipper::AbstractCursor<Values::SegmentVal>



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

abstract :zipper