diff options
author | Kevin Newton <[email protected]> | 2023-10-30 11:21:16 -0400 |
---|---|---|
committer | git <[email protected]> | 2023-10-30 15:53:37 +0000 |
commit | 7bf3d9343fc9ca34124026cb3806107a3a6ddf09 (patch) | |
tree | 5bcd16e551689648a54893f266a812c89a1926bf | |
parent | 7d8cfa0a40475c59364605f83b1b892bf068ae30 (diff) |
[ruby/prism] parse_inline_comments -> parse_comments
https://github.com/ruby/prism/commit/bd4d248fd6
-rw-r--r-- | lib/prism/ffi.rb | 14 | ||||
-rw-r--r-- | prism/extension.c | 29 | ||||
-rw-r--r-- | prism/prism.c | 5 | ||||
-rw-r--r-- | prism/prism.h | 5 | ||||
-rw-r--r-- | test/prism/parse_comments_test.rb (renamed from test/prism/parse_inline_comments_test.rb) | 10 |
5 files changed, 24 insertions, 39 deletions
diff --git a/lib/prism/ffi.rb b/lib/prism/ffi.rb index ae13474c31..39d51efdc4 100644 --- a/lib/prism/ffi.rb +++ b/lib/prism/ffi.rb @@ -70,7 +70,7 @@ module Prism "prism.h", "pm_version", "pm_parse_serialize", - "pm_parse_serialize_inline_comments", + "pm_parse_serialize_comments", "pm_lex_serialize", "pm_parse_lex_serialize" ) @@ -225,11 +225,11 @@ module Prism end end - # Mirror the Prism.parse_inline_comments API by using the serialization API. - def self.parse_inline_comments(code, filepath = nil) + # Mirror the Prism.parse_comments API by using the serialization API. + def self.parse_comments(code, filepath = nil) LibRubyParser::PrismBuffer.with do |buffer| metadata = [filepath.bytesize, filepath.b, 0].pack("LA*L") if filepath - LibRubyParser.pm_parse_serialize_inline_comments(code, code.bytesize, buffer.pointer, metadata) + LibRubyParser.pm_parse_serialize_comments(code, code.bytesize, buffer.pointer, metadata) source = Source.new(code) loader = Serialize::Loader.new(source, buffer.read) @@ -240,12 +240,12 @@ module Prism end end - # Mirror the Prism.parse_file_inline_comments API by using the serialization + # 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. - def self.parse_file_inline_comments(filepath) + def self.parse_file_comments(filepath) LibRubyParser::PrismString.with(filepath) do |string| - parse_inline_comments(string.read, filepath) + parse_comments(string.read, filepath) end end diff --git a/prism/extension.c b/prism/extension.c index 27dbc5c8f7..5346bc1bd3 100644 --- a/prism/extension.c +++ b/prism/extension.c @@ -398,7 +398,7 @@ parse_input(pm_string_t *input, const char *filepath) { // Parse the given input and return an array of Comment objects. static VALUE -parse_input_inline_comments(pm_string_t *input, const char *filepath) { +parse_input_comments(pm_string_t *input, const char *filepath) { pm_parser_t parser; pm_parser_init(&parser, pm_string_source(input), pm_string_length(input), filepath); @@ -406,20 +406,7 @@ parse_input_inline_comments(pm_string_t *input, const char *filepath) { rb_encoding *encoding = rb_enc_find(parser.encoding.name); VALUE source = pm_source_new(&parser, encoding); - VALUE comments = rb_ary_new(); - - for (pm_comment_t *comment = (pm_comment_t *) parser.comment_list.head; comment != NULL; comment = (pm_comment_t *) comment->node.next) { - if (comment->type != PM_COMMENT_INLINE) continue; - - VALUE location_argv[] = { - source, - LONG2FIX(comment->start - parser.start), - LONG2FIX(comment->end - comment->start) - }; - - VALUE comment_argv[] = { ID2SYM(rb_intern("inline")), rb_class_new_instance(3, location_argv, rb_cPrismLocation) }; - rb_ary_push(comments, rb_class_new_instance(2, comment_argv, rb_cPrismComment)); - } + VALUE comments = parser_comments(&parser, source); pm_node_destroy(&parser, node); pm_parser_free(&parser); @@ -469,7 +456,7 @@ parse_file(VALUE self, VALUE filepath) { // Parse the given string and return an array of Comment objects. static VALUE -parse_inline_comments(int argc, VALUE *argv, VALUE self) { +parse_comments(int argc, VALUE *argv, VALUE self) { VALUE string; VALUE filepath; rb_scan_args(argc, argv, "11", &string, &filepath); @@ -477,18 +464,18 @@ parse_inline_comments(int argc, VALUE *argv, VALUE self) { pm_string_t input; input_load_string(&input, string); - return parse_input_inline_comments(&input, check_string(filepath)); + return parse_input_comments(&input, check_string(filepath)); } // Parse the given file and return an array of Comment objects. static VALUE -parse_file_inline_comments(VALUE self, VALUE filepath) { +parse_file_comments(VALUE self, VALUE filepath) { pm_string_t input; const char *checked = check_string(filepath); if (!pm_string_mapped_init(&input, checked)) return Qnil; - VALUE value = parse_input_inline_comments(&input, checked); + VALUE value = parse_input_comments(&input, checked); pm_string_free(&input); return value; @@ -679,8 +666,8 @@ Init_prism(void) { rb_define_singleton_method(rb_cPrism, "lex_file", lex_file, 1); rb_define_singleton_method(rb_cPrism, "parse", parse, -1); rb_define_singleton_method(rb_cPrism, "parse_file", parse_file, 1); - rb_define_singleton_method(rb_cPrism, "parse_inline_comments", parse_inline_comments, -1); - rb_define_singleton_method(rb_cPrism, "parse_file_inline_comments", parse_file_inline_comments, 1); + rb_define_singleton_method(rb_cPrism, "parse_comments", parse_comments, -1); + rb_define_singleton_method(rb_cPrism, "parse_file_comments", parse_file_comments, 1); rb_define_singleton_method(rb_cPrism, "parse_lex", parse_lex, -1); rb_define_singleton_method(rb_cPrism, "parse_lex_file", parse_lex_file, 1); diff --git a/prism/prism.c b/prism/prism.c index 9d40d28d01..83e75b4b69 100644 --- a/prism/prism.c +++ b/prism/prism.c @@ -15723,10 +15723,9 @@ pm_parse_serialize(const uint8_t *source, size_t size, pm_buffer_t *buffer, cons pm_parser_free(&parser); } -// Parse and serialize the inline comments in the given source to the given -// buffer. +// Parse and serialize the comments in the given source to the given buffer. PRISM_EXPORTED_FUNCTION void -pm_parse_serialize_inline_comments(const uint8_t *source, size_t size, pm_buffer_t *buffer, const char *metadata) { +pm_parse_serialize_comments(const uint8_t *source, size_t size, pm_buffer_t *buffer, const char *metadata) { pm_parser_t parser; pm_parser_init(&parser, source, size, NULL); if (metadata) pm_parser_metadata(&parser, metadata); diff --git a/prism/prism.h b/prism/prism.h index 227b233ea1..9dcecbb976 100644 --- a/prism/prism.h +++ b/prism/prism.h @@ -65,9 +65,8 @@ PRISM_EXPORTED_FUNCTION void pm_serialize(pm_parser_t *parser, pm_node_t *node, // Parse the given source to the AST and serialize the AST to the given buffer. PRISM_EXPORTED_FUNCTION void pm_parse_serialize(const uint8_t *source, size_t size, pm_buffer_t *buffer, const char *metadata); -// Parse and serialize the inline comments in the given source to the given -// buffer. -PRISM_EXPORTED_FUNCTION void pm_parse_serialize_inline_comments(const uint8_t *source, size_t size, pm_buffer_t *buffer, const char *metadata); +// Parse and serialize the comments in the given source to the given buffer. +PRISM_EXPORTED_FUNCTION void pm_parse_serialize_comments(const uint8_t *source, size_t size, pm_buffer_t *buffer, const char *metadata); // Lex the given source and serialize to the given buffer. PRISM_EXPORTED_FUNCTION void pm_lex_serialize(const uint8_t *source, size_t size, const char *filepath, pm_buffer_t *buffer); diff --git a/test/prism/parse_inline_comments_test.rb b/test/prism/parse_comments_test.rb index d90d0abf88..30086e3155 100644 --- a/test/prism/parse_inline_comments_test.rb +++ b/test/prism/parse_comments_test.rb @@ -3,16 +3,16 @@ require_relative "test_helper" module Prism - class ParseInlineCommentsTest < TestCase - def test_parse_inline_comments - comments = Prism.parse_inline_comments("# foo") + class ParseCommentsTest < TestCase + def test_parse_comments + comments = Prism.parse_comments("# foo") assert_kind_of Array, comments assert_equal 1, comments.length end - def test_parse_file_inline_comments - comments = Prism.parse_file_inline_comments(__FILE__) + def test_parse_file_comments + comments = Prism.parse_file_comments(__FILE__) assert_kind_of Array, comments assert_equal 1, comments.length |