authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-15 17:40:12-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-15 17:40:12-07:00
log0311b35a21e9896772cb982750be680b0ef71f1f
tree11ad9a19e13d0a4c4f6fd7853a3a0b25f2eb282b
parent74b1665586bafe0fcd063480b9480a77cbf93ab2

reduce precedence of {} suffix operator

this makes []u8 {1, 2, 3, 4} work for array literal

2 files changed, 104 insertions(+), 84 deletions(-)

doc/langref.md+13-11
......@@ -34,7 +34,7 @@ Root : many(TopLevelDecl) token(EOF)
3434
3535TopLevelDecl : FnDef | ExternBlock | RootExportDecl | Use | ContainerDecl | VariableDeclaration
3636
37VariableDeclaration : option(FnVisibleMod) (token(Var) | token(Const)) token(Symbol) (token(Eq) Expression | token(Colon) UnwrapMaybeExpression option(token(Eq) Expression))
37VariableDeclaration : option(FnVisibleMod) (token(Var) | token(Const)) token(Symbol) (token(Eq) Expression | token(Colon) PrefixOpExpression option(token(Eq) Expression))
3838
3939ContainerDecl : many(Directive) option(FnVisibleMod) (token(Struct) | token(Enum)) token(Symbol) token(LBrace) many(StructMember) token(RBrace)
4040
......@@ -48,7 +48,7 @@ RootExportDecl : many(Directive) token(Export) token(Symbol) token(String) token
4848
4949ExternBlock : many(Directive) token(Extern) token(LBrace) many(FnDecl) token(RBrace)
5050
51FnProto : many(Directive) option(FnVisibleMod) token(Fn) token(Symbol) ParamDeclList option(UnwrapMaybeExpression)
51FnProto : many(Directive) option(FnVisibleMod) token(Fn) token(Symbol) ParamDeclList option(PrefixOpExpression)
5252
5353Directive : token(NumberSign) token(Symbol) token(LParen) token(String) token(RParen)
5454
......@@ -60,7 +60,7 @@ FnDef : FnProto token(FatArrow) Block
6060
6161ParamDeclList : token(LParen) list(ParamDecl, token(Comma)) token(RParen)
6262
63ParamDecl : option(token(NoAlias)) token(Symbol) token(Colon) UnwrapMaybeExpression | token(Ellipsis)
63ParamDecl : option(token(NoAlias)) token(Symbol) token(Colon) PrefixOpExpression | token(Ellipsis)
6464
6565Block : token(LBrace) list(option(Statement), token(Semicolon)) token(RBrace)
6666
......@@ -78,7 +78,7 @@ AsmOutput : token(Colon) list(AsmOutputItem, token(Comma)) option(AsmInput)
7878
7979AsmInput : token(Colon) list(AsmInputItem, token(Comma)) option(AsmClobbers)
8080
81AsmOutputItem : token(LBracket) token(Symbol) token(RBracket) token(String) token(LParen) (token(Symbol) | token(Arrow) UnwrapMaybeExpression) token(RParen)
81AsmOutputItem : token(LBracket) token(Symbol) token(RBracket) token(String) token(LParen) (token(Symbol) | token(Arrow) PrefixOpExpression) token(RParen)
8282
8383AsmInputItem : token(LBracket) token(Symbol) token(RBracket) token(String) token(LParen) Expression token(RParen)
8484
......@@ -102,7 +102,7 @@ IfExpression : IfVarExpression | IfBoolExpression
102102
103103IfBoolExpression : token(If) token(LParen) Expression token(RParen) Expression option(Else)
104104
105IfVarExpression : token(If) token(LParen) (token(Const) | token(Var)) token(Symbol) option(token(Colon) UnwrapMaybeExpression) Token(MaybeAssign) Expression token(RParen) Expression Option(Else)
105IfVarExpression : token(If) token(LParen) (token(Const) | token(Var)) token(Symbol) option(token(Colon) PrefixOpExpression) Token(MaybeAssign) Expression token(RParen) Expression Option(Else)
106106
107107Else : token(Else) Expression
108108
......@@ -126,13 +126,15 @@ AdditionExpression : MultiplyExpression AdditionOperator AdditionExpression | Mu
126126
127127AdditionOperator : token(Plus) | token(Minus)
128128
129MultiplyExpression : PrefixOpExpression MultiplyOperator MultiplyExpression | PrefixOpExpression
129MultiplyExpression : CurlySuffixExpression MultiplyOperator MultiplyExpression | CurlySuffixExpression
130
131CurlySuffixExpression : PrefixOpExpression option(ContainerInitExpression)
130132
131133MultiplyOperator : token(Star) | token(Slash) | token(Percent)
132134
133135PrefixOpExpression : PrefixOp PrefixOpExpression | SuffixOpExpression
134136
135SuffixOpExpression : PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression | ContainerInitExpression)
137SuffixOpExpression : PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression)
136138
137139FieldAccessExpression : token(Dot) token(Symbol)
138140
......@@ -152,7 +154,7 @@ PrefixOp : token(Not) | token(Dash) | token(Tilde) | token(Star) | (token(Ampers
152154
153155PrimaryExpression : token(Number) | token(String) | token(CharLiteral) | KeywordLiteral | GroupedExpression | GotoExpression | BlockExpression | token(Symbol) | (token(AtSign) token(Symbol) FnCallExpression) | ArrayType | AsmExpression
154156
155ArrayType : token(LBracket) option(Expression) token(RBracket) option(token(Const)) UnwrapMaybeExpression
157ArrayType : token(LBracket) option(Expression) token(RBracket) option(token(Const)) PrefixOpExpression
156158
157159GotoExpression: token(Goto) token(Symbol)
158160
......@@ -164,9 +166,9 @@ KeywordLiteral : token(True) | token(False) | token(Null) | token(Break) | token
164166## Operator Precedence
165167
166168```
167x() x[] x{} x.y
168!x -x ~x *x &x
169as
169x() x[] x.y
170!x -x ~x *x &x ?x
171x{}
170172* / %
171173+ -
172174<< >>
src/parser.cpp+91-73
......@@ -903,6 +903,7 @@ static AstNode *ast_parse_block(ParseContext *pc, int *token_index, bool mandato
903903static AstNode *ast_parse_if_expr(ParseContext *pc, int *token_index, bool mandatory);
904904static AstNode *ast_parse_block_expr(ParseContext *pc, int *token_index, bool mandatory);
905905static AstNode *ast_parse_unwrap_maybe_expr(ParseContext *pc, int *token_index, bool mandatory);
906static AstNode *ast_parse_prefix_op_expr(ParseContext *pc, int *token_index, bool mandatory);
906907
907908static void ast_expect_token(ParseContext *pc, Token *token, TokenId token_id) {
908909 if (token->id == token_id) {
......@@ -998,7 +999,7 @@ static AstNode *ast_parse_param_decl(ParseContext *pc, int *token_index) {
998999 *token_index += 1;
9991000 ast_expect_token(pc, colon, TokenIdColon);
10001001
1001 node->data.param_decl.type = ast_parse_unwrap_maybe_expr(pc, token_index, true);
1002 node->data.param_decl.type = ast_parse_prefix_op_expr(pc, token_index, true);
10021003
10031004 return node;
10041005}
......@@ -1114,7 +1115,7 @@ static AstNode *ast_parse_array_type_expr(ParseContext *pc, int *token_index, bo
11141115 node->data.array_type.is_const = true;
11151116 }
11161117
1117 node->data.array_type.child_type = ast_parse_unwrap_maybe_expr(pc, token_index, true);
1118 node->data.array_type.child_type = ast_parse_prefix_op_expr(pc, token_index, true);
11181119
11191120 return node;
11201121}
......@@ -1159,7 +1160,7 @@ static void ast_parse_asm_output_item(ParseContext *pc, int *token_index, AstNod
11591160 if (token->id == TokenIdSymbol) {
11601161 ast_buf_from_token(pc, token, &asm_output->variable_name);
11611162 } else if (token->id == TokenIdArrow) {
1162 asm_output->return_type = ast_parse_unwrap_maybe_expr(pc, token_index, true);
1163 asm_output->return_type = ast_parse_prefix_op_expr(pc, token_index, true);
11631164 } else {
11641165 ast_invalid_token_error(pc, token);
11651166 }
......@@ -1402,81 +1403,23 @@ static AstNode *ast_parse_primary_expr(ParseContext *pc, int *token_index, bool
14021403}
14031404
14041405/*
1405SuffixOpExpression : PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression | ContainerInitExpression)
1406FnCallExpression : token(LParen) list(Expression, token(Comma)) token(RParen)
1407ArrayAccessExpression : token(LBracket) Expression token(RBracket)
1408SliceExpression : token(LBracket) Expression token(Ellipsis) option(Expression) token(RBracket) option(token(Const))
1409FieldAccessExpression : token(Dot) token(Symbol)
1406CurlySuffixExpression : PrefixOpExpression option(ContainerInitExpression)
14101407ContainerInitExpression : token(LBrace) ContainerInitBody token(RBrace)
14111408ContainerInitBody : list(StructLiteralField, token(Comma)) | list(Expression, token(Comma))
1412StructLiteralField : token(Dot) token(Symbol) token(Eq) Expression
14131409*/
1414static AstNode *ast_parse_suffix_op_expr(ParseContext *pc, int *token_index, bool mandatory) {
1415 AstNode *primary_expr = ast_parse_primary_expr(pc, token_index, mandatory);
1416 if (!primary_expr) {
1410static AstNode *ast_parse_curly_suffix_expr(ParseContext *pc, int *token_index, bool mandatory) {
1411 AstNode *prefix_op_expr = ast_parse_prefix_op_expr(pc, token_index, mandatory);
1412 if (!prefix_op_expr) {
14171413 return nullptr;
14181414 }
14191415
14201416 while (true) {
14211417 Token *first_token = &pc->tokens->at(*token_index);
1422 if (first_token->id == TokenIdLParen) {
1423 *token_index += 1;
1424
1425 AstNode *node = ast_create_node(pc, NodeTypeFnCallExpr, first_token);
1426 node->data.fn_call_expr.fn_ref_expr = primary_expr;
1427 ast_parse_fn_call_param_list(pc, token_index, &node->data.fn_call_expr.params);
1428
1429 primary_expr = node;
1430 } else if (first_token->id == TokenIdLBracket) {
1431 *token_index += 1;
1432
1433 AstNode *expr_node = ast_parse_expression(pc, token_index, true);
1434
1435 Token *ellipsis_or_r_bracket = &pc->tokens->at(*token_index);
1436
1437 if (ellipsis_or_r_bracket->id == TokenIdEllipsis) {
1438 *token_index += 1;
1439
1440 AstNode *node = ast_create_node(pc, NodeTypeSliceExpr, first_token);
1441 node->data.slice_expr.array_ref_expr = primary_expr;
1442 node->data.slice_expr.start = expr_node;
1443 node->data.slice_expr.end = ast_parse_expression(pc, token_index, false);
1444
1445 ast_eat_token(pc, token_index, TokenIdRBracket);
1446
1447 Token *const_tok = &pc->tokens->at(*token_index);
1448 if (const_tok->id == TokenIdKeywordConst) {
1449 *token_index += 1;
1450 node->data.slice_expr.is_const = true;
1451 }
1452
1453 primary_expr = node;
1454 } else if (ellipsis_or_r_bracket->id == TokenIdRBracket) {
1455 *token_index += 1;
1456
1457 AstNode *node = ast_create_node(pc, NodeTypeArrayAccessExpr, first_token);
1458 node->data.array_access_expr.array_ref_expr = primary_expr;
1459 node->data.array_access_expr.subscript = expr_node;
1460
1461 primary_expr = node;
1462 } else {
1463 ast_invalid_token_error(pc, first_token);
1464 }
1465 } else if (first_token->id == TokenIdDot) {
1466 *token_index += 1;
1467
1468 Token *name_token = ast_eat_token(pc, token_index, TokenIdSymbol);
1469
1470 AstNode *node = ast_create_node(pc, NodeTypeFieldAccessExpr, first_token);
1471 node->data.field_access_expr.struct_expr = primary_expr;
1472 ast_buf_from_token(pc, name_token, &node->data.field_access_expr.field_name);
1473
1474 primary_expr = node;
1475 } else if (first_token->id == TokenIdLBrace) {
1418 if (first_token->id == TokenIdLBrace) {
14761419 *token_index += 1;
14771420
14781421 AstNode *node = ast_create_node(pc, NodeTypeContainerInitExpr, first_token);
1479 node->data.container_init_expr.type = primary_expr;
1422 node->data.container_init_expr.type = prefix_op_expr;
14801423
14811424 Token *token = &pc->tokens->at(*token_index);
14821425 if (token->id == TokenIdDot) {
......@@ -1536,6 +1479,81 @@ static AstNode *ast_parse_suffix_op_expr(ParseContext *pc, int *token_index, boo
15361479 }
15371480 }
15381481
1482 prefix_op_expr = node;
1483 } else {
1484 return prefix_op_expr;
1485 }
1486 }
1487}
1488
1489/*
1490SuffixOpExpression : PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression)
1491FnCallExpression : token(LParen) list(Expression, token(Comma)) token(RParen)
1492ArrayAccessExpression : token(LBracket) Expression token(RBracket)
1493SliceExpression : token(LBracket) Expression token(Ellipsis) option(Expression) token(RBracket) option(token(Const))
1494FieldAccessExpression : token(Dot) token(Symbol)
1495StructLiteralField : token(Dot) token(Symbol) token(Eq) Expression
1496*/
1497static AstNode *ast_parse_suffix_op_expr(ParseContext *pc, int *token_index, bool mandatory) {
1498 AstNode *primary_expr = ast_parse_primary_expr(pc, token_index, mandatory);
1499 if (!primary_expr) {
1500 return nullptr;
1501 }
1502
1503 while (true) {
1504 Token *first_token = &pc->tokens->at(*token_index);
1505 if (first_token->id == TokenIdLParen) {
1506 *token_index += 1;
1507
1508 AstNode *node = ast_create_node(pc, NodeTypeFnCallExpr, first_token);
1509 node->data.fn_call_expr.fn_ref_expr = primary_expr;
1510 ast_parse_fn_call_param_list(pc, token_index, &node->data.fn_call_expr.params);
1511
1512 primary_expr = node;
1513 } else if (first_token->id == TokenIdLBracket) {
1514 *token_index += 1;
1515
1516 AstNode *expr_node = ast_parse_expression(pc, token_index, true);
1517
1518 Token *ellipsis_or_r_bracket = &pc->tokens->at(*token_index);
1519
1520 if (ellipsis_or_r_bracket->id == TokenIdEllipsis) {
1521 *token_index += 1;
1522
1523 AstNode *node = ast_create_node(pc, NodeTypeSliceExpr, first_token);
1524 node->data.slice_expr.array_ref_expr = primary_expr;
1525 node->data.slice_expr.start = expr_node;
1526 node->data.slice_expr.end = ast_parse_expression(pc, token_index, false);
1527
1528 ast_eat_token(pc, token_index, TokenIdRBracket);
1529
1530 Token *const_tok = &pc->tokens->at(*token_index);
1531 if (const_tok->id == TokenIdKeywordConst) {
1532 *token_index += 1;
1533 node->data.slice_expr.is_const = true;
1534 }
1535
1536 primary_expr = node;
1537 } else if (ellipsis_or_r_bracket->id == TokenIdRBracket) {
1538 *token_index += 1;
1539
1540 AstNode *node = ast_create_node(pc, NodeTypeArrayAccessExpr, first_token);
1541 node->data.array_access_expr.array_ref_expr = primary_expr;
1542 node->data.array_access_expr.subscript = expr_node;
1543
1544 primary_expr = node;
1545 } else {
1546 ast_invalid_token_error(pc, first_token);
1547 }
1548 } else if (first_token->id == TokenIdDot) {
1549 *token_index += 1;
1550
1551 Token *name_token = ast_eat_token(pc, token_index, TokenIdSymbol);
1552
1553 AstNode *node = ast_create_node(pc, NodeTypeFieldAccessExpr, first_token);
1554 node->data.field_access_expr.struct_expr = primary_expr;
1555 ast_buf_from_token(pc, name_token, &node->data.field_access_expr.field_name);
1556
15391557 primary_expr = node;
15401558 } else {
15411559 return primary_expr;
......@@ -1623,10 +1641,10 @@ static BinOpType ast_parse_mult_op(ParseContext *pc, int *token_index, bool mand
16231641}
16241642
16251643/*
1626MultiplyExpression : PrefixOpExpression MultiplyOperator MultiplyExpression | PrefixOpExpression
1644MultiplyExpression : CurlySuffixExpression MultiplyOperator MultiplyExpression | CurlySuffixExpression
16271645*/
16281646static AstNode *ast_parse_mult_expr(ParseContext *pc, int *token_index, bool mandatory) {
1629 AstNode *operand_1 = ast_parse_prefix_op_expr(pc, token_index, mandatory);
1647 AstNode *operand_1 = ast_parse_curly_suffix_expr(pc, token_index, mandatory);
16301648 if (!operand_1)
16311649 return nullptr;
16321650
......@@ -1636,7 +1654,7 @@ static AstNode *ast_parse_mult_expr(ParseContext *pc, int *token_index, bool man
16361654 if (mult_op == BinOpTypeInvalid)
16371655 return operand_1;
16381656
1639 AstNode *operand_2 = ast_parse_prefix_op_expr(pc, token_index, true);
1657 AstNode *operand_2 = ast_parse_curly_suffix_expr(pc, token_index, true);
16401658
16411659 AstNode *node = ast_create_node(pc, NodeTypeBinOpExpr, token);
16421660 node->data.bin_op_expr.op1 = operand_1;
......@@ -1948,7 +1966,7 @@ static AstNode *ast_parse_if_expr(ParseContext *pc, int *token_index, bool manda
19481966 node->data.if_var_expr.var_decl.expr = ast_parse_expression(pc, token_index, true);
19491967 } else if (eq_or_colon->id == TokenIdColon) {
19501968 *token_index += 1;
1951 node->data.if_var_expr.var_decl.type = ast_parse_unwrap_maybe_expr(pc, token_index, true);
1969 node->data.if_var_expr.var_decl.type = ast_parse_prefix_op_expr(pc, token_index, true);
19521970
19531971 ast_eat_token(pc, token_index, TokenIdMaybeAssign);
19541972 node->data.if_var_expr.var_decl.expr = ast_parse_expression(pc, token_index, true);
......@@ -2028,7 +2046,7 @@ static AstNode *ast_parse_variable_declaration_expr(ParseContext *pc, int *token
20282046 node->data.variable_declaration.expr = ast_parse_expression(pc, token_index, true);
20292047 return node;
20302048 } else if (eq_or_colon->id == TokenIdColon) {
2031 node->data.variable_declaration.type = ast_parse_unwrap_maybe_expr(pc, token_index, true);
2049 node->data.variable_declaration.type = ast_parse_prefix_op_expr(pc, token_index, true);
20322050 Token *eq_token = &pc->tokens->at(*token_index);
20332051 if (eq_token->id == TokenIdEq) {
20342052 *token_index += 1;
......@@ -2394,7 +2412,7 @@ static AstNode *ast_parse_fn_proto(ParseContext *pc, int *token_index, bool mand
23942412 ast_parse_param_decl_list(pc, token_index, &node->data.fn_proto.params, &node->data.fn_proto.is_var_args);
23952413
23962414 Token *next_token = &pc->tokens->at(*token_index);
2397 node->data.fn_proto.return_type = ast_parse_unwrap_maybe_expr(pc, token_index, false);
2415 node->data.fn_proto.return_type = ast_parse_prefix_op_expr(pc, token_index, false);
23982416 if (!node->data.fn_proto.return_type) {
23992417 node->data.fn_proto.return_type = ast_create_void_type_node(pc, next_token);
24002418 }