/// <summary> /// The "token_list" is assumed to be f ( ... ). /// </summary> void parse_function_arguments(TokenList token_list) { int index = 2; // start of the current argument while (index < token_list.Count) { // look for comma int comma_index = token_list.find_token_outside_of_paren( TokenType.Comma, index, token_list.Count - 1); // Handle final argument case if (comma_index < 0) { // the rest of this token_list is treated as one argument int right_paren_index = token_list.find_token_outside_of_paren( TokenType.Right_Paren, index, token_list.Count - 1); if (right_paren_index < 0) { throw new Exception("Unable to locate the ending ')' in \"" + token_list + "\"."); } if (right_paren_index == index) { // The function might be f(), totally no argument at all. // In this case, the index = 2. if (index == 2) { return; } throw new Exception("Unable to parse the final argument for \"" + token_list + "\"."); } else { arguments.add_argument(token_list, index, right_paren_index - 1); index = right_paren_index + 1; } } // Handle empty argument error, as in f(1,2,,3) else if (comma_index == index) { throw new Exception("Empty argument detected inside \"" + token_list + "\"."); } // Handle the standard case else { arguments.add_argument(token_list, index, comma_index - 1); index = comma_index + 1; } } }