class Prism::Translation::Parser

这个类是将 prism 语法树转换为 whitequark/parser gem 语法树的入口点。它继承自 parser gem 的基础解析器,并覆盖了 parse* 方法,以便使用 prism 进行解析,然后进行转换。

公共实例方法

default_encoding() 点击切换源代码

Ruby 文件的默认编码是 UTF-8。

# File prism/translation/parser.rb, line 41
def default_encoding
  Encoding::UTF_8
end
parse(source_buffer) 点击切换源代码

解析源缓冲区并返回 AST。

# File prism/translation/parser.rb, line 49
def parse(source_buffer)
  @source_buffer = source_buffer
  source = source_buffer.source

  offset_cache = build_offset_cache(source)
  result = unwrap(Prism.parse(source, filepath: source_buffer.name, version: convert_for_prism(version), partial_script: true, encoding: false), offset_cache)

  build_ast(result.value, offset_cache)
ensure
  @source_buffer = nil
end
parse_with_comments(source_buffer) 点击切换源代码

解析源缓冲区并返回 AST 和源代码注释。

# File prism/translation/parser.rb, line 62
def parse_with_comments(source_buffer)
  @source_buffer = source_buffer
  source = source_buffer.source

  offset_cache = build_offset_cache(source)
  result = unwrap(Prism.parse(source, filepath: source_buffer.name, version: convert_for_prism(version), partial_script: true, encoding: false), offset_cache)

  [
    build_ast(result.value, offset_cache),
    build_comments(result.comments, offset_cache)
  ]
ensure
  @source_buffer = nil
end
tokenize(source_buffer, recover = false) 点击切换源代码

解析源缓冲区并返回 AST、源代码注释和词法分析器发出的标记。

# File prism/translation/parser.rb, line 79
def tokenize(source_buffer, recover = false)
  @source_buffer = source_buffer
  source = source_buffer.source

  offset_cache = build_offset_cache(source)
  result =
    begin
      unwrap(Prism.parse_lex(source, filepath: source_buffer.name, version: convert_for_prism(version), partial_script: true, encoding: false), offset_cache)
    rescue ::Parser::SyntaxError
      raise if !recover
    end

  program, tokens = result.value
  ast = build_ast(program, offset_cache) if result.success?

  [
    ast,
    build_comments(result.comments, offset_cache),
    build_tokens(tokens, offset_cache)
  ]
ensure
  @source_buffer = nil
end
try_declare_numparam(node) 点击切换源代码

由于 prism 为我们解析了数字参数,所以我们不需要在这里支持这种逻辑。

# File prism/translation/parser.rb, line 105
def try_declare_numparam(node)
  node.children[0].match?(/\A_[1-9]\z/)
end

私有实例方法

build_ast(program, offset_cache) 点击切换源代码

从 prism AST 构建 parser gem AST。

# File prism/translation/parser.rb, line 263
def build_ast(program, offset_cache)
  program.accept(Compiler.new(self, offset_cache))
end
build_comments(comments, offset_cache) 点击切换源代码

从 prism 注释构建 parser gem 注释。

# File prism/translation/parser.rb, line 268
def build_comments(comments, offset_cache)
  comments.map do |comment|
    ::Parser::Source::Comment.new(build_range(comment.location, offset_cache))
  end
end
build_offset_cache(source) 点击切换源代码

Prism 处理字节偏移量,而 parser gem 处理字符偏移量。为了构建 parser gem AST,我们需要处理这种转换。

如果源的字节大小与长度相同,那么我们可以直接使用偏移量。否则,我们构建一个数组,其中索引是字节偏移量,值是字符偏移量。

# File prism/translation/parser.rb, line 246
def build_offset_cache(source)
  if source.bytesize == source.length
    -> (offset) { offset }
  else
    offset_cache = []
    offset = 0

    source.each_char do |char|
      char.bytesize.times { offset_cache << offset }
      offset += 1
    end

    offset_cache << offset
  end
