Skip to content

bug: cannot parse more than one attribute item for function's parameter #276

Open
@uriyay-jfrog

Description

@uriyay-jfrog

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 of tree-sitter-rust

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

No response

Describe the bug

Parsing more than one attribute item of a parameter will produce an error.
I think that the problem is in grammar.js:

parameters: $ => seq(
      '(',
      sepBy(',', seq(
        optional($.attribute_item),
        choice(
          $.parameter,
          $.self_parameter,
          $.variadic_parameter,
          '_',
          $._type,
        ))),
      optional(','),
      ')',
    ),

instead of optional($.attribute_item) there should be:
optional(seq($.attribute_item))

Steps To Reproduce/Bad Parse Tree

The following code:

fn func(
    #[allow(unused)]
    #[allow(non_snake_case)]
    param: i32
) -> i32 {
    return 10;
}

Produces this parse tree:

source_file [0, 0] - [8, 0]
  function_item [0, 0] - [6, 1]
    name: identifier [0, 3] - [0, 7]
    parameters: parameters [0, 7] - [4, 1]
      ERROR [1, 4] - [1, 20]
        attribute_item [1, 4] - [1, 20]
          attribute [1, 6] - [1, 19]
            identifier [1, 6] - [1, 11]
            arguments: token_tree [1, 11] - [1, 19]
              identifier [1, 12] - [1, 18]
      attribute_item [2, 4] - [2, 28]
        attribute [2, 6] - [2, 27]
          identifier [2, 6] - [2, 11]
          arguments: token_tree [2, 11] - [2, 27]
            identifier [2, 12] - [2, 26]
      parameter [3, 4] - [3, 14]
        pattern: identifier [3, 4] - [3, 9]
        type: primitive_type [3, 11] - [3, 14]
    return_type: primitive_type [4, 5] - [4, 8]
    body: block [4, 9] - [6, 1]
      expression_statement [5, 4] - [5, 14]
        return_expression [5, 4] - [5, 13]
          integer_literal [5, 11] - [5, 13]

Expected Behavior/Parse Tree

Both of the attributes should be parsed correctly

Repro

fn func(
    #[allow(unused)]
    #[allow(non_snake_case)]
    param: i32
) -> i32 {
    return 10;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions