Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: "2.7.0"
ruby-version: "2.7"
bundler-cache: true
- name: Generate templates
run: bundle exec rake templates
Expand Down
4 changes: 2 additions & 2 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ tokens:
comment: ","
- name: EMBEXPR_END
comment: "}"
- name: HEREDOC_END
comment: "the end of a heredoc"
- name: KEYWORD_DO
comment: "do"
- name: KEYWORD_ELSE
Expand Down Expand Up @@ -464,8 +466,6 @@ tokens:
comment: ">>"
- name: GREATER_GREATER_EQUAL
comment: ">>="
- name: HEREDOC_END
comment: "the end of a heredoc"
- name: HEREDOC_START
comment: "the start of a heredoc"
- name: IDENTIFIER
Expand Down
10 changes: 10 additions & 0 deletions include/prism/internal/line_offset_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ void pm_line_offset_list_clear(pm_line_offset_list_t *list);
/* Append a new offset to the list (slow path with resize). */
void pm_line_offset_list_append_slow(pm_arena_t *arena, pm_line_offset_list_t *list, uint32_t cursor);

/* Add the zero-based line index to the starting line with defined 32-bit
* wraparound. Keep both the addition and conversion free of signed overflow. */
static PRISM_FORCE_INLINE int32_t
pm_line_offset_list_line_number(size_t index, int32_t start_line) {
uint32_t line = (uint32_t) index + (uint32_t) start_line;
if (line <= INT32_MAX) return (int32_t) line;

return INT32_MIN + (int32_t) (line - ((uint32_t) INT32_MAX + 1));
}

/* Append a new offset to the list. */
static PRISM_FORCE_INLINE void
pm_line_offset_list_append(pm_arena_t *arena, pm_line_offset_list_t *list, uint32_t cursor) {
Expand Down
13 changes: 13 additions & 0 deletions include/prism/internal/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@
/* The offset just past the end of the given location. */
#define PM_LOCATION_END(location_) ((location_)->start + (location_)->length)

/* Return the span covering two initialized locations. Read both endpoints
* before changing either field, so moving the start preserves the old end.
* An unset location must be initialized separately from the first child. */
static PRISM_FORCE_INLINE pm_location_t
pm_location_join(pm_location_t left, pm_location_t right) {
uint32_t start = left.start < right.start ? left.start : right.start;
uint32_t left_end = PM_LOCATION_END(&left);
uint32_t right_end = PM_LOCATION_END(&right);
uint32_t end = left_end > right_end ? left_end : right_end;
pm_location_t location = { .start = start, .length = end - start };
return location;
}

/*
* Slow path for pm_node_list_append_inline: grow the list and append the node.
* Do not call directly — use pm_node_list_append_inline instead.
Expand Down
2 changes: 1 addition & 1 deletion src/encoding.c
Original file line number Diff line number Diff line change
Expand Up @@ -1537,7 +1537,7 @@ pm_encoding_char_width(const pm_encoding_t *encoding, const uint8_t *b, ptrdiff_
case PM_ENCODING_KIND_ASCII:
return ((n > 0) && (*b < 0x80)) ? 1 : 0;
case PM_ENCODING_KIND_SINGLE_BYTE:
return 1;
return n > 0 ? 1 : 0;
case PM_ENCODING_KIND_EUC_JP:
return pm_encoding_euc_jp_char_width(b, n);
case PM_ENCODING_KIND_SHIFT_JIS:
Expand Down
8 changes: 4 additions & 4 deletions src/line_offset_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pm_line_offset_list_line(const pm_line_offset_list_t *list, uint32_t cursor, int
size_t mid = left + (right - left) / 2;

if (list->offsets[mid] == cursor) {
return ((int32_t) mid) + start_line;
return pm_line_offset_list_line_number(mid, start_line);
}

if (list->offsets[mid] < cursor) {
Expand All @@ -65,7 +65,7 @@ pm_line_offset_list_line(const pm_line_offset_list_t *list, uint32_t cursor, int
}
}

return ((int32_t) left) + start_line - 1;
return pm_line_offset_list_line_number(left - 1, start_line);
}

/**
Expand All @@ -82,7 +82,7 @@ pm_line_offset_list_line_column(const pm_line_offset_list_t *list, uint32_t curs
size_t mid = left + (right - left) / 2;

if (list->offsets[mid] == cursor) {
return ((pm_line_column_t) { ((int32_t) mid) + start_line, 0 });
return ((pm_line_column_t) { pm_line_offset_list_line_number(mid, start_line), 0 });
}

if (list->offsets[mid] < cursor) {
Expand All @@ -93,7 +93,7 @@ pm_line_offset_list_line_column(const pm_line_offset_list_t *list, uint32_t curs
}

return ((pm_line_column_t) {
.line = ((int32_t) left) + start_line - 1,
.line = pm_line_offset_list_line_number(left - 1, start_line),
.column = cursor - list->offsets[left - 1]
});
}
Loading
Loading