From 222941727f2b094449838135c3157120e0176e58 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 28 May 2019 12:45:20 +0100 Subject: Add `..<` operator for ranges; Add extra checking for bit set assignments --- src/parser.cpp | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'src/parser.cpp') diff --git a/src/parser.cpp b/src/parser.cpp index 0d70f4a5c..a72bd8803 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -1146,6 +1146,19 @@ Token expect_token_after(AstFile *f, TokenKind kind, char *msg) { } +bool is_token_range(TokenKind kind) { + switch (kind) { + case Token_Ellipsis: + case Token_RangeHalf: + return true; + } + return false; +} +bool is_token_range(Token tok) { + return is_token_range(tok.kind); +} + + Token expect_operator(AstFile *f) { Token prev = f->curr_token; if ((prev.kind == Token_in || prev.kind == Token_notin) && (f->expr_level >= 0 || f->allow_in_expr)) { @@ -1153,7 +1166,7 @@ Token expect_operator(AstFile *f) { } else if (!gb_is_between(prev.kind, Token__OperatorBegin+1, Token__OperatorEnd-1)) { syntax_error(f->curr_token, "Expected an operator, got '%.*s'", LIT(token_strings[prev.kind])); - } else if (!f->allow_range && (prev.kind == Token_Ellipsis)) { + } else if (!f->allow_range && is_token_range(prev)) { syntax_error(f->curr_token, "Expected an non-range operator, got '%.*s'", LIT(token_strings[prev.kind])); } @@ -2131,9 +2144,9 @@ Ast *parse_call_expr(AstFile *f, Ast *operand) { } bool prefix_ellipsis = false; - if (f->curr_token.kind == Token_Ellipsis) { + if (is_token_range(f->curr_token)) { prefix_ellipsis = true; - ellipsis = expect_token(f, Token_Ellipsis); + ellipsis = expect_token(f, f->curr_token.kind); } Ast *arg = parse_expr(f, false); @@ -2320,12 +2333,7 @@ bool is_ast_range(Ast *expr) { if (expr->kind != Ast_BinaryExpr) { return false; } - TokenKind op = expr->BinaryExpr.op.kind; - switch (op) { - case Token_Ellipsis: - return true; - } - return false; + return is_token_range(expr->BinaryExpr.op.kind); } // NOTE(bill): result == priority @@ -2334,6 +2342,7 @@ i32 token_precedence(AstFile *f, TokenKind t) { case Token_Question: return 1; case Token_Ellipsis: + case Token_RangeHalf: if (!f->allow_range) { return 0; } -- cgit v1.2.3