Skip to content

Commit

Permalink
Fix: Parser supports [:] & [::] slicing
Browse files Browse the repository at this point in the history
  • Loading branch information
KennyOliver committed Jan 6, 2025
1 parent 37b90bd commit 9e3ac4c
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/parser/array_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ ASTNode *parse_index_access(ASTNode *array, ParserState *state) {
node->array_slice_access.start = parse_expression(state);
}

// Expect and consume `:`
// Expect and consume first `:`
expect_token(state, TOKEN_COLON, "Expected `:` in slice expression");

// Parse end expression (optional)
Expand All @@ -122,8 +122,13 @@ ASTNode *parse_index_access(ASTNode *array, ParserState *state) {

// Check for optional step
if (get_current_token(state)->type == TOKEN_COLON) {
advance_token(state); // Consume `:`
node->array_slice_access.step = parse_expression(state);
advance_token(state); // consume second `:`
if (get_current_token(state)->type != TOKEN_SQ_BRACKET_CLOSE) {
node->array_slice_access.step = parse_expression(state);
} else {
// Handle cases like `[::]`
node->array_slice_access.step = NULL;
}
}

// Expect and consume `]`
Expand Down

0 comments on commit 9e3ac4c

Please sign in to comment.