diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2017-02-05 18:58:58 -0500 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2017-02-05 18:58:58 -0500 |
| commit | 7749ffd797e927f93051060db49b76c595879697 (patch) | |
| tree | 53f3861a67d42e47ea35636f81a6a43618e44483 /src/parser.cpp | |
| parent | b9c943b0667ed8253382cb787d4705f6619f7296 (diff) | |
| download | zig-7749ffd797e927f93051060db49b76c595879697.tar.gz zig-7749ffd797e927f93051060db49b76c595879697.zip | |
try expression can omit variable assignments
Diffstat (limited to 'src/parser.cpp')
| -rw-r--r-- | src/parser.cpp | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/src/parser.cpp b/src/parser.cpp index 68ccd6aff4..57b226270a 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -628,7 +628,7 @@ static AstNode *ast_parse_comptime_expr(ParseContext *pc, size_t *token_index, b } /* -TryExpression = "try" "(" ("const" | "var") option("*") Symbol "=" Expression ")" Expression option("else" option("|" Symbol "|") Expression) +TryExpression = "try" "(" option(("const" | "var") option("*") Symbol "=") Expression ")" Expression option("else" option("|" Symbol "|") Expression) */ static AstNode *ast_parse_try_expr(ParseContext *pc, size_t *token_index, bool mandatory) { Token *try_token = &pc->tokens->at(*token_index); @@ -646,26 +646,31 @@ static AstNode *ast_parse_try_expr(ParseContext *pc, size_t *token_index, bool m ast_eat_token(pc, token_index, TokenIdLParen); Token *var_token = &pc->tokens->at(*token_index); + bool have_vars; if (var_token->id == TokenIdKeywordVar) { node->data.try_expr.var_is_const = false; *token_index += 1; + have_vars = true; } else if (var_token->id == TokenIdKeywordConst) { node->data.try_expr.var_is_const = true; *token_index += 1; + have_vars = true; } else { - ast_invalid_token_error(pc, var_token); + have_vars = false; } - Token *star_token = &pc->tokens->at(*token_index); - if (star_token->id == TokenIdStar) { - node->data.try_expr.var_is_ptr = true; - *token_index += 1; - } + if (have_vars) { + Token *star_token = &pc->tokens->at(*token_index); + if (star_token->id == TokenIdStar) { + node->data.try_expr.var_is_ptr = true; + *token_index += 1; + } - Token *var_name_tok = ast_eat_token(pc, token_index, TokenIdSymbol); - node->data.try_expr.var_symbol = token_buf(var_name_tok); + Token *var_name_tok = ast_eat_token(pc, token_index, TokenIdSymbol); + node->data.try_expr.var_symbol = token_buf(var_name_tok); - ast_eat_token(pc, token_index, TokenIdEq); + ast_eat_token(pc, token_index, TokenIdEq); + } node->data.try_expr.target_node = ast_parse_expression(pc, token_index, true); |
