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...@@ -796,34 +796,33 @@ static Token *ast_eat_token(ParseContext *pc, int *token_index, TokenId token_id
796}796}
797797
798798
799static AstNode *ast_parse_directive(ParseContext *pc, int token_index, int *new_token_index) {799static AstNode *ast_parse_directive(ParseContext *pc, int *token_index) {
800 Token *number_sign = &pc->tokens->at(token_index);800 Token *number_sign = &pc->tokens->at(*token_index);
801 token_index += 1;801 *token_index += 1;
802 ast_expect_token(pc, number_sign, TokenIdNumberSign);802 ast_expect_token(pc, number_sign, TokenIdNumberSign);
803803
804 AstNode *node = ast_create_node(pc, NodeTypeDirective, number_sign);804 AstNode *node = ast_create_node(pc, NodeTypeDirective, number_sign);
805805
806 Token *name_symbol = &pc->tokens->at(token_index);806 Token *name_symbol = &pc->tokens->at(*token_index);
807 token_index += 1;807 *token_index += 1;
808 ast_expect_token(pc, name_symbol, TokenIdSymbol);808 ast_expect_token(pc, name_symbol, TokenIdSymbol);
809809
810 ast_buf_from_token(pc, name_symbol, &node->data.directive.name);810 ast_buf_from_token(pc, name_symbol, &node->data.directive.name);
811811
812 Token *l_paren = &pc->tokens->at(token_index);812 Token *l_paren = &pc->tokens->at(*token_index);
813 token_index += 1;813 *token_index += 1;
814 ast_expect_token(pc, l_paren, TokenIdLParen);814 ast_expect_token(pc, l_paren, TokenIdLParen);
815815
816 Token *param_str = &pc->tokens->at(token_index);816 Token *param_str = &pc->tokens->at(*token_index);
817 token_index += 1;817 *token_index += 1;
818 ast_expect_token(pc, param_str, TokenIdStringLiteral);818 ast_expect_token(pc, param_str, TokenIdStringLiteral);
819819
820 parse_string_literal(pc, param_str, &node->data.directive.param, nullptr, nullptr);820 parse_string_literal(pc, param_str, &node->data.directive.param, nullptr, nullptr);
821821
822 Token *r_paren = &pc->tokens->at(token_index);822 Token *r_paren = &pc->tokens->at(*token_index);
823 token_index += 1;823 *token_index += 1;
824 ast_expect_token(pc, r_paren, TokenIdRParen);824 ast_expect_token(pc, r_paren, TokenIdRParen);
825825
826 *new_token_index = token_index;
827 return node;826 return node;
828}827}
829828
...@@ -833,7 +832,7 @@ static void ast_parse_directives(ParseContext *pc, int *token_index,...@@ -833,7 +832,7 @@ static void ast_parse_directives(ParseContext *pc, int *token_index,
833 for (;;) {832 for (;;) {
834 Token *token = &pc->tokens->at(*token_index);833 Token *token = &pc->tokens->at(*token_index);
835 if (token->id == TokenIdNumberSign) {834 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);
837 directives->append(directive_node);836 directives->append(directive_node);
838 } else {837 } else {
839 return;838 return;
...@@ -848,9 +847,9 @@ Type : token(Symbol) | token(Unreachable) | token(Void) | PointerType | ArrayTyp...@@ -848,9 +847,9 @@ Type : token(Symbol) | token(Unreachable) | token(Void) | PointerType | ArrayTyp
848PointerType : token(Star) (token(Const) | token(Mut)) Type847PointerType : token(Star) (token(Const) | token(Mut)) Type
849ArrayType : token(LBracket) Type token(Semicolon) token(Number) token(RBracket)848ArrayType : token(LBracket) Type token(Semicolon) token(Number) token(RBracket)
850*/849*/
851static AstNode *ast_parse_type(ParseContext *pc, int token_index, int *new_token_index) {850static AstNode *ast_parse_type(ParseContext *pc, int *token_index) {
852 Token *token = &pc->tokens->at(token_index);851 Token *token = &pc->tokens->at(*token_index);
853 token_index += 1;852 *token_index += 1;
854853
855 AstNode *node = ast_create_node(pc, NodeTypeType, token);854 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...@@ -865,8 +864,8 @@ static AstNode *ast_parse_type(ParseContext *pc, int token_index, int *new_token
865 ast_buf_from_token(pc, token, &node->data.type.primitive_name);864 ast_buf_from_token(pc, token, &node->data.type.primitive_name);
866 } else if (token->id == TokenIdStar) {865 } else if (token->id == TokenIdStar) {
867 node->data.type.type = AstNodeTypeTypePointer;866 node->data.type.type = AstNodeTypeTypePointer;
868 Token *const_or_mut = &pc->tokens->at(token_index);867 Token *const_or_mut = &pc->tokens->at(*token_index);
869 token_index += 1;868 *token_index += 1;
870 if (const_or_mut->id == TokenIdKeywordMut) {869 if (const_or_mut->id == TokenIdKeywordMut) {
871 node->data.type.is_const = false;870 node->data.type.is_const = false;
872 } else if (const_or_mut->id == TokenIdKeywordConst) {871 } else if (const_or_mut->id == TokenIdKeywordConst) {
...@@ -875,51 +874,48 @@ static AstNode *ast_parse_type(ParseContext *pc, int token_index, int *new_token...@@ -875,51 +874,48 @@ static AstNode *ast_parse_type(ParseContext *pc, int token_index, int *new_token
875 ast_invalid_token_error(pc, const_or_mut);874 ast_invalid_token_error(pc, const_or_mut);
876 }875 }
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);
879 } else if (token->id == TokenIdLBracket) {878 } else if (token->id == TokenIdLBracket) {
880 node->data.type.type = AstNodeTypeTypeArray;879 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);883 Token *semicolon_token = &pc->tokens->at(*token_index);
885 token_index += 1;884 *token_index += 1;
886 ast_expect_token(pc, semicolon_token, TokenIdSemicolon);885 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);889 Token *rbracket_token = &pc->tokens->at(*token_index);
891 token_index += 1;890 *token_index += 1;
892 ast_expect_token(pc, rbracket_token, TokenIdRBracket);891 ast_expect_token(pc, rbracket_token, TokenIdRBracket);
893 } else {892 } else {
894 ast_invalid_token_error(pc, token);893 ast_invalid_token_error(pc, token);
895 }894 }
896895
897 *new_token_index = token_index;
898 return node;896 return node;
899}897}
900898
901/*899/*
902ParamDecl : token(Symbol) token(Colon) Type | token(Ellipsis)900ParamDecl : token(Symbol) token(Colon) Type | token(Ellipsis)
903*/901*/
904static AstNode *ast_parse_param_decl(ParseContext *pc, int token_index, int *new_token_index) {902static AstNode *ast_parse_param_decl(ParseContext *pc, int *token_index) {
905 Token *param_name = &pc->tokens->at(token_index);903 Token *param_name = &pc->tokens->at(*token_index);
906 token_index += 1;904 *token_index += 1;
907905
908 if (param_name->id == TokenIdSymbol) {906 if (param_name->id == TokenIdSymbol) {
909 AstNode *node = ast_create_node(pc, NodeTypeParamDecl, param_name);907 AstNode *node = ast_create_node(pc, NodeTypeParamDecl, param_name);
910908
911 ast_buf_from_token(pc, param_name, &node->data.param_decl.name);909 ast_buf_from_token(pc, param_name, &node->data.param_decl.name);
912910
913 Token *colon = &pc->tokens->at(token_index);911 Token *colon = &pc->tokens->at(*token_index);
914 token_index += 1;912 *token_index += 1;
915 ast_expect_token(pc, colon, TokenIdColon);913 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;
920 return node;917 return node;
921 } else if (param_name->id == TokenIdEllipsis) {918 } else if (param_name->id == TokenIdEllipsis) {
922 *new_token_index = token_index;
923 return nullptr;919 return nullptr;
924 } else {920 } else {
925 ast_invalid_token_error(pc, param_name);921 ast_invalid_token_error(pc, param_name);
...@@ -927,24 +923,23 @@ static AstNode *ast_parse_param_decl(ParseContext *pc, int token_index, int *new...@@ -927,24 +923,23 @@ static AstNode *ast_parse_param_decl(ParseContext *pc, int token_index, int *new
927}923}
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,
931 ZigList<AstNode *> *params, bool *is_var_args)927 ZigList<AstNode *> *params, bool *is_var_args)
932{928{
933 *is_var_args = false;929 *is_var_args = false;
934930
935 Token *l_paren = &pc->tokens->at(token_index);931 Token *l_paren = &pc->tokens->at(*token_index);
936 token_index += 1;932 *token_index += 1;
937 ast_expect_token(pc, l_paren, TokenIdLParen);933 ast_expect_token(pc, l_paren, TokenIdLParen);
938934
939 Token *token = &pc->tokens->at(token_index);935 Token *token = &pc->tokens->at(*token_index);
940 if (token->id == TokenIdRParen) {936 if (token->id == TokenIdRParen) {
941 token_index += 1;937 *token_index += 1;
942 *new_token_index = token_index;
943 return;938 return;
944 }939 }
945940
946 for (;;) {941 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);
948 bool expect_end = false;943 bool expect_end = false;
949 if (param_decl_node) {944 if (param_decl_node) {
950 params->append(param_decl_node);945 params->append(param_decl_node);
...@@ -953,10 +948,9 @@ static void ast_parse_param_decl_list(ParseContext *pc, int token_index, int *ne...@@ -953,10 +948,9 @@ static void ast_parse_param_decl_list(ParseContext *pc, int token_index, int *ne
953 expect_end = true;948 expect_end = true;
954 }949 }
955950
956 Token *token = &pc->tokens->at(token_index);951 Token *token = &pc->tokens->at(*token_index);
957 token_index += 1;952 *token_index += 1;
958 if (token->id == TokenIdRParen) {953 if (token->id == TokenIdRParen) {
959 *new_token_index = token_index;
960 return;954 return;
961 } else if (expect_end) {955 } else if (expect_end) {
962 ast_invalid_token_error(pc, token);956 ast_invalid_token_error(pc, token);
...@@ -967,24 +961,20 @@ static void ast_parse_param_decl_list(ParseContext *pc, int token_index, int *ne...@@ -967,24 +961,20 @@ static void ast_parse_param_decl_list(ParseContext *pc, int token_index, int *ne
967 zig_unreachable();961 zig_unreachable();
968}962}
969963
970static void ast_parse_fn_call_param_list(ParseContext *pc, int token_index, int *new_token_index,964static void ast_parse_fn_call_param_list(ParseContext *pc, int *token_index, ZigList<AstNode*> *params) {
971 ZigList<AstNode*> *params)965 Token *token = &pc->tokens->at(*token_index);
972{
973 Token *token = &pc->tokens->at(token_index);
974 if (token->id == TokenIdRParen) {966 if (token->id == TokenIdRParen) {
975 token_index += 1;967 *token_index += 1;
976 *new_token_index = token_index;
977 return;968 return;
978 }969 }
979970
980 for (;;) {971 for (;;) {
981 AstNode *expr = ast_parse_expression(pc, &token_index, true);972 AstNode *expr = ast_parse_expression(pc, token_index, true);
982 params->append(expr);973 params->append(expr);
983974
984 Token *token = &pc->tokens->at(token_index);975 Token *token = &pc->tokens->at(*token_index);
985 token_index += 1;976 *token_index += 1;
986 if (token->id == TokenIdRParen) {977 if (token->id == TokenIdRParen) {
987 *new_token_index = token_index;
988 return;978 return;
989 } else {979 } else {
990 ast_expect_token(pc, token, TokenIdComma);980 ast_expect_token(pc, token, TokenIdComma);
...@@ -1102,7 +1092,7 @@ static AstNode *ast_parse_suffix_op_expr(ParseContext *pc, int *token_index, boo...@@ -1102,7 +1092,7 @@ static AstNode *ast_parse_suffix_op_expr(ParseContext *pc, int *token_index, boo
11021092
1103 AstNode *node = ast_create_node(pc, NodeTypeFnCallExpr, token);1093 AstNode *node = ast_create_node(pc, NodeTypeFnCallExpr, token);
1104 node->data.fn_call_expr.fn_ref_expr = primary_expr;1094 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);
1106 return node;1096 return node;
1107 } else if (token->id == TokenIdLBracket) {1097 } else if (token->id == TokenIdLBracket) {
1108 *token_index += 1;1098 *token_index += 1;
...@@ -1192,7 +1182,7 @@ static AstNode *ast_parse_cast_expression(ParseContext *pc, int *token_index, bo...@@ -1192,7 +1182,7 @@ static AstNode *ast_parse_cast_expression(ParseContext *pc, int *token_index, bo
1192 AstNode *node = ast_create_node(pc, NodeTypeCastExpr, as_kw);1182 AstNode *node = ast_create_node(pc, NodeTypeCastExpr, as_kw);
1193 node->data.cast_expr.expr = operand_1;1183 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
1197 operand_1 = node;1187 operand_1 = node;
1198 }1188 }
...@@ -1592,7 +1582,7 @@ static AstNode *ast_parse_variable_declaration_expr(ParseContext *pc, int *token...@@ -1592,7 +1582,7 @@ static AstNode *ast_parse_variable_declaration_expr(ParseContext *pc, int *token
1592 node->data.variable_declaration.expr = ast_parse_expression(pc, token_index, true);1582 node->data.variable_declaration.expr = ast_parse_expression(pc, token_index, true);
1593 return node;1583 return node;
1594 } else if (eq_or_colon->id == TokenIdColon) {1584 } 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
1597 Token *eq_token = &pc->tokens->at(*token_index);1587 Token *eq_token = &pc->tokens->at(*token_index);
1598 if (eq_token->id == TokenIdEq) {1588 if (eq_token->id == TokenIdEq) {
...@@ -2065,13 +2055,12 @@ static AstNode *ast_parse_fn_proto(ParseContext *pc, int *token_index, bool mand...@@ -2065,13 +2055,12 @@ static AstNode *ast_parse_fn_proto(ParseContext *pc, int *token_index, bool mand
2065 ast_buf_from_token(pc, fn_name, &node->data.fn_proto.name);2055 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,2058 ast_parse_param_decl_list(pc, token_index, &node->data.fn_proto.params, &node->data.fn_proto.is_var_args);
2069 &node->data.fn_proto.params, &node->data.fn_proto.is_var_args);
20702059
2071 Token *arrow = &pc->tokens->at(*token_index);2060 Token *arrow = &pc->tokens->at(*token_index);
2072 if (arrow->id == TokenIdArrow) {2061 if (arrow->id == TokenIdArrow) {
2073 *token_index += 1;2062 *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);
2075 } else {2064 } else {
2076 node->data.fn_proto.return_type = ast_create_void_type_node(pc, arrow);2065 node->data.fn_proto.return_type = ast_create_void_type_node(pc, arrow);
2077 }2066 }
...@@ -2097,17 +2086,16 @@ static AstNode *ast_parse_fn_def(ParseContext *pc, int *token_index, bool mandat...@@ -2097,17 +2086,16 @@ static AstNode *ast_parse_fn_def(ParseContext *pc, int *token_index, bool mandat
2097/*2086/*
2098FnDecl : FnProto token(Semicolon)2087FnDecl : FnProto token(Semicolon)
2099*/2088*/
2100static AstNode *ast_parse_fn_decl(ParseContext *pc, int token_index, int *new_token_index) {2089static AstNode *ast_parse_fn_decl(ParseContext *pc, int *token_index) {
2101 AstNode *fn_proto = ast_parse_fn_proto(pc, &token_index, true);2090 AstNode *fn_proto = ast_parse_fn_proto(pc, token_index, true);
2102 AstNode *node = ast_create_node_with_node(pc, NodeTypeFnDecl, fn_proto);2091 AstNode *node = ast_create_node_with_node(pc, NodeTypeFnDecl, fn_proto);
21032092
2104 node->data.fn_decl.fn_proto = fn_proto;2093 node->data.fn_decl.fn_proto = fn_proto;
21052094
2106 Token *semicolon = &pc->tokens->at(token_index);2095 Token *semicolon = &pc->tokens->at(*token_index);
2107 token_index += 1;2096 *token_index += 1;
2108 ast_expect_token(pc, semicolon, TokenIdSemicolon);2097 ast_expect_token(pc, semicolon, TokenIdSemicolon);
21092098
2110 *new_token_index = token_index;
2111 return node;2099 return node;
2112}2100}
21132101
...@@ -2152,7 +2140,7 @@ static AstNode *ast_parse_extern_block(ParseContext *pc, int *token_index, bool...@@ -2152,7 +2140,7 @@ static AstNode *ast_parse_extern_block(ParseContext *pc, int *token_index, bool
2152 *token_index += 1;2140 *token_index += 1;
2153 return node;2141 return node;
2154 } else {2142 } else {
2155 AstNode *child = ast_parse_fn_decl(pc, *token_index, token_index);2143 AstNode *child = ast_parse_fn_decl(pc, token_index);
2156 node->data.extern_block.fn_decls.append(child);2144 node->data.extern_block.fn_decls.append(child);
2157 }2145 }
2158 }2146 }
...@@ -2256,7 +2244,7 @@ static AstNode *ast_parse_struct_decl(ParseContext *pc, int *token_index) {...@@ -2256,7 +2244,7 @@ static AstNode *ast_parse_struct_decl(ParseContext *pc, int *token_index) {
22562244
2257 ast_eat_token(pc, token_index, TokenIdColon);2245 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
2261 ast_eat_token(pc, token_index, TokenIdComma);2249 ast_eat_token(pc, token_index, TokenIdComma);
22622250