Skip to content

Commit aa27d61

Browse files
Earlopainkddnewton
authored andcommitted
Fix Prism.parse_comments locations for FFI backend
Offsets are not serialized but the locations require them to work correctly.
1 parent 07e63f7 commit aa27d61

5 files changed

Lines changed: 14 additions & 1 deletion

File tree

include/prism/internal/serialize.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
* PRISM_EXCLUDE_SERIALIZATION define. */
1515
#ifndef PRISM_EXCLUDE_SERIALIZATION
1616

17+
/*
18+
* Serialize the given list of offsets to the given buffer.
19+
*/
20+
void pm_serialize_line_offset_list(pm_line_offset_list_t *list, pm_buffer_t *buffer);
21+
1722
/*
1823
* Serialize the given list of comments to the given buffer.
1924
*/

src/prism.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23079,6 +23079,7 @@ pm_serialize_parse_comments(pm_buffer_t *buffer, const uint8_t *source, size_t s
2307923079
pm_serialize_header(buffer);
2308023080
pm_serialize_encoding(parser.encoding, buffer);
2308123081
pm_buffer_append_varsint(buffer, parser.start_line);
23082+
pm_serialize_line_offset_list(&parser.line_offsets, buffer);
2308223083
pm_serialize_comment_list(&parser.comment_list, buffer);
2308323084

2308423085
pm_parser_cleanup(&parser);

templates/lib/prism/serialize.rb.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,10 @@ module Prism
135135
loader.load_header
136136
loader.load_encoding
137137
start_line = loader.load_varsint
138+
offsets = loader.load_line_offsets(freeze)
138139

139140
source.replace_start_line(start_line)
141+
source.replace_offsets(offsets)
140142

141143
result = loader.load_comments(freeze)
142144
raise unless loader.eof?

templates/src/serialize.c.erb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ pm_serialize_node(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer) {
156156
}
157157
}
158158

159-
static void
159+
/*
160+
* Serialize the given list of offsets to the given buffer.
161+
*/
162+
void
160163
pm_serialize_line_offset_list(pm_line_offset_list_t *list, pm_buffer_t *buffer) {
161164
uint32_t size = pm_sizet_to_u32(list->size);
162165
pm_buffer_append_varuint(buffer, size);

test/prism/api/parse_comments_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ def test_parse_comments
99

1010
assert_kind_of Array, comments
1111
assert_equal 1, comments.length
12+
assert_equal 1, comments[0].location.start_line
13+
assert_equal "# foo", comments[0].slice
1214
end
1315

1416
def test_parse_file_comments

0 commit comments

Comments
 (0)