tree-sitter / tree-sitter-go

Go grammar for tree-sitter
MIT License
310 stars 65 forks source link

var_spec includes the Comma in the "name" Field. #45

Closed berchn closed 2 years ago

berchn commented 3 years ago

To determine whether an unnamed node is or isn't interesting, I look at whether it is part of a field. In the case of var_spec, the comma is included in the "name" field, but it isn't interesting in the context of the node. The following is an example:

var a, b, c;

(:SOURCE-FILE ((0 0) (12 0))
 ((:VAR-DECLARATION ((0 0) (11 0))
   ((:VAR ((0 0) (3 0)) NIL)
    (:VAR-SPEC ((4 0) (11 0))
     (((:NAME :IDENTIFIER) ((4 0) (5 0)) NIL) ((:NAME :|,|) ((5 0) (6 0)) NIL)
      ((:NAME :IDENTIFIER) ((7 0) (8 0)) NIL) ((:NAME :|,|) ((8 0) (9 0)) NIL)
      ((:NAME :IDENTIFIER) ((10 0) (11 0)) NIL)
      ((:TYPE :TYPE-IDENTIFIER) ((11 0) (11 0)) NIL)))))
  (:|;| ((11 0) (12 0)) NIL)))
adonovan commented 2 years ago

I think the fix might be to change this:

https://github.com/tree-sitter/tree-sitter-go/blob/07d722831382a043b16547b6d9202f3da07f3cb3/grammar.js#L347

to this:

  field_declaration: $ => seq(
      choice(
        seq(
          field('name', $._field_identifier),
          repeat(seq(',', field('name', $._field_identifier))),
          field('type', $._type)
        ),

There's a similar problem in parameter_declaration here:

https://github.com/tree-sitter/tree-sitter-go/blob/07d722831382a043b16547b6d9202f3da07f3cb3/grammar.js#L235

which should be changed to:

    parameter_declaration: $ => seq(
      field('name', $._identifier),
      repeat(seq(',', field('name', $._identifier))),
      field('type', $._type)
    ),