tree-sitter-c icon indicating copy to clipboard operation
tree-sitter-c copied to clipboard

ERROR when putting "else { }" block below "#else"

Open botbotty opened this issue 3 years ago • 1 comments

Versions:

[email protected]
[email protected]

Code to reproduce this issue:

#include <stdio.h>
int main()
{
	int i;

	scanf("%d", &i);
	if (i > 10) {
		printf("big\n");
	}
#ifdef ONE_MORE_CHANCE
	else {
		printf("small\n");
	}
#endif

	return 0;
}

What tree-sitter parse gives:

(translation_unit [0, 0] - [17, 0]
  (preproc_include [0, 0] - [1, 0]
    path: (system_lib_string [0, 9] - [0, 18]))
  (function_definition [1, 0] - [16, 1]
    type: (primitive_type [1, 0] - [1, 3])
    declarator: (function_declarator [1, 4] - [1, 10]
      declarator: (identifier [1, 4] - [1, 8])
      parameters: (parameter_list [1, 8] - [1, 10]))
    body: (compound_statement [2, 0] - [16, 1]
      (declaration [3, 1] - [3, 7]
        type: (primitive_type [3, 1] - [3, 4])
        declarator: (identifier [3, 5] - [3, 6]))
      (expression_statement [5, 1] - [5, 17]
        (call_expression [5, 1] - [5, 16]
          function: (identifier [5, 1] - [5, 6])
          arguments: (argument_list [5, 6] - [5, 16]
            (string_literal [5, 7] - [5, 11])
            (pointer_expression [5, 13] - [5, 15]
              argument: (identifier [5, 14] - [5, 15])))))
      (if_statement [6, 1] - [8, 2]
        condition: (parenthesized_expression [6, 4] - [6, 12]
          (binary_expression [6, 5] - [6, 11]
            left: (identifier [6, 5] - [6, 6])
            right: (number_literal [6, 9] - [6, 11])))
        consequence: (compound_statement [6, 13] - [8, 2]
          (expression_statement [7, 2] - [7, 18]
            (call_expression [7, 2] - [7, 17]
              function: (identifier [7, 2] - [7, 8])
              arguments: (argument_list [7, 8] - [7, 17]
                (string_literal [7, 9] - [7, 16]
                  (escape_sequence [7, 13] - [7, 15])))))))
      (preproc_ifdef [9, 0] - [13, 6]
        name: (identifier [9, 7] - [9, 22])
        (ERROR [10, 1] - [10, 5] <----------------------------------- ERROR HERE
          (identifier [10, 1] - [10, 5]))
        (compound_statement [10, 6] - [12, 2]
          (expression_statement [11, 2] - [11, 20]
            (call_expression [11, 2] - [11, 19]
              function: (identifier [11, 2] - [11, 8])
              arguments: (argument_list [11, 8] - [11, 19]
                (string_literal [11, 9] - [11, 18]
                  (escape_sequence [11, 15] - [11, 17])))))))
      (return_statement [15, 1] - [15, 10]
        (number_literal [15, 8] - [15, 9])))))
/tmp/test.c     0 ms    (ERROR [10, 1] - [10, 5])

botbotty avatar May 26 '21 16:05 botbotty

Just ran into this with some code that looks like:

	if (argc > 1 && strcmp(argv[1], "--check") == 0)
		BootstrapModeMain(argc, argv, true);
	else if (argc > 1 && strcmp(argv[1], "--boot") == 0)
		BootstrapModeMain(argc, argv, false);
#ifdef EXEC_BACKEND
	else if (argc > 1 && strncmp(argv[1], "--fork", 6) == 0)
		SubPostmasterMain(argc, argv);
#endif
	else if (argc > 1 && strcmp(argv[1], "--describe-config") == 0)
		GucInfoMain();
	else if (argc > 1 && strcmp(argv[1], "--single") == 0)
		PostgresSingleUserMain(argc, argv,
							   strdup(get_user_name_or_exit(progname)));
	else if (argc > 1 && strcmp(argv[1], "--wal-redo") == 0)
		CallExtMain("neon_walredo", "WalRedoMain", argc, argv, false);
	else if (argc > 1 && strcmp(argv[1], "--sync-safekeepers") == 0)
		CallExtMain("neon", "WalProposerSync", argc, argv, true);
	else
		PostmasterMain(argc, argv);

tristan957 avatar Mar 05 '24 00:03 tristan957