Skip to content

Commit 00b76ef

Browse files
authored
Fix locations derived from arguments. (#1897)
1 parent 5046d49 commit 00b76ef

File tree

4 files changed

+39
-45
lines changed

4 files changed

+39
-45
lines changed

src/prism.c

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,27 @@ typedef struct {
796796
pm_node_t *block;
797797
} pm_arguments_t;
798798

799+
/**
800+
* Retrieve the end location of a `pm_arguments_t` object.
801+
*/
802+
static inline const uint8_t *
803+
pm_arguments_end(pm_arguments_t *arguments) {
804+
if (arguments->block != NULL) {
805+
const uint8_t *end = arguments->block->location.end;
806+
if (arguments->closing_loc.start != NULL && arguments->closing_loc.end > end) {
807+
end = arguments->closing_loc.end;
808+
}
809+
return end;
810+
}
811+
if (arguments->closing_loc.start != NULL) {
812+
return arguments->closing_loc.end;
813+
}
814+
if (arguments->arguments != NULL) {
815+
return arguments->arguments->base.location.end;
816+
}
817+
return arguments->closing_loc.end;
818+
}
819+
799820
/**
800821
* Check that we're not about to attempt to attach a brace block to a call that
801822
* has arguments without parentheses.
@@ -1611,11 +1632,7 @@ pm_call_node_aref_create(pm_parser_t *parser, pm_node_t *receiver, pm_arguments_
16111632
pm_call_node_t *node = pm_call_node_create(parser);
16121633

16131634
node->base.location.start = receiver->location.start;
1614-
if (arguments->block != NULL) {
1615-
node->base.location.end = arguments->block->location.end;
1616-
} else {
1617-
node->base.location.end = arguments->closing_loc.end;
1618-
}
1635+
node->base.location.end = pm_arguments_end(arguments);
16191636

16201637
node->receiver = receiver;
16211638
node->message_loc.start = arguments->opening_loc.start;
@@ -1664,15 +1681,11 @@ pm_call_node_call_create(pm_parser_t *parser, pm_node_t *receiver, pm_token_t *o
16641681
pm_call_node_t *node = pm_call_node_create(parser);
16651682

16661683
node->base.location.start = receiver->location.start;
1667-
if (arguments->block != NULL) {
1668-
node->base.location.end = arguments->block->location.end;
1669-
} else if (arguments->closing_loc.start != NULL) {
1670-
node->base.location.end = arguments->closing_loc.end;
1671-
} else if (arguments->arguments != NULL) {
1672-
node->base.location.end = arguments->arguments->base.location.end;
1673-
} else {
1674-
node->base.location.end = message->end;
1684+
const uint8_t *end = pm_arguments_end(arguments);
1685+
if (end == NULL) {
1686+
end = message->end;
16751687
}
1688+
node->base.location.end = end;
16761689

16771690
node->receiver = receiver;
16781691
node->call_operator_loc = PM_OPTIONAL_LOCATION_TOKEN_VALUE(operator);
@@ -1699,15 +1712,7 @@ pm_call_node_fcall_create(pm_parser_t *parser, pm_token_t *message, pm_arguments
16991712
pm_call_node_t *node = pm_call_node_create(parser);
17001713

17011714
node->base.location.start = message->start;
1702-
if (arguments->block != NULL) {
1703-
node->base.location.end = arguments->block->location.end;
1704-
} else if (arguments->closing_loc.start != NULL) {
1705-
node->base.location.end = arguments->closing_loc.end;
1706-
} else if (arguments->arguments != NULL) {
1707-
node->base.location.end = arguments->arguments->base.location.end;
1708-
} else {
1709-
node->base.location.end = arguments->closing_loc.end;
1710-
}
1715+
node->base.location.end = pm_arguments_end(arguments);
17111716

17121717
node->message_loc = PM_OPTIONAL_LOCATION_TOKEN_VALUE(message);
17131718
node->opening_loc = arguments->opening_loc;
@@ -1755,11 +1760,7 @@ pm_call_node_shorthand_create(pm_parser_t *parser, pm_node_t *receiver, pm_token
17551760
pm_call_node_t *node = pm_call_node_create(parser);
17561761

17571762
node->base.location.start = receiver->location.start;
1758-
if (arguments->block != NULL) {
1759-
node->base.location.end = arguments->block->location.end;
1760-
} else {
1761-
node->base.location.end = arguments->closing_loc.end;
1762-
}
1763+
node->base.location.end = pm_arguments_end(arguments);
17631764

17641765
node->receiver = receiver;
17651766
node->call_operator_loc = PM_OPTIONAL_LOCATION_TOKEN_VALUE(operator);
@@ -5136,16 +5137,9 @@ pm_super_node_create(pm_parser_t *parser, const pm_token_t *keyword, pm_argument
51365137
assert(keyword->type == PM_TOKEN_KEYWORD_SUPER);
51375138
pm_super_node_t *node = PM_ALLOC_NODE(parser, pm_super_node_t);
51385139

5139-
const uint8_t *end;
5140-
if (arguments->block != NULL) {
5141-
end = arguments->block->location.end;
5142-
} else if (arguments->closing_loc.start != NULL) {
5143-
end = arguments->closing_loc.end;
5144-
} else if (arguments->arguments != NULL) {
5145-
end = arguments->arguments->base.location.end;
5146-
} else {
5140+
const uint8_t *end = pm_arguments_end(arguments);
5141+
if (end == NULL) {
51475142
assert(false && "unreachable");
5148-
end = NULL;
51495143
}
51505144

51515145
*node = (pm_super_node_t) {

test/prism/snapshots/super.txt

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/prism/snapshots/unparser/corpus/literal/send.txt

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/prism/snapshots/unparser/corpus/literal/super.txt

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)