2023-06-20 11:53:02 -04:00
|
|
|
# frozen_string_literal: true
|
2025-05-28 19:25:48 +09:00
|
|
|
# :markup: markdown
|
2023-06-20 11:53:02 -04:00
|
|
|
|
2023-10-30 13:49:32 -04:00
|
|
|
# The Prism Ruby parser.
|
|
|
|
#
|
|
|
|
# "Parsing Ruby is suddenly manageable!"
|
|
|
|
# - You, hopefully
|
|
|
|
#
|
2023-09-27 12:24:48 -04:00
|
|
|
module Prism
|
|
|
|
# There are many files in prism that are templated to handle every node type,
|
2023-09-22 10:32:34 -04:00
|
|
|
# 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.
|
2023-10-30 13:49:32 -04:00
|
|
|
|
2023-09-27 12:24:48 -04:00
|
|
|
autoload :BasicVisitor, "prism/visitor"
|
|
|
|
autoload :Compiler, "prism/compiler"
|
|
|
|
autoload :DesugarCompiler, "prism/desugar_compiler"
|
|
|
|
autoload :Dispatcher, "prism/dispatcher"
|
2023-11-03 13:36:28 -04:00
|
|
|
autoload :DotVisitor, "prism/dot_visitor"
|
2023-09-27 12:24:48 -04:00
|
|
|
autoload :DSL, "prism/dsl"
|
2024-04-24 11:58:24 -04:00
|
|
|
autoload :InspectVisitor, "prism/inspect_visitor"
|
2023-09-27 12:24:48 -04:00
|
|
|
autoload :LexCompat, "prism/lex_compat"
|
|
|
|
autoload :LexRipper, "prism/lex_compat"
|
|
|
|
autoload :MutationCompiler, "prism/mutation_compiler"
|
|
|
|
autoload :Pack, "prism/pack"
|
|
|
|
autoload :Pattern, "prism/pattern"
|
2024-04-17 11:28:52 -04:00
|
|
|
autoload :Reflection, "prism/reflection"
|
2024-10-15 08:37:34 -04:00
|
|
|
autoload :Relocation, "prism/relocation"
|
2023-09-27 12:24:48 -04:00
|
|
|
autoload :Serialize, "prism/serialize"
|
2024-10-11 14:43:23 -04:00
|
|
|
autoload :StringQuery, "prism/string_query"
|
2024-01-10 11:04:39 -05:00
|
|
|
autoload :Translation, "prism/translation"
|
2023-09-27 12:24:48 -04:00
|
|
|
autoload :Visitor, "prism/visitor"
|
2023-09-22 10:50:08 -04:00
|
|
|
|
2023-09-22 11:47:32 -04:00
|
|
|
# Some of these constants are not meant to be exposed, so marking them as
|
|
|
|
# private here.
|
2023-10-30 13:49:32 -04:00
|
|
|
|
2023-09-22 11:47:32 -04:00
|
|
|
private_constant :LexCompat
|
|
|
|
private_constant :LexRipper
|
|
|
|
|
2023-10-30 13:49:32 -04:00
|
|
|
# :call-seq:
|
2024-04-19 14:53:39 -04:00
|
|
|
# Prism::lex_compat(source, **options) -> LexCompat::Result
|
2023-10-30 13:49:32 -04:00
|
|
|
#
|
2023-12-04 12:15:13 -05:00
|
|
|
# Returns a parse result whose value is an array of tokens that closely
|
|
|
|
# resembles the return value of Ripper::lex. The main difference is that the
|
|
|
|
# `:on_sp` token is not emitted.
|
2023-11-02 14:01:20 -04:00
|
|
|
#
|
|
|
|
# For supported options, see Prism::parse.
|
|
|
|
def self.lex_compat(source, **options)
|
2024-02-20 13:48:31 -08:00
|
|
|
LexCompat.new(source, **options).result # steep:ignore
|
2023-09-22 11:47:32 -04:00
|
|
|
end
|
|
|
|
|
2023-10-30 13:49:32 -04:00
|
|
|
# :call-seq:
|
|
|
|
# Prism::lex_ripper(source) -> Array
|
|
|
|
#
|
2023-09-22 11:47:32 -04:00
|
|
|
# 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)
|
2024-02-20 13:48:31 -08:00
|
|
|
LexRipper.new(source).result # steep:ignore
|
2023-09-22 11:47:32 -04:00
|
|
|
end
|
2023-09-22 11:40:14 -04:00
|
|
|
|
2023-10-30 13:49:32 -04:00
|
|
|
# :call-seq:
|
2025-01-11 21:10:09 -05:00
|
|
|
# Prism::load(source, serialized, freeze) -> ParseResult
|
2023-10-30 13:49:32 -04:00
|
|
|
#
|
2023-09-22 10:50:08 -04:00
|
|
|
# Load the serialized AST using the source as a reference into a tree.
|
2025-01-11 21:10:09 -05:00
|
|
|
def self.load(source, serialized, freeze = false)
|
2025-01-12 20:06:47 -05:00
|
|
|
Serialize.load_parse(source, serialized, freeze)
|
2023-09-22 10:50:08 -04:00
|
|
|
end
|
2023-06-20 11:53:02 -04:00
|
|
|
end
|
|
|
|
|
2024-05-03 14:58:40 -04:00
|
|
|
require_relative "prism/polyfill/byteindex"
|
2025-03-19 14:54:12 -04:00
|
|
|
require_relative "prism/polyfill/warn"
|
2023-09-27 12:24:48 -04:00
|
|
|
require_relative "prism/node"
|
|
|
|
require_relative "prism/node_ext"
|
|
|
|
require_relative "prism/parse_result"
|
2023-09-08 14:27:17 -04:00
|
|
|
|
2023-09-27 12:24:48 -04:00
|
|
|
# This is a Ruby implementation of the prism parser. If we're running on CRuby
|
|
|
|
# and we haven't explicitly set the PRISM_FFI_BACKEND environment variable, then
|
2023-09-22 10:38:48 -04:00
|
|
|
# it's going to require the built library. Otherwise, it's going to require a
|
|
|
|
# module that uses FFI to call into the library.
|
2023-09-27 12:24:48 -04:00
|
|
|
if RUBY_ENGINE == "ruby" and !ENV["PRISM_FFI_BACKEND"]
|
2024-02-29 17:38:16 +01:00
|
|
|
# The C extension is the default backend on CRuby.
|
2024-02-28 15:48:17 -05:00
|
|
|
Prism::BACKEND = :CEXT
|
|
|
|
|
2024-10-11 14:43:23 -04:00
|
|
|
require "prism/prism"
|
|
|
|
else
|
2024-02-29 17:38:16 +01:00
|
|
|
# The FFI backend is used on other Ruby implementations.
|
2024-02-28 15:48:17 -05:00
|
|
|
Prism::BACKEND = :FFI
|
2024-10-11 14:43:23 -04:00
|
|
|
|
|
|
|
require_relative "prism/ffi"
|
2023-08-15 10:00:54 -07:00
|
|
|
end
|