Module: Prism

Defined in:
lib/prism.rb,
lib/prism/dsl.rb,
lib/prism/ffi.rb,
lib/prism/node.rb,
lib/prism/pack.rb,
lib/prism/debug.rb,
lib/prism/pattern.rb,
lib/prism/visitor.rb,
lib/prism/compiler.rb,
lib/prism/node_ext.rb,
lib/prism/serialize.rb,
lib/prism/dispatcher.rb,
lib/prism/lex_compat.rb,
lib/prism/dot_visitor.rb,
lib/prism/parse_result.rb,
lib/prism/ripper_compat.rb,
lib/prism/node_inspector.rb,
lib/prism/desugar_compiler.rb,
lib/prism/mutation_compiler.rb,
lib/prism/parse_result/comments.rb,
lib/prism/parse_result/newlines.rb,
ext/prism/api_pack.c,
ext/prism/extension.c

Overview

This file is generated by the templates/template.rb script and should not be modified manually. See templates/lib/prism/mutation_compiler.rb.erb if you are looking to modify the template

Defined Under Namespace

Modules: ArgumentsNodeFlags, ArrayNodeFlags, CallNodeFlags, DSL, EncodingFlags, IntegerBaseFlags, KeywordHashNodeFlags, LoopFlags, Pack, RangeFlags, RegularExpressionFlags, Serialize, StringFlags, SymbolFlags Classes: AliasGlobalVariableNode, AliasMethodNode, AlternationPatternNode, AndNode, ArgumentsNode, ArrayNode, ArrayPatternNode, AssocNode, AssocSplatNode, BackReferenceReadNode, BasicVisitor, BeginNode, BlockArgumentNode, BlockLocalVariableNode, BlockNode, BlockParameterNode, BlockParametersNode, BreakNode, CallAndWriteNode, CallNode, CallOperatorWriteNode, CallOrWriteNode, CallTargetNode, CapturePatternNode, CaseMatchNode, CaseNode, ClassNode, ClassVariableAndWriteNode, ClassVariableOperatorWriteNode, ClassVariableOrWriteNode, ClassVariableReadNode, ClassVariableTargetNode, ClassVariableWriteNode, Comment, Compiler, ConstantAndWriteNode, ConstantOperatorWriteNode, ConstantOrWriteNode, ConstantPathAndWriteNode, ConstantPathNode, ConstantPathOperatorWriteNode, ConstantPathOrWriteNode, ConstantPathTargetNode, ConstantPathWriteNode, ConstantReadNode, ConstantTargetNode, ConstantWriteNode, DefNode, DefinedNode, DesugarCompiler, Dispatcher, DotVisitor, ElseNode, EmbDocComment, EmbeddedStatementsNode, EmbeddedVariableNode, EnsureNode, FalseNode, FindPatternNode, FlipFlopNode, FloatNode, ForNode, ForwardingArgumentsNode, ForwardingParameterNode, ForwardingSuperNode, GlobalVariableAndWriteNode, GlobalVariableOperatorWriteNode, GlobalVariableOrWriteNode, GlobalVariableReadNode, GlobalVariableTargetNode, GlobalVariableWriteNode, HashNode, HashPatternNode, IfNode, ImaginaryNode, ImplicitNode, ImplicitRestNode, InNode, IndexAndWriteNode, IndexOperatorWriteNode, IndexOrWriteNode, IndexTargetNode, InlineComment, InstanceVariableAndWriteNode, InstanceVariableOperatorWriteNode, InstanceVariableOrWriteNode, InstanceVariableReadNode, InstanceVariableTargetNode, InstanceVariableWriteNode, IntegerNode, InterpolatedMatchLastLineNode, InterpolatedRegularExpressionNode, InterpolatedStringNode, InterpolatedSymbolNode, InterpolatedXStringNode, KeywordHashNode, KeywordRestParameterNode, LambdaNode, LocalVariableAndWriteNode, LocalVariableOperatorWriteNode, LocalVariableOrWriteNode, LocalVariableReadNode, LocalVariableTargetNode, LocalVariableWriteNode, Location, MagicComment, MatchLastLineNode, MatchPredicateNode, MatchRequiredNode, MatchWriteNode, MissingNode, ModuleNode, MultiTargetNode, MultiWriteNode, MutationCompiler, NextNode, NilNode, NoKeywordsParameterNode, Node, NodeInspector, NumberedParametersNode, NumberedReferenceReadNode, OptionalKeywordParameterNode, OptionalParameterNode, OrNode, ParametersNode, ParenthesesNode, ParseError, ParseResult, ParseWarning, Pattern, PinnedExpressionNode, PinnedVariableNode, PostExecutionNode, PreExecutionNode, ProgramNode, RangeNode, RationalNode, RedoNode, RegularExpressionNode, RequiredKeywordParameterNode, RequiredParameterNode, RescueModifierNode, RescueNode, RestParameterNode, RetryNode, ReturnNode, RipperCompat, SelfNode, SingletonClassNode, Source, SourceEncodingNode, SourceFileNode, SourceLineNode, SplatNode, StatementsNode, StringNode, SuperNode, SymbolNode, Token, TrueNode, UndefNode, UnlessNode, UntilNode, Visitor, WhenNode, WhileNode, XStringNode, YieldNode

