diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2016-01-04 16:57:22 -0700 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2016-01-04 16:57:22 -0700 |
| commit | 4514661cfef1cd0090ba9888b69037af3ef9805a (patch) | |
| tree | a298e8c90440837a9cbf7ec83d9c6408ca59c768 /src/parser.cpp | |
| parent | fcacc85b4e7f0a60ae11bbefcdae620665426297 (diff) | |
| download | zig-4514661cfef1cd0090ba9888b69037af3ef9805a.tar.gz zig-4514661cfef1cd0090ba9888b69037af3ef9805a.zip | |
add member functions
Diffstat (limited to 'src/parser.cpp')
| -rw-r--r-- | src/parser.cpp | 36 |
1 files changed, 32 insertions, 4 deletions
diff --git a/src/parser.cpp b/src/parser.cpp index bddd8a4229..1fc4f538d4 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -395,6 +395,14 @@ void ast_print(AstNode *node, int indent) { case NodeTypeStructDecl: fprintf(stderr, "%s '%s'\n", node_type_str(node->type), buf_ptr(&node->data.struct_decl.name)); + for (int i = 0; i < node->data.struct_decl.fields.length; i += 1) { + AstNode *child = node->data.struct_decl.fields.at(i); + ast_print(child, indent + 2); + } + for (int i = 0; i < node->data.struct_decl.fns.length; i += 1) { + AstNode *child = node->data.struct_decl.fns.at(i); + ast_print(child, indent + 2); + } break; case NodeTypeStructField: fprintf(stderr, "%s '%s'\n", node_type_str(node->type), buf_ptr(&node->data.struct_field.name)); @@ -2572,7 +2580,8 @@ static AstNode *ast_parse_use(ParseContext *pc, int *token_index) { } /* -StructDecl : many(Directive) token(Struct) token(Symbol) token(LBrace) many(StructField) token(RBrace) +StructDecl : many(Directive) token(Struct) token(Symbol) token(LBrace) many(StructMember) token(RBrace) +StructMember: StructField | FnDecl StructField : token(Symbol) token(Colon) Type token(Comma) */ static AstNode *ast_parse_struct_decl(ParseContext *pc, int *token_index) { @@ -2590,16 +2599,38 @@ static AstNode *ast_parse_struct_decl(ParseContext *pc, int *token_index) { ast_eat_token(pc, token_index, TokenIdLBrace); + node->data.struct_decl.directives = pc->directive_list; + pc->directive_list = nullptr; + for (;;) { + assert(!pc->directive_list); + pc->directive_list = allocate<ZigList<AstNode*>>(1); + Token *directive_token = &pc->tokens->at(*token_index); + ast_parse_directives(pc, token_index, pc->directive_list); + + AstNode *fn_def_node = ast_parse_fn_def(pc, token_index, false); + if (fn_def_node) { + node->data.struct_decl.fns.append(fn_def_node); + continue; + } + Token *token = &pc->tokens->at(*token_index); if (token->id == TokenIdRBrace) { + if (pc->directive_list->length > 0) { + ast_error(pc, directive_token, "invalid directive"); + } + pc->directive_list = nullptr; + *token_index += 1; break; } else if (token->id == TokenIdSymbol) { AstNode *field_node = ast_create_node(pc, NodeTypeStructField, token); *token_index += 1; + field_node->data.struct_field.directives = pc->directive_list; + pc->directive_list = nullptr; + ast_buf_from_token(pc, token, &field_node->data.struct_field.name); ast_eat_token(pc, token_index, TokenIdColon); @@ -2615,9 +2646,6 @@ static AstNode *ast_parse_struct_decl(ParseContext *pc, int *token_index) { } - node->data.struct_decl.directives = pc->directive_list; - pc->directive_list = nullptr; - return node; } |
