类 SyntaxSuggest::LexAll

Ripper.lex 不能保证解析整个源代码文档

此类保证通过迭代解析 Ripper 停止的地方来解析整个文档。

Prism 可能没有相同的问题。一旦移除 Ripper 支持,我们就可以减少这里的复杂性,甚至可以删除整个概念。

示例用法

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

公共类方法

lex(source, line_number) 点击切换源代码
# File lib/syntax_suggest/lex_all.rb, line 44
def self.lex(source, line_number)
  Prism.lex_compat(source, line: line_number).value.sort_by { |values| values[0] }
end
new(source:, source_lines: nil) 点击切换源代码
# File lib/syntax_suggest/lex_all.rb, line 21
def initialize(source:, source_lines: nil)
  @lex = self.class.lex(source, 1)
  lineno = @lex.last[0][0] + 1
  source_lines ||= source.lines
  last_lineno = source_lines.length

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

    @lex.concat(
      self.class.lex(lines.join, lineno + 1)
    )

    lineno = @lex.last[0].first + 1
  end

  last_lex = nil
  @lex.map! { |elem|
    last_lex = LexValue.new(elem[0].first, elem[1], elem[2], elem[3], last_lex)
  }
end

公共实例方法

[](index) 点击切换源代码
# File lib/syntax_suggest/lex_all.rb, line 64
def [](index)
  @lex[index]
end
each() { |x| ... } 点击切换源代码
# File lib/syntax_suggest/lex_all.rb, line 57
def each
  return @lex.each unless block_given?
  @lex.each do |x|
    yield x
  end
end
last() 点击切换源代码
# File lib/syntax_suggest/lex_all.rb, line 68
def last
  @lex.last
end
to_a() 点击切换源代码
# File lib/syntax_suggest/lex_all.rb, line 53
def to_a
  @lex
end