Class: Stupidedi::Reader::TokenReader

Inherits:
Object
  • Object
show all
Includes:
Inspect
Defined in:
lib/stupidedi/reader/token_reader.rb

Constant Summary collapse

SEGMENT_ID =
/\A[A-Z][A-Z0-9]{1,2}\Z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Inspect

#inspect

Constructor Details

#initialize(input, separators, segment_dict = SegmentDict.empty) ⇒ TokenReader

Returns a new instance of TokenReader.



24
25
26
27
# File 'lib/stupidedi/reader/token_reader.rb', line 24

def initialize(input, separators, segment_dict = SegmentDict.empty)
  @input, @separators, @segment_dict =
    input, separators, segment_dict
end

Instance Attribute Details

#inputString, Input (readonly)

Returns:



16
17
18
# File 'lib/stupidedi/reader/token_reader.rb', line 16

def input
  @input
end

#segment_dictSegmentDict

Returns:



22
23
24
# File 'lib/stupidedi/reader/token_reader.rb', line 22

def segment_dict
  @segment_dict
end

#separatorsSeparators (readonly)

Returns:



19
20
21
# File 'lib/stupidedi/reader/token_reader.rb', line 19

def separators
  @separators
end

Instance Method Details

#consume(s) ⇒ Either<TokenReader>

If ‘s` occurs within #input, then the input up to and including `s` is skipped and the remaining input is returned as a new `TokenReader` wrapped by `Either.success`. Otherwise, Either::Failure is returned.

Returns:



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/stupidedi/reader/token_reader.rb', line 102

def consume(s)
  return success(self) if s.empty?

  position = 0
  buffer   = " " * s.length

  while @input.defined_at?(position)
    character = @input.at(position)

    unless is_control?(character)
      # Slide the "window" forward one character
      buffer = buffer.slice(1..-1) << character
    end

    position += 1

    if s == buffer
      return success(advance(position))
    end
  end

  failure("reached end of input without finding #{s.inspect}")
end

#consume_control_charsObject



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/stupidedi/reader/token_reader.rb', line 83

def consume_control_chars
  position = 0

  while @input.defined_at?(position) and is_control?(@input.at(position))
    position += 1
  end

  if position.zero?
    success(self)
  else
    success(advance(position))
  end
end

#consume_prefix(s) ⇒ Either<TokenReader>

If ‘s` is a prefix of #input, then `s` is skipped and the remaining input is returned as a new `TokenReader` wrapped by `Either.success`. Otherwise, an Either::Failure is returned.

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/reader/token_reader.rb', line 56

def consume_prefix(s)
  return success(self) if s.empty?

  position = 0
  buffer   = ""

  while @input.defined_at?(position)
    character = @input.at(position)
    position += 1

    unless is_control?(character)
      # buffer << character
      buffer = buffer + character

      if s.length == buffer.length
        if s == buffer
          return success(advance(position))
        else
          return failure("found #{buffer.inspect} instead of #{s.inspect}")
        end
      end
    end
  end

  failure("reached end of input without finding #{s.inspect}")
end

#copy(changes = {}) ⇒ TokenReader

Returns:



30
31
32
33
34
35
# File 'lib/stupidedi/reader/token_reader.rb', line 30

def copy(changes = {})
  TokenReader.new \
    changes.fetch(:input, @input),
    changes.fetch(:separators, @separators),
    changes.fetch(:segment_dict, @segment_dict)
end

#empty?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/stupidedi/reader/token_reader.rb', line 47

def empty?
  @input.empty?
end

#pretty_print(q) ⇒ void

This method returns an undefined value.



405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/stupidedi/reader/token_reader.rb', line 405

def pretty_print(q)
  q.text("TokenReader")
  q.group(2, "(", ")") do
    q.breakable ""

    q.pp @input
    q.text ","
    q.breakable

    q.pp @separators
  end
end

#read_characterEither<Result<String>>

