tree-sitter-php icon indicating copy to clipboard operation
tree-sitter-php copied to clipboard

function names ending with the word `_function` is not parsed correctly

Open etu opened this issue 1 year ago • 4 comments

PHP code:

<?php

function foobar_function(): void
{
    echo 'foobar';
    echo "foobar";
}

foobar_function();

Debug data:

program:
  php_tag:
  function_definition:
    name:
    formal_parameters:
    union_type:
      primitive_type:
    compound_statement:
      echo_statement:
        string:
          string_value:
      echo_statement:
        encapsed_string:
          string_value:
  expression_statement:
    function_call_expression:
      name:
      arguments:

image

It doesn't seem to be an issue for methods:

<?php

class Foobar {
    public function __construct()
    {
        $this->foobar_function();
    }

    public function foobar_function(): void
    {
        echo 'foobar';
        echo "foobar";
    }
}

Debug:

program:
  php_tag:
  class_declaration:
    name:
    declaration_list:
      method_declaration:
        visibility_modifier:
        name:
        formal_parameters:
        compound_statement:
          expression_statement:
            member_call_expression:
              variable_name:
                name:
              name:
              arguments:
      method_declaration:
        visibility_modifier:
        name:
        formal_parameters:
        union_type:
          primitive_type:
        compound_statement:
          echo_statement:
            string:
              string_value:
          echo_statement:
            encapsed_string:
              string_value:

I found this due to usage of this function: https://www.php.net/manual/en/function.register-shutdown-function.php

etu avatar Aug 24 '22 17:08 etu

And yet again I don't think there's an issue with the parser here... rather something else... Not sure where to look to for all of the three (#147, https://github.com/tree-sitter/tree-sitter-php/issues/122#issuecomment-1225996849 and this) different problems I've had so far...

etu avatar Aug 24 '22 17:08 etu

@etu Which editor is this? It may require an update of its syntax highlights queries. That's where the parsed tree is queried to map it to highlight styles. Here's the queries used by nvim-treesitter for example: https://github.com/nvim-treesitter/nvim-treesitter/blob/master/queries/php/highlights.scm

mikehaertl avatar Aug 24 '22 18:08 mikehaertl

@mikehaertl It's Emacs, living a bit on the edge though GNU Emacs 29.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.34, cairo version 1.16.0) running wayland native with pgtk and native-comp :slightly_smiling_face:

etu avatar Aug 24 '22 18:08 etu

@etu I don't know much about Emacs, but I assume it's using this package under the hood for treesitter integration:

https://github.com/emacs-tree-sitter/tree-sitter-langs

It seems that it's also using its own queries from this file:

https://github.com/emacs-tree-sitter/tree-sitter-langs/blob/master/queries/php/highlights.scm

You could play around with it and maybe also compare it with what they have at neovim. From a quick look it's probably the @string capture query:

Neovim:

[
 (string)
 (encapsed_string)
 (heredoc_body)
 (nowdoc_body)
 (shell_command_expression) ; backtick operator: `ls -la`
 ] @string

Emacs:

[
 (string)
 (heredoc)
 ] @string

UPDATE: I should have read the title. I only checked the different highlighting of the strings and thought that's your issue. I'm not sure about the _function thing. It might require a fix in the parser. Still the queries could be brought up-to-date too.

mikehaertl avatar Aug 25 '22 06:08 mikehaertl