Skip to content

Commit

Permalink
Merge pull request #1748 from ruby/parse-comments
Browse files Browse the repository at this point in the history
parse_inline_comments -> parse_comments
  • Loading branch information
kddnewton authored Oct 30, 2023
2 parents 4a7be1f + bd4d248 commit 04d6994
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 41 deletions.
4 changes: 2 additions & 2 deletions docs/ruby_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ The full API is documented below.
* `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
* `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
* `Prism.load(source, serialized)` - load the serialized syntax tree using the source as a reference into a syntax tree
* `Prism.parse_inline_comments(source)` - parse the inline comments corresponding to the given source string and return them
* `Prism.parse_file_inline_comments(source)` - parse the inline comments corresponding to the given source file and return them
* `Prism.parse_comments(source)` - parse the comments corresponding to the given source string and return them
* `Prism.parse_file_comments(source)` - parse the comments corresponding to the given source file and return them
29 changes: 8 additions & 21 deletions ext/prism/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,28 +398,15 @@ 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);

pm_node_t *node = pm_parse(&parser);
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);
Expand Down Expand Up @@ -469,26 +456,26 @@ 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);

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;
Expand Down Expand Up @@ -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);

Expand Down
5 changes: 2 additions & 3 deletions include/prism.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions lib/prism/ffi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def self.load_exported_functions_from(header, *functions)
"prism.h",
"pm_version",
"pm_parse_serialize",
"pm_parse_serialize_inline_comments",
"pm_parse_serialize_comments",
"pm_lex_serialize",
"pm_parse_lex_serialize"
)
Expand Down Expand Up @@ -225,11 +225,11 @@ def self.parse_file(filepath)
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)
Expand All @@ -240,12 +240,12 @@ def self.parse_inline_comments(code, filepath = nil)
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

Expand Down
5 changes: 2 additions & 3 deletions src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 04d6994

Please sign in to comment.