Returns a single character and the remaining input as a Result with a ‘value` of the character and a `remainder` of the reamining input as a new instance of Stupidedi::Reader::TokenReader. If #input has less than a single character, returns an Either::Failure

Returns:



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/stupidedi/reader/token_reader.rb', line 132

def read_character
  position = 0
  buffer   = ""

  while @input.defined_at?(position)
    character = @input.at(position)
    position += 1

    if is_control?(character)
      next
    end

    return result(character, advance(position))
  end

  failure("less than one character available")
end

#read_component_element(repeatable = false) ⇒ Either<Result<ComponentElementTok, TokenReader>>



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/stupidedi/reader/token_reader.rb', line 348

def read_component_element(repeatable = false)
  position = 0
  buffer   = ""

  while @input.defined_at?(position)
    character = @input.at(position)
    position += 1

    if is_control?(character)
      next
    end

    case character
    when @separators.element,
         @separators.segment,
         @separators.component
      # Don't consume the separator/terminator
      token = component(buffer, @input, @input.drop(position))
      return result(token, advance(position - 1))
    when @separators.repetition
      if repeatable
        # Don't consume the repetition separator
        token = component(buffer, @input, @input.drop(position))
        return result(token, advance(position - 1))
    # else
    #   # @todo: Read this as data but sound the alarms
      end
    end

    # buffer << character
    buffer = buffer + character
  end

  failure("reached end of input without finding a component data element")
end

#read_component_elements(repeatable = false) ⇒ Either<Result<Array<ComponentElementTok, TokenReader>>>



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/stupidedi/reader/token_reader.rb', line 221

def read_component_elements(repeatable = false)
  read_component_element(repeatable).flatmap do |component, aR|
    aR.read_delimiter.flatmap do |delim, bR|
      case delim
      when @separators.segment,
           @separators.element,
           @separators.repetition
        # This is the last component element within the composite element,
        # so make it into a singleton list and don't consume the delimiter
        result(component.cons, aR)
      when @separators.component
        rest = bR.read_component_elements(repeatable)
        rest.map{|es, _| component.cons(es) }
      end
    end
  end
end

#read_composite_element(repeatable = false) ⇒ Either<Result<CompositeElementTok, TokenReader>>



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/stupidedi/reader/token_reader.rb', line 385

def read_composite_element(repeatable = false)
  read_component_elements(repeatable).flatmap do |components, aR|
    token = composite(components, @input, aR.input)

    aR.read_delimiter.flatmap do |delim, bR|
      case delim
      when @separators.segment,
           @separators.element
        token = token.repeated if repeatable
        result(token, aR)
      when @separators.repetition
        bR.read_composite_element(repeatable).map do |c, cR|
          c.repeated(token)
        end
      end
    end
  end
end

#read_delimiterEither<Result<Character, TokenReader>>

Returns:



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/stupidedi/reader/token_reader.rb', line 285

def read_delimiter
  position = 0

  while @input.defined_at?(position)
    character = @input.at(position)
    position += 1

    if is_control?(character)
      next
    end

    if is_delimiter?(character)
      return result(character, advance(position))
    else
      return failure("found #{character.inspect} instead of a delimiter")
    end
  end

  failure("reached end of input without finding a delimiter")
end

#read_elements(segment_id, element_uses) ⇒ Either<Result<Array<SimpleElementTok, CompositeElementTok>, TokenReader>>



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
# File 'lib/stupidedi/reader/token_reader.rb', line 185

def read_elements(segment_id, element_uses)
  if element_uses.empty?
    read_simple_element
  else
    element_use = element_uses.head
    repeatable  = element_use.repeatable?

    if element_use.composite?
      read_composite_element(repeatable)
    else
      read_simple_element(repeatable)
    end
  end.flatmap do |element, aR|
    aR.read_delimiter.flatmap do |delim, bR|
      case delim
      when @separators.segment
        remainder =
          if segment_id == :IEA
            bR.stream
          else
            bR
          end

        # This is the last element before the segment terminator, make
        # it into a singleton list and _do_ consume the delimiter
        result(element.cons, remainder)
      when @separators.element
        # There is another element following the delimiter
        rest = bR.read_elements(segment_id, element_uses.tail)
        rest.map{|es, _| element.cons(es) }
      end
    end
  end
end

#read_segmentEither<Result<SegmentTok, TokenReader>>



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
181
182
# File 'lib/stupidedi/reader/token_reader.rb', line 151

def read_segment
  consume_control_chars.flatmap do |start|
    # We might start reading a segment at "\nNM1...", where the "\n" is on
    # line 5, but "NM1" is on line 6. So to ensure the segment position is
    # line 6, we start with consume_control_characters.
    start.read_segment_id.flatmap do |segment_id, aR|
      if @segment_dict.defined_at?(segment_id)
        element_uses = @segment_dict.at(segment_id).element_uses
      else
        element_uses = []
      end

      aR.read_delimiter.flatmap do |delim, bR|
        case delim
        when @separators.element
          rest = bR.read_elements(segment_id, element_uses)
          rest.map{|es, cR| segment(segment_id, start.input, cR.input, es) }
        when @separators.segment
          remainder =
            if segment_id == :IEA
              bR.stream
            else
              bR
            end

          # Consume the segment terminator
          result(segment(segment_id, start.input, bR.input), remainder)
        end
      end
    end
  end
end

#read_segment_idEither<Result<Symbol, TokenReader>>

Returns:



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
277
278
279
280
281
282
# File 'lib/stupidedi/reader/token_reader.rb', line 240

def read_segment_id
  position = 0
  buffer   = ""

  while true
    unless @input.defined_at?(position)
      return eof("reached end of input without finding a segment identifier")
    end

    character = @input.at(position)
    position += 1

    if is_delimiter?(character)
      break
    end

    unless is_control?(character)
      if buffer.length == 3
        break
      end

      # buffer << character
      buffer = buffer + character
    end
  end

  # We only arrive here if {character} is a delimiter, or if we read
  # three characters into {buffer} and an additional into {character}
  if buffer =~ SEGMENT_ID
    remainder = advance(position - 1)

    case character
    when @separators.segment,
         @separators.element
      # Don't consume the delimiter
      result(buffer.upcase.to_sym, remainder)
    else
      failure("found #{character.inspect} following segment identifier")
    end
  else
    failure("found #{(buffer + character).inspect} instead of segment identifier")
  end
end

#read_simple_element(repeatable = false) ⇒ Either<Result<SimpleElementToken, TokenReader>>

Returns:



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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/stupidedi/reader/token_reader.rb', line 307

def read_simple_element(repeatable = false)
  position = 0
  buffer   = ""

  while @input.defined_at?(position)
    character = @input.at(position)
    position += 1

    if is_control?(character)
      next
    end

    case character
    when @separators.segment,
         @separators.element
      # These delimiters mark the end of the element. We don't consume
      # the delimiter because the next reader can use the delimiter to
      # know which token to next expect.
      token = simple(buffer, @input, @input.drop(position))
      token = token.repeated if repeatable
      return result(token, advance(position - 1))
    when @separators.repetition
      if repeatable
        token = simple(buffer, @input, @input.drop(position))
        rest  = advance(position).read_simple_element(repeatable)
        return rest.map{|e, _| e.repeated(token) }
    # else
    #   # @todo: Read this as data but sound the alarms
      end
  # when @separators.component
  #   # @todo: Read this as data but sound the alarms
    end

    # buffer << character
    buffer = buffer + character
  end

  failure("reached end of input without finding a simple data element")
end

#streamStreamReader

Returns:



43
44
45
# File 'lib/stupidedi/reader/token_reader.rb', line 43

def stream
  StreamReader.new(@input)
end

#stream?Boolean

Returns false.

Returns:

  • (Boolean)

    false



38
39
40
# File 'lib/stupidedi/reader/token_reader.rb', line 38

def stream?
  false
end