Class: Ibex::Runtime::LexerInput
- Inherits:
-
Object
- Object
- Ibex::Runtime::LexerInput
- Defined in:
- lib/json5/generated_parser.rb
Overview
Incremental String, IO, or Fiber input used by generated lexers.
Constant Summary collapse
- DEFAULT_CHUNK_SIZE =
16 * 1024
Instance Attribute Summary collapse
- #buffer ⇒ Object readonly
Instance Method Summary collapse
- #consume(prefix) ⇒ Object
- #eof? ⇒ Boolean
-
#initialize(source, chunk_size: DEFAULT_CHUNK_SIZE) ⇒ LexerInput
constructor
A new instance of LexerInput.
-
#read_more? ⇒ Boolean
Read until at least one byte is added or EOF is observed.
-
#source_bytes ⇒ Object
Return every source byte read so far, including the unconsumed buffer.
-
#source_line(line) ⇒ Object
Return one source line, reading ahead only to its line boundary.
Constructor Details
#initialize(source, chunk_size: DEFAULT_CHUNK_SIZE) ⇒ LexerInput
Returns a new instance of LexerInput.
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
#buffer ⇒ Object (readonly)
8820 8821 8822 |
# File 'lib/json5/generated_parser.rb', line 8820 def buffer @buffer end |
Instance Method Details
#consume(prefix) ⇒ Object
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
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.
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_bytes ⇒ Object
Return every source byte read so far, including the unconsumed buffer.
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.
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 |