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
3 changes: 0 additions & 3 deletions include/prism/internal/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ typedef enum {
/* Append a slice of source code to the buffer. */
void pm_buffer_append_source(pm_buffer_t *buffer, const uint8_t *source, size_t length, pm_buffer_escaping_t escaping);

/* Prepend the given string to the buffer. */
void pm_buffer_prepend_string(pm_buffer_t *buffer, const char *value, size_t length);

/* Concatenate one buffer onto another. */
void pm_buffer_concat(pm_buffer_t *destination, const pm_buffer_t *source);

Expand Down
14 changes: 1 addition & 13 deletions src/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pm_buffer_length(const pm_buffer_t *buffer) {
* Append the given amount of space to the buffer. Returns false when there is
* nothing to write because the length is zero. A zero-initialized buffer has a
* NULL value until its first allocation, and passing NULL to
* memcpy/memset/memmove is undefined behavior even for zero bytes, so callers
* memcpy or memset is undefined behavior even for zero bytes, so callers
* must not write in the zero case.
*
* The value and capacity are only updated together once the allocation has
Expand Down Expand Up @@ -392,18 +392,6 @@ pm_buffer_append_source(pm_buffer_t *buffer, const uint8_t *source, size_t lengt
}
}

/**
* Prepend the given string to the buffer.
*/
void
pm_buffer_prepend_string(pm_buffer_t *buffer, const char *value, size_t length) {
size_t cursor = buffer->length;
if (pm_buffer_append_length(buffer, length)) {
memmove(buffer->value + length, buffer->value, cursor);
memcpy(buffer->value, value, length);
}
}

/**
* Concatenate one buffer onto another.
*/
Expand Down
106 changes: 34 additions & 72 deletions src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -13227,66 +13227,43 @@ parse_starred_expression(pm_parser_t *parser, pm_binding_power_t binding_power,
return parse_value_expression(parser, binding_power, flags, diag_id, depth);
}

/* Block-exit validation and implicit-parameter diagnostics depend on the order
* of the remaining nodes. */
static void
pm_scratch_node_list_remove(pm_scratch_node_list_t *list, const pm_node_t *node) {
for (uint32_t index = 0; index < list->size; index++) {
if (list->nodes[index] == node) {
list->size--;
for (; index < list->size; index++) {
list->nodes[index] = list->nodes[index + 1];
}
return;
}
}
}

static pm_visit_result_t
pm_node_unreference_each(const pm_node_t *node, void *data) {
pm_parser_t *parser = (pm_parser_t *) data;

switch (PM_NODE_TYPE(node)) {
/* When we are about to destroy a set of nodes that could potentially
* contain block exits for the current scope, we need to check if they
* are contained in the list of block exits and remove them if they are.
*/
case PM_BREAK_NODE:
case PM_NEXT_NODE:
case PM_REDO_NODE: {
pm_parser_t *parser = (pm_parser_t *) data;
size_t index = 0;

while (index < parser->current_block_exits->size) {
pm_node_t *block_exit = parser->current_block_exits->nodes[index];

if (block_exit == node) {
if (index + 1 < parser->current_block_exits->size) {
memmove(
&parser->current_block_exits->nodes[index],
&parser->current_block_exits->nodes[index + 1],
(parser->current_block_exits->size - index - 1) * sizeof(pm_node_t *)
);
}
parser->current_block_exits->size--;

/* Note descending here because these nodes could have
* arguments that are themselves block exits. */
return PM_VISIT_DESCEND;
}

index++;
}

case PM_REDO_NODE:
pm_scratch_node_list_remove(parser->current_block_exits, node);
/* The arguments can themselves contain block exits. */
return PM_VISIT_DESCEND;
}
/* When an implicit local variable is written to or targeted, it becomes
* a regular, named local variable. This branch removes it from the list
* of implicit parameters when that happens. */
case PM_LOCAL_VARIABLE_READ_NODE:
case PM_IT_LOCAL_VARIABLE_READ_NODE: {
pm_parser_t *parser = (pm_parser_t *) data;
pm_scratch_node_list_t *implicit_parameters = &parser->current_scope->implicit_parameters;

for (size_t index = 0; index < implicit_parameters->size; index++) {
if (implicit_parameters->nodes[index] == node) {
/* If the node is not the last one in the list, we need to
* shift the remaining nodes down to fill the gap. This is
* extremely unlikely to happen. */
if (index != implicit_parameters->size - 1) {
memmove(&implicit_parameters->nodes[index], &implicit_parameters->nodes[index + 1], (implicit_parameters->size - index - 1) * sizeof(pm_node_t *));
}

implicit_parameters->size--;
break;
}
}

case PM_IT_LOCAL_VARIABLE_READ_NODE:
pm_scratch_node_list_remove(&parser->current_scope->implicit_parameters, node);
return PM_VISIT_SKIP;
}
default:
return PM_VISIT_DESCEND;
}
Expand Down Expand Up @@ -16817,44 +16794,29 @@ parse_method_definition_name(pm_parser_t *parser) {

static void
parse_heredoc_dedent_string(pm_arena_t *arena, pm_string_t *string, size_t common_whitespace) {
// Make a writable copy of the string in the arena, since strings only view
// their bytes. We keep a mutable pointer to the arena memory so we can
// memmove into it below without casting away const from the string's
// source field.
const uint8_t *source = pm_string_source(string);
size_t length = pm_string_length(string);
uint8_t *writable = (uint8_t *) pm_arena_memdup(arena, pm_string_source(string), length, PRISM_ALIGNOF(uint8_t));
pm_string_init(string, writable, length);

// Now get the bounds of the existing string. We'll use this as a
// destination to move bytes into. We'll also use it for bounds checking
// since we don't require that these strings be null terminated.
size_t dest_length = pm_string_length(string);
const uint8_t *source_cursor = writable;
const uint8_t *source_end = source_cursor + dest_length;

// We're going to move bytes backward in the string when we get leading
// whitespace, so we'll maintain a pointer to the current position in the
// string that we're writing to.
size_t trimmed_whitespace = 0;

// While we haven't reached the amount of common whitespace that we need to
// trim and we haven't reached the end of the string, we'll keep trimming
// whitespace. Trimming in this context means skipping over these bytes such
// that they aren't copied into the new string.
while ((source_cursor < source_end) && pm_char_is_inline_whitespace(*source_cursor) && trimmed_whitespace < common_whitespace) {
if (*source_cursor == '\t') {
while (length && trimmed_whitespace < common_whitespace && pm_char_is_inline_whitespace(*source)) {
if (*source == '\t') {
// Keep a tab that would extend past the common indentation.
trimmed_whitespace = (trimmed_whitespace / PM_TAB_WHITESPACE_SIZE + 1) * PM_TAB_WHITESPACE_SIZE;
if (trimmed_whitespace > common_whitespace) break;
} else {
trimmed_whitespace++;
}

source_cursor++;
dest_length--;
source++;
length--;
}

memmove(writable, source_cursor, (size_t) (source_end - source_cursor));
string->length = dest_length;
if (length) {
const uint8_t *dedented = pm_arena_memdup(arena, source, length, PRISM_ALIGNOF(uint8_t));
pm_string_init(string, dedented, length);
} else {
*string = PM_STRING_EMPTY;
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions test/prism/errors/3.4/it_with_ordinary_parameter.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
proc { || it }
^~ 'it' is not allowed when an ordinary parameter is defined
proc { || it _1, it }
^~ numbered parameters are not allowed when an ordinary parameter is defined

Loading