module YARP
Reopening the YARP
module after yarp/node is required so that constant reflection APIs will find the constants defined in the node file before these. This block is meant to contain extra APIs we define on YARP
nodes that aren’t templated and are meant as convenience methods.
This file is generated by the templates/template.rb script and should not be modified manually. See templates/lib/yarp/mutation_visitor.rb.erb if you are looking to modify the template
This file is generated by the templates/template.rb script and should not be modified manually. See templates/lib/yarp/node.rb.erb if you are looking to modify the template
Constants
- BACKEND
- VERSION
The version constant is set by reading the result of calling yp_version.
Public Class Methods
Mirror the YARP.dump
API by using the serialization API.
# File yarp/ffi.rb, line 188 def self.dump(code, filepath = nil) dump_internal(code, code.bytesize, filepath) end
Mirror the YARP.dump_file
API by using the serialization API.
# File yarp/ffi.rb, line 193 def self.dump_file(filepath) LibRubyParser::YPString.with(filepath) do |string| dump_internal(string.source, string.length, filepath) end end
Mirror the YARP.lex
API by using the serialization API.
# File yarp/ffi.rb, line 200 def self.lex(code, filepath = nil) LibRubyParser::YPBuffer.with do |buffer| LibRubyParser.yp_lex_serialize(code, code.bytesize, filepath, buffer.pointer) Serialize.load_tokens(Source.new(code), buffer.read) end end
Returns an array of tokens that closely resembles that of the Ripper lexer. The only difference is that since we don’t keep track of lexer state in the same way, it’s going to always return the NONE state.
# File yarp/lex_compat.rb, line 804 def self.lex_compat(source, filepath = "") LexCompat.new(source, filepath).result end
Mirror the YARP.lex_file
API by using the serialization API.
# File yarp/ffi.rb, line 208 def self.lex_file(filepath) LibRubyParser::YPString.with(filepath) do |string| lex(string.read, filepath) end end
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.
# File yarp/lex_compat.rb, line 811 def self.lex_ripper(source) previous = [] results = [] Ripper.lex(source, raise_errors: true).each do |token| case token[1] when :on_sp # skip when :on_tstring_content if previous[1] == :on_tstring_content && (token[2].start_with?("\#$") || token[2].start_with?("\#@")) previous[2] << token[2] else results << token previous = token end when :on_words_sep if previous[1] == :on_words_sep previous[2] << token[2] else results << token previous = token end else results << token previous = token end end results end
Load the serialized AST using the source as a reference into a tree.
# File yarp.rb, line 380 def self.load(source, serialized) Serialize.load(source, serialized) end
Mirror the YARP.parse
API by using the serialization API.
# File yarp/ffi.rb, line 215 def self.parse(code, filepath = nil) YARP.load(code, dump(code, filepath)) end
Mirror the YARP.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.
# File yarp/ffi.rb, line 222 def self.parse_file(filepath) LibRubyParser::YPString.with(filepath) do |string| parse(string.read, filepath) end end
Mirror the YARP.parse_lex
API by using the serialization API.
# File yarp/ffi.rb, line 229 def self.parse_lex(code, filepath = nil) LibRubyParser::YPBuffer.with do |buffer| metadata = [filepath.bytesize, filepath.b, 0].pack("LA*L") if filepath LibRubyParser.yp_parse_lex_serialize(code, code.bytesize, buffer.pointer, metadata) source = Source.new(code) loader = Serialize::Loader.new(source, buffer.read) tokens = loader.load_tokens node, comments, errors, warnings = loader.load_nodes tokens.each { |token,| token.value.force_encoding(loader.encoding) } ParseResult.new([node, tokens], comments, errors, warnings, source) end end
Mirror the YARP.parse_lex_file
API by using the serialization API.
# File yarp/ffi.rb, line 247 def self.parse_lex_file(filepath) LibRubyParser::YPString.with(filepath) do |string| parse_lex(string.read, filepath) end end
Private Class Methods
# File yarp/ffi.rb, line 178 def self.dump_internal(source, source_size, filepath) LibRubyParser::YPBuffer.with do |buffer| metadata = [filepath.bytesize, filepath.b, 0].pack("LA*L") if filepath LibRubyParser.yp_parse_serialize(source, source_size, buffer.pointer, metadata) buffer.read end end