tree-sitter / tree-sitter-c

C grammar for tree-sitter
MIT License
251 stars 108 forks source link

Incorrect AST for typedef void * #14

Closed genotrance closed 6 years ago

genotrance commented 6 years ago
typedef void * Bus;

Results in:

     (type_definition 2856 2890
      (primitive_type 2864 2868)
      (pointer_declarator 2869 2889
       (type_identifier 2871 2889)
      )
     )

Instead of grouping void * together, tree-sitter groups * Bus together instead.

I would have expected:

     (type_definition 2856 2890
      (pointer_declarator 2869 2889
       (primitive_type 2864 2868)
      (type_identifier 2871 2889)
      )
     )
genotrance commented 6 years ago

Seems this way for function declarations too:

const char * Soloud_getErrorString(Soloud * aSoloud, int aErrorCode);
     (declaration 4156 4225
      (type_qualifier 4156 4161)
      (primitive_type 4162 4166)
      (pointer_declarator 4167 4224
       (function_declarator 4169 4224
        (identifier 4169 4190)
        (parameter_list 4190 4224
         (parameter_declaration 4191 4207
          (type_identifier 4191 4197)
          (pointer_declarator 4198 4207
           (identifier 4200 4207)
          )
         )
         (parameter_declaration 4209 4223
          (primitive_type 4209 4212)
          (identifier 4213 4223)
         )
        )
       )
      )
     )

Just want to confirm the rationale for such a structure before I look into utilizing it.

maxbrunsfeld commented 6 years ago

Yeah, that's the correct structure for a C AST. Conceptually, the * operator is attached to the variable being declared, it's not attached to the type. This is somewhat unique to the C family of languages.

Consider these examples:

int *a, *b;        // a and b are both pointers
int (*a), (*b);    // same as above

typedef void *A;   // A is a void pointer
typedef void (*A); // same as above
maxbrunsfeld commented 6 years ago

One good overview of how these "declarators" work in C is here: https://msdn.microsoft.com/en-us/library/tb971bed.aspx.

genotrance commented 6 years ago

Thank you for clarifying. I'll proceed accordingly.