[ruby/prism] Fix up Prism and Debug docs

https://github.com/ruby/prism/commit/c2b7724d91
This commit is contained in:
Kevin Newton 2023-10-30 13:49:32 -04:00
parent 73b6934cf2
commit f12617ec98
2 changed files with 41 additions and 1 deletions

View File

@ -1,10 +1,16 @@
# frozen_string_literal: true
# The Prism Ruby parser.
#
# "Parsing Ruby is suddenly manageable!"
# - You, hopefully
#
module Prism
# There are many files in prism that are templated to handle every node type,
# which means the files can end up being quite large. We autoload them to make
# our require speed faster since consuming libraries are unlikely to use all
# of these features.
autoload :BasicVisitor, "prism/visitor"
autoload :Compiler, "prism/compiler"
autoload :Debug, "prism/debug"
@ -23,10 +29,14 @@ module Prism
# Some of these constants are not meant to be exposed, so marking them as
# private here.
private_constant :Debug
private_constant :LexCompat
private_constant :LexRipper
# :call-seq:
# Prism::lex_compat(source, filepath = "") -> Array
#
# Returns an array of tokens that closely resembles that of the Ripper lexer.
# The only difference is that since we don't keep track of lexer state in the
# same way, it's going to always return the NONE state.
@ -34,6 +44,9 @@ module Prism
LexCompat.new(source, filepath).result
end
# :call-seq:
# Prism::lex_ripper(source) -> Array
#
# This lexes with the Ripper lex. It drops any space events but otherwise
# returns the same tokens. Raises SyntaxError if the syntax in source is
# invalid.
@ -41,6 +54,9 @@ module Prism
LexRipper.new(source).result
end
# :call-seq:
# Prism::load(source, serialized) -> ParseResult
#
# Load the serialized AST using the source as a reference into a tree.
def self.load(source, serialized)
Serialize.load(source, serialized)

View File

@ -4,7 +4,9 @@ module Prism
# This module is used for testing and debugging and is not meant to be used by
# consumers of this library.
module Debug
class ISeq
# A wrapper around a RubyVM::InstructionSequence that provides a more
# convenient interface for accessing parts of the iseq.
class ISeq # :nodoc:
attr_reader :parts
def initialize(parts)
@ -42,6 +44,11 @@ module Prism
end
end
private_constant :ISeq
# :call-seq:
# Debug::cruby_locals(source) -> Array
#
# For the given source, compiles with CRuby and returns a list of all of the
# sets of local variables that were encountered.
def self.cruby_locals(source)
@ -76,8 +83,16 @@ module Prism
end
end
# Used to hold the place of a local that will be in the local table but
# cannot be accessed directly from the source code. For example, the
# iteration variable in a for loop or the positional parameter on a method
# definition that is destructured.
AnonymousLocal = Object.new
private_constant :AnonymousLocal
# :call-seq:
# Debug::prism_locals(source) -> Array
#
# For the given source, parses with prism and returns a list of all of the
# sets of local variables that were encountered.
def self.prism_locals(source)
@ -164,10 +179,19 @@ module Prism
locals
end
# :call-seq:
# Debug::newlines(source) -> Array
#
# For the given source string, return the byte offsets of every newline in
# the source.
def self.newlines(source)
Prism.parse(source).source.offsets
end
# :call-seq:
# Debug::parse_serialize_file(filepath) -> dumped
#
# For the given file, parse the AST and dump it to a string.
def self.parse_serialize_file(filepath)
parse_serialize_file_metadata(filepath, [filepath.bytesize, filepath.b, 0].pack("LA*L"))
end