Constant Summary collapse

BACKEND =

The backend of the parser that prism is using to parse Ruby code. This can be either :CEXT or :FFI. On runtimes that support C extensions, we default to :CEXT. Otherwise we use :FFI.

ID2SYM(rb_intern("CEXT"))
VERSION =

The version of the prism library.

rb_str_new2(EXPECTED_PRISM_VERSION)

Class Method Summary collapse

Class Method Details

.Prism::dump(source, **options) ⇒ String

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

Returns:

  • (String)


182
183
184
185
186
187
# File 'lib/prism/ffi.rb', line 182

def dump(code, **options)
  LibRubyParser::PrismBuffer.with do |buffer|
    LibRubyParser.pm_serialize_parse(buffer.pointer, code, code.bytesize, dump_options(options))
    buffer.read
  end
end

.Prism::dump_file(filepath, **options) ⇒ String

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

Returns:

  • (String)


190
191
192
193
194
# File 'lib/prism/ffi.rb', line 190

def dump_file(filepath, **options)
  LibRubyParser::PrismString.with(filepath) do |string|
    dump(string.read, **options, filepath: filepath)
  end
end

.Prism::lex(source, **options) ⇒ Array

Return an array of Token instances corresponding to the given string. For supported options, see Prism::parse.

Returns:

  • (Array)


197
198
199
200
201
202
# File 'lib/prism/ffi.rb', line 197

def lex(code, **options)
  LibRubyParser::PrismBuffer.with do |buffer|
    LibRubyParser.pm_serialize_lex(buffer.pointer, code, code.bytesize, dump_options(options))
    Serialize.load_tokens(Source.new(code), buffer.read)
  end
end

.lex_compat(source, **options) ⇒ Object

:call-seq:

Prism::lex_compat(source, **options) -> ParseResult

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.

For supported options, see Prism::parse.



46
47
48
# File 'lib/prism.rb', line 46

def self.lex_compat(source, **options)
  LexCompat.new(source, **options).result
end

.Prism::lex_file(filepath, **options) ⇒ Array

Return an array of Token instances corresponding to the given file. For supported options, see Prism::parse.

Returns:

  • (Array)


205
206
207
208
209
# File 'lib/prism/ffi.rb', line 205

def lex_file(filepath, **options)
  LibRubyParser::PrismString.with(filepath) do |string|
    lex(string.read, **options, filepath: filepath)
  end
end

.lex_ripper(source) ⇒ Object

: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.



56
57
58
# File 'lib/prism.rb', line 56

def self.lex_ripper(source)
  LexRipper.new(source).result
end

.load(source, serialized) ⇒ Object

:call-seq:

Prism::load(source, serialized) -> ParseResult

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



64
65
66
# File 'lib/prism.rb', line 64

def self.load(source, serialized)
  Serialize.load(source, serialized)
end

.Prism::parse(source, **options) ⇒ ParseResult

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

  • ‘filepath` - the filepath of the source being parsed. This should be a

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

    encoding or nil
    
  • ‘line` - the line number that the parse starts on. This should be an

    integer or nil. Note that this is 1-indexed.
    
  • ‘frozen_string_literal` - whether or not the frozen string literal pragma

    has been set. This should be a boolean or nil.
    
  • ‘verbose` - the current level of verbosity. This controls whether or not

    the parser emits warnings. This should be a boolean or nil.
    
  • ‘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.
    

