authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2015-12-14 12:58:06-07:00
committergravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2015-12-14 12:58:06-07:00
log7dd29291853a30973c19867385514b6d807cb644
tree5f6ca4bebc7cc24c8a64c86154cf318447b924e1
parent9dbedb02cc7291b2d67407183399667205c4e0af

prefer `int *token_index` over new_token_index


1 files changed, 55 insertions(+), 67 deletions(-)

src/parser.cpp+55-67
......@@ -796,34 +796,33 @@ static Token *ast_eat_token(ParseContext *pc, int *token_index, TokenId token_id
796796}
797797
798798
799static AstNode *ast_parse_directive(ParseContext *pc, int token_index, int *new_token_index) {
800 Token *number_sign = &pc->tokens->at(token_index);
801 token_index += 1;
799static AstNode *ast_parse_directive(ParseContext *pc, int *token_index) {
800 Token *number_sign = &pc->tokens->at(*token_index);
801 *token_index += 1;
802802 ast_expect_token(pc, number_sign, TokenIdNumberSign);
803803
804804 AstNode *node = ast_create_node(pc, NodeTypeDirective, number_sign);
805805
806 Token *name_symbol = &pc->tokens->at(token_index);
807 token_index += 1;
806 Token *name_symbol = &pc->tokens->at(*token_index);
807 *token_index += 1;
808808 ast_expect_token(pc, name_symbol, TokenIdSymbol);
809809
810810 ast_buf_from_token(pc, name_symbol, &node->data.directive.name);
811811
812 Token *l_paren = &pc->tokens->at(token_index);
813 token_index += 1;
812 Token *l_paren = &pc->tokens->at(*token_index);
813 *token_index += 1;
814814 ast_expect_token(pc, l_paren, TokenIdLParen);
815815
816 Token *param_str = &pc->tokens->at(token_index);
817 token_index += 1;
816 Token *param_str = &pc->tokens->at(*token_index);
817 *token_index += 1;
818818 ast_expect_token(pc, param_str, TokenIdStringLiteral);
819819
820820 parse_string_literal(pc, param_str, &node->data.directive.param, nullptr, nullptr);
821821
822 Token *r_paren = &pc->tokens->at(token_index);
823 token_index += 1;
822 Token *r_paren = &pc->tokens->at(*token_index);
823 *token_index += 1;
824824 ast_expect_token(pc, r_paren, TokenIdRParen);
825825
826 *new_token_index = token_index;
827826 return node;
828827}
829828
......@@ -833,7 +832,7 @@ static void ast_parse_directives(ParseContext *pc, int *token_index,
833832 for (;;) {
834833 Token *token = &pc->tokens->at(*token_index);
835834 if (token->id == TokenIdNumberSign) {
836 AstNode *directive_node = ast_parse_directive(pc, *token_index, token_index);
835 AstNode *directive_node = ast_parse_directive(pc, token_index);
837836 directives->append(directive_node);
838837 } else {
839838 return;
......@@ -848,9 +847,9 @@ Type : token(Symbol) | token(Unreachable) | token(Void) | PointerType | ArrayTyp
848847PointerType : token(Star) (token(Const) | token(Mut)) Type
849848ArrayType : token(LBracket) Type token(Semicolon) token(Number) token(RBracket)
850849*/
851static AstNode *ast_parse_type(ParseContext *pc, int token_index, int *new_token_index) {
852 Token *token = &pc->tokens->at(token_index);
853 token_index += 1;
850static AstNode *ast_parse_type(ParseContext *pc, int *token_index) {
851 Token *token = &pc->tokens->at(*token_index);
852 *token_index += 1;
854853
855854 AstNode *node = ast_create_node(pc, NodeTypeType, token);
856855
......@@ -865,8 +864,8 @@ static AstNode *ast_parse_type(ParseContext *pc, int token_index, int *new_token
865864 ast_buf_from_token(pc, token, &node->data.type.primitive_name);
866865 } else if (token->id == TokenIdStar) {
867866 node->data.type.type = AstNodeTypeTypePointer;
868 Token *const_or_mut = &pc->tokens->at(token_index);
869 token_index += 1;
867 Token *const_or_mut = &pc->tokens->at(*token_index);
868 *token_index += 1;
870869 if (const_or_mut->id == TokenIdKeywordMut) {
871870 node->data.type.is_const = false;
872871 } else if (const_or_mut->id == TokenIdKeywordConst) {
......@@ -875,51 +874,48 @@ static AstNode *ast_parse_type(ParseContext *pc, int token_index, int *new_token
875874 ast_invalid_token_error(pc, const_or_mut);
876875 }
877876
878 node->data.type.child_type = ast_parse_type(pc, token_index, &token_index);
877 node->data.type.child_type = ast_parse_type(pc, token_index);
879878 } else if (token->id == TokenIdLBracket) {
880879 node->data.type.type = AstNodeTypeTypeArray;
881880
882 node->data.type.child_type = ast_parse_type(pc, token_index, &token_index);
881 node->data.type.child_type = ast_parse_type(pc, token_index);
883882
884 Token *semicolon_token = &pc->tokens->at(token_index);
885 token_index += 1;
883 Token *semicolon_token = &pc->tokens->at(*token_index);
884 *token_index += 1;
886885 ast_expect_token(pc, semicolon_token, TokenIdSemicolon);
887886
888 node->data.type.array_size = ast_parse_expression(pc, &token_index, true);
887 node->data.type.array_size = ast_parse_expression(pc, token_index, true);
889888
890 Token *rbracket_token = &pc->tokens->at(token_index);
891 token_index += 1;
889 Token *rbracket_token = &pc->tokens->at(*token_index);
890 *token_index += 1;
892891 ast_expect_token(pc, rbracket_token, TokenIdRBracket);
893892 } else {
894893 ast_invalid_token_error(pc, token);
895894 }
896895
897 *new_token_index = token_index;
898896 return node;
899897}
900898
901899/*
902900ParamDecl : token(Symbol) token(Colon) Type | token(Ellipsis)
903901*/
904static AstNode *ast_parse_param_decl(ParseContext *pc, int token_index, int *new_token_index) {
905 Token *param_name = &pc->tokens->at(token_index);
906 token_index += 1;
902static AstNode *ast_parse_param_decl(ParseContext *pc, int *token_index) {
903 Token *param_name = &pc->tokens->at(*token_index);
904 *token_index += 1;
907905
908906 if (param_name->id == TokenIdSymbol) {
909907 AstNode *node = ast_create_node(pc, NodeTypeParamDecl, param_name);
910908
911909 ast_buf_from_token(pc, param_name, &node->data.param_decl.name);
912910
913 Token *colon = &pc->tokens->at(token_index);
914 token_index += 1;
911 Token *colon = &pc->tokens->at(*token_index);
912 *token_index += 1;
915913 ast_expect_token(pc, colon, TokenIdColon);
916914
917 node->data.param_decl.type = ast_parse_type(pc, token_index, &token_index);
915 node->data.param_decl.type = ast_parse_type(pc, token_index);
918916
919 *new_token_index = token_index;
920917 return node;
921918 } else if (param_name->id == TokenIdEllipsis) {
922 *new_token_index = token_index;
923919 return nullptr;
924920 } else {
925921 ast_invalid_token_error(pc, param_name);
......@@ -927,24 +923,23 @@ static AstNode *ast_parse_param_decl(ParseContext *pc, int token_index, int *new
927923}
928924
929925
930static void ast_parse_param_decl_list(ParseContext *pc, int token_index, int *new_token_index,
926static void ast_parse_param_decl_list(ParseContext *pc, int *token_index,
931927 ZigList<AstNode *> *params, bool *is_var_args)
932928{
933929 *is_var_args = false;
934930
935 Token *l_paren = &pc->tokens->at(token_index);
936 token_index += 1;
931 Token *l_paren = &pc->tokens->at(*token_index);
932 *token_index += 1;
937933 ast_expect_token(pc, l_paren, TokenIdLParen);
938934
939 Token *token = &pc->tokens->at(token_index);
935 Token *token = &pc->tokens->at(*token_index);
940936 if (token->id == TokenIdRParen) {
941 token_index += 1;
942 *new_token_index = token_index;
937 *token_index += 1;
943938 return;
944939 }
945940
946941 for (;;) {
947 AstNode *param_decl_node = ast_parse_param_decl(pc, token_index, &token_index);
942 AstNode *param_decl_node = ast_parse_param_decl(pc, token_index);
948943 bool expect_end = false;
949944 if (param_decl_node) {
950945 params->append(param_decl_node);
......@@ -953,10 +948,9 @@ static void ast_parse_param_decl_list(ParseContext *pc, int token_index, int *ne
953948 expect_end = true;
954949 }
955950
956 Token *token = &pc->tokens->at(token_index);
957 token_index += 1;
951 Token *token = &pc->tokens->at(*token_index);
952 *token_index += 1;
958953 if (token->id == TokenIdRParen) {
959 *new_token_index = token_index;
960954 return;
961955 } else if (expect_end) {
962956 ast_invalid_token_error(pc, token);
......@@ -967,24 +961,20 @@ static void ast_parse_param_decl_list(ParseContext *pc, int token_index, int *ne
967961 zig_unreachable();
968962}
969963
970static void ast_parse_fn_call_param_list(ParseContext *pc, int token_index, int *new_token_index,
971 ZigList<AstNode*> *params)
972{
973 Token *token = &pc->tokens->at(token_index);
964static void ast_parse_fn_call_param_list(ParseContext *pc, int *token_index, ZigList<AstNode*> *params) {
965 Token *token = &pc->tokens->at(*token_index);
974966 if (token->id == TokenIdRParen) {
975 token_index += 1;
976 *new_token_index = token_index;
967 *token_index += 1;
977968 return;
978969 }
979970
980971 for (;;) {
981 AstNode *expr = ast_parse_expression(pc, &token_index, true);
972 AstNode *expr = ast_parse_expression(pc, token_index, true);
982973 params->append(expr);
983974
984 Token *token = &pc->tokens->at(token_index);
985 token_index += 1;
975 Token *token = &pc->tokens->at(*token_index);
976 *token_index += 1;
986977 if (token->id == TokenIdRParen) {
987 *new_token_index = token_index;
988978 return;
989979 } else {
990980 ast_expect_token(pc, token, TokenIdComma);
......@@ -1102,7 +1092,7 @@ static AstNode *ast_parse_suffix_op_expr(ParseContext *pc, int *token_index, boo
11021092
11031093 AstNode *node = ast_create_node(pc, NodeTypeFnCallExpr, token);
11041094 node->data.fn_call_expr.fn_ref_expr = primary_expr;
1105 ast_parse_fn_call_param_list(pc, *token_index, token_index, &node->data.fn_call_expr.params);
1095 ast_parse_fn_call_param_list(pc, token_index, &node->data.fn_call_expr.params);
11061096 return node;
11071097 } else if (token->id == TokenIdLBracket) {
11081098 *token_index += 1;
......@@ -1192,7 +1182,7 @@ static AstNode *ast_parse_cast_expression(ParseContext *pc, int *token_index, bo
11921182 AstNode *node = ast_create_node(pc, NodeTypeCastExpr, as_kw);
11931183 node->data.cast_expr.expr = operand_1;
11941184
1195 node->data.cast_expr.type = ast_parse_type(pc, *token_index, token_index);
1185 node->data.cast_expr.type = ast_parse_type(pc, token_index);
11961186
11971187 operand_1 = node;
11981188 }
......@@ -1592,7 +1582,7 @@ static AstNode *ast_parse_variable_declaration_expr(ParseContext *pc, int *token
15921582 node->data.variable_declaration.expr = ast_parse_expression(pc, token_index, true);
15931583 return node;
15941584 } else if (eq_or_colon->id == TokenIdColon) {
1595 node->data.variable_declaration.type = ast_parse_type(pc, *token_index, token_index);
1585 node->data.variable_declaration.type = ast_parse_type(pc, token_index);
15961586
15971587 Token *eq_token = &pc->tokens->at(*token_index);
15981588 if (eq_token->id == TokenIdEq) {
......@@ -2065,13 +2055,12 @@ static AstNode *ast_parse_fn_proto(ParseContext *pc, int *token_index, bool mand
20652055 ast_buf_from_token(pc, fn_name, &node->data.fn_proto.name);
20662056
20672057
2068 ast_parse_param_decl_list(pc, *token_index, token_index,
2069 &node->data.fn_proto.params, &node->data.fn_proto.is_var_args);
2058 ast_parse_param_decl_list(pc, token_index, &node->data.fn_proto.params, &node->data.fn_proto.is_var_args);
20702059
20712060 Token *arrow = &pc->tokens->at(*token_index);
20722061 if (arrow->id == TokenIdArrow) {
20732062 *token_index += 1;
2074 node->data.fn_proto.return_type = ast_parse_type(pc, *token_index, token_index);
2063 node->data.fn_proto.return_type = ast_parse_type(pc, token_index);
20752064 } else {
20762065 node->data.fn_proto.return_type = ast_create_void_type_node(pc, arrow);
20772066 }
......@@ -2097,17 +2086,16 @@ static AstNode *ast_parse_fn_def(ParseContext *pc, int *token_index, bool mandat
20972086/*
20982087FnDecl : FnProto token(Semicolon)
20992088*/
2100static AstNode *ast_parse_fn_decl(ParseContext *pc, int token_index, int *new_token_index) {
2101 AstNode *fn_proto = ast_parse_fn_proto(pc, &token_index, true);
2089static AstNode *ast_parse_fn_decl(ParseContext *pc, int *token_index) {
2090 AstNode *fn_proto = ast_parse_fn_proto(pc, token_index, true);
21022091 AstNode *node = ast_create_node_with_node(pc, NodeTypeFnDecl, fn_proto);
21032092
21042093 node->data.fn_decl.fn_proto = fn_proto;
21052094
2106 Token *semicolon = &pc->tokens->at(token_index);
2107 token_index += 1;
2095 Token *semicolon = &pc->tokens->at(*token_index);
2096 *token_index += 1;
21082097 ast_expect_token(pc, semicolon, TokenIdSemicolon);
21092098
2110 *new_token_index = token_index;
21112099 return node;
21122100}
21132101
......@@ -2152,7 +2140,7 @@ static AstNode *ast_parse_extern_block(ParseContext *pc, int *token_index, bool
21522140 *token_index += 1;
21532141 return node;
21542142 } else {
2155 AstNode *child = ast_parse_fn_decl(pc, *token_index, token_index);
2143 AstNode *child = ast_parse_fn_decl(pc, token_index);
21562144 node->data.extern_block.fn_decls.append(child);
21572145 }
21582146 }
......@@ -2256,7 +2244,7 @@ static AstNode *ast_parse_struct_decl(ParseContext *pc, int *token_index) {
22562244
22572245 ast_eat_token(pc, token_index, TokenIdColon);
22582246
2259 field_node->data.struct_field.type = ast_parse_type(pc, *token_index, token_index);
2247 field_node->data.struct_field.type = ast_parse_type(pc, token_index);
22602248
22612249 ast_eat_token(pc, token_index, TokenIdComma);
22622250