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

Incorrect precedence for indexing with comparison #155

Open
nbrahms opened this issue Jan 20, 2021 · 1 comment
Open

Incorrect precedence for indexing with comparison #155

nbrahms opened this issue Jan 20, 2021 · 1 comment

Comments

@nbrahms
Copy link
Contributor

nbrahms commented Jan 20, 2021

While investigating #146 , I discovered that the tree for a comparison of an indexed object appears incorrect.

Consider the following program:

x [0] == 1

tree-sitter-ruby will emit the following CST:

(program [0, 0] - [1, 0]
  (call [0, 0] - [0, 10]
    method: (identifier [0, 0] - [0, 1])
    arguments: (argument_list [0, 2] - [0, 10]
      (binary [0, 2] - [0, 10]
        left: (array [0, 2] - [0, 5]
          (integer [0, 3] - [0, 4]))
        right: (integer [0, 9] - [0, 10])))))

That is, this is interpreted as equivalent to:

x([0] == 1)

whereas Ruby evaluates this as:

(x[0]) == 1

This can be confirmed by running

x = [1, 2, 3]
z = x [0] == 1
puts z

which prints true.

I believe the correct CST should be:

(program [0, 0] - [1, 0]
  (binary [0, 0] - [0, 10]
    left: (element_reference [0, 0] - [0, 5]
        object: (identifier [0, 0] - [0, 1])
        (integer [0, 3] - [0, 4]))
    right: (integer [0, 9] - [0, 10])))
@nbrahms
Copy link
Contributor Author

nbrahms commented Jan 20, 2021

I did a little more investigation with irb, and I think my conclusion is that the tree-sitter CST would need to be context-sensitive in order to represent programs consistently with mruby:

irb(main):001:0> x = [0, 1, 2]
=> [0, 1, 2]
irb(main):002:0> x [0]
=> 0
irb(main):003:0> def y(z)
irb(main):004:1>   z
irb(main):005:1> end
=> :y
irb(main):006:0> y [0]
=> [0]
irb(main):007:0> x [0] == 0
=> true
irb(main):008:0> y [0] == 0
=> false
irb(main):009:0> y [0] == [0]
=> true

One suggestion here would be to completely collapse element referencing with method invocation.

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