Class: Rux::RuxLexer

Inherits:
Object
  • Object
show all
Defined in:
lib/rux/rux_lexer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_buffer, init_pos) ⇒ RuxLexer



44
45
46
47
48
49
# File 'lib/rux/rux_lexer.rb', line 44

def initialize(source_buffer, init_pos)
  @p = init_pos
  @source_buffer = source_buffer
  @source_pts = @source_buffer.source.unpack('U*')
  @generator = to_enum(:each_token)
end

Class Method Details

.parse_pattern(pattern) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rux/rux_lexer.rb', line 30

def parse_pattern(pattern)
  if pattern == "(space)"
    Lex::CharsetPattern.new([' ', "\r", "\n"])
  elsif pattern == "(default)"
    Lex::DefaultPattern.new
  elsif pattern.start_with?('[^')
    Lex::NegatedCharsetPattern.parse(pattern[2..-2])
  else
    Lex::CharsetPattern.parse(pattern[1..-2])
  end
end

.state_tableObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rux/rux_lexer.rb', line 7

def state_table
  @state_table ||= {}.tap do |table|
    state_table_data = CSV.read(state_table_path)
    input_patterns = state_table_data[0][1..-1]

    inputs = input_patterns.map do |pattern|
      parse_pattern(pattern)
    end

    state_table_data[1..-1].each do |row|
      next unless row[0]  # allows blank lines in csv

      state = Lex::State.parse(row[0], row[1..-1], inputs)
      table[state.name] = state
    end
  end
end

.state_table_pathObject



25
26
27
28
# File 'lib/rux/rux_lexer.rb', line 25

def state_table_path
  @state_table_path ||=
    ::File.expand_path(::File.join('.', 'lex', 'states.csv'), __dir__)
end

Instance Method Details

#advanceObject



51
52
53
# File 'lib/rux/rux_lexer.rb', line 51

def advance
  @generator.next
end

#next_lexer(pos) ⇒ Object



60
61
62
# File 'lib/rux/rux_lexer.rb', line 60

def next_lexer(pos)
  RubyLexer.new(@source_buffer, pos)
end

#reset_to(pos) ⇒ Object



55
56
57
58
# File 'lib/rux/rux_lexer.rb', line 55

def reset_to(pos)
  @p = pos
  @eof = false
end