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
-
.dump(source, **options) ⇒ Object
Mirror the Prism.dump API by using the serialization API.
-
.dump_file(filepath, **options) ⇒ Object
Mirror the Prism.dump_file API by using the serialization API.
-
.lex(code, **options) ⇒ Object
Mirror the Prism.lex API by using the serialization API.
-
.lex_compat(source, **options) ⇒ Object
:call-seq: Prism::lex_compat(source, **options) -> LexCompat::Result.
-
.lex_file(filepath, **options) ⇒ Object
Mirror the Prism.lex_file API by using the serialization API.
-
.lex_ripper(source) ⇒ Object
:call-seq: Prism::lex_ripper(source) -> Array.
-
.load(source, serialized) ⇒ Object
:call-seq: Prism::load(source, serialized) -> ParseResult.
-
.parse(code, **options) ⇒ Object
Mirror the Prism.parse API by using the serialization API.
-
.parse_comments(code, **options) ⇒ Object
Mirror the Prism.parse_comments API by using the serialization API.
-
.parse_failure?(code, **options) ⇒ Boolean
Mirror the Prism.parse_failure? API by using the serialization API.
-
.parse_file(filepath, **options) ⇒ Object
Mirror the Prism.parse_file API by using the serialization API.
-
.parse_file_comments(filepath, **options) ⇒ Object
Mirror the Prism.parse_file_comments API by using the serialization API.
-
.parse_file_failure?(filepath, **options) ⇒ Boolean
Mirror the Prism.parse_file_failure? API by using the serialization API.
-
.parse_file_success?(filepath, **options) ⇒ Boolean
Mirror the Prism.parse_file_success? API by using the serialization API.
-
.parse_lex(code, **options) ⇒ Object
Mirror the Prism.parse_lex API by using the serialization API.
-
.parse_lex_file(filepath, **options) ⇒ Object
Mirror the Prism.parse_lex_file API by using the serialization API.
-
.parse_stream(stream, **options) ⇒ Object
Mirror the Prism.parse_stream API by using the serialization API.
-
.parse_success?(code, **options) ⇒ Boolean
Mirror the Prism.parse_success? API by using the serialization API.
-
.profile(source, **options) ⇒ Object
Mirror the Prism.profile API by using the serialization API.
-
.profile_file(filepath, **options) ⇒ Object
Mirror the Prism.profile_file API by using the serialization API.
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, **) LibRubyParser::PrismString.with_string(source) { |string| dump_common(string, ) } 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, **) [:filepath] = filepath LibRubyParser::PrismString.with_file(filepath) { |string| dump_common(string, ) } 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, **) LibRubyParser::PrismString.with_string(code) { |string| lex_common(string, code, ) } 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, **) LexCompat.new(source, **).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, **) [:filepath] = filepath LibRubyParser::PrismString.with_file(filepath) { |string| lex_common(string, string.read, ) } 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, **) LibRubyParser::PrismString.with_string(code) { |string| parse_common(string, code, ) } 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, **) LibRubyParser::PrismString.with_string(code) { |string| parse_comments_common(string, code, ) } end |
.parse_failure?(code, **options) ⇒ Boolean
Mirror the Prism.parse_failure? API by using the serialization API.
316 317 318 |
# File 'lib/prism/ffi.rb', line 316 def parse_failure?(code, **) !parse_success?(code, **) 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, **) [:filepath] = filepath LibRubyParser::PrismString.with_file(filepath) { |string| parse_common(string, string.read, ) } 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, **) [:filepath] = filepath LibRubyParser::PrismString.with_file(filepath) { |string| parse_comments_common(string, string.read, ) } end |
.parse_file_failure?(filepath, **options) ⇒ Boolean
Mirror the Prism.parse_file_failure? API by using the serialization API.
327 328 329 |
# File 'lib/prism/ffi.rb', line 327 def parse_file_failure?(filepath, **) !parse_file_success?(filepath, **) end |
.parse_file_success?(filepath, **options) ⇒ Boolean
Mirror the Prism.parse_file_success? API by using the serialization API.
321 322 323 324 |
# File 'lib/prism/ffi.rb', line 321 def parse_file_success?(filepath, **) [:filepath] = filepath LibRubyParser::PrismString.with_file(filepath) { |string| parse_file_success_common(string, ) } 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, **) LibRubyParser::PrismString.with_string(code) { |string| parse_lex_common(string, code, ) } 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, **) [:filepath] = filepath LibRubyParser::PrismString.with_file(filepath) { |string| parse_lex_common(string, string.read, ) } 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, **) 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, ()) Prism.load(source, buffer.read) end end |
.parse_success?(code, **options) ⇒ Boolean
Mirror the Prism.parse_success? API by using the serialization API.
311 312 313 |
# File 'lib/prism/ffi.rb', line 311 def parse_success?(code, **) LibRubyParser::PrismString.with_string(code) { |string| parse_file_success_common(string, ) } 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, **) LibRubyParser::PrismString.with_string(source) do |string| LibRubyParser::PrismBuffer.with do |buffer| LibRubyParser.pm_serialize_parse(buffer.pointer, string.pointer, string.length, ()) 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, **) LibRubyParser::PrismString.with_file(filepath) do |string| LibRubyParser::PrismBuffer.with do |buffer| [:filepath] = filepath LibRubyParser.pm_serialize_parse(buffer.pointer, string.pointer, string.length, ()) nil end end end |