end
build_range(location, offset_cache) 点击切换源代码

从 prism 位置构建范围。

# File prism/translation/parser.rb, line 280
def build_range(location, offset_cache)
  ::Parser::Source::Range.new(
    source_buffer,
    offset_cache[location.start_offset],
    offset_cache[location.end_offset]
  )
end
build_tokens(tokens, offset_cache) 点击切换源代码

从 prism 标记构建 parser gem 标记。

# File prism/translation/parser.rb, line 275
def build_tokens(tokens, offset_cache)
  Lexer.new(source_buffer, tokens, offset_cache).to_a
end
convert_for_prism(version) 点击切换源代码

Parser 处理的版本格式转换为 Prism 处理的格式。

# File prism/translation/parser.rb, line 289
def convert_for_prism(version)
  case version
  when 33
    "3.3.1"
  when 34
    "3.4.0"
  else
    "latest"
  end
end
error_diagnostic(error, offset_cache) 点击切换源代码

从给定的 prism 解析错误构建诊断信息。

# File prism/translation/parser.rb, line 124
def error_diagnostic(error, offset_cache)
  location = error.location
  diagnostic_location = build_range(location, offset_cache)

  case error.type
  when :argument_block_multi
    Diagnostic.new(:error, :block_and_blockarg, {}, diagnostic_location, [])
  when :argument_formal_constant
    Diagnostic.new(:error, :argument_const, {}, diagnostic_location, [])
  when :argument_formal_class
    Diagnostic.new(:error, :argument_cvar, {}, diagnostic_location, [])
  when :argument_formal_global
    Diagnostic.new(:error, :argument_gvar, {}, diagnostic_location, [])
  when :argument_formal_ivar
    Diagnostic.new(:error, :argument_ivar, {}, diagnostic_location, [])
  when :argument_no_forwarding_amp
    Diagnostic.new(:error, :no_anonymous_blockarg, {}, diagnostic_location, [])
  when :argument_no_forwarding_star
    Diagnostic.new(:error, :no_anonymous_restarg, {}, diagnostic_location, [])
  when :argument_no_forwarding_star_star
    Diagnostic.new(:error, :no_anonymous_kwrestarg, {}, diagnostic_location, [])
  when :begin_lonely_else
    location = location.copy(length: 4)
    diagnostic_location = build_range(location, offset_cache)
    Diagnostic.new(:error, :useless_else, {}, diagnostic_location, [])
  when :class_name, :module_name
    Diagnostic.new(:error, :module_name_const, {}, diagnostic_location, [])
  when :class_in_method
    Diagnostic.new(:error, :class_in_def, {}, diagnostic_location, [])
  when :def_endless_setter
    Diagnostic.new(:error, :endless_setter, {}, diagnostic_location, [])
  when :embdoc_term
    Diagnostic.new(:error, :embedded_document, {}, diagnostic_location, [])
  when :incomplete_variable_class, :incomplete_variable_class_3_3
    location = location.copy(length: location.length + 1)
    diagnostic_location = build_range(location, offset_cache)

    Diagnostic.new(:error, :cvar_name, { name: location.slice }, diagnostic_location, [])
  when :incomplete_variable_instance, :incomplete_variable_instance_3_3
    location = location.copy(length: location.length + 1)
    diagnostic_location = build_range(location, offset_cache)

    Diagnostic.new(:error, :ivar_name, { name: location.slice }, diagnostic_location, [])
  when :invalid_variable_global, :invalid_variable_global_3_3
    Diagnostic.new(:error, :gvar_name, { name: location.slice }, diagnostic_location, [])
  when :module_in_method
    Diagnostic.new(:error, :module_in_def, {}, diagnostic_location, [])
  when :numbered_parameter_ordinary
    Diagnostic.new(:error, :ordinary_param_defined, {}, diagnostic_location, [])
  when :numbered_parameter_outer_scope
    Diagnostic.new(:error, :numparam_used_in_outer_scope, {}, diagnostic_location, [])
  when :parameter_circular
    Diagnostic.new(:error, :circular_argument_reference, { var_name: location.slice }, diagnostic_location, [])
  when :parameter_name_repeat
    Diagnostic.new(:error, :duplicate_argument, {}, diagnostic_location, [])
  when :parameter_numbered_reserved
    Diagnostic.new(:error, :reserved_for_numparam, { name: location.slice }, diagnostic_location, [])
  when :regexp_unknown_options
    Diagnostic.new(:error, :regexp_options, { options: location.slice[1..] }, diagnostic_location, [])
  when :singleton_for_literals
    Diagnostic.new(:error, :singleton_literal, {}, diagnostic_location, [])
  when :string_literal_eof
    Diagnostic.new(:error, :string_eof, {}, diagnostic_location, [])
  when :unexpected_token_ignore
    Diagnostic.new(:error, :unexpected_token, { token: location.slice }, diagnostic_location, [])
  when :write_target_in_method
    Diagnostic.new(:error, :dynamic_const, {}, diagnostic_location, [])
  else
    PrismDiagnostic.new(error.message, :error, error.type, diagnostic_location)
  end
