tree-sitter-python
tree-sitter-python copied to clipboard
Difference in fields between parameters
If I have this code:
def func(first, second: str, third = 7, four: int = 1):
print()
I got the following abstract tree:
module [0, 0] - [2, 0]
function_definition [0, 0] - [1, 11]
name: identifier [0, 4] - [0, 8]
parameters: parameters [0, 8] - [0, 54]
identifier [0, 9] - [0, 14]
typed_parameter [0, 16] - [0, 27]
identifier [0, 16] - [0, 22]
type: type [0, 24] - [0, 27]
identifier [0, 24] - [0, 27]
default_parameter [0, 29] - [0, 38]
name: identifier [0, 29] - [0, 34]
value: integer [0, 37] - [0, 38]
typed_default_parameter [0, 40] - [0, 53]
name: identifier [0, 40] - [0, 44]
type: type [0, 46] - [0, 49]
identifier [0, 46] - [0, 49]
value: integer [0, 52] - [0, 53]
body: block [1, 4] - [1, 11]
expression_statement [1, 4] - [1, 11]
call [1, 4] - [1, 11]
function: identifier [1, 4] - [1, 9]
arguments: argument_list [1, 9] - [1, 11]
My question is: Why is the first parameter and typed_parameter
does not have a name
field like default_parameter
and typed_default_parameter
.
Or in the other way, why default_parameter
and typed_default_parameter
need a name
field ?
Is there any reason to this design ?
Maybe it's possible to homogenize all this.
My guess is typed_parameter can have list/dict splat patterns, hence it's not always a name, though I'm not against adding a name field
Using identifier
is very confusing while doing program logic.
It would be better to parse it like this
parameters: parameters [0, 8] - [0, 54]
parameter: [0, 9] - [0, 14]
name: identifier [0, 9] - [0, 14]