authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-10 16:58:21-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-10 16:58:21-07:00
logfc748e2ccdf69dd02ae2e7e6e9913e2d98711129
tree09c61380319db35dbdd2bf8a06bc6778bcd3aa10
parent3ef2f7058b0bbe2c81f79f990398aaab2e5a7a2f

update enum parsing to new plan


3 files changed, 35 insertions(+), 127 deletions(-)

doc/langref.md+4-14
......@@ -32,23 +32,15 @@ zig | C equivalent | Description
3232```
3333Root : many(TopLevelDecl) token(EOF)
3434
35TopLevelDecl : FnDef | ExternBlock | RootExportDecl | Use | StructDecl | VariableDeclaration | EnumDecl
35TopLevelDecl : FnDef | ExternBlock | RootExportDecl | Use | ContainerDecl | VariableDeclaration
3636
3737VariableDeclaration : option(FnVisibleMod) (token(Var) | token(Const)) token(Symbol) (token(Eq) Expression | token(Colon) Type option(token(Eq) Expression))
3838
39StructDecl : many(Directive) option(FnVisibleMod) token(Struct) StructPayload
40
41StructPayload: token(Symbol) token(LBrace) many(StructMember) token(RBrace)
42
43EnumDecl : many(Directive) option(FnVisibleMod) token(Enum) token(Symbol) token(LBrace) many(EnumField) token(RBrace)
39ContainerDecl : many(Directive) option(FnVisibleMod) (token(Struct) | token(Enum)) token(Symbol) token(LBrace) many(StructMember) token(RBrace)
4440
4541StructMember: StructField | FnDecl
4642
47StructField : token(Symbol) token(Colon) Type token(Comma)
48
49EnumField : (EnumDiscriminant | StructPayload) token(Comma)
50
51EnumDiscriminant : token(Symbol) option(token(Eq) Expression)
43StructField : token(Symbol) option(token(Colon) Type token(Comma))
5244
5345Use : many(Directive) token(Use) token(String) token(Semicolon)
5446
......@@ -168,9 +160,7 @@ SliceExpression : token(LBracket) Expression token(Ellipsis) option(Expression)
168160
169161PrefixOp : token(Not) | token(Dash) | token(Tilde) | token(Star) | (token(Ampersand) option(token(Const)))
170162
171PrimaryExpression : token(Number) | token(String) | token(CharLiteral) | KeywordLiteral | GroupedExpression | Goto | token(Break) | token(Continue) | BlockExpression | token(Symbol) | StructValueExpression | CompilerFnType | (token(AtSign) token(Symbol) FnCallExpression) | NamespaceSymbol
172
173NamespaceSymbol : token(Symbol) token(ColonColon) token(Symbol)
163PrimaryExpression : token(Number) | token(String) | token(CharLiteral) | KeywordLiteral | GroupedExpression | Goto | token(Break) | token(Continue) | BlockExpression | token(Symbol) | StructValueExpression | CompilerFnType | (token(AtSign) token(Symbol) FnCallExpression)
174164
175165StructValueExpression : token(Type) token(LBrace) list(StructValueExpressionField, token(Comma)) token(RBrace)
176166
src/all_types.hpp+6
......@@ -451,8 +451,14 @@ struct AstNodeAsmExpr {
451451 Expr resolved_expr;
452452};
453453
454enum ContainerKind {
455 ContainerKindStruct,
456 ContainerKindEnum,
457};
458
454459struct AstNodeStructDecl {
455460 Buf name;
461 ContainerKind kind;
456462 ZigList<AstNode *> fields;
457463 ZigList<AstNode *> fns;
458464 ZigList<AstNode *> *directives;
src/parser.cpp+25-113
......@@ -431,7 +431,9 @@ void ast_print(AstNode *node, int indent) {
431431 break;
432432 case NodeTypeStructField:
433433 fprintf(stderr, "%s '%s'\n", node_type_str(node->type), buf_ptr(&node->data.struct_field.name));
434 ast_print(node->data.struct_field.type, indent + 2);
434 if (node->data.struct_field.type) {
435 ast_print(node->data.struct_field.type, indent + 2);
436 }
435437 break;
436438 case NodeTypeEnumDecl:
437439 fprintf(stderr, "%s '%s'\n",
......@@ -2727,106 +2729,7 @@ static AstNode *ast_parse_use(ParseContext *pc, int *token_index) {
27272729}
27282730
27292731/*
2730EnumDecl : many(Directive) option(FnVisibleMod) token(Enum) token(Symbol) token(LBrace) many(EnumField) token(RBrace)
2731EnumField : (EnumDiscriminant | StructPayload) token(Comma)
2732EnumDiscriminant : token(Symbol) option(token(Eq) Expression)
2733*/
2734static AstNode *ast_parse_enum_decl(ParseContext *pc, int *token_index) {
2735 Token *first_token = &pc->tokens->at(*token_index);
2736
2737 VisibMod visib_mod;
2738 if (first_token->id == TokenIdKeywordPub) {
2739 Token *next_token = &pc->tokens->at(*token_index + 1);
2740 if (next_token->id == TokenIdKeywordEnum) {
2741 visib_mod = VisibModPub;
2742 *token_index += 2;
2743 } else {
2744 return nullptr;
2745 }
2746 } else if (first_token->id == TokenIdKeywordExport) {
2747 Token *next_token = &pc->tokens->at(*token_index + 1);
2748 if (next_token->id == TokenIdKeywordEnum) {
2749 visib_mod = VisibModExport;
2750 *token_index += 2;
2751 } else {
2752 return nullptr;
2753 }
2754 } else if (first_token->id == TokenIdKeywordEnum) {
2755 visib_mod = VisibModPrivate;
2756 *token_index += 1;
2757 } else {
2758 return nullptr;
2759 }
2760
2761 Token *enum_name = ast_eat_token(pc, token_index, TokenIdSymbol);
2762 AstNode *node = ast_create_node(pc, NodeTypeEnumDecl, first_token);
2763 ast_buf_from_token(pc, enum_name, &node->data.enum_decl.name);
2764 node->data.enum_decl.visib_mod = visib_mod;
2765
2766 node->data.enum_decl.directives = pc->directive_list;
2767 pc->directive_list = nullptr;
2768
2769 ast_eat_token(pc, token_index, TokenIdLBrace);
2770
2771 for (;;) {
2772 Token *token = &pc->tokens->at(*token_index);
2773
2774 if (token->id == TokenIdRBrace) {
2775 *token_index += 1;
2776 break;
2777 } else if (token->id == TokenIdSymbol) {
2778 AstNode *field_node = ast_create_node(pc, NodeTypeEnumField, token);
2779 *token_index += 1;
2780
2781 ast_buf_from_token(pc, token, &field_node->data.enum_field.name);
2782
2783 Token *eq_tok = &pc->tokens->at(*token_index);
2784 if (eq_tok->id == TokenIdEq) {
2785 *token_index += 1;
2786
2787 field_node->data.enum_field.val_expr = ast_parse_expression(pc, token_index, true);
2788 } else if (eq_tok->id == TokenIdLBrace) {
2789 *token_index += 1;
2790
2791 for (;;) {
2792 Token *token = &pc->tokens->at(*token_index);
2793
2794 if (token->id == TokenIdRBrace) {
2795 *token_index += 1;
2796 break;
2797 } else if (token->id == TokenIdSymbol) {
2798 AstNode *sub_field_node = ast_create_node(pc, NodeTypeStructField, token);
2799 *token_index += 1;
2800
2801 ast_buf_from_token(pc, token, &sub_field_node->data.struct_field.name);
2802
2803 ast_eat_token(pc, token_index, TokenIdColon);
2804
2805 sub_field_node->data.struct_field.type = ast_parse_type(pc, token_index);
2806
2807 ast_eat_token(pc, token_index, TokenIdComma);
2808
2809 field_node->data.enum_decl.fields.append(sub_field_node);
2810 } else {
2811 ast_invalid_token_error(pc, token);
2812 }
2813 }
2814 }
2815
2816 ast_eat_token(pc, token_index, TokenIdComma);
2817
2818 node->data.enum_decl.fields.append(field_node);
2819 } else {
2820 ast_invalid_token_error(pc, token);
2821 }
2822 }
2823
2824 return node;
2825}
2826
2827/*
2828StructDecl : many(Directive) option(FnVisibleMod) token(Struct) StructPayload
2829StructPayload: token(Symbol) token(LBrace) many(StructMember) token(RBrace)
2732ContainerDecl : many(Directive) option(FnVisibleMod) (token(Struct) | token(Enum)) token(Symbol) token(LBrace) many(StructMember) token(RBrace)
28302733StructMember: StructField | FnDecl
28312734StructField : token(Symbol) token(Colon) Type token(Comma)
28322735*/
......@@ -2834,25 +2737,35 @@ static AstNode *ast_parse_struct_decl(ParseContext *pc, int *token_index) {
28342737 Token *first_token = &pc->tokens->at(*token_index);
28352738
28362739 VisibMod visib_mod;
2740 ContainerKind kind;
28372741
28382742 if (first_token->id == TokenIdKeywordPub) {
28392743 Token *next_token = &pc->tokens->at(*token_index + 1);
2840 if (next_token->id == TokenIdKeywordStruct) {
2744 if (next_token->id == TokenIdKeywordStruct ||
2745 next_token->id == TokenIdKeywordEnum)
2746 {
28412747 visib_mod = VisibModPub;
2748 kind = (next_token->id == TokenIdKeywordStruct) ? ContainerKindStruct : ContainerKindEnum;
28422749 *token_index += 2;
28432750 } else {
28442751 return nullptr;
28452752 }
28462753 } else if (first_token->id == TokenIdKeywordExport) {
28472754 Token *next_token = &pc->tokens->at(*token_index + 1);
2848 if (next_token->id == TokenIdKeywordStruct) {
2755 if (next_token->id == TokenIdKeywordStruct ||
2756 next_token->id == TokenIdKeywordEnum)
2757 {
28492758 visib_mod = VisibModExport;
2759 kind = (next_token->id == TokenIdKeywordStruct) ? ContainerKindStruct : ContainerKindEnum;
28502760 *token_index += 2;
28512761 } else {
28522762 return nullptr;
28532763 }
2854 } else if (first_token->id == TokenIdKeywordStruct) {
2764 } else if (first_token->id == TokenIdKeywordStruct ||
2765 first_token->id == TokenIdKeywordEnum)
2766 {
28552767 visib_mod = VisibModPrivate;
2768 kind = (first_token->id == TokenIdKeywordStruct) ? ContainerKindStruct : ContainerKindEnum;
28562769 *token_index += 1;
28572770 } else {
28582771 return nullptr;
......@@ -2861,6 +2774,7 @@ static AstNode *ast_parse_struct_decl(ParseContext *pc, int *token_index) {
28612774 Token *struct_name = ast_eat_token(pc, token_index, TokenIdSymbol);
28622775
28632776 AstNode *node = ast_create_node(pc, NodeTypeStructDecl, first_token);
2777 node->data.struct_decl.kind = kind;
28642778 ast_buf_from_token(pc, struct_name, &node->data.struct_decl.name);
28652779 node->data.struct_decl.visib_mod = visib_mod;
28662780
......@@ -2900,9 +2814,13 @@ static AstNode *ast_parse_struct_decl(ParseContext *pc, int *token_index) {
29002814
29012815 ast_buf_from_token(pc, token, &field_node->data.struct_field.name);
29022816
2903 ast_eat_token(pc, token_index, TokenIdColon);
2904
2905 field_node->data.struct_field.type = ast_parse_type(pc, token_index);
2817 Token *colon_tok = &pc->tokens->at(*token_index);
2818 if (colon_tok->id == TokenIdColon) {
2819 *token_index += 1;
2820 field_node->data.struct_field.type = ast_parse_type(pc, token_index);
2821 } else {
2822 field_node->data.struct_field.type = ast_create_void_type_node(pc, colon_tok);
2823 }
29062824
29072825 ast_eat_token(pc, token_index, TokenIdComma);
29082826
......@@ -2956,12 +2874,6 @@ static void ast_parse_top_level_decls(ParseContext *pc, int *token_index, ZigLis
29562874 continue;
29572875 }
29582876
2959 AstNode *enum_node = ast_parse_enum_decl(pc, token_index);
2960 if (enum_node) {
2961 top_level_decls->append(enum_node);
2962 continue;
2963 }
2964
29652877 if (pc->directive_list->length > 0) {
29662878 ast_error(pc, directive_token, "invalid directive");
29672879 }