Debian Codegraph @

lib/prism.rb

raw symbols SCIP
1 # frozen_string_literal: true
2 # :markup: markdown
3
4 # The Prism Ruby parser.
5 #
6 # "Parsing Ruby is suddenly manageable!"
7 # - You, hopefully
8 #
9 module Prism
10 # There are many files in prism that are templated to handle every node type,
11 # which means the files can end up being quite large. We autoload them to make
12 # our require speed faster since consuming libraries are unlikely to use all
13 # of these features.
14
15 autoload :BasicVisitor, "prism/visitor"
16 autoload :Compiler, "prism/compiler"
17 autoload :DesugarCompiler, "prism/desugar_compiler"
18 autoload :Dispatcher, "prism/dispatcher"
19 autoload :DotVisitor, "prism/dot_visitor"
20 autoload :DSL, "prism/dsl"
21 autoload :InspectVisitor, "prism/inspect_visitor"
22 autoload :LexCompat, "prism/lex_compat"
23 autoload :MutationCompiler, "prism/mutation_compiler"
24 autoload :Pack, "prism/pack"
25 autoload :Pattern, "prism/pattern"
26 autoload :Reflection, "prism/reflection"
27 autoload :Relocation, "prism/relocation"
28 autoload :Serialize, "prism/serialize"
29 autoload :StringQuery, "prism/string_query"
30 autoload :Translation, "prism/translation"
31 autoload :Visitor, "prism/visitor"
32
33 # Some of these constants are not meant to be exposed, so marking them as
34 # private here.
35
36 private_constant :LexCompat
37
38 # Raised when requested to parse as the currently running Ruby version but Prism has no support for it.
39 class CurrentVersionError < ArgumentError
40 # Initialize a new exception for the given ruby version string.
41 def initialize(version)
42 message = +"invalid version: Requested to parse as `version: 'current'`; "
43 segments =
44 if version.match?(/\A\d+\.\d+.\d+\z/)
45 version.split(".").map(&:to_i)
46 end
47
48 if segments && ((segments[0] < 3) || (segments[0] == 3 && segments[1] < 3))
49 message << " #{version} is below the minimum supported syntax."
50 else
51 message << " #{version} is unknown. Please update the `prism` gem."
52 end
53
54 super(message)
55 end
56 end
57
58 # :call-seq:
59 # Prism::lex_compat(source, **options) -> LexCompat::Result
60 #
61 # Returns a parse result whose value is an array of tokens that closely
62 # resembles the return value of Ripper::lex.
63 #
64 # For supported options, see Prism::parse.
65 def self.lex_compat(source, **options)
66 LexCompat.new(source, **options).result # steep:ignore
67 end
68
69 # :call-seq:
70 # Prism::load(source, serialized, freeze) -> ParseResult
71 #
72 # Load the serialized AST using the source as a reference into a tree.
73 def self.load(source, serialized, freeze = false)
74 Serialize.load_parse(source, serialized, freeze)
75 end
76 end
77
78 require_relative "prism/polyfill/byteindex"
79 require_relative "prism/polyfill/warn"
80 require_relative "prism/node"
81 require_relative "prism/node_ext"
82 require_relative "prism/parse_result"
83
84 # This is a Ruby implementation of the prism parser. If we're running on CRuby
85 # and we haven't explicitly set the PRISM_FFI_BACKEND environment variable, then
86 # it's going to require the built library. Otherwise, it's going to require a
87 # module that uses FFI to call into the library.
88 if RUBY_ENGINE == "ruby" and !ENV["PRISM_FFI_BACKEND"]
89 # The C extension is the default backend on CRuby.
90 Prism::BACKEND = :CEXT
91
92 require "prism/prism"
93 else
94 # The FFI backend is used on other Ruby implementations.
95 Prism::BACKEND = :FFI
96
97 require_relative "prism/ffi"
98 end