Skip to content

bug: field names in table constructors are impossible to highlight correctly #61

@digitcrusher

Description

@digitcrusher

Did you check existing issues?

  • I have read all the tree-sitter docs if it relates to using the parser
  • I have searched the existing issues

Tree-Sitter CLI Version, if relevant (output of tree-sitter --version)

tree-sitter 0.25.8 (f2f197b6b27ce75c280c20f131d4f71e906b86f7)

Describe the bug

Currently, it is impossible to distinguish field name identifiers and variables as field name expressions in table constructors and highlight both differently and correctly. This is due to there not being enough information in the parse tree, as demonstrated below.

One solution is to imitate the official C Tree-sitter grammar and make field identifiers a different node type, as I did in my earlier pull request (#60).

Steps To Reproduce/Bad Parse Tree

The parse tree for the repro below is as follows:

(chunk [0, 0] - [1, 0]
  (assignment_statement [0, 0] - [0, 39]
    (variable_list [0, 0] - [0, 1]
      name: (identifier [0, 0] - [0, 1]))
    (expression_list [0, 4] - [0, 39]
      value: (table_constructor [0, 4] - [0, 39]
        (field [0, 6] - [0, 13]
          name: (identifier [0, 6] - [0, 9])
          value: (number [0, 12] - [0, 13]))
        (field [0, 15] - [0, 24]
          name: (identifier [0, 16] - [0, 19])
          value: (number [0, 23] - [0, 24]))
        (field [0, 26] - [0, 37]
          name: (string [0, 27] - [0, 32]
            content: (string_content [0, 28] - [0, 31]))
          value: (number [0, 36] - [0, 37]))))))

Expected Behavior/Parse Tree

The tree should contain additional information, so that a highlights query can correctly capture field 1's name identifier as field and field 2's name expression as variable. This bug manifests itself in this grammar's highlights queries.

Repro

t = { foo = 1, [bar] = 1, ['baz'] = 1 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions