Class: LexerKit::Runner

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#programObject (readonly)

Returns the value of attribute program.



12
13
14
# File 'lib/lexer_kit/runner.rb', line 12

def program
  @program
end

#streamObject (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

#advanceObject

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

Returns:

  • (Boolean)


24
# File 'lib/lexer_kit/runner.rb', line 24

def eof?     = @stream.eof?

#error?(id = nil) ⇒ Boolean

Error detection (uses constant, no @program needed)

Returns:

  • (Boolean)


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

#inputObject



28
# File 'lib/lexer_kit/runner.rb', line 28

def input    = @stream.input

#lenObject



27
# File 'lib/lexer_kit/runner.rb', line 27

def len      = @stream.len

#line_colObject

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_tokenObject

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.token_meta[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

#startObject



26
# File 'lib/lexer_kit/runner.rb', line 26

def start    = @stream.start

#textObject

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_idObject



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