Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions ext/prism/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,12 @@ parser_location(const pm_parser_t *parser, VALUE source, bool freeze, const uint
*/
static inline VALUE
parser_comment(const pm_parser_t *parser, VALUE source, bool freeze, const pm_comment_t *comment) {
VALUE argv[] = { PARSER_LOCATION_LOC(parser, source, freeze, comment->location) };
VALUE argv[] = {
comment->trailing ? Qtrue : Qfalse,
PARSER_LOCATION_LOC(parser, source, freeze, comment->location)
};
VALUE type = (comment->type == PM_COMMENT_EMBDOC) ? rb_cPrismEmbDocComment : rb_cPrismInlineComment;
return rb_class_new_instance_freeze(1, argv, type, freeze);
return rb_class_new_instance_freeze(2, argv, type, freeze);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions include/prism/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,8 @@ typedef struct pm_comment {

/** The type of comment that we've found. */
pm_comment_type_t type;

bool trailing;
} pm_comment_t;

/**
Expand Down
20 changes: 8 additions & 12 deletions lib/prism/parse_result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,15 @@ class Comment
# The location of this comment in the source.
attr_reader :location

# Returns true if this comment happens on the same line as other code and
# false if the comment is by itself.
def trailing?
@trailing
end

# Create a new comment object with the given location.
def initialize(location)
def initialize(trailing, location)
@trailing = trailing
@location = location
end

Expand All @@ -532,12 +539,6 @@ def slice
# InlineComment objects are the most common. They correspond to comments in
# the source file like this one that start with #.
class InlineComment < Comment
# Returns true if this comment happens on the same line as other code and
# false if the comment is by itself.
def trailing?
!location.start_line_slice.strip.empty?
end

# Returns a string representation of this comment.
def inspect
"#<Prism::InlineComment @location=#{location.inspect}>"
Expand All @@ -547,11 +548,6 @@ def inspect
# EmbDocComment objects correspond to comments that are surrounded by =begin
# and =end.
class EmbDocComment < Comment
# This can only be true for inline comments.
def trailing?
false
end

# Returns a string representation of this comment.
def inspect
"#<Prism::EmbDocComment @location=#{location.inspect}>"
Expand Down
11 changes: 8 additions & 3 deletions src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -9291,12 +9291,13 @@ parser_lex_callback(pm_parser_t *parser) {
* Return a new comment node of the specified type.
*/
static inline pm_comment_t *
parser_comment(pm_parser_t *parser, pm_comment_type_t type) {
parser_comment(pm_parser_t *parser, pm_comment_type_t type, bool trailing) {
pm_comment_t *comment = (pm_comment_t *) xcalloc(1, sizeof(pm_comment_t));
if (comment == NULL) return NULL;

*comment = (pm_comment_t) {
.type = type,
.trailing = trailing,
.location = { parser->current.start, parser->current.end }
};

Expand Down Expand Up @@ -9324,7 +9325,7 @@ lex_embdoc(pm_parser_t *parser) {
parser_lex_callback(parser);

// Now, create a comment that is going to be attached to the parser.
pm_comment_t *comment = parser_comment(parser, PM_COMMENT_EMBDOC);
pm_comment_t *comment = parser_comment(parser, PM_COMMENT_EMBDOC, false);
if (comment == NULL) return PM_TOKEN_EOF;

// Now, loop until we find the end of the embedded documentation or the end
Expand Down Expand Up @@ -9831,7 +9832,11 @@ parser_lex(pm_parser_t *parser) {
// If we found a comment while lexing, then we're going to
// add it to the list of comments in the file and keep
// lexing.
pm_comment_t *comment = parser_comment(parser, PM_COMMENT_INLINE);
bool trailing = true;
if (parser->previous.type == PM_TOKEN_EOF || parser->previous.type == PM_TOKEN_NEWLINE) {
trailing = false;
}
pm_comment_t *comment = parser_comment(parser, PM_COMMENT_INLINE, trailing);
pm_list_append(&parser->comment_list, (pm_list_node_t *) comment);

if (ending) parser->current.end++;
Expand Down
8 changes: 6 additions & 2 deletions templates/lib/prism/serialize.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ module Prism
Array.new(load_varuint) do
comment =
case load_varuint
when 0 then InlineComment.new(load_location_object(freeze))
when 1 then EmbDocComment.new(load_location_object(freeze))
when 0 then InlineComment.new(load_boolean, load_location_object(freeze))
when 1 then EmbDocComment.new(load_boolean, load_location_object(freeze))
end

comment.freeze if freeze
Expand Down Expand Up @@ -450,6 +450,10 @@ module Prism
value
end

def load_boolean
io.getbyte != 0
end

def load_double
io.read(8).unpack1("D")
end
Expand Down
3 changes: 3 additions & 0 deletions templates/src/serialize.c.erb
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ pm_serialize_comment(pm_parser_t *parser, pm_comment_t *comment, pm_buffer_t *bu
// serialize type
pm_buffer_append_byte(buffer, (uint8_t) comment->type);

// serialize trailing
pm_buffer_append_byte(buffer, (uint8_t) comment->trailing);

// serialize location
pm_serialize_location(parser, &comment->location, buffer);
}
Expand Down
Loading