1#include "prism/internal/comments.h"
3#include "prism/internal/arena.h"
4#include "prism/internal/assert.h"
5#include "prism/internal/char.h"
6#include "prism/internal/memchr.h"
7#include "prism/internal/node.h"
8#include "prism/internal/parser.h"
21 const uint8_t *source;
40 const pm_node_t **attached;
57 const pm_node_t **nodes;
69pm_comments_sort(
pm_parser_t *parser, uint32_t size) {
73 for (pm_list_node_t *node = parser->comment_list.head; node != NULL; node = node->next) {
75 uint32_t position = index++;
76 while (position > 0 && comments[position - 1]->location.start > comment->location.
start) {
77 comments[position] = comments[position - 1];
83 for (index = 0; index + 1 < size; index++) {
87 comments[size - 1]->node.next = NULL;
88 parser->comment_list.head = &
comments[0]->node;
89 parser->comment_list.tail = &
comments[size - 1]->node;
99pm_comments_lower_bound(
const pm_comments_walk_t *walk, uint32_t left, uint32_t right, uint32_t offset) {
100 while (left < right) {
101 uint32_t middle = left + (right - left) / 2;
103 if (walk->starts[middle] < offset) {
121 const uint32_t *starts = walk->starts;
123 uint32_t size = walk->size;
124 uint32_t cursor = walk->cursor;
129 if (cursor == size || starts[cursor] >= offset) {
137 while (left > 0 && starts[left - 1] >= offset) {
139 left = (left - 1 > step) ? left - 1 - step : 0;
150 while (right < size && starts[right] < offset) {
152 right = (size - left > step) ? left + step : size;
157 walk->cursor = pm_comments_lower_bound(walk, left, right, offset);
167 uint32_t *unclaimed = walk->unclaimed;
169 while (unclaimed[index] != index) {
170 unclaimed[index] = unclaimed[unclaimed[index]];
171 index = unclaimed[index];
185 uint32_t index = pm_comments_lower_bound(walk, 0, walk->size, offset);
188 offset -= (uint32_t) pm_strrspn_whitespace(walk->source, (ptrdiff_t) offset);
189 if (index == 0)
return offset;
193 const pm_location_t *location = &walk->comments[index - 1]->location;
194 if (location->
start >= offset || PM_LOCATION_END(location) < offset)
return offset;
196 offset = location->
start;
208pm_comments_blank_after(
const pm_comments_walk_t *walk, uint32_t offset, uint32_t end) {
209 uint32_t index = pm_comments_lower_bound(walk, 0, walk->size, offset);
212 offset += (uint32_t) pm_strspn_whitespace(walk->source + offset, (ptrdiff_t) end - (ptrdiff_t) offset);
213 if (index == walk->size || walk->starts[index] != offset)
return offset;
215 offset = PM_LOCATION_END(&walk->comments[index]->location);
226pm_comments_push(
const pm_node_t *node,
void *data) {
229 if (walk->stack.size == walk->stack.capacity) {
230 size_t capacity = walk->stack.capacity == 0 ? 64 : walk->stack.capacity * 2;
231 const pm_node_t **nodes = PM_ARENA_ALLOC_ARRAY(walk->arena,
const pm_node_t *, capacity);
232 if (walk->stack.size > 0) memcpy((
void *) nodes, (
const void *) walk->stack.nodes,
sizeof(pm_node_t *) * walk->stack.size);
234 walk->stack.nodes = nodes;
235 walk->stack.capacity = capacity;
238 walk->stack.nodes[walk->stack.size++] = node;
249 size_t base = walk->stack.size;
250 pm_visit_child_nodes(node, pm_comments_push, walk);
251 size_t top = walk->stack.size;
254 for (
size_t index = base + 1; index < top; index++) {
255 const pm_node_t *child = walk->stack.nodes[index];
256 size_t insert = index;
259 walk->stack.nodes[insert] = walk->stack.nodes[insert - 1];
263 walk->stack.nodes[insert] = child;
267 for (
size_t index = base; index < top; index++) {
268 pm_comments_visit(walk, walk->stack.nodes[index]);
272 uint32_t end = PM_LOCATION_END(&node->
location);
273 uint32_t after = pm_comments_seek(walk, end);
280 after < walk->size &&
281 !PM_NODE_TYPE_P(node, PM_PROGRAM_NODE) &&
282 !PM_NODE_TYPE_P(node, PM_STATEMENTS_NODE) &&
285 uint32_t comment_start = walk->starts[after];
287 if (pm_memchr(walk->source + end,
'\n', comment_start - end) == NULL) {
288 const pm_node_t *attached = walk->attached[after];
289 uint32_t attached_end = attached == NULL ? 0 : PM_LOCATION_END(&attached->
location);
291 if (end >= attached_end || attached_end > comment_start) {
292 walk->attached[after] = node;
305 if (after > 0 && walk->starts[after - 1] >= start) {
306 uint32_t index = pm_comments_unclaimed(walk, pm_comments_lower_bound(walk, 0, after, start));
307 size_t child_index = base;
312 const pm_node_t *leading = NULL;
313 uint32_t leading_boundary = 0;
314 const pm_node_t *trailing = NULL;
315 uint32_t trailing_boundary = 0;
317 while (index < after && PM_LOCATION_END(&walk->comments[index]->location) <= end) {
318 if (walk->attached[index] == NULL) {
319 uint32_t comment_end = PM_LOCATION_END(&walk->comments[index]->location);
320 const pm_node_t *attached = node;
322 while (child_index < top && walk->stack.nodes[child_index]->location.start < comment_end) child_index++;
324 if (child_index < top) {
325 const pm_node_t *child = walk->stack.nodes[child_index];
327 if (PM_NODE_TYPE_P(child, PM_STATEMENTS_NODE)) {
328 if (child != leading) {
330 leading_boundary = pm_comments_blank_before(walk, child->
location.
start);
333 if (walk->starts[index] >= leading_boundary) attached = child;
337 if (attached == node && child_index > base) {
338 const pm_node_t *child = walk->stack.nodes[child_index - 1];
345 if (PM_NODE_TYPE_P(child, PM_RESCUE_NODE)) {
346 const pm_rescue_node_t *rescue = (
const pm_rescue_node_t *) child;
348 child = (
const pm_node_t *) rescue->
statements;
351 if (child != NULL && PM_NODE_TYPE_P(child, PM_STATEMENTS_NODE) && PM_LOCATION_END(&child->
location) <= walk->starts[index]) {
352 if (child != trailing) {
354 trailing_boundary = pm_comments_blank_after(walk, PM_LOCATION_END(&child->
location), end);
357 if (comment_end <= trailing_boundary) attached = child;
361 walk->attached[index] = attached;
364 walk->unclaimed[index] = index + 1;
365 index = pm_comments_unclaimed(walk, index + 1);
370 walk->stack.size = base;
385pm_comments_attach(
pm_parser_t *parser,
const pm_node_t *root) {
386 PRISM_ASSERT(PM_NODE_TYPE_P(root, PM_PROGRAM_NODE));
388 uint32_t size = (uint32_t) parser->comment_list.size;
389 if (size == 0)
return;
394 .source = parser->start,
395 .comments = pm_comments_sort(parser, size),
397 .starts = PM_ARENA_ALLOC_ARRAY(arena, uint32_t, size),
398 .attached = PM_ARENA_ZALLOC_ARRAY(arena,
const pm_node_t *, size),
399 .unclaimed = PM_ARENA_ALLOC_ARRAY(arena, uint32_t, size + 1)
402 for (uint32_t index = 0; index < size; index++) walk.starts[index] = walk.comments[index]->location.
start;
403 for (uint32_t index = 0; index <= size; index++) walk.unclaimed[index] = index;
405 pm_comments_visit(&walk, root);
413 const pm_node_t *statements = (
const pm_node_t *) ((
const pm_program_node_t *) root)->statements;
414 uint32_t ranges_size = 0;
416 for (uint32_t index = 0; index < size; index++) {
417 if (walk.attached[index] == NULL) walk.attached[index] = statements;
419 uint32_t node_id = walk.attached[index]->
node_id;
420 PRISM_ASSERT(node_id <= parser->node_id);
421 if (node_id >= ranges_size) ranges_size = node_id + 1;
427 uint32_t *attachments = PM_ARENA_ALLOC_ARRAY(&parser->metadata_arena, uint32_t, size);
429 for (uint32_t index = 0; index < size; index++) {
434 for (uint32_t index = 0; index < ranges_size; index++) {
435 ranges[index].
index = offset;
436 offset += ranges[index].
length;
440 for (uint32_t index = 0; index < size; index++) {
442 attachments[range->index + range->length++] = index;
446 .comments = walk.comments,
447 .attachments = attachments,
449 .ranges_size = ranges_size
comment_range comments(const pm_parser_t *parser PRISM_LIFETIMEBOUND) noexcept
The comments found while parsing, in source order.
pm_visit_result_t
The value returned by a visitor callback, which determines where the walk goes next.
@ PM_VISIT_SKIP
Continue the walk without visiting this node's children.
This struct represents a slice in the source code, defined by an offset and a length.
uint32_t start
The offset of the location from the start of the source.
uint32_t length
The length of the location.
uint32_t node_id
The unique identifier for this node, which is deterministic based on the source.
pm_location_t location
This is the location of the node in the source.
PM_NODE_ALIGNAS struct pm_statements_node * statements
RescueNode::statements.
PM_NODE_ALIGNAS struct pm_rescue_node * subsequent
RescueNode::subsequent.