類別 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