Skip to content

Commit d4362b3

Browse files
committed
Set "trailing" on the comment node
Comment nodes can know if they are trailing comments by looking at the previous token. If we set this on the comment object then we don't need to look at source slices when attaching comments
1 parent 434c4c5 commit d4362b3

File tree

6 files changed

+30
-20
lines changed

6 files changed

+30
-20
lines changed

ext/prism/extension.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,12 @@ parser_location(const pm_parser_t *parser, VALUE source, bool freeze, const uint
471471
*/
472472
static inline VALUE
473473
parser_comment(const pm_parser_t *parser, VALUE source, bool freeze, const pm_comment_t *comment) {
474-
VALUE argv[] = { PARSER_LOCATION_LOC(parser, source, freeze, comment->location) };
474+
VALUE argv[] = {
475+
comment->trailing ? Qtrue : Qfalse,
476+
PARSER_LOCATION_LOC(parser, source, freeze, comment->location)
477+
};
475478
VALUE type = (comment->type == PM_COMMENT_EMBDOC) ? rb_cPrismEmbDocComment : rb_cPrismInlineComment;
476-
return rb_class_new_instance_freeze(1, argv, type, freeze);
479+
return rb_class_new_instance_freeze(2, argv, type, freeze);
477480
}
478481

479482
/**

include/prism/parser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,8 @@ typedef struct pm_comment {
467467

468468
/** The type of comment that we've found. */
469469
pm_comment_type_t type;
470+
471+
bool trailing;
470472
} pm_comment_t;
471473

472474
/**

lib/prism/parse_result.rb

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -513,8 +513,15 @@ class Comment
513513
# The location of this comment in the source.
514514
attr_reader :location
515515

516+
# Returns true if this comment happens on the same line as other code and
517+
# false if the comment is by itself.
518+
def trailing?
519+
@trailing
520+
end
521+
516522
# Create a new comment object with the given location.
517-
def initialize(location)
523+
def initialize(trailing, location)
524+
@trailing = trailing
518525
@location = location
519526
end
520527

@@ -532,12 +539,6 @@ def slice
532539
# InlineComment objects are the most common. They correspond to comments in
533540
# the source file like this one that start with #.
534541
class InlineComment < Comment
535-
# Returns true if this comment happens on the same line as other code and
536-
# false if the comment is by itself.
537-
def trailing?
538-
!location.start_line_slice.strip.empty?
539-
end
540-
541542
# Returns a string representation of this comment.
542543
def inspect
543544
"#<Prism::InlineComment @location=#{location.inspect}>"
@@ -547,11 +548,6 @@ def inspect
547548
# EmbDocComment objects correspond to comments that are surrounded by =begin
548549
# and =end.
549550
class EmbDocComment < Comment
550-
# This can only be true for inline comments.
551-
def trailing?
552-
false
553-
end
554-
555551
# Returns a string representation of this comment.
556552
def inspect
557553
"#<Prism::EmbDocComment @location=#{location.inspect}>"

lib/prism/parse_result/comments.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def nearest_targets(node, comment)
122122
comment_end = comment.location.end_offset
123123

124124
targets = [] #: Array[_Target]
125-
node.comment_targets.map do |value|
125+
node.comment_targets.each do |value|
126126
case value
127127
when StatementsNode
128128
targets.concat(value.body.map { |node| NodeTarget.new(node) })

src/prism.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9291,12 +9291,13 @@ parser_lex_callback(pm_parser_t *parser) {
92919291
* Return a new comment node of the specified type.
92929292
*/
92939293
static inline pm_comment_t *
9294-
parser_comment(pm_parser_t *parser, pm_comment_type_t type) {
9294+
parser_comment(pm_parser_t *parser, pm_comment_type_t type, bool trailing) {
92959295
pm_comment_t *comment = (pm_comment_t *) xcalloc(1, sizeof(pm_comment_t));
92969296
if (comment == NULL) return NULL;
92979297

92989298
*comment = (pm_comment_t) {
92999299
.type = type,
9300+
.trailing = trailing,
93009301
.location = { parser->current.start, parser->current.end }
93019302
};
93029303

@@ -9324,7 +9325,7 @@ lex_embdoc(pm_parser_t *parser) {
93249325
parser_lex_callback(parser);
93259326

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

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

98379842
if (ending) parser->current.end++;

templates/lib/prism/serialize.rb.erb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,8 @@ module Prism
295295
Array.new(load_varuint) do
296296
comment =
297297
case load_varuint
298-
when 0 then InlineComment.new(load_location_object(freeze))
299-
when 1 then EmbDocComment.new(load_location_object(freeze))
298+
when 0 then InlineComment.new(load_boolean, load_location_object(freeze))
299+
when 1 then EmbDocComment.new(load_boolean, load_location_object(freeze))
300300
end
301301

302302
comment.freeze if freeze
@@ -450,6 +450,10 @@ module Prism
450450
value
451451
end
452452

453+
def load_boolean
454+
io.getbyte != 0
455+
end
456+
453457
def load_double
454458
io.read(8).unpack1("D")
455459
end

0 commit comments

Comments
 (0)