Class: Ibex::Runtime::LexerInput
- Inherits:
-
Object
- Object
- Ibex::Runtime::LexerInput
- Defined in:
- lib/ibex/runtime/lexer_input.rb
Overview
Incremental String, IO, or Fiber input used by generated lexers.
Constant Summary collapse
- DEFAULT_CHUNK_SIZE =
: Integer
16 * 1024
Instance Attribute Summary collapse
-
#buffer ⇒ Object
readonly
: String.
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.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/ibex/runtime/lexer_input.rb', line 13 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)
: String
10 11 12 |
# File 'lib/ibex/runtime/lexer_input.rb', line 10 def buffer @buffer end |
Instance Method Details
#consume(prefix) ⇒ Object
46 47 48 49 50 |
# File 'lib/ibex/runtime/lexer_input.rb', line 46 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
31 |
# File 'lib/ibex/runtime/lexer_input.rb', line 31 def eof? = @eof |
#read_more? ⇒ Boolean
Read until at least one byte is added or EOF is observed.
39 40 41 42 43 |
# File 'lib/ibex/runtime/lexer_input.rb', line 39 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.
35 |
# File 'lib/ibex/runtime/lexer_input.rb', line 35 def source_bytes = @archive.dup |
#source_line(line) ⇒ Object
Return one source line, reading ahead only to its line boundary.
54 55 56 57 58 59 |
# File 'lib/ibex/runtime/lexer_input.rb', line 54 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 |