Class: DeadEnd::LexAll

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/dead_end/lex_all.rb

Overview

Ripper.lex is not guaranteed to lex the entire source document

lex = LexAll.new(source: source) lex.each do |value|

puts value.line

end

Instance Method Summary collapse

Constructor Details

#initialize(source:, source_lines: nil) ⇒ LexAll

Returns a new instance of LexAll.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dead_end/lex_all.rb', line 13

def initialize(source:, source_lines: nil)
  @lex = Ripper::Lexer.new(source, "-", 1).parse.sort_by(&:pos)
  lineno = @lex.last.pos.first + 1
  source_lines ||= source.lines
  last_lineno = source_lines.length

  until lineno >= last_lineno
    lines = source_lines[lineno..-1]

    @lex.concat(
      Ripper::Lexer.new(lines.join, "-", lineno + 1).parse.sort_by(&:pos)
    )
    lineno = @lex.last.pos.first + 1
  end

  last_lex = nil
  @lex.map! { |elem|
    last_lex = LexValue.new(elem.pos.first, elem.event, elem.tok, elem.state, last_lex)
  }
end

Instance Method Details

#[](index) ⇒ Object



45
46
47
# File 'lib/dead_end/lex_all.rb', line 45

def [](index)
  @lex[index]
end

#eachObject



38
39
40
41
42
43
# File 'lib/dead_end/lex_all.rb', line 38

def each
  return @lex.each unless block_given?
  @lex.each do |x|
    yield x
  end
end

#lastObject



49
50
51
# File 'lib/dead_end/lex_all.rb', line 49

def last
  @lex.last
end

#to_aObject



34
35
36
# File 'lib/dead_end/lex_all.rb', line 34

def to_a
  @lex
end