Improve support for debugging a list of subtypes.
# Some background about my use case.

I need to parse a type consisting of multiple subtypes of variable length each and once in a while it happens that some length byte of the subtypes is wrong because of some flipped bit or whatever. In such cases I put the problematic telegrams in some file and open it using the Ruby visualizer to have a closer look which subtype actually is the problem and why. While parsing often ends in the error message `attempted to read X bytes, got only Y`, the visualizer helps a lot, because it still makes the data up until the point the error occurred visible, which makes identifying the broken bits easier.

At least that's how I thought things work, but after changing my parser a bit they don't anymore and I would like to document my findings here and maybe one has a brilliant idea on how to better support my use case.

# What has changed.

While my outer type is pretty easy, the problem already is that a list of subtypes is expected:

    # fmt_recs_clt:
    seq:
      - id:     records
        type:   fmt_rec_clt
        repeat: eos

That leads to the following code:

	  def _read
		@records = []
		i = 0
		while not @_io.eof?
		  (@_debug['records'][:arr] ||= [])[@records.size] = {:start => @_io.pos}
		  _t_records = FmtRecClt.new(@_io)
		  _t_records._read
		  @records << _t_records
		  i += 1
		end
		self
	  end

`_t_records._read` is the problem currently, because parsing the subtype is handled there completely and the exception occurs there as well. If that happens, `_t_records` is not added to the list of records and therefore not visible in the visualizer at all.

The former version of my parser handled things a little different, in that case parsing did not happen entirely within `_read`, but in and additional parsing instance instead. Therefore `_read` always succeeded, the record got added to the list of records and the exception occurred when showing the details of the parsing instance in the visualizer. While an error message has been presented as well, the underlying subtype has already published some/most of its data, the instance is always available to the visualizer and caches created types, which results in that the instance can be opened again in the visualizer.

Have a look at the following example:

	  def oms_rec
		return @oms_rec unless @oms_rec.nil?
		io = oms_rec_pre_proc._io
		_pos = io.pos
		io.seek(0)
		@oms_rec = FmtRecOms.new(io)
		@oms_rec._read
		io.seek(_pos)
		@oms_rec
	  end

During the first call, the type is created and cached in the parent, which leads during the second invocation of the instance to not run into the parsing exception anymore. Instead, the type gets shown to whichever level of data has been successfully parsed before, which is pretty much in my use cases.

# Some examples using screenshots.

The following screenshots are from the newer version of the parser:

![Clipboard01](https://user-images.githubusercontent.com/6223655/55019883-05d67280-4ff6-11e9-9f2e-1d8572861b40.png)
![Clipboard02](https://user-images.githubusercontent.com/6223655/55019891-08d16300-4ff6-11e9-90ed-f39f30d21bf6.png)

The following screenshots are from the older version. As you can see, the exception happens as well, but I can navigate down to `vdbs` which tells me that the last VDB in the telegram contains the wrong length most likely.

![Clipboard03](https://user-images.githubusercontent.com/6223655/55020031-50f08580-4ff6-11e9-9e9a-2dc534f99a39.png)
![Clipboard04](https://user-images.githubusercontent.com/6223655/55020043-5352df80-4ff6-11e9-97f8-44e8c506e3b4.png)

# Why things have changed.

The current implementation of the parser is the more correct one. Using instances in the past was a workaround because I needed [custom processing routines](https://doc.kaitai.io/user_guide.html#custom-process) when they were not available yet. So I created an [opaque type](https://doc.kaitai.io/user_guide.html#opaque-types) to implement some special pre-processing, but recently changed that to use custom processing and get rid of the additional instances.

# Current workaround.

To get back better debugging support like in the past, I need to use instances again to get parsing out of `_read`. But that's pretty ugly currently, because position handling within the stream is somewhat difficult with instances and multiple types of different lengths within one stream and such. It doesn't seem easy to store the current parsing position during `seq`, so I needed to copy raw data into some dummy type and use the associated `_io` of that type to base my processing on.

	seq:
	  - id:   length
		type: u1

	  - id:   oms_rec_raw_wo_pre_proc
		size: oms_rec_length
		type: fmt_rec_oms_raw_wo_pre_proc

	[...]

	instances:
	  oms_rec:
		io:       oms_rec_raw_wo_pre_proc._io
		pos:      0
		size:     oms_rec_length
		type:     fmt_rec_oms
		process:  de.am_soft.sm_mtg.format.parser.oms.pre_proc.fmt_opp_dispatcher

# How to improve the visualizer?

Parsing is handled by whatever gets generated using KSC in the end, so not sure what the visualizer can do here. Would be great if it would be able to somehow store the partially parsed type. KSC already distinguishes debug-code vs. non, maybe that could be used to change parsing as well:

    while not @_io.eof?
      (@_debug['records'][:arr] ||= [])[@records.size] = {:start => @_io.pos}
      _t_records = FmtRecClt.new(@_io)
      begin
        _t_records._read
      rescue ...
      end
      @records << _t_records
      @_debug['records'][:arr][@records.size - 1][:end] = @_io.pos
      i += 1
    end

So that the exception during parsing is ignored and the partially parsed type added to the list. Of course, depending on the actual KSY that might break the next types in the stream and all such. Maybe someone has a better idea, but as I see the visualizer, it could focus on handle things like broken data.