end
unwrap(result, offset_cache) 点击切换源代码

如果在解析过程中生成了错误,则引发相应的语法错误。否则,返回结果。

# File prism/translation/parser.rb, line 224
def unwrap(result, offset_cache)
  result.errors.each do |error|
    next unless valid_error?(error)
    diagnostics.process(error_diagnostic(error, offset_cache))
  end

  result.warnings.each do |warning|
    next unless valid_warning?(warning)
    diagnostic = warning_diagnostic(warning, offset_cache)
    diagnostics.process(diagnostic) if diagnostic
  end

  result
end
valid_error?(error) 点击切换源代码

这是一个钩子,允许消费者禁用某些错误,如果他们不希望这些错误阻止创建语法树。

# File prism/translation/parser.rb, line 113
def valid_error?(error)
  true
end
valid_warning?(warning) 点击切换源代码

这是一个钩子,允许消费者禁用某些警告,如果他们不希望这些警告阻止创建语法树。

# File prism/translation/parser.rb, line 119
def valid_warning?(warning)
  true
end
warning_diagnostic(warning, offset_cache) 点击切换源代码

从给定的 prism 解析警告构建诊断信息。

# File prism/translation/parser.rb, line 197
def warning_diagnostic(warning, offset_cache)
  diagnostic_location = build_range(warning.location, offset_cache)

  case warning.type
  when :ambiguous_first_argument_plus
    Diagnostic.new(:warning, :ambiguous_prefix, { prefix: "+" }, diagnostic_location, [])
  when :ambiguous_first_argument_minus
    Diagnostic.new(:warning, :ambiguous_prefix, { prefix: "-" }, diagnostic_location, [])
  when :ambiguous_prefix_ampersand
    Diagnostic.new(:warning, :ambiguous_prefix, { prefix: "&" }, diagnostic_location, [])
  when :ambiguous_prefix_star
    Diagnostic.new(:warning, :ambiguous_prefix, { prefix: "*" }, diagnostic_location, [])
  when :ambiguous_prefix_star_star
    Diagnostic.new(:warning, :ambiguous_prefix, { prefix: "**" }, diagnostic_location, [])
  when :ambiguous_slash
    Diagnostic.new(:warning, :ambiguous_regexp, {}, diagnostic_location, [])
  when :dot_dot_dot_eol
    Diagnostic.new(:warning, :triple_dot_at_eol, {}, diagnostic_location, [])
  when :duplicated_hash_key
    # skip, parser does this on its own
  else
    PrismDiagnostic.new(warning.message, :warning, warning.type, diagnostic_location)
  end
end