Skip to content

Commit d12e42d

Browse files
committed
Switch arguments over to using slices
1 parent 9558faa commit d12e42d

1 file changed

Lines changed: 59 additions & 54 deletions

File tree

src/prism.c

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,13 +1550,13 @@ pm_conditional_predicate(pm_parser_t *parser, pm_node_t *node, pm_conditional_pr
15501550
*/
15511551
typedef struct {
15521552
/** The optional location of the opening parenthesis or bracket. */
1553-
pm_location_t opening_loc;
1553+
pm_slice_t opening_loc;
15541554

15551555
/** The lazily-allocated optional arguments node. */
15561556
pm_arguments_node_t *arguments;
15571557

15581558
/** The optional location of the closing parenthesis or bracket. */
1559-
pm_location_t closing_loc;
1559+
pm_slice_t closing_loc;
15601560

15611561
/** The optional block attached to the call. */
15621562
pm_node_t *block;
@@ -1569,21 +1569,26 @@ typedef struct {
15691569
* Retrieve the end location of a `pm_arguments_t` object.
15701570
*/
15711571
static inline const uint8_t *
1572-
pm_arguments_end(pm_arguments_t *arguments) {
1572+
pm_arguments_end(const pm_parser_t *parser, pm_arguments_t *arguments) {
15731573
if (arguments->block != NULL) {
15741574
const uint8_t *end = arguments->block->location.end;
1575-
if (arguments->closing_loc.start != NULL && arguments->closing_loc.end > end) {
1576-
end = arguments->closing_loc.end;
1575+
1576+
if (arguments->closing_loc.length > 0) {
1577+
const uint8_t *arguments_end = parser->start + arguments->closing_loc.start + arguments->closing_loc.length;
1578+
1579+
if (arguments_end > end) {
1580+
end = arguments_end;
1581+
}
15771582
}
15781583
return end;
15791584
}
1580-
if (arguments->closing_loc.start != NULL) {
1581-
return arguments->closing_loc.end;
1585+
if (arguments->closing_loc.length > 0) {
1586+
return parser->start + arguments->closing_loc.start + arguments->closing_loc.length;
15821587
}
15831588
if (arguments->arguments != NULL) {
15841589
return arguments->arguments->base.location.end;
15851590
}
1586-
return arguments->closing_loc.end;
1591+
return NULL;
15871592
}
15881593

15891594
/**
@@ -1594,7 +1599,7 @@ static void
15941599
pm_arguments_validate_block(pm_parser_t *parser, pm_arguments_t *arguments, pm_block_node_t *block) {
15951600
// First, check that we have arguments and that we don't have a closing
15961601
// location for them.
1597-
if (arguments->arguments == NULL || arguments->closing_loc.start != NULL) {
1602+
if (arguments->arguments == NULL || arguments->closing_loc.length > 0) {
15981603
return;
15991604
}
16001605

@@ -2550,15 +2555,15 @@ pm_call_node_aref_create(pm_parser_t *parser, pm_node_t *receiver, pm_arguments_
25502555
pm_call_node_t *node = pm_call_node_create(parser, flags);
25512556

25522557
node->base.location.start = receiver->location.start;
2553-
node->base.location.end = pm_arguments_end(arguments);
2558+
node->base.location.end = pm_arguments_end(parser, arguments);
25542559

25552560
node->receiver = receiver;
2556-
node->message_loc.start = (arguments->opening_loc.start == NULL ? 0 : ((uint32_t) (arguments->opening_loc.start - parser->start)));
2557-
node->message_loc.length = (arguments->closing_loc.end == NULL ? 0 : ((uint32_t) (arguments->closing_loc.end - arguments->opening_loc.start)));
2561+
node->message_loc.start = arguments->opening_loc.start;
2562+
node->message_loc.length = (arguments->closing_loc.start + arguments->closing_loc.length) - arguments->opening_loc.start;
25582563

2559-
node->opening_loc = arguments->opening_loc.start == NULL ? ((pm_slice_t) { 0 }) : LOCATION2SLICE(parser, &arguments->opening_loc);
2564+
node->opening_loc = arguments->opening_loc;
25602565
node->arguments = arguments->arguments;
2561-
node->closing_loc = arguments->closing_loc.start == NULL ? ((pm_slice_t) { 0 }) : LOCATION2SLICE(parser, &arguments->closing_loc);
2566+
node->closing_loc = arguments->closing_loc;
25622567
node->block = arguments->block;
25632568

25642569
node->name = pm_parser_constant_id_constant(parser, "[]", 2);
@@ -2601,7 +2606,7 @@ pm_call_node_call_create(pm_parser_t *parser, pm_node_t *receiver, pm_token_t *o
26012606
pm_call_node_t *node = pm_call_node_create(parser, pm_call_node_ignore_visibility_flag(receiver));
26022607

26032608
node->base.location.start = receiver->location.start;
2604-
const uint8_t *end = pm_arguments_end(arguments);
2609+
const uint8_t *end = pm_arguments_end(parser, arguments);
26052610
if (end == NULL) {
26062611
end = message->end;
26072612
}
@@ -2610,9 +2615,9 @@ pm_call_node_call_create(pm_parser_t *parser, pm_node_t *receiver, pm_token_t *o
26102615
node->receiver = receiver;
26112616
node->call_operator_loc = TOKEN2SLICE(parser, operator);
26122617
node->message_loc = TOKEN2SLICE(parser, message);
2613-
node->opening_loc = arguments->opening_loc.start == NULL ? ((pm_slice_t) { 0 }) : LOCATION2SLICE(parser, &arguments->opening_loc);
2618+
node->opening_loc = arguments->opening_loc;
26142619
node->arguments = arguments->arguments;
2615-
node->closing_loc = arguments->closing_loc.start == NULL ? ((pm_slice_t) { 0 }) : LOCATION2SLICE(parser, &arguments->closing_loc);
2620+
node->closing_loc = arguments->closing_loc;
26162621
node->block = arguments->block;
26172622

26182623
if (operator->type == PM_TOKEN_AMPERSAND_DOT) {
@@ -2652,12 +2657,12 @@ pm_call_node_fcall_create(pm_parser_t *parser, pm_token_t *message, pm_arguments
26522657
pm_call_node_t *node = pm_call_node_create(parser, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY);
26532658

26542659
node->base.location.start = message->start;
2655-
node->base.location.end = pm_arguments_end(arguments);
2660+
node->base.location.end = pm_arguments_end(parser, arguments);
26562661

26572662
node->message_loc = TOKEN2SLICE(parser, message);
2658-
node->opening_loc = arguments->opening_loc.start == NULL ? ((pm_slice_t) { 0 }) : LOCATION2SLICE(parser, &arguments->opening_loc);
2663+
node->opening_loc = arguments->opening_loc;
26592664
node->arguments = arguments->arguments;
2660-
node->closing_loc = arguments->closing_loc.start == NULL ? ((pm_slice_t) { 0 }) : LOCATION2SLICE(parser, &arguments->closing_loc);
2665+
node->closing_loc = arguments->closing_loc;
26612666
node->block = arguments->block;
26622667

26632668
node->name = pm_parser_constant_id_token(parser, message);
@@ -2690,18 +2695,18 @@ pm_call_node_not_create(pm_parser_t *parser, pm_node_t *receiver, pm_token_t *me
26902695
pm_call_node_t *node = pm_call_node_create(parser, receiver == NULL ? 0 : pm_call_node_ignore_visibility_flag(receiver));
26912696

26922697
node->base.location.start = message->start;
2693-
if (arguments->closing_loc.start != NULL) {
2694-
node->base.location.end = arguments->closing_loc.end;
2698+
if (arguments->closing_loc.length > 0) {
2699+
node->base.location.end = parser->start + arguments->closing_loc.start + arguments->closing_loc.length;
26952700
} else {
26962701
assert(receiver != NULL);
26972702
node->base.location.end = receiver->location.end;
26982703
}
26992704

27002705
node->receiver = receiver;
27012706
node->message_loc = TOKEN2SLICE(parser, message);
2702-
node->opening_loc = arguments->opening_loc.start == NULL ? ((pm_slice_t) { 0 }) : LOCATION2SLICE(parser, &arguments->opening_loc);
2707+
node->opening_loc = arguments->opening_loc;
27032708
node->arguments = arguments->arguments;
2704-
node->closing_loc = arguments->closing_loc.start == NULL ? ((pm_slice_t) { 0 }) : LOCATION2SLICE(parser, &arguments->closing_loc);
2709+
node->closing_loc = arguments->closing_loc;
27052710

27062711
node->name = pm_parser_constant_id_constant(parser, "!", 1);
27072712
return node;
@@ -2717,13 +2722,13 @@ pm_call_node_shorthand_create(pm_parser_t *parser, pm_node_t *receiver, pm_token
27172722
pm_call_node_t *node = pm_call_node_create(parser, pm_call_node_ignore_visibility_flag(receiver));
27182723

27192724
node->base.location.start = receiver->location.start;
2720-
node->base.location.end = pm_arguments_end(arguments);
2725+
node->base.location.end = pm_arguments_end(parser, arguments);
27212726

27222727
node->receiver = receiver;
27232728
node->call_operator_loc = TOKEN2SLICE(parser, operator);
2724-
node->opening_loc = arguments->opening_loc.start == NULL ? ((pm_slice_t) { 0 }) : LOCATION2SLICE(parser, &arguments->opening_loc);
2729+
node->opening_loc = arguments->opening_loc;
27252730
node->arguments = arguments->arguments;
2726-
node->closing_loc = arguments->closing_loc.start == NULL ? ((pm_slice_t) { 0 }) : LOCATION2SLICE(parser, &arguments->closing_loc);
2731+
node->closing_loc = arguments->closing_loc;
27272732
node->block = arguments->block;
27282733

27292734
if (operator->type == PM_TOKEN_AMPERSAND_DOT) {
@@ -6251,17 +6256,17 @@ pm_super_node_create(pm_parser_t *parser, const pm_token_t *keyword, pm_argument
62516256
assert(keyword->type == PM_TOKEN_KEYWORD_SUPER);
62526257
pm_super_node_t *node = PM_NODE_ALLOC(parser, pm_super_node_t);
62536258

6254-
const uint8_t *end = pm_arguments_end(arguments);
6259+
const uint8_t *end = pm_arguments_end(parser, arguments);
62556260
if (end == NULL) {
62566261
assert(false && "unreachable");
62576262
}
62586263

62596264
*node = (pm_super_node_t) {
62606265
.base = PM_NODE_INIT(parser, PM_SUPER_NODE, 0, keyword->start, end),
62616266
.keyword_loc = TOKEN2SLICE(parser, keyword),
6262-
.lparen_loc = arguments->opening_loc.start == NULL ? ((pm_slice_t) { 0 }) : LOCATION2SLICE(parser, &arguments->opening_loc),
6267+
.lparen_loc = arguments->opening_loc,
62636268
.arguments = arguments->arguments,
6264-
.rparen_loc = arguments->closing_loc.start == NULL ? ((pm_slice_t) { 0 }) : LOCATION2SLICE(parser, &arguments->closing_loc),
6269+
.rparen_loc = arguments->closing_loc,
62656270
.block = arguments->block
62666271
};
62676272

@@ -6962,26 +6967,26 @@ pm_xstring_node_create(pm_parser_t *parser, const pm_token_t *opening, const pm_
69626967
* Allocate a new YieldNode node.
69636968
*/
69646969
static pm_yield_node_t *
6965-
pm_yield_node_create(pm_parser_t *parser, const pm_token_t *keyword, const pm_location_t *lparen_loc, pm_arguments_node_t *arguments, const pm_location_t *rparen_loc) {
6970+
pm_yield_node_create(pm_parser_t *parser, const pm_token_t *keyword, const pm_slice_t *lparen_loc, pm_arguments_node_t *arguments, const pm_slice_t *rparen_loc) {
69666971
pm_yield_node_t *node = PM_NODE_ALLOC(parser, pm_yield_node_t);
69676972

69686973
const uint8_t *end;
6969-
if (rparen_loc != NULL) {
6970-
end = rparen_loc->end;
6974+
if (rparen_loc->length > 0) {
6975+
end = parser->start + rparen_loc->start + rparen_loc->length;
69716976
} else if (arguments != NULL) {
69726977
end = arguments->base.location.end;
6973-
} else if (lparen_loc != NULL) {
6974-
end = lparen_loc->end;
6978+
} else if (lparen_loc->length > 0) {
6979+
end = parser->start + lparen_loc->start + lparen_loc->length;
69756980
} else {
69766981
end = keyword->end;
69776982
}
69786983

69796984
*node = (pm_yield_node_t) {
69806985
.base = PM_NODE_INIT(parser, PM_YIELD_NODE, 0, keyword->start, end),
69816986
.keyword_loc = TOKEN2SLICE(parser, keyword),
6982-
.lparen_loc = MAYBELOCATION2SLICE(parser, lparen_loc),
6987+
.lparen_loc = *lparen_loc,
69836988
.arguments = arguments,
6984-
.rparen_loc = MAYBELOCATION2SLICE(parser, rparen_loc)
6989+
.rparen_loc = *rparen_loc
69856990
};
69866991

69876992
return node;
@@ -14677,10 +14682,10 @@ parse_arguments_list(pm_parser_t *parser, pm_arguments_t *arguments, bool accept
1467714682

1467814683
if (accept1(parser, PM_TOKEN_PARENTHESIS_LEFT)) {
1467914684
found |= true;
14680-
arguments->opening_loc = PM_LOCATION_TOKEN_VALUE(&parser->previous);
14685+
arguments->opening_loc = TOKEN2SLICE(parser, &parser->previous);
1468114686

1468214687
if (accept1(parser, PM_TOKEN_PARENTHESIS_RIGHT)) {
14683-
arguments->closing_loc = PM_LOCATION_TOKEN_VALUE(&parser->previous);
14688+
arguments->closing_loc = TOKEN2SLICE(parser, &parser->previous);
1468414689
} else {
1468514690
pm_accepts_block_stack_push(parser, true);
1468614691
parse_arguments(parser, arguments, accepts_block, PM_TOKEN_PARENTHESIS_RIGHT, (uint16_t) (depth + 1));
@@ -14692,7 +14697,7 @@ parse_arguments_list(pm_parser_t *parser, pm_arguments_t *arguments, bool accept
1469214697
}
1469314698

1469414699
pm_accepts_block_stack_pop(parser);
14695-
arguments->closing_loc = PM_LOCATION_TOKEN_VALUE(&parser->previous);
14700+
arguments->closing_loc = TOKEN2SLICE(parser, &parser->previous);
1469614701
}
1469714702
} else if (accepts_command_call && (token_begins_expression_p(parser->current.type) || match3(parser, PM_TOKEN_USTAR, PM_TOKEN_USTAR_STAR, PM_TOKEN_UAMPERSAND)) && !match1(parser, PM_TOKEN_BRACE_LEFT)) {
1469814703
found |= true;
@@ -16244,7 +16249,7 @@ parse_pattern_hash_implicit_value(pm_parser_t *parser, pm_constant_id_list_t *ca
1624416249
.end = parser->start + value_slice->start + value_slice->length
1624516250
};
1624616251
const pm_location_t *value_loc = &value_location;
16247-
16252+
1624816253
pm_constant_id_t constant_id = pm_parser_constant_id_location(parser, value_loc->start, value_loc->end);
1624916254
int depth = -1;
1625016255

@@ -17769,12 +17774,12 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1776917774
// variable call bit in the flags.
1777017775
pm_node_flag_unset((pm_node_t *)call, PM_CALL_NODE_FLAGS_VARIABLE_CALL);
1777117776

17772-
call->opening_loc = arguments.opening_loc.start == NULL ? ((pm_slice_t) { 0 }) : LOCATION2SLICE(parser, &arguments.opening_loc);
17777+
call->opening_loc = arguments.opening_loc;
1777317778
call->arguments = arguments.arguments;
17774-
call->closing_loc = arguments.closing_loc.start == NULL ? ((pm_slice_t) { 0 }) : LOCATION2SLICE(parser, &arguments.closing_loc);
17779+
call->closing_loc = arguments.closing_loc;
1777517780
call->block = arguments.block;
1777617781

17777-
const uint8_t *end = pm_arguments_end(&arguments);
17782+
const uint8_t *end = pm_arguments_end(parser, &arguments);
1777817783
if (!end) {
1777917784
end = parser->start + call->message_loc.start + call->message_loc.length;
1778017785
}
@@ -18351,7 +18356,7 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1835118356
parse_arguments_list(parser, &arguments, true, accepts_command_call, (uint16_t) (depth + 1));
1835218357

1835318358
if (
18354-
arguments.opening_loc.start == NULL &&
18359+
arguments.opening_loc.length == 0 &&
1835518360
arguments.arguments == NULL &&
1835618361
((arguments.block == NULL) || PM_NODE_TYPE_P(arguments.block, PM_BLOCK_NODE))
1835718362
) {
@@ -18377,7 +18382,7 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1837718382
arguments.block = NULL;
1837818383
}
1837918384

18380-
pm_node_t *node = (pm_node_t *) pm_yield_node_create(parser, &keyword, arguments.opening_loc.start == NULL ? NULL : &arguments.opening_loc, arguments.arguments, arguments.closing_loc.start == NULL ? NULL : &arguments.closing_loc);
18385+
pm_node_t *node = (pm_node_t *) pm_yield_node_create(parser, &keyword, &arguments.opening_loc, arguments.arguments, &arguments.closing_loc);
1838118386
if (!parser->parsing_eval && !parser->partial_script) parse_yield(parser, node);
1838218387

1838318388
return node;
@@ -18999,13 +19004,13 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1899919004
if (accept1(parser, PM_TOKEN_PARENTHESIS_RIGHT)) {
1900019005
receiver = (pm_node_t *) pm_parentheses_node_create(parser, &lparen, NULL, &parser->previous, 0);
1900119006
} else {
19002-
arguments.opening_loc = PM_LOCATION_TOKEN_VALUE(&lparen);
19007+
arguments.opening_loc = TOKEN2SLICE(parser, &lparen);
1900319008
receiver = parse_expression(parser, PM_BINDING_POWER_COMPOSITION, true, false, PM_ERR_NOT_EXPRESSION, (uint16_t) (depth + 1));
1900419009

1900519010
if (!parser->recovering) {
1900619011
accept1(parser, PM_TOKEN_NEWLINE);
1900719012
expect1(parser, PM_TOKEN_PARENTHESIS_RIGHT, PM_ERR_EXPECT_RPAREN);
19008-
arguments.closing_loc = PM_LOCATION_TOKEN_VALUE(&parser->previous);
19013+
arguments.closing_loc = TOKEN2SLICE(parser, &parser->previous);
1900919014
}
1901019015
}
1901119016
} else {
@@ -20627,7 +20632,7 @@ parse_expression_infix(pm_parser_t *parser, pm_node_t *node, pm_binding_power_t
2062720632
if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_VARIABLE_CALL)) {
2062820633
pm_location_t message_loc = (pm_location_t) {
2062920634
.start = parser->start + cast->message_loc.start,
20630-
.end = parser->start + cast->message_loc.start + cast->message_loc.length
20635+
.end = parser->start + cast->message_loc.start + cast->message_loc.length
2063120636
};
2063220637

2063320638
pm_refute_numbered_parameter(parser, message_loc.start, message_loc.end);
@@ -20775,9 +20780,9 @@ parse_expression_infix(pm_parser_t *parser, pm_node_t *node, pm_binding_power_t
2077520780
if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_VARIABLE_CALL)) {
2077620781
pm_location_t message_loc = (pm_location_t) {
2077720782
.start = parser->start + cast->message_loc.start,
20778-
.end = parser->start + cast->message_loc.start + cast->message_loc.length
20783+
.end = parser->start + cast->message_loc.start + cast->message_loc.length
2077920784
};
20780-
20785+
2078120786
pm_refute_numbered_parameter(parser, message_loc.start, message_loc.end);
2078220787
pm_constant_id_t constant_id = pm_parser_local_add_slice(parser, &cast->message_loc, 1);
2078320788
pm_node_t *value = parse_assignment_value(parser, previous_binding_power, binding_power, accepts_command_call, PM_ERR_EXPECT_EXPRESSION_AFTER_OPERATOR, (uint16_t) (depth + 1));
@@ -21027,7 +21032,7 @@ parse_expression_infix(pm_parser_t *parser, pm_node_t *node, pm_binding_power_t
2102721032
if (
2102821033
(previous_binding_power == PM_BINDING_POWER_STATEMENT) &&
2102921034
arguments.arguments == NULL &&
21030-
arguments.opening_loc.start == NULL &&
21035+
arguments.opening_loc.length == 0 &&
2103121036
match1(parser, PM_TOKEN_COMMA)
2103221037
) {
2103321038
return parse_targets_validate(parser, (pm_node_t *) call, PM_BINDING_POWER_INDEX, (uint16_t) (depth + 1));
@@ -21199,7 +21204,7 @@ parse_expression_infix(pm_parser_t *parser, pm_node_t *node, pm_binding_power_t
2119921204
parser_lex(parser);
2120021205

2120121206
pm_arguments_t arguments = { 0 };
21202-
arguments.opening_loc = PM_LOCATION_TOKEN_VALUE(&parser->previous);
21207+
arguments.opening_loc = TOKEN2SLICE(parser, &parser->previous);
2120321208

2120421209
if (!accept1(parser, PM_TOKEN_BRACKET_RIGHT)) {
2120521210
pm_accepts_block_stack_push(parser, true);
@@ -21208,7 +21213,7 @@ parse_expression_infix(pm_parser_t *parser, pm_node_t *node, pm_binding_power_t
2120821213
expect1(parser, PM_TOKEN_BRACKET_RIGHT, PM_ERR_EXPECT_RBRACKET);
2120921214
}
2121021215

21211-
arguments.closing_loc = PM_LOCATION_TOKEN_VALUE(&parser->previous);
21216+
arguments.closing_loc = TOKEN2SLICE(parser, &parser->previous);
2121221217

2121321218
// If we have a comma after the closing bracket then this is a multiple
2121421219
// assignment and we should parse the targets.

0 commit comments

Comments
 (0)