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

bug: operator < is parsed as begnning of template argument list. #269

Closed
1 of 2 tasks
scturtle opened this issue May 30, 2024 · 2 comments
Closed
1 of 2 tasks

bug: operator < is parsed as begnning of template argument list. #269

scturtle opened this issue May 30, 2024 · 2 comments
Labels

Comments

@scturtle
Copy link

scturtle commented May 30, 2024

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-cpp

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

tree-sitter 0.22.6

Describe the bug

Operator < is parsed as beginning of template argument list.

Steps To Reproduce/Bad Parse Tree

(translation_unit
 (function_definition type: (primitive_type)
  declarator: 
   (function_declarator declarator: (identifier)
    parameters: (parameter_list ( )))
  body: 
   (compound_statement {
    (declaration
     type: 
      (struct_specifier struct name: (type_identifier)
       body: 
        (field_declaration_list {
         (field_declaration type: (primitive_type) declarator: (field_identifier) ;)
         }))
     declarator: (identifier) ;)
    (declaration type: (primitive_type)
     declarator: 
      (init_declarator declarator: (identifier) =
       value: 
        (field_expression argument: (identifier) operator: .
         field: 
          (template_method name: (field_identifier)
           arguments: (template_argument_list < (number_literal) >))))
     ;)
    })))

Expected Behavior/Parse Tree

(translation_unit
 (function_definition type: (primitive_type)
  declarator: 
   (function_declarator declarator: (identifier)
    parameters: (parameter_list ( )))
  body: 
   (compound_statement {
    (declaration
     type: 
      (struct_specifier struct name: (type_identifier)
       body: 
        (field_declaration_list {
         (field_declaration type: (primitive_type) declarator: (field_identifier) ;)
         }))
     declarator: (identifier) ;)
    (declaration type: (primitive_type)
     declarator: 
      (init_declarator declarator: (identifier) =
       value: 
        (binary_expression
         left: (field_expression argument: (identifier) operator: . field: (field_identifier))
         operator: < right: (number_literal)))
     ;)
    })))

Repro

int main() {
  struct A {int b;} a;
  bool t = a.b < 3;
}
@scturtle scturtle added the bug label May 30, 2024
@touzeauv
Copy link

touzeauv commented Jun 6, 2024

You ran into the same problem that is described in issue #231.
I suggested a fixed (see pull request #267) to solve it.
My understanding is that when the parser has read "bool t = a.b < 3", it is in state where both interpretations (as a template or a comparison) are possible, but he gives priority to the template version and discard the other.

The change I introduced lead the parser to fork and consider both interpretations until one of them leads to an error and is then discarded. In case both could be valid, the comparison version is preferred.

Here is the tree I obtained:

(translation_unit
  (function_definition
    type: (primitive_type)
    declarator: (function_declarator
      declarator: (identifier)
      parameters: (parameter_list))
    body: (compound_statement
      (declaration
        type: (struct_specifier
          name: (type_identifier)
          body: (field_declaration_list
            (field_declaration
              type: (primitive_type)
              declarator: (field_identifier))))
        declarator: (identifier))
      (declaration
        type: (primitive_type)
        declarator: (init_declarator
          declarator: (identifier)
          value: (binary_expression
            left: (field_expression
              argument: (identifier)
              field: (field_identifier))
            right: (number_literal)))))))

@jdrouhard
Copy link
Collaborator

Fixed by #267

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

No branches or pull requests

3 participants