tree-sitter-c
tree-sitter-c copied to clipboard
Invalid AST for primitive type
When run on a C standard header which defines size_t
:
typedef unsigned long size_t;
Output is:
translation_unit [0, 0] - [1, 0])
type_definition [0, 0] - [0, 29])
type: sized_type_specifier [0, 8] - [0, 28])
type: primitive_type [0, 22] - [0, 28])
declarator: MISSING type_identifier [0, 28] - [0, 28])
Even if you remove the unsigned:
typedef long size_t;
Output is:
translation_unit [0, 0] - [1, 0])
type_definition [0, 0] - [0, 20])
type: sized_type_specifier [0, 8] - [0, 19])
type: primitive_type [0, 13] - [0, 19])
declarator: MISSING type_identifier [0, 19] - [0, 19])
Changing size_t
to non-primitive asize_t
and it works fine:
typedef unsigned long asize_t;
Output is:
translation_unit [0, 0] - [1, 0])
type_definition [0, 0] - [0, 30])
type: sized_type_specifier [0, 8] - [0, 21])
declarator: type_identifier [0, 22] - [0, 29])
Seems to be caused by this: https://github.com/tree-sitter/tree-sitter-c/blob/99151b1e9293c9e025498fee7e6691e1a52e1d03/grammar.js#L463
Which is plain wrong, because there should be some distinction between built-in types and aliases (like ptrdiff_t
or size_t
).
primitive_type: $ => token(choice(
- 'bool',
+ '_Bool',
+ '_Complex',
+ '_Imaginary',
'char',
'int',
'float',
'double',
'void',
- 'size_t',
- 'ssize_t',
- 'intptr_t',
- 'uintptr_t',
- 'charptr_t',
- ...[8, 16, 32, 64].map(n => `int${n}_t`),
- ...[8, 16, 32, 64].map(n => `uint${n}_t`),
- ...[8, 16, 32, 64].map(n => `char${n}_t`)
)),
This does fix the issue, but there’s a bit of fallout regarding highlighting, in that the removed typedefs are standardized (either in the language standard or POSIX) while not being built-in types, so not sure how to deal with that.