Skip to content

Commit

Permalink
fixes #2235
Browse files Browse the repository at this point in the history
  • Loading branch information
Hejsil committed May 11, 2019
1 parent ba3d18a commit 6cf7fb1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1158,10 +1158,6 @@ static AstNode *ast_parse_prefix_expr(ParseContext *pc) {
// / Block
// / CurlySuffixExpr
static AstNode *ast_parse_primary_expr(ParseContext *pc) {
AstNode *enum_lit = ast_parse_enum_lit(pc);
if (enum_lit != nullptr)
return enum_lit;

AstNode *asm_expr = ast_parse_asm_expr(pc);
if (asm_expr != nullptr)
return asm_expr;
Expand Down Expand Up @@ -1496,6 +1492,7 @@ static AstNode *ast_parse_suffix_expr(ParseContext *pc) {
// <- BUILTINIDENTIFIER FnCallArguments
// / CHAR_LITERAL
// / ContainerDecl
// / DOT IDENTIFIER
// / ErrorSetDecl
// / FLOAT
// / FnProto
Expand Down Expand Up @@ -1556,6 +1553,10 @@ static AstNode *ast_parse_primary_type_expr(ParseContext *pc) {
if (container_decl != nullptr)
return container_decl;

AstNode *enum_lit = ast_parse_enum_lit(pc);
if (enum_lit != nullptr)
return enum_lit;

AstNode *error_set_decl = ast_parse_error_set_decl(pc);
if (error_set_decl != nullptr)
return error_set_decl;
Expand Down Expand Up @@ -1958,7 +1959,14 @@ static AstNode *ast_parse_field_init(ParseContext *pc) {
return nullptr;

Token *name = expect_token(pc, TokenIdSymbol);
expect_token(pc, TokenIdEq);
if (eat_token_if(pc, TokenIdEq) == nullptr) {
// Because ".Name" can also be intepreted as an enum literal, we should put back
// those two tokens again so that the parser can try to parse them as the enum
// literal later.
put_back_token(pc);
put_back_token(pc);
return nullptr;
}
AstNode *expr = ast_expect(pc, ast_parse_expr);

AstNode *res = ast_create_node(pc, NodeTypeStructValueField, first);
Expand Down
15 changes: 15 additions & 0 deletions test/stage1/behavior/enum.zig
Original file line number Diff line number Diff line change
Expand Up @@ -923,3 +923,18 @@ test "peer type resolution with enum literal" {
expect(Items.two == .two);
expect(.two == Items.two);
}

test "enum literal in array literal" {
const Items = enum {
one,
two,
};

const array = []Items {
.one,
.two,
};

expect(array[0] == .one);
expect(array[1] == .two);
}

0 comments on commit 6cf7fb1

Please sign in to comment.