Module: Prism

Defined in:
lib/prism.rb,
lib/prism/ffi.rb,
lib/prism/pack.rb,
lib/prism/pattern.rb,
lib/prism/node_ext.rb,
lib/prism/lex_compat.rb,
lib/prism/relocation.rb,
lib/prism/translation.rb,
lib/prism/parse_result.rb,
lib/prism/string_query.rb,
lib/prism/desugar_compiler.rb,
lib/prism/translation/parser.rb,
lib/prism/translation/ripper.rb,
lib/prism/parse_result/errors.rb,
lib/prism/translation/parser33.rb,
lib/prism/translation/parser34.rb,
lib/prism/parse_result/comments.rb,
lib/prism/parse_result/newlines.rb,
lib/prism/translation/ripper/sexp.rb,
lib/prism/translation/ruby_parser.rb,
lib/prism/translation/parser/lexer.rb,
lib/prism/translation/parser/compiler.rb

Overview

Here we are reopening the prism module to provide methods on nodes that aren’t templated and are meant as convenience methods.

Defined Under Namespace

Modules: Pack, Relocation, Translation Classes: ASCIISource, BeginNode, CallNode, CallOperatorWriteNode, CaseMatchNode, CaseNode, ClassVariableAndWriteNode, ClassVariableOperatorWriteNode, ClassVariableOrWriteNode, CodeUnitsCache, Comment, ConstantAndWriteNode, ConstantOperatorWriteNode, ConstantOrWriteNode, ConstantPathNode, ConstantPathOperatorWriteNode, ConstantPathTargetNode, ConstantReadNode, ConstantTargetNode, ConstantWriteNode, DesugarCompiler, EmbDocComment, GlobalVariableAndWriteNode, GlobalVariableOperatorWriteNode, GlobalVariableOrWriteNode, IfNode, ImaginaryNode, IndexOperatorWriteNode, InlineComment, InstanceVariableAndWriteNode, InstanceVariableOperatorWriteNode, InstanceVariableOrWriteNode, InterpolatedMatchLastLineNode, InterpolatedRegularExpressionNode, InterpolatedStringNode, InterpolatedSymbolNode, InterpolatedXStringNode, LexResult, LocalVariableAndWriteNode, LocalVariableOperatorWriteNode, LocalVariableOrWriteNode, Location, MagicComment, MatchLastLineNode, Node, ParametersNode, ParenthesesNode, ParseError, ParseLexResult, ParseResult, ParseWarning, Pattern, RationalNode, RegularExpressionNode, RescueModifierNode, RescueNode, Result, Source, StringNode, StringQuery, Token, UnlessNode, UntilNode, WhileNode, XStringNode

Constant Summary collapse

VERSION =

The version constant is set by reading the result of calling pm_version.

LibRubyParser.pm_version.read_string
BACKEND =

The FFI backend is used on other Ruby implementations.

:FFI

Class Method Summary collapse

Class Method Details

.dump(source, **options) ⇒ Object

Mirror the Prism.dump API by using the serialization API.



229
230
231
# File 'lib/prism/ffi.rb', line 229

def dump(source, **options)
  LibRubyParser::PrismString.with_string(source) { |string| dump_common(string, options) }
end

.dump_file(filepath, **options) ⇒ Object

Mirror the Prism.dump_file API by using the serialization API.



234
235
236
237
# File 'lib/prism/ffi.rb', line 234

def dump_file(filepath, **options)
  options[:filepath] = filepath
  LibRubyParser::PrismString.with_file(filepath) { |string| dump_common(string, options) }
end

.lex(code, **options) ⇒ Object

Mirror the Prism.lex API by using the serialization API.



240
241
242
# File 'lib/prism/ffi.rb', line 240

def lex(code, **options)
  LibRubyParser::PrismString.with_string(code) { |string| lex_common(string, code, options) }
end

.lex_compat(source, **options) ⇒ Object

:call-seq:

Prism::lex_compat(source, **options) -> LexCompat::Result

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.



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

def self.lex_compat(source, **options)
  LexCompat.new(source, **options).result # steep:ignore
end

.lex_file(filepath, **options) ⇒ Object

Mirror the Prism.lex_file API by using the serialization API.



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

def lex_file(filepath, **options)
  options[:filepath] = filepath
  LibRubyParser::PrismString.with_file(filepath) { |string| lex_common(string, string.read, options) }
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.



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

def self.lex_ripper(source)
  LexRipper.new(source).result # steep:ignore
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.



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

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

.parse(code, **options) ⇒ Object

Mirror the Prism.parse API by using the serialization API.



251
252
253
# File 'lib/prism/ffi.rb', line 251

def parse(code, **options)
  LibRubyParser::PrismString.with_string(code) { |string| parse_common(string, code, options) }
end

.parse_comments(code, **options) ⇒ Object

Mirror the Prism.parse_comments API by using the serialization API.



287
288
289
# File 'lib/prism/ffi.rb', line 287

def parse_comments(code, **options)
  LibRubyParser::PrismString.with_string(code) { |string| parse_comments_common(string, code, options) }
end

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

Mirror the Prism.parse_failure? API by using the serialization API.

Returns:

  • (Boolean)


316
317
318
# File 'lib/prism/ffi.rb', line 316

def parse_failure?(code, **options)
  !parse_success?(code, **options)
end

.parse_file(filepath, **options) ⇒ Object

Mirror the Prism.parse_file API by using the serialization API. This uses native strings instead of Ruby strings because it allows us to use mmap when it is available.



258
259
260
261
# File 'lib/prism/ffi.rb', line 258

def parse_file(filepath, **options)
  options[:filepath] = filepath
  LibRubyParser::PrismString.with_file(filepath) { |string| parse_common(string, string.read, options) }
end

.parse_file_comments(filepath, **options) ⇒ Object

Mirror the Prism.parse_file_comments API by using the serialization API. This uses native strings instead of Ruby strings because it allows us to use mmap when it is available.



294
295
296
297
# File 'lib/prism/ffi.rb', line 294

def parse_file_comments(filepath, **options)
  options[:filepath] = filepath
  LibRubyParser::PrismString.with_file(filepath) { |string| parse_comments_common(string, string.read, options) }
end

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

Mirror the Prism.parse_file_failure? API by using the serialization API.

Returns:

  • (Boolean)


327
328
329
# File 'lib/prism/ffi.rb', line 327

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

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

Mirror the Prism.parse_file_success? API by using the serialization API.

Returns:

  • (Boolean)


321
322
323
324
# File 'lib/prism/ffi.rb', line 321

def parse_file_success?(filepath, **options)
  options[:filepath] = filepath
  LibRubyParser::PrismString.with_file(filepath) { |string| parse_file_success_common(string, options) }
end

.parse_lex(code, **options) ⇒ Object

Mirror the Prism.parse_lex API by using the serialization API.



300
301
302
# File 'lib/prism/ffi.rb', line 300

def parse_lex(code, **options)
  LibRubyParser::PrismString.with_string(code) { |string| parse_lex_common(string, code, options) }
end

.parse_lex_file(filepath, **options) ⇒ Object

Mirror the Prism.parse_lex_file API by using the serialization API.



305
306
307
308
# File 'lib/prism/ffi.rb', line 305

def parse_lex_file(filepath, **options)
  options[:filepath] = filepath
  LibRubyParser::PrismString.with_file(filepath) { |string| parse_lex_common(string, string.read, options) }
end

.parse_stream(stream, **options) ⇒ Object

Mirror the Prism.parse_stream API by using the serialization API.



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/prism/ffi.rb', line 264

def parse_stream(stream, **options)
  LibRubyParser::PrismBuffer.with do |buffer|
    source = +""
    callback = -> (string, size, _) {
      raise "Expected size to be >= 0, got: #{size}" if size <= 0

      if !(line = stream.gets(size - 1)).nil?
        source << line
        string.write_string("#{line}\x00", line.bytesize + 1)
      end
    }

    # In the pm_serialize_parse_stream function it accepts a pointer to the
    # IO object as a void* and then passes it through to the callback as the
    # third argument, but it never touches it itself. As such, since we have
    # access to the IO object already through the closure of the lambda, we
    # can pass a null pointer here and not worry.
    LibRubyParser.pm_serialize_parse_stream(buffer.pointer, nil, callback, dump_options(options))
    Prism.load(source, buffer.read)
  end
end

.parse_success?(code, **options) ⇒ Boolean

Mirror the Prism.parse_success? API by using the serialization API.

Returns:

  • (Boolean)


311
312
313
# File 'lib/prism/ffi.rb', line 311

def parse_success?(code, **options)
  LibRubyParser::PrismString.with_string(code) { |string| parse_file_success_common(string, options) }
end

.profile(source, **options) ⇒ Object

Mirror the Prism.profile API by using the serialization API.



332
333
334
335
336
337
338
339
# File 'lib/prism/ffi.rb', line 332

def profile(source, **options)
  LibRubyParser::PrismString.with_string(source) do |string|
    LibRubyParser::PrismBuffer.with do |buffer|
      LibRubyParser.pm_serialize_parse(buffer.pointer, string.pointer, string.length, dump_options(options))
      nil
    end
  end
end

.profile_file(filepath, **options) ⇒ Object

Mirror the Prism.profile_file API by using the serialization API.



342
343
344
345
346
347
348
349
350
# File 'lib/prism/ffi.rb', line 342

def profile_file(filepath, **options)
  LibRubyParser::PrismString.with_file(filepath) do |string|
    LibRubyParser::PrismBuffer.with do |buffer|
      options[:filepath] = filepath
      LibRubyParser.pm_serialize_parse(buffer.pointer, string.pointer, string.length, dump_options(options))
      nil
    end
  end
end