summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorKevin Newton <kddnewton@gmail.com>2023-08-25 15:22:42 -0400
committergit <svn-admin@ruby-lang.org>2023-08-25 21:10:19 +0000
commit9b8602dd903b2515463a1a314cb8fdf735a354aa (patch)
tree9ae69aabbc89658e22abcd9b5c770ac8572ae79d /lib
parent76512d78fcde99458db211c0f958bd39cb23dd98 (diff)
[ruby/yarp] Introduce parse_lex instead of asking for a block
https://github.com/ruby/yarp/commit/7e70339fe1
Diffstat (limited to 'lib')
-rw-r--r--lib/yarp/ffi.rb28
1 files changed, 27 insertions, 1 deletions
diff --git a/lib/yarp/ffi.rb b/lib/yarp/ffi.rb
index 31c1ce3cc7..73e5d60dfa 100644
--- a/lib/yarp/ffi.rb
+++ b/lib/yarp/ffi.rb
@@ -70,7 +70,8 @@ module YARP
"yarp.h",
"yp_version",
"yp_parse_serialize",
- "yp_lex_serialize"
+ "yp_lex_serialize",
+ "yp_parse_lex_serialize"
)
load_exported_functions_from(
@@ -225,4 +226,29 @@ module YARP
parse(string.read, filepath)
end
end
+
+ # Mirror the YARP.parse_lex API by using the serialization API.
+ 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.
+ def self.parse_lex_file(filepath)
+ LibRubyParser::YPString.with(filepath) do |string|
+ parse_lex(string.read, filepath)
+ end
+ end
end