Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable declaration with parenthesis mistaken for function call #107

Open
jrwrigh opened this issue Jun 22, 2022 · 0 comments
Open

Variable declaration with parenthesis mistaken for function call #107

jrwrigh opened this issue Jun 22, 2022 · 0 comments

Comments

@jrwrigh
Copy link

jrwrigh commented Jun 22, 2022

It is possible (and necessary) to declare a custom defined variable by enclosing the result in parenthesis. For example:

typedef double CeedScalar;
void example(CeedScalar in[]) {
  const CeedScalar (*q) = in;
  CeedScalar (*w) = in;
  CeedScalar (*v);
}

This results in the following syntax highlighting (in neovim):
image

Interestingly, the markdown parser also turns up the same result (copied below in case GH updates it):
image

And playground for the above example returns:

[translation_unit](https://tree-sitter.github.io/tree-sitter/playground#) [0, 0] - [6, 0]
  [type_definition](https://tree-sitter.github.io/tree-sitter/playground#) [0, 0] - [0, 26]
    type: [primitive_type](https://tree-sitter.github.io/tree-sitter/playground#) [0, 8] - [0, 14]
    declarator: [type_identifier](https://tree-sitter.github.io/tree-sitter/playground#) [0, 15] - [0, 25]
  [function_definition](https://tree-sitter.github.io/tree-sitter/playground#) [1, 0] - [5, 1]
    type: [primitive_type](https://tree-sitter.github.io/tree-sitter/playground#) [1, 0] - [1, 4]
    declarator: [function_declarator](https://tree-sitter.github.io/tree-sitter/playground#) [1, 5] - [1, 29]
      declarator: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [1, 5] - [1, 12]
      parameters: [parameter_list](https://tree-sitter.github.io/tree-sitter/playground#) [1, 12] - [1, 29]
        [parameter_declaration](https://tree-sitter.github.io/tree-sitter/playground#) [1, 13] - [1, 28]
          type: [type_identifier](https://tree-sitter.github.io/tree-sitter/playground#) [1, 13] - [1, 23]
          declarator: [array_declarator](https://tree-sitter.github.io/tree-sitter/playground#) [1, 24] - [1, 28]
            declarator: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [1, 24] - [1, 26]
    body: [compound_statement](https://tree-sitter.github.io/tree-sitter/playground#) [1, 30] - [5, 1]
      [declaration](https://tree-sitter.github.io/tree-sitter/playground#) [2, 2] - [2, 39]
        [type_qualifier](https://tree-sitter.github.io/tree-sitter/playground#) [2, 2] - [2, 7]
        type: [type_identifier](https://tree-sitter.github.io/tree-sitter/playground#) [2, 8] - [2, 18]
        declarator: [init_declarator](https://tree-sitter.github.io/tree-sitter/playground#) [2, 19] - [2, 38]
          declarator: [parenthesized_declarator](https://tree-sitter.github.io/tree-sitter/playground#) [2, 19] - [2, 23]
            [pointer_declarator](https://tree-sitter.github.io/tree-sitter/playground#) [2, 20] - [2, 22]
              declarator: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [2, 21] - [2, 22]
          value: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [2, 36] - [2, 38]
      [expression_statement](https://tree-sitter.github.io/tree-sitter/playground#) [3, 2] - [3, 39]
        [assignment_expression](https://tree-sitter.github.io/tree-sitter/playground#) [3, 2] - [3, 38]
          left: [call_expression](https://tree-sitter.github.io/tree-sitter/playground#) [3, 2] - [3, 17]
            function: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [3, 2] - [3, 12]
            arguments: [argument_list](https://tree-sitter.github.io/tree-sitter/playground#) [3, 13] - [3, 17]
              [pointer_expression](https://tree-sitter.github.io/tree-sitter/playground#) [3, 14] - [3, 16]
                argument: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [3, 15] - [3, 16]
          right: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [3, 36] - [3, 38]
      [expression_statement](https://tree-sitter.github.io/tree-sitter/playground#) [4, 2] - [4, 18]
        [call_expression](https://tree-sitter.github.io/tree-sitter/playground#) [4, 2] - [4, 17]
          function: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [4, 2] - [4, 12]
          arguments: [argument_list](https://tree-sitter.github.io/tree-sitter/playground#) [4, 13] - [4, 17]
            [pointer_expression](https://tree-sitter.github.io/tree-sitter/playground#) [4, 14] - [4, 16]
              argument: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [4, 15] - [4, 16]

where const CeedScalar (*q) = in; is treated as a declaration, while CeedScalar (*w) = in; and CeedScalar (*v); are treated as expression_statements.

Although the markdown syntax highlighter also treats the second CeedScalar as a function call, it really shouldn't be. The first bug (CeedScalar (*w) = in;) can be easily differentiated since a function call cannot be on the LHS of an expression statement. The second CeedScalar (*v); is probably a bit trickier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant