Sha256: 2d60b13effd458d996d1904283bcf0e330ac4bfb44a4c7cefb69a5ba6297cba1

Contents?: true

Size: 1.65 KB

Versions: 14

Compression:

Stored size: 1.65 KB

Contents

require 'antlr4/runtime/char_stream'

module Antlr4::Runtime

  class CodePointCharStream < CharStream
    def initialize(position, remaining, name, byte_array)
      @size = remaining
      @name = name
      @position = position
      @byte_array = byte_array
    end

    def text(interval)
      start_idx = [interval.a, @size].min
      len = [interval.b - interval.a + 1, @size - start_idx].min

      # We know the maximum code point in byte_array is U+00FF,
      # so we can treat this as if it were ISO-8859-1, aka Latin-1,
      # which shares the same code points up to 0xFF.
      chars = @byte_array.slice(start_idx, len)
      result = ''
      chars.each do |c|
        result << c
      end
      result
    end

    def la(i)
      case Integer.signum(i)
      when -1
        offset = @position + i
        return IntStream::EOF if offset < 0

        return @byte_array[offset] & 0xFF
      when 0
        # Undefined
        return 0
      when 1
        offset = @position + i - 1
        return IntStream::EOF if offset >= @size

        return @byte_array[offset] & 0xFF
      else
        # type code here
      end
      raise UnsupportedOperationException, 'Not reached'
    end

    def internal_storage
      @byte_array
    end

    def consume
      raise IllegalStateException, 'cannot consume EOF' if (@size - @position).zero?

      @position += 1
    end

    def index
      @position
    end

    attr_reader :size

    def mark
      -1
    end

    def release(marker)
      ;
    end

    def seek(index)
      @position = index
    end

    def source_name
      return UNKNOWN_SOURCE_NAME if @name.nil? || @name.empty?

      @name
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
antlr4-runtime-0.2.12 lib/antlr4/runtime/code_point_char_stream.rb
antlr4-runtime-0.2.11 lib/antlr4/runtime/code_point_char_stream.rb
antlr4-runtime-0.2.10 lib/antlr4/runtime/code_point_char_stream.rb
antlr4-runtime-0.2.9 lib/antlr4/runtime/code_point_char_stream.rb
antlr4-runtime-0.2.8 lib/antlr4/runtime/code_point_char_stream.rb
antlr4-runtime-0.2.7 lib/antlr4/runtime/code_point_char_stream.rb
antlr4-runtime-0.2.6 lib/antlr4/runtime/code_point_char_stream.rb
antlr4-runtime-0.2.5 lib/antlr4/runtime/code_point_char_stream.rb
antlr4-runtime-0.2.4 lib/antlr4/runtime/code_point_char_stream.rb
antlr4-runtime-0.2.3 lib/antlr4/runtime/code_point_char_stream.rb
antlr4-runtime-0.2.2 lib/antlr4/runtime/code_point_char_stream.rb
antlr4-runtime-0.2.1 lib/antlr4/runtime/code_point_char_stream.rb
antlr4-runtime-0.2.0 lib/antlr4/runtime/code_point_char_stream.rb
antlr4-runtime-0.1.0 lib/antlr4/runtime/code_point_char_stream.rb