authorgravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2019-05-11 20:51:59+02:00
committergravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2019-05-11 20:51:59+02:00
log6cf7fb1177848c2f2a20c1f4093b1e003c477911
tree2b9ca820cdd1f6dec58f9e28262b3b67ebc3f066
parentba3d18a80e6b33e943ecebd9aaf07c0a84de84ee

fixes #2235


2 files changed, 28 insertions(+), 5 deletions(-)

src/parser.cpp+13-5
...@@ -1158,10 +1158,6 @@ static AstNode *ast_parse_prefix_expr(ParseContext *pc) {...@@ -1158,10 +1158,6 @@ static AstNode *ast_parse_prefix_expr(ParseContext *pc) {
1158// / Block1158// / Block
1159// / CurlySuffixExpr1159// / CurlySuffixExpr
1160static AstNode *ast_parse_primary_expr(ParseContext *pc) {1160static AstNode *ast_parse_primary_expr(ParseContext *pc) {
1161 AstNode *enum_lit = ast_parse_enum_lit(pc);
1162 if (enum_lit != nullptr)
1163 return enum_lit;
1164
1165 AstNode *asm_expr = ast_parse_asm_expr(pc);1161 AstNode *asm_expr = ast_parse_asm_expr(pc);
1166 if (asm_expr != nullptr)1162 if (asm_expr != nullptr)
1167 return asm_expr;1163 return asm_expr;
...@@ -1496,6 +1492,7 @@ static AstNode *ast_parse_suffix_expr(ParseContext *pc) {...@@ -1496,6 +1492,7 @@ static AstNode *ast_parse_suffix_expr(ParseContext *pc) {
1496// <- BUILTINIDENTIFIER FnCallArguments1492// <- BUILTINIDENTIFIER FnCallArguments
1497// / CHAR_LITERAL1493// / CHAR_LITERAL
1498// / ContainerDecl1494// / ContainerDecl
1495// / DOT IDENTIFIER
1499// / ErrorSetDecl1496// / ErrorSetDecl
1500// / FLOAT1497// / FLOAT
1501// / FnProto1498// / FnProto
...@@ -1556,6 +1553,10 @@ static AstNode *ast_parse_primary_type_expr(ParseContext *pc) {...@@ -1556,6 +1553,10 @@ static AstNode *ast_parse_primary_type_expr(ParseContext *pc) {
1556 if (container_decl != nullptr)1553 if (container_decl != nullptr)
1557 return container_decl;1554 return container_decl;
15581555
1556 AstNode *enum_lit = ast_parse_enum_lit(pc);
1557 if (enum_lit != nullptr)
1558 return enum_lit;
1559
1559 AstNode *error_set_decl = ast_parse_error_set_decl(pc);1560 AstNode *error_set_decl = ast_parse_error_set_decl(pc);
1560 if (error_set_decl != nullptr)1561 if (error_set_decl != nullptr)
1561 return error_set_decl;1562 return error_set_decl;
...@@ -1958,7 +1959,14 @@ static AstNode *ast_parse_field_init(ParseContext *pc) {...@@ -1958,7 +1959,14 @@ static AstNode *ast_parse_field_init(ParseContext *pc) {
1958 return nullptr;1959 return nullptr;
19591960
1960 Token *name = expect_token(pc, TokenIdSymbol);1961 Token *name = expect_token(pc, TokenIdSymbol);
1961 expect_token(pc, TokenIdEq);1962 if (eat_token_if(pc, TokenIdEq) == nullptr) {
1963 // Because ".Name" can also be intepreted as an enum literal, we should put back
1964 // those two tokens again so that the parser can try to parse them as the enum
1965 // literal later.
1966 put_back_token(pc);
1967 put_back_token(pc);
1968 return nullptr;
1969 }
1962 AstNode *expr = ast_expect(pc, ast_parse_expr);1970 AstNode *expr = ast_expect(pc, ast_parse_expr);
19631971
1964 AstNode *res = ast_create_node(pc, NodeTypeStructValueField, first);1972 AstNode *res = ast_create_node(pc, NodeTypeStructValueField, first);
test/stage1/behavior/enum.zig+15
...@@ -923,3 +923,18 @@ test "peer type resolution with enum literal" {...@@ -923,3 +923,18 @@ test "peer type resolution with enum literal" {
923 expect(Items.two == .two);923 expect(Items.two == .two);
924 expect(.two == Items.two);924 expect(.two == Items.two);
925}925}
926
927test "enum literal in array literal" {
928 const Items = enum {
929 one,
930 two,
931 };
932
933 const array = []Items {
934 .one,
935 .two,
936 };
937
938 expect(array[0] == .one);
939 expect(array[1] == .two);
940}