elixir-lang / tree-sitter-elixir

Elixir grammar for tree-sitter
https://elixir-lang.org/tree-sitter-elixir
Apache License 2.0
245 stars 24 forks source link

"when" is treated as an operator even without a space after #53

Closed yaglo closed 1 year ago

yaglo commented 1 year ago

when doesn't require a space after it but it should.

Example:

def func(arg) whenis_number(arg)

tree-sitter-elixir parses it without issues (as you can see above):

source [0, 0] - [1, 0]
  call [0, 0] - [0, 32]
    target: identifier [0, 0] - [0, 3]
    arguments [0, 4] - [0, 32]
      binary_operator [0, 4] - [0, 32]
        left: call [0, 4] - [0, 13]
          target: identifier [0, 4] - [0, 8]
          arguments [0, 8] - [0, 13]
            identifier [0, 9] - [0, 12]
        right: call [0, 18] - [0, 32]
          target: identifier [0, 18] - [0, 27]
          arguments [0, 27] - [0, 32]
            identifier [0, 28] - [0, 31]

Elixir's parser doesn't consider this valid:

** (SyntaxError) iex:18:15: syntax error before: whenis_number
    |
 18 | def func(arg) whenis_number(arg)
    |               ^
    (iex 1.14.5) lib/iex/evaluator.ex:292: IEx.Evaluator.parse_eval_inspect/3
    (iex 1.14.5) lib/iex/evaluator.ex:187: IEx.Evaluator.loop/1
    (iex 1.14.5) lib/iex/evaluator.ex:32: IEx.Evaluator.init/4
    (stdlib 4.3.1) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
jonatanklosko commented 1 year ago

Hey @yaglo, this is essentially the same case as this:

1 or1
1or1
1or 1
1 or 1

All of the above are parsed without error (but only the last one is valid). I discussed this upstream in the past (https://github.com/tree-sitter/tree-sitter/issues/1404) and the conclusion was that this behaviour was kinda expected given all the context.

Generally false positives in edge cases like these are not critical. It is important to correctly parse all valid syntax, but it's not really the goal to strictly reject all invalid syntax :)