aboutsummaryrefslogtreecommitdiff
path: root/src/parser.cpp
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2017-01-23 16:40:17 -0500
committerAndrew Kelley <superjoe30@gmail.com>2017-01-23 16:40:17 -0500
commit17cb85dfb837949cd3a559fe8e99dee1f72463a4 (patch)
treea35d23e26938586ba0fb706964e2b7c2b4b894f7 /src/parser.cpp
parent1826a961608037405237f12df5aec94f8f5f3a10 (diff)
downloadzig-17cb85dfb837949cd3a559fe8e99dee1f72463a4.tar.gz
zig-17cb85dfb837949cd3a559fe8e99dee1f72463a4.zip
basic support for functions with variable length arguments
See #77
Diffstat (limited to 'src/parser.cpp')
-rw-r--r--src/parser.cpp25
1 files changed, 12 insertions, 13 deletions
diff --git a/src/parser.cpp b/src/parser.cpp
index dbb6836e60..04c31fcfe0 100644
--- a/src/parser.cpp
+++ b/src/parser.cpp
@@ -250,16 +250,11 @@ static AstNode *ast_parse_type_expr(ParseContext *pc, size_t *token_index, bool
}
/*
-ParamDecl = option("noalias" | "comptime") option(Symbol ":") TypeExpr | "..."
+ParamDecl = option("noalias" | "comptime") option(Symbol ":") (TypeExpr | "...")
*/
static AstNode *ast_parse_param_decl(ParseContext *pc, size_t *token_index) {
Token *token = &pc->tokens->at(*token_index);
- if (token->id == TokenIdEllipsis) {
- *token_index += 1;
- return nullptr;
- }
-
AstNode *node = ast_create_node(pc, NodeTypeParamDecl, token);
if (token->id == TokenIdKeywordNoAlias) {
@@ -282,7 +277,13 @@ static AstNode *ast_parse_param_decl(ParseContext *pc, size_t *token_index) {
}
}
- node->data.param_decl.type = ast_parse_type_expr(pc, token_index, true);
+ Token *ellipsis_tok = &pc->tokens->at(*token_index);
+ if (ellipsis_tok->id == TokenIdEllipsis) {
+ *token_index += 1;
+ node->data.param_decl.is_var_args = true;
+ } else {
+ node->data.param_decl.type = ast_parse_type_expr(pc, token_index, true);
+ }
return node;
}
@@ -304,12 +305,10 @@ static void ast_parse_param_decl_list(ParseContext *pc, size_t *token_index,
for (;;) {
AstNode *param_decl_node = ast_parse_param_decl(pc, token_index);
bool expect_end = false;
- if (param_decl_node) {
- params->append(param_decl_node);
- } else {
- *is_var_args = true;
- expect_end = true;
- }
+ assert(param_decl_node);
+ params->append(param_decl_node);
+ expect_end = param_decl_node->data.param_decl.is_var_args;
+ *is_var_args = expect_end;
Token *token = &pc->tokens->at(*token_index);
*token_index += 1;