Class: RuboCop::AST::PrismParser Private
| Relationships & Source Files | |
| Inherits: | Object |
| Defined in: | lib/rubocop/ast/prism_parser.rb |
Overview
Parses a source with Prism and builds the AST, the comments, the tokens
and the diagnostics from the result, without Prism::Translation::Parser
and without the parser classes of the parser gem. What it returns is
meant to be the same as what the translation layer returns. Needs Prism 2.
Constant Summary
-
Diagnostic =
private
# File 'lib/rubocop/ast/prism_parser.rb', line 23::Parser::Diagnostic
-
PRISM_VERSIONS =
private
# File 'lib/rubocop/ast/prism_parser.rb', line 24{ 3.3 => '3.3.1', 3.4 => '3.4.0', 3.5 => '4.0.0', 4.0 => '4.0.0', 4.1 => '4.1.0' }.freeze
Class Method Summary
- .new(ruby_version, prism: ::Prism) ⇒ PrismParser constructor Internal use only
Instance Attribute Summary
- #diagnostics readonly Internal use only
Instance Method Summary
- #tokenize_deferred(source_buffer) ⇒ Array Internal use only
- #build_ast(program) private Internal use only
- #build_comments(comments) private Internal use only
-
#build_offset_cache(source)
private
Internal use only
Prism counts in bytes,
parserin characters. - #deferred_tokens(tokens) private Internal use only
-
#error_diagnostic(error)
private
Internal use only
Naming/VariableNumber — one branch for each kind of error of Prism.
- #prism_options private Internal use only
- #process_diagnostics(result) private Internal use only
- #range(location) private Internal use only
- #warning_diagnostic(warning) private Internal use only
Constructor Details
.new(ruby_version, prism: ::Prism) ⇒ PrismParser
# File 'lib/rubocop/ast/prism_parser.rb', line 32
def initialize(ruby_version, prism: ::Prism) # Prism 1 has other names for some nodes and their locations, and its # tokens need lexer states to be converted. if ::Prism::VERSION.to_i < 2 raise ArgumentError, 'The `prism` parser engine needs Prism 2.0 or later. ' \ "The version of Prism in use is #{::Prism::VERSION}." end @prism_version = PRISM_VERSIONS.find { |version, _| version == ruby_version }&.last unless @prism_version raise ArgumentError, 'RuboCop supports target Ruby versions 3.3 and above with Prism. ' \ "Specified target Ruby version: #{ruby_version.inspect}" end @prism = prism @diagnostics = Diagnostic::Engine.new end
Instance Attribute Details
#diagnostics (readonly)
[ GitHub ]# File 'lib/rubocop/ast/prism_parser.rb', line 28
attr_reader :diagnostics
Instance Method Details
#build_ast(program) (private)
[ GitHub ]# File 'lib/rubocop/ast/prism_parser.rb', line 91
def build_ast(program) program.accept(PrismCompiler.new(@source_buffer, @offset_cache, diagnostics)) end
#build_comments(comments) (private)
[ GitHub ]#build_offset_cache(source) (private)
Prism counts in bytes, parser in characters.
# File 'lib/rubocop/ast/prism_parser.rb', line 73
def build_offset_cache(source) return ->(offset) { offset } if source.bytesize == source.length offset_cache = [] offset = 0 source.each_char do |char| char.bytesize.times { offset_cache << offset } offset += 1 end offset_cache << offset end
#deferred_tokens(tokens) (private)
[ GitHub ]# File 'lib/rubocop/ast/prism_parser.rb', line 99
def deferred_tokens(tokens) -> { PrismLexer.new(@source_buffer, tokens, @offset_cache).to_a } end
#error_diagnostic(error) (private)
Naming/VariableNumber — one branch for each kind of error of Prism
# File 'lib/rubocop/ast/prism_parser.rb', line 112
def error_diagnostic(error) location = error.location diagnostic_location = range(location) 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 = range(location) 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, :, {}, diagnostic_location, []) when :incomplete_variable_class, :incomplete_variable_class_3_3 location = location.copy(length: location.length + 1) diagnostic_location = range(location) 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 = range(location) 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 : Diagnostic.new(:error, :, { 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., :error, error.type, diagnostic_location) end end
#prism_options (private)
[ GitHub ]# File 'lib/rubocop/ast/prism_parser.rb', line 66
def = { filepath: @source_buffer.name, version: @prism_version, partial_script: true } [:encoding] = false if @source_buffer.source.encoding != Encoding::BINARY end
#process_diagnostics(result) (private)
[ GitHub ]# File 'lib/rubocop/ast/prism_parser.rb', line 103
def process_diagnostics(result) result.errors.each { |error| diagnostics.process(error_diagnostic(error)) } result.warnings.each do |warning| diagnostic = warning_diagnostic(warning) diagnostics.process(diagnostic) if diagnostic end end
#range(location) (private)
[ GitHub ]
#tokenize_deferred(source_buffer) ⇒ Array
# File 'lib/rubocop/ast/prism_parser.rb', line 51
def tokenize_deferred(source_buffer) @source_buffer = source_buffer source = source_buffer.source @offset_cache = build_offset_cache(source) result = @prism.parse_lex(source, **) process_diagnostics(result) program, tokens = result.value ast = build_ast(program) if result.success? [ast, build_comments(result.comments), deferred_tokens(tokens)] end
#warning_diagnostic(warning) (private)
[ GitHub ]# File 'lib/rubocop/ast/prism_parser.rb', line 184
def warning_diagnostic(warning) diagnostic_location = range(warning.location) 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 # PrismCompiler reports it, in the way `parser` does. else PrismDiagnostic.new(warning., :warning, warning.type, diagnostic_location) end end