vkazanov / tree-sitter-quakec

A Tree-sitter grammar for QuakeC
MIT License
3 stars 0 forks source link

Function body parsed as array literal #2

Open krizej opened 2 months ago

krizej commented 2 months ago

notice the missing semicolon:

void() func =
{
    FunctionCall()
};

parse result:

(source_file [0, 0] - [3, 2]
  (function_definition [0, 0] - [3, 2]
    result: (simple_type [0, 0] - [0, 4])
    parameters: (parameter_list [0, 4] - [0, 6])
    name: (identifier [0, 7] - [0, 11])
    body: (array_literal [1, 0] - [3, 1]
      (funcall_expression [2, 4] - [2, 18]
        function: (identifier [2, 4] - [2, 16])))))

the fix is simple:

function_definition: $ => seq(
...
    choice(
        $.builtin_literal,
        field('body', $._statement) // replace _statement with compound_statement
    ),
    optional(';')
),

and then you even get an error about the missing semicolon

(source_file [0, 0] - [3, 2]
  (function_definition [0, 0] - [3, 2]
    result: (simple_type [0, 0] - [0, 4])
    parameters: (parameter_list [0, 4] - [0, 6])
    name: (identifier [0, 7] - [0, 11])
    body: (compound_statement [1, 0] - [3, 1]
      (funcall_expression [2, 4] - [2, 18]
        function: (identifier [2, 4] - [2, 16])))))
test.txt           0.06 ms         579 bytes/ms (MISSING ";" [2, 18] - [2, 18])

however, when the name is too short (below 8 characters) the MISSING ";" error does not appear

void() func =
{
    FunCall()
};
(source_file [0, 0] - [3, 2]
  (function_definition [0, 0] - [3, 2]
    result: (simple_type [0, 0] - [0, 4])
    parameters: (parameter_list [0, 4] - [0, 6])
    name: (identifier [0, 7] - [0, 11])
    body: (compound_statement [1, 0] - [3, 1]
      (ERROR [2, 4] - [2, 13]
        (funcall_expression [2, 4] - [2, 13]
          function: (identifier [2, 4] - [2, 11]))))))
test.txt           0.07 ms         448 bytes/ms (ERROR [2, 4] - [2, 13])

i would make a PR for this as it's simple enough but i have no clue what to do about that second error :/