Class: Stupidedi::Reader::TokenReader

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

Constant Summary

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



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

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

Instance Attribute Details

#inputString, Input (readonly)

Returns:



12
13
14
# File 'lib/stupidedi/reader/token_reader.rb', line 12

def input
  @input
end

#segment_dictSegmentDict

Returns:



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

def segment_dict
  @segment_dict
end

#separatorsSeparators (readonly)

Returns:



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

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:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/stupidedi/reader/token_reader.rb', line 97

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_chars



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/stupidedi/reader/token_reader.rb', line 78

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:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/stupidedi/reader/token_reader.rb', line 52

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

      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:



26
27
28
29
30
31
# File 'lib/stupidedi/reader/token_reader.rb', line 26

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @input.empty?
end

#pretty_print(q)

This method returns an undefined value.



397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/stupidedi/reader/token_reader.rb', line 397

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:



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/stupidedi/reader/token_reader.rb', line 127

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



341
342
343
344
345
346
347
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
# File 'lib/stupidedi/reader/token_reader.rb', line 341

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
  end

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

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



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/stupidedi/reader/token_reader.rb', line 216

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



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/stupidedi/reader/token_reader.rb', line 377

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:



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/stupidedi/reader/token_reader.rb', line 279

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



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

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



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

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



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
273
274
275
276
# File 'lib/stupidedi/reader/token_reader.rb', line 235

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



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
332
333
334
335
336
337
338
# File 'lib/stupidedi/reader/token_reader.rb', line 301

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
  end

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

#streamStreamReader

Returns:



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

def stream
  StreamReader.new(@input)
end

#stream?Boolean

Returns false

Returns:

  • (Boolean)

    false



34
35
36
# File 'lib/stupidedi/reader/token_reader.rb', line 34

def stream?
  false
end