Class: Ibex::Runtime::LexerInput

Inherits:
Object
  • Object
show all
Defined in:
lib/json5/generated_parser.rb

Overview

Incremental String, IO, or Fiber input used by generated lexers.

Constant Summary collapse

DEFAULT_CHUNK_SIZE =

Signature:

  • Integer

16 * 1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, chunk_size: DEFAULT_CHUNK_SIZE) ⇒ LexerInput

Returns a new instance of LexerInput.

RBS:

  • (String | IO | Fiber source, ?chunk_size: Integer) -> void

Raises:

  • (ArgumentError)


8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
# File 'lib/json5/generated_parser.rb', line 8823

def initialize(source, chunk_size: DEFAULT_CHUNK_SIZE)
  raise ArgumentError, "lexer chunk_size must be positive" unless chunk_size.positive?
  unless source.is_a?(String) || source.respond_to?(:read) || source.is_a?(Fiber)
    raise ArgumentError, "lexer input must be a String, IO, or Fiber"
  end

  @source = source
  @chunk_size = chunk_size
  @buffer = +""
  @archive = +""
  @eof = false
  return unless source.is_a?(String)

  append(source)
  @eof = true
end

Instance Attribute Details

#bufferObject (readonly)

Signature:

  • String



8820
8821
8822
# File 'lib/json5/generated_parser.rb', line 8820

def buffer
  @buffer
end

Instance Method Details

#consume(prefix) ⇒ Object

RBS:

  • (String prefix) -> void

Raises:

  • (ArgumentError)


8856
8857
8858
8859
8860
# File 'lib/json5/generated_parser.rb', line 8856

def consume(prefix)
  raise ArgumentError, "lexer input prefix does not match the buffer" unless @buffer.start_with?(prefix)

  @buffer.slice!(0, prefix.length)
end

#eof?Boolean

RBS:

  • () -> bool

Returns:

  • (Boolean)


8841
# File 'lib/json5/generated_parser.rb', line 8841

def eof? = @eof

#read_more?Boolean

Read until at least one byte is added or EOF is observed.

RBS:

  • () -> bool

Returns:

  • (Boolean)


8849
8850
8851
8852
8853
# File 'lib/json5/generated_parser.rb', line 8849

def read_more?
  before = @buffer.bytesize
  read_chunk until @eof || @buffer.bytesize > before
  @buffer.bytesize > before
end

#source_bytesObject

Return every source byte read so far, including the unconsumed buffer.

RBS:

  • () -> String



8845
# File 'lib/json5/generated_parser.rb', line 8845

def source_bytes = @archive.dup

#source_line(line) ⇒ Object

Return one source line, reading ahead only to its line boundary.

RBS:

  • (Integer line) -> String

Raises:

  • (ArgumentError)


8864
8865
8866
8867
8868
8869
# File 'lib/json5/generated_parser.rb', line 8864

def source_line(line)
  raise ArgumentError, "source line must be positive" unless line.positive?

  read_more? while !@eof && @archive.count("\n") < line
  (@archive.lines[line - 1] || "").delete_suffix("\n").delete_suffix("\r")
end