123456789_123456789_123456789_123456789_123456789_

Class: Prism::Translation::RubyParser

Relationships & Source Files
Namespace Children
Classes:
Inherits: Object
Defined in: lib/prism/translation/ruby_parser.rb

Overview

This module is the entry-point for converting a prism syntax tree into the seattlerb/ruby_parser gem's syntax tree.

Class Method Summary

Instance Attribute Summary

  • #scopes readonly

    Optional scopes to pass to the parser.

Instance Method Summary

Constructor Details

.new(scopes: nil) ⇒ RubyParser

This method is for internal use only.

: (?scopes: Array[Array[Symbol]]?) -> void

[ GitHub ]

  
# File 'lib/prism/translation/ruby_parser.rb', line 1632

def initialize(scopes: nil)
  super()
  @scopes = scopes
end

Class Method Details

.parse(source, filepath = "(string)", scopes: nil)

Parse the given source and translate it into the seattlerb/ruby_parser gem's Sexp format.

[ GitHub ]

  
# File 'lib/prism/translation/ruby_parser.rb', line 1660

def parse(source, filepath = "(string)", scopes: nil)
  new(scopes: scopes).parse(source, filepath)
end

.parse_file(filepath, scopes: nil)

Parse the given file and translate it into the seattlerb/ruby_parser gem's Sexp format.

[ GitHub ]

  
# File 'lib/prism/translation/ruby_parser.rb', line 1666

def parse_file(filepath, scopes: nil)
  new(scopes: scopes).parse_file(filepath)
end

Instance Attribute Details

#scopes (readonly)

Optional scopes to pass to the parser.

[ GitHub ]

  
# File 'lib/prism/translation/ruby_parser.rb', line 1628

attr_reader :scopes #: Array[Array[Symbol]]?

Instance Method Details

#parse(source, filepath = "(string)")

Parse the given source and translate it into the seattlerb/ruby_parser gem's Sexp format.

[ GitHub ]

  
# File 'lib/prism/translation/ruby_parser.rb', line 1639

def parse(source, filepath = "(string)")
  translate(Prism.parse(source, filepath: filepath, partial_script: true, scopes: scopes), filepath)
end

#parse_file(filepath)

Parse the given file and translate it into the seattlerb/ruby_parser gem's Sexp format.

[ GitHub ]

  
# File 'lib/prism/translation/ruby_parser.rb', line 1645

def parse_file(filepath)
  translate(Prism.parse_file(filepath, partial_script: true, scopes: scopes), filepath)
end

#process(ruby, file = "(string)", timeout = nil)

Parse the give file and translate it into the seattlerb/ruby_parser gem's Sexp format. This method is provided for API compatibility to RubyParser and takes an optional timeout argument.

[ GitHub ]

  
# File 'lib/prism/translation/ruby_parser.rb', line 1653

def process(ruby, file = "(string)", timeout = nil)
  Timeout.timeout(timeout) { parse(ruby, file) }
end

#translate(result, filepath) (private)

Translate the given parse result and filepath into the seattlerb/ruby_parser gem's Sexp format.

[ GitHub ]

  
# File 'lib/prism/translation/ruby_parser.rb', line 1675

def translate(result, filepath)
  if result.failure?
    error = result.errors.first
    raise ::RubyParser::SyntaxError, "#{filepath}:#{error.location.start_line} :: #{error.message}"
  end

  result.attach_comments!
  result.value.accept(self.class::Compiler.new(filepath))
end