The Prism Ruby parser.

“Parsing Ruby is suddenly manageable!” - You, hopefully

Constants

The C extension is the default backend on CRuby.

The version of the prism library.

Class Methods

Dump the AST corresponding to the given string to a string. For supported options, see Prism.parse.

Dump the AST corresponding to the given file to a string. For supported options, see Prism.parse.

Return a LexResult instance that contains an array of Token instances corresponding to the given string. For supported options, see Prism.parse.

Returns a parse result whose value is an array of tokens that closely resembles the return value of Ripper.lex.

For supported options, see Prism.parse.

Return a LexResult instance that contains an array of Token instances corresponding to the given file. For supported options, see Prism.parse.

Load the serialized AST using the source as a reference into a tree.

Parse the given string and return a ParseResult instance. The options that are supported are:

  • command_line - either nil or a string of the various options that were set on the command line. Valid values are combinations of “a”, “l”, “n”, “p”, and “x”.

  • encoding - the encoding of the source being parsed. This should be an encoding or nil.

  • filepath - the filepath of the source being parsed. This should be a string or nil.

  • freeze - whether or not to deeply freeze the AST. This should be a boolean or nil.

  • frozen_string_literal - whether or not the frozen string literal pragma has been set. This should be a boolean or nil.

  • line - the line number that the parse starts on. This should be an integer or nil. Note that this is 1-indexed.

  • main_script - a boolean indicating whether or not the source being parsed is the main script being run by the interpreter. This controls whether or not shebangs are parsed for additional flags and whether or not the parser will attempt to find a matching shebang if the first one does not contain the word “ruby”.

  • partial_script - when the file being parsed is considered a “partial” script, jumps will not be marked as errors if they are not contained within loops/blocks. This is used in the case that you’re parsing a script that you know will be embedded inside another script later, but you do not have that context yet. For example, when parsing an ERB template that will be evaluated inside another script.

  • scopes - the locals that are in scope surrounding the code that is being parsed. This should be an array of arrays of symbols or nil. Scopes are ordered from the outermost scope to the innermost one.

  • version - the version of Ruby syntax that prism should used to parse Ruby code. By default prism assumes you want to parse with the latest version of Ruby syntax (which you can trigger with nil or "latest"). You may also restrict the syntax to a specific version of Ruby, e.g., with "3.3.0". To parse with the same syntax version that the current Ruby is running use version: "current". To parse with the nearest version to the current Ruby that is running, use version: "nearest". Raises ArgumentError if the version is not currently supported by Prism.

Parse the given string and return an array of Comment objects. For supported options, see Prism.parse.

Parse the given string and return true if it parses with errors. For supported options, see Prism.parse.

Parse the given file and return a ParseResult instance. For supported options, see Prism.parse.

Parse the given file and return an array of Comment objects. For supported options, see Prism.parse.

Parse the given file and return true if it parses with errors. For supported options, see Prism.parse.

Parse the given file and return true if it parses without errors. For supported options, see Prism.parse.

Parse the given string and return a ParseLexResult instance that contains a 2-element array, where the first element is the AST and the second element is an array of Token instances.

This API is only meant to be used in the case where you need both the AST and the tokens. If you only need one or the other, use either Prism.parse or Prism.lex.

For supported options, see Prism.parse.

Parse the given file and return a ParseLexResult instance that contains a 2-element array, where the first element is the AST and the second element is an array of Token instances.

This API is only meant to be used in the case where you need both the AST and the tokens. If you only need one or the other, use either Prism.parse_file or Prism.lex_file.

For supported options, see Prism.parse.

Parse the given object that responds to gets and return a ParseResult instance. The options that are supported are the same as Prism.parse.

Parse the given string and return true if it parses without errors. For supported options, see Prism.parse.

Parse the given string and return nothing. This method is meant to allow profilers to avoid the overhead of reifying the AST to Ruby. For supported options, see Prism.parse.

Parse the given file and return nothing. This method is meant to allow profilers to avoid the overhead of reifying the AST to Ruby. For supported options, see Prism.parse.

Create a new scope with the given locals and forwarding options that is suitable for passing into one of the Prism.* methods that accepts the scopes option.