slackhq / tree-sitter-hack

Hack grammar for tree-sitter
MIT License
33 stars 16 forks source link

Treat include and require as keywords (#64) #65

Closed andryak closed 1 year ago

andryak commented 1 year ago

Summary

This diff addresses the issue described in https://github.com/slackhq/tree-sitter-hack/issues/64

The parser does not treat include, include_once, require and require_once as keywords, and thus gets confused when these are used as the name of a function called on a pipe expression.

For instance, the following code:

new Foo() |> $$->include();
new Foo() |> $$->include_once();
new Foo() |> $$->require();
new Foo() |> $$->require_once();

Is incorrectly parsed as follows:

(ERROR
      (new_expression
        (qualified_identifier
          (identifier))
        (arguments))
      (pipe_variable)
      (ERROR
        (parameters))
      (new_expression
        (qualified_identifier
          (identifier))
        (arguments))
      (pipe_variable)
      (ERROR
        (parameters))
      (new_expression
        (qualified_identifier
          (identifier))
        (arguments))
      (pipe_variable)
      (ERROR
        (parameters))
      (new_expression
        (qualified_identifier
          (identifier))
        (arguments))
      (pipe_variable)
      (parameters))

After the changes in this PR, it's parsed correctly:

(script
  (expression_statement
    (binary_expression
      (new_expression
        (qualified_identifier
          (identifier))
        (arguments))
      (call_expression
        (selection_expression
          (pipe_variable)
          (identifier))
        (arguments))))
  (expression_statement
    (binary_expression
      (new_expression
        (qualified_identifier
          (identifier))
        (arguments))
      (call_expression
        (selection_expression
          (pipe_variable)
          (identifier))
        (arguments))))
  (expression_statement
    (binary_expression
      (new_expression
        (qualified_identifier
          (identifier))
        (arguments))
      (call_expression
        (selection_expression
          (pipe_variable)
          (identifier))
        (arguments))))
  (expression_statement
    (binary_expression
      (new_expression
        (qualified_identifier
          (identifier))
        (arguments))
      (call_expression
        (selection_expression
          (pipe_variable)
          (identifier))
        (arguments)))))

Requirements