Class: LexerKit::Runner
- Inherits:
-
Object
- Object
- LexerKit::Runner
- Defined in:
- lib/lexer_kit/runner.rb
Overview
Runner coordinates program, stream, and source for token processing. It provides a high-level interface for lexing with full Token support.
Runner has the same interface as LexStream (duck-typing compatible) plus additional methods for Token creation.
Created via CompiledProgram#stream(input, filename:).
Instance Attribute Summary collapse
-
#program ⇒ Object
readonly
Returns the value of attribute program.
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
Instance Method Summary collapse
-
#advance ⇒ Object
Advance to next token, returns self for chaining.
-
#eof? ⇒ Boolean
Delegate basic stream operations.
-
#error?(id = nil) ⇒ Boolean
Error detection (uses constant, no @program needed).
-
#initialize(program, lex_stream, filename: nil) ⇒ Runner
constructor
private
Use CompiledProgram#stream instead.
- #input ⇒ Object
- #len ⇒ Object
-
#line_col ⇒ Object
Line/col for current position.
-
#make_token ⇒ Object
Create Token object (lazy Source creation).
- #peek_len(n = 1) ⇒ Object
- #peek_start(n = 1) ⇒ Object
- #peek_text(n = 1) ⇒ Object
-
#peek_token_id(n = 1) ⇒ Object
Peek operations (delegate to stream).
- #peek_token_name(n = 1) ⇒ Object
- #start ⇒ Object
-
#text ⇒ Object
Text extraction.
- #token_id ⇒ Object
-
#token_name(id = nil) ⇒ Object
Token name lookup (via program for correctness).
Constructor Details
#initialize(program, lex_stream, filename: nil) ⇒ Runner
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Use CompiledProgram#stream instead
16 17 18 19 20 21 |
# File 'lib/lexer_kit/runner.rb', line 16 def initialize(program, lex_stream, filename: nil) @program = program @stream = lex_stream @filename = filename @source = nil # lazy end |
Instance Attribute Details
#program ⇒ Object (readonly)
Returns the value of attribute program.
12 13 14 |
# File 'lib/lexer_kit/runner.rb', line 12 def program @program end |
#stream ⇒ Object (readonly)
Returns the value of attribute stream.
12 13 14 |
# File 'lib/lexer_kit/runner.rb', line 12 def stream @stream end |
Instance Method Details
#advance ⇒ Object
Advance to next token, returns self for chaining
31 32 33 34 |
# File 'lib/lexer_kit/runner.rb', line 31 def advance @stream.advance self end |
#eof? ⇒ Boolean
Delegate basic stream operations
24 |
# File 'lib/lexer_kit/runner.rb', line 24 def eof? = @stream.eof? |
#error?(id = nil) ⇒ Boolean
Error detection (uses constant, no @program needed)
56 57 58 59 60 61 |
# File 'lib/lexer_kit/runner.rb', line 56 def error?(id = nil) id ||= token_id return false if id.nil? || id < 0 id == LexerKit::INVALID_TOKEN_ID end |
#input ⇒ Object
28 |
# File 'lib/lexer_kit/runner.rb', line 28 def input = @stream.input |
#len ⇒ Object
27 |
# File 'lib/lexer_kit/runner.rb', line 27 def len = @stream.len |
#line_col ⇒ Object
Line/col for current position
84 85 86 87 88 89 90 91 92 |
# File 'lib/lexer_kit/runner.rb', line 84 def line_col return nil if eof? s = start return nil if s.nil? @source ||= Core::Source.new(input, filename: @filename) @source.line_col(s) end |
#make_token ⇒ Object
Create Token object (lazy Source creation)
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/lexer_kit/runner.rb', line 64 def make_token return nil if eof? tok_id = token_id s = start l = len return nil if tok_id.nil? || s.nil? || l.nil? @source ||= Core::Source.new(input, filename: @filename) Core::Token.new( id: tok_id, name: @program.token_name(tok_id), start: s, len: l, source: @source, meta: @program.[tok_id] ) end |
#peek_len(n = 1) ⇒ Object
97 |
# File 'lib/lexer_kit/runner.rb', line 97 def peek_len(n = 1) = @stream.peek_len(n) |
#peek_start(n = 1) ⇒ Object
96 |
# File 'lib/lexer_kit/runner.rb', line 96 def peek_start(n = 1) = @stream.peek_start(n) |
#peek_text(n = 1) ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/lexer_kit/runner.rb', line 106 def peek_text(n = 1) s = peek_start(n) l = peek_len(n) return nil if s.nil? || l.nil? input.byteslice(s, l) end |
#peek_token_id(n = 1) ⇒ Object
Peek operations (delegate to stream)
95 |
# File 'lib/lexer_kit/runner.rb', line 95 def peek_token_id(n = 1) = @stream.peek_token_id(n) |
#peek_token_name(n = 1) ⇒ Object
99 100 101 102 103 104 |
# File 'lib/lexer_kit/runner.rb', line 99 def peek_token_name(n = 1) id = peek_token_id(n) return nil if id.nil? || id < 0 @program.token_name(id) end |
#start ⇒ Object
26 |
# File 'lib/lexer_kit/runner.rb', line 26 def start = @stream.start |
#text ⇒ Object
Text extraction
45 46 47 48 49 50 51 52 53 |
# File 'lib/lexer_kit/runner.rb', line 45 def text return nil if eof? s = start l = len return nil if s.nil? || l.nil? input.byteslice(s, l) end |
#token_id ⇒ Object
25 |
# File 'lib/lexer_kit/runner.rb', line 25 def token_id = @stream.token_id |
#token_name(id = nil) ⇒ Object
Token name lookup (via program for correctness)
37 38 39 40 41 42 |
# File 'lib/lexer_kit/runner.rb', line 37 def token_name(id = nil) id ||= token_id return nil if id.nil? || id < 0 @program.token_name(id) end |