Skip to content

Commit

Permalink
Fix up Prism and Debug docs
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Nov 1, 2023
1 parent cfd4431 commit c2b7724
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
16 changes: 16 additions & 0 deletions lib/prism.rb
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -23,24 +29,34 @@ 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.
def self.lex_compat(source, filepath = "")
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.
def self.lex_ripper(source)
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)
Expand Down
26 changes: 25 additions & 1 deletion lib/prism/debug.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -42,6 +44,11 @@ def each_child
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)
Expand Down Expand Up @@ -76,8 +83,16 @@ def self.cruby_locals(source)
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)
Expand Down Expand Up @@ -164,10 +179,19 @@ def self.prism_locals(source)
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
Expand Down

0 comments on commit c2b7724

Please sign in to comment.