Returns:



212
213
214
# File 'lib/prism/ffi.rb', line 212

def parse(code, **options)
  Prism.load(code, dump(code, **options))
end

.Prism::parse_comments(source, **options) ⇒ Array

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

Returns:

  • (Array)


226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/prism/ffi.rb', line 226

def parse_comments(code, **options)
  LibRubyParser::PrismBuffer.with do |buffer|
    LibRubyParser.pm_serialize_parse_comments(buffer.pointer, code, code.bytesize, dump_options(options))

    source = Source.new(code)
    loader = Serialize::Loader.new(source, buffer.read)

    loader.load_header
    loader.load_encoding
    loader.load_start_line
    loader.load_comments
  end
end

.parse_failure?(source, **options) ⇒ Boolean

:call-seq:

Prism::parse_failure?(source, **options) -> bool

Returns true if the source parses with errors.

Returns:

  • (Boolean)


72
73
74
# File 'lib/prism.rb', line 72

def self.parse_failure?(source, **options)
  !parse_success?(source, **options)
end

.Prism::parse_file(filepath, **options) ⇒ ParseResult

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

Returns:



219
220
221
222
223
# File 'lib/prism/ffi.rb', line 219

def parse_file(filepath, **options)
  LibRubyParser::PrismString.with(filepath) do |string|
    parse(string.read, **options, filepath: filepath)
  end
end

.Prism::parse_file_comments(filepath, **options) ⇒ Array

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

Returns:

  • (Array)


243
244
245
246
247
# File 'lib/prism/ffi.rb', line 243

def parse_file_comments(filepath, **options)
  LibRubyParser::PrismString.with(filepath) do |string|
    parse_comments(string.read, **options, filepath: filepath)
  end
end

.parse_file_failure?(filepath, **options) ⇒ Boolean

:call-seq:

Prism::parse_file_failure?(filepath, **options) -> bool

Returns true if the file at filepath parses with errors.

Returns:

  • (Boolean)


80
81
82
# File 'lib/prism.rb', line 80

def self.parse_file_failure?(filepath, **options)
  !parse_file_success?(filepath, **options)
end

.Prism::parse_file_success?(filepath, **options) ⇒ Array

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

Returns:

  • (Array)


278
279
280
281
282
# File 'lib/prism/ffi.rb', line 278

def parse_file_success?(filepath, **options)
  LibRubyParser::PrismString.with(filepath) do |string|
    parse_success?(string.read, **options, filepath: filepath)
  end
end

.Prism::parse_lex(source, **options) ⇒ ParseResult

Parse the given string and return a ParseResult 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.

Returns:



250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/prism/ffi.rb', line 250

def parse_lex(code, **options)
  LibRubyParser::PrismBuffer.with do |buffer|
    LibRubyParser.pm_serialize_parse_lex(buffer.pointer, code, code.bytesize, dump_options(options))

    source = Source.new(code)
    loader = Serialize::Loader.new(source, buffer.read)

    tokens = loader.load_tokens
    node, comments, magic_comments, data_loc, errors, warnings = loader.load_nodes
    tokens.each { |token,| token.value.force_encoding(loader.encoding) }

    ParseResult.new([node, tokens], comments, magic_comments, data_loc, errors, warnings, source)
  end
end

.Prism::parse_lex_file(filepath, **options) ⇒ ParseResult

Parse the given file and return a ParseResult 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.

Returns:



266
267
268
269
270
# File 'lib/prism/ffi.rb', line 266

def parse_lex_file(filepath, **options)
  LibRubyParser::PrismString.with(filepath) do |string|
    parse_lex(string.read, **options, filepath: filepath)
  end
end

.Prism::parse_success?(source, **options) ⇒ Array

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

Returns:

  • (Array)


273
274
275
# File 'lib/prism/ffi.rb', line 273

def parse_success?(code, **options)
  LibRubyParser.pm_parse_success_p(code, code.bytesize, dump_options(options))
end