aboutsummaryrefslogtreecommitdiff
path: root/src/parser.cpp
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2019-07-09 11:18:50 +0100
committergingerBill <bill@gingerbill.org>2019-07-09 11:18:50 +0100
commit308300c1fc9c7cbee18f51790df46eeb06964aff (patch)
treeb5d4f721742a1d46d42d2e9ed8149e46e59b3ef5 /src/parser.cpp
parent927d6814f250c3120b8396c98cf43c576f5a5083 (diff)
Add extra error handling for parsing slices
Diffstat (limited to 'src/parser.cpp')
-rw-r--r--src/parser.cpp28
1 files changed, 20 insertions, 8 deletions
diff --git a/src/parser.cpp b/src/parser.cpp
index e262b3196..d2e64f23c 100644
--- a/src/parser.cpp
+++ b/src/parser.cpp
@@ -2245,31 +2245,43 @@ Ast *parse_atom_expr(AstFile *f, Ast *operand, bool lhs) {
Token open = {}, close = {}, interval = {};
Ast *indices[2] = {};
- Token ellipsis = {};
- bool is_ellipsis = false;
+ bool is_interval = false;
f->expr_level++;
open = expect_token(f, Token_OpenBracket);
- if (f->curr_token.kind != Token_Colon) {
+ switch (f->curr_token.kind) {
+ case Token_Ellipsis:
+ case Token_RangeHalf:
+ // NOTE(bill): Do not err yet
+ case Token_Colon:
+ break;
+ default:
indices[0] = parse_expr(f, false);
+ break;
}
- if (f->curr_token.kind == Token_Colon) {
- ellipsis = advance_token(f);
- is_ellipsis = true;
+ switch (f->curr_token.kind) {
+ case Token_Ellipsis:
+ case Token_RangeHalf:
+ syntax_error(f->curr_token, "Expected a colon, not a range");
+ /* fallthrough */
+ case Token_Colon:
+ interval = advance_token(f);
+ is_interval = true;
if (f->curr_token.kind != Token_CloseBracket &&
f->curr_token.kind != Token_EOF) {
indices[1] = parse_expr(f, false);
}
+ break;
}
f->expr_level--;
close = expect_token(f, Token_CloseBracket);
- if (is_ellipsis) {
- operand = ast_slice_expr(f, operand, open, close, ellipsis, indices[0], indices[1]);
+ if (is_interval) {
+ operand = ast_slice_expr(f, operand, open, close, interval, indices[0], indices[1]);
} else {
operand = ast_index_expr(f, operand, indices[0], open, close);
}