Skip to content

Commit bd4d248

Browse files
committed
parse_inline_comments -> parse_comments
1 parent 4a7be1f commit bd4d248

File tree

6 files changed

+26
-41
lines changed

6 files changed

+26
-41
lines changed

docs/ruby_api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ The full API is documented below.
2323
* `Prism.parse_lex(source)` - parse the syntax tree corresponding to the given source string and return it within a parse result, along with the tokens
2424
* `Prism.parse_lex_file(filepath)` - parse the syntax tree corresponding to the given source file and return it within a parse result, along with the tokens
2525
* `Prism.load(source, serialized)` - load the serialized syntax tree using the source as a reference into a syntax tree
26-
* `Prism.parse_inline_comments(source)` - parse the inline comments corresponding to the given source string and return them
27-
* `Prism.parse_file_inline_comments(source)` - parse the inline comments corresponding to the given source file and return them
26+
* `Prism.parse_comments(source)` - parse the comments corresponding to the given source string and return them
27+
* `Prism.parse_file_comments(source)` - parse the comments corresponding to the given source file and return them

ext/prism/extension.c

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -398,28 +398,15 @@ parse_input(pm_string_t *input, const char *filepath) {
398398

399399
// Parse the given input and return an array of Comment objects.
400400
static VALUE
401-
parse_input_inline_comments(pm_string_t *input, const char *filepath) {
401+
parse_input_comments(pm_string_t *input, const char *filepath) {
402402
pm_parser_t parser;
403403
pm_parser_init(&parser, pm_string_source(input), pm_string_length(input), filepath);
404404

405405
pm_node_t *node = pm_parse(&parser);
406406
rb_encoding *encoding = rb_enc_find(parser.encoding.name);
407407

408408
VALUE source = pm_source_new(&parser, encoding);
409-
VALUE comments = rb_ary_new();
410-
411-
for (pm_comment_t *comment = (pm_comment_t *) parser.comment_list.head; comment != NULL; comment = (pm_comment_t *) comment->node.next) {
412-
if (comment->type != PM_COMMENT_INLINE) continue;
413-
414-
VALUE location_argv[] = {
415-
source,
416-
LONG2FIX(comment->start - parser.start),
417-
LONG2FIX(comment->end - comment->start)
418-
};
419-
420-
VALUE comment_argv[] = { ID2SYM(rb_intern("inline")), rb_class_new_instance(3, location_argv, rb_cPrismLocation) };
421-
rb_ary_push(comments, rb_class_new_instance(2, comment_argv, rb_cPrismComment));
422-
}
409+
VALUE comments = parser_comments(&parser, source);
423410

424411
pm_node_destroy(&parser, node);
425412
pm_parser_free(&parser);
@@ -469,26 +456,26 @@ parse_file(VALUE self, VALUE filepath) {
469456

470457
// Parse the given string and return an array of Comment objects.
471458
static VALUE
472-
parse_inline_comments(int argc, VALUE *argv, VALUE self) {
459+
parse_comments(int argc, VALUE *argv, VALUE self) {
473460
VALUE string;
474461
VALUE filepath;
475462
rb_scan_args(argc, argv, "11", &string, &filepath);
476463

477464
pm_string_t input;
478465
input_load_string(&input, string);
479466

480-
return parse_input_inline_comments(&input, check_string(filepath));
467+
return parse_input_comments(&input, check_string(filepath));
481468
}
482469

483470
// Parse the given file and return an array of Comment objects.
484471
static VALUE
485-
parse_file_inline_comments(VALUE self, VALUE filepath) {
472+
parse_file_comments(VALUE self, VALUE filepath) {
486473
pm_string_t input;
487474

488475
const char *checked = check_string(filepath);
489476
if (!pm_string_mapped_init(&input, checked)) return Qnil;
490477

491-
VALUE value = parse_input_inline_comments(&input, checked);
478+
VALUE value = parse_input_comments(&input, checked);
492479
pm_string_free(&input);
493480

494481
return value;
@@ -679,8 +666,8 @@ Init_prism(void) {
679666
rb_define_singleton_method(rb_cPrism, "lex_file", lex_file, 1);
680667
rb_define_singleton_method(rb_cPrism, "parse", parse, -1);
681668
rb_define_singleton_method(rb_cPrism, "parse_file", parse_file, 1);
682-
rb_define_singleton_method(rb_cPrism, "parse_inline_comments", parse_inline_comments, -1);
683-
rb_define_singleton_method(rb_cPrism, "parse_file_inline_comments", parse_file_inline_comments, 1);
669+
rb_define_singleton_method(rb_cPrism, "parse_comments", parse_comments, -1);
670+
rb_define_singleton_method(rb_cPrism, "parse_file_comments", parse_file_comments, 1);
684671
rb_define_singleton_method(rb_cPrism, "parse_lex", parse_lex, -1);
685672
rb_define_singleton_method(rb_cPrism, "parse_lex_file", parse_lex_file, 1);
686673

include/prism.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@ PRISM_EXPORTED_FUNCTION void pm_serialize(pm_parser_t *parser, pm_node_t *node,
6565
// Parse the given source to the AST and serialize the AST to the given buffer.
6666
PRISM_EXPORTED_FUNCTION void pm_parse_serialize(const uint8_t *source, size_t size, pm_buffer_t *buffer, const char *metadata);
6767

68-
// Parse and serialize the inline comments in the given source to the given
69-
// buffer.
70-
PRISM_EXPORTED_FUNCTION void pm_parse_serialize_inline_comments(const uint8_t *source, size_t size, pm_buffer_t *buffer, const char *metadata);
68+
// Parse and serialize the comments in the given source to the given buffer.
69+
PRISM_EXPORTED_FUNCTION void pm_parse_serialize_comments(const uint8_t *source, size_t size, pm_buffer_t *buffer, const char *metadata);
7170

7271
// Lex the given source and serialize to the given buffer.
7372
PRISM_EXPORTED_FUNCTION void pm_lex_serialize(const uint8_t *source, size_t size, const char *filepath, pm_buffer_t *buffer);

lib/prism/ffi.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def self.load_exported_functions_from(header, *functions)
7070
"prism.h",
7171
"pm_version",
7272
"pm_parse_serialize",
73-
"pm_parse_serialize_inline_comments",
73+
"pm_parse_serialize_comments",
7474
"pm_lex_serialize",
7575
"pm_parse_lex_serialize"
7676
)
@@ -225,11 +225,11 @@ def self.parse_file(filepath)
225225
end
226226
end
227227

228-
# Mirror the Prism.parse_inline_comments API by using the serialization API.
229-
def self.parse_inline_comments(code, filepath = nil)
228+
# Mirror the Prism.parse_comments API by using the serialization API.
229+
def self.parse_comments(code, filepath = nil)
230230
LibRubyParser::PrismBuffer.with do |buffer|
231231
metadata = [filepath.bytesize, filepath.b, 0].pack("LA*L") if filepath
232-
LibRubyParser.pm_parse_serialize_inline_comments(code, code.bytesize, buffer.pointer, metadata)
232+
LibRubyParser.pm_parse_serialize_comments(code, code.bytesize, buffer.pointer, metadata)
233233

234234
source = Source.new(code)
235235
loader = Serialize::Loader.new(source, buffer.read)
@@ -240,12 +240,12 @@ def self.parse_inline_comments(code, filepath = nil)
240240
end
241241
end
242242

243-
# Mirror the Prism.parse_file_inline_comments API by using the serialization
243+
# Mirror the Prism.parse_file_comments API by using the serialization
244244
# API. This uses native strings instead of Ruby strings because it allows us
245245
# to use mmap when it is available.
246-
def self.parse_file_inline_comments(filepath)
246+
def self.parse_file_comments(filepath)
247247
LibRubyParser::PrismString.with(filepath) do |string|
248-
parse_inline_comments(string.read, filepath)
248+
parse_comments(string.read, filepath)
249249
end
250250
end
251251

src/prism.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15723,10 +15723,9 @@ pm_parse_serialize(const uint8_t *source, size_t size, pm_buffer_t *buffer, cons
1572315723
pm_parser_free(&parser);
1572415724
}
1572515725

15726-
// Parse and serialize the inline comments in the given source to the given
15727-
// buffer.
15726+
// Parse and serialize the comments in the given source to the given buffer.
1572815727
PRISM_EXPORTED_FUNCTION void
15729-
pm_parse_serialize_inline_comments(const uint8_t *source, size_t size, pm_buffer_t *buffer, const char *metadata) {
15728+
pm_parse_serialize_comments(const uint8_t *source, size_t size, pm_buffer_t *buffer, const char *metadata) {
1573015729
pm_parser_t parser;
1573115730
pm_parser_init(&parser, source, size, NULL);
1573215731
if (metadata) pm_parser_metadata(&parser, metadata);

test/prism/parse_inline_comments_test.rb renamed to test/prism/parse_comments_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
require_relative "test_helper"
44

55
module Prism
6-
class ParseInlineCommentsTest < TestCase
7-
def test_parse_inline_comments
8-
comments = Prism.parse_inline_comments("# foo")
6+
class ParseCommentsTest < TestCase
7+
def test_parse_comments
8+
comments = Prism.parse_comments("# foo")
99

1010
assert_kind_of Array, comments
1111
assert_equal 1, comments.length
1212
end
1313

14-
def test_parse_file_inline_comments
15-
comments = Prism.parse_file_inline_comments(__FILE__)
14+
def test_parse_file_comments
15+
comments = Prism.parse_file_comments(__FILE__)
1616

1717
assert_kind_of Array, comments
1818
assert_equal 1, comments.length

0 commit comments

Comments
 (0)