tree-sitter/tree-sitter-c-sharp

Parser fails for dotnet script

intrasight opened this issue · 7 comments

I had opened the issue in the csharp-mode repo but they pointed me over here.

In short, the parser fails if there is no function wrapping the statements - as is typically the case with a script.

Details here: emacs-csharp/csharp-mode#254

I just tried the sample you posted:

foreach (XElement xe in gamapi.XPathSelectElements("//dimension")) {
    id = (string)xe.Attribute("name");
    xpath = $"raw/*/group/*[@name='{id}']";
    if (false) {
        xpath = $"raw/*/group/*[@sn='{sn}']";
    }
}

And it parses correctly as:

(compilation_unit
  (global_statement
    (for_each_statement (identifier) (identifier)
        (invocation_expression
          (member_access_expression (identifier) (identifier))
          (argument_list (argument (string_literal))))
     (block (expression_statement (assignment_expression (identifier) (assignment_operator) (cast_expression (predefined_type) (invocation_expression (member_access_expression (identifier) (identifier)) (argument_list (argument (string_literal))))))) (expression_statement (assignment_expression (identifier) (assignment_operator) (interpolated_string_expression (interpolated_string_text) (interpolation (identifier)) (interpolated_string_text)))) (if_statement (boolean_literal) (block (expression_statement (assignment_expression (identifier) (assignment_operator) (interpolated_string_expression (interpolated_string_text) (interpolation (identifier)) (interpolated_string_text))))))))))

What version of tree-sitter-c-sharp are they using? It might be an old one - they should update to latest.

We do, and have, supported top-level statements for quite some time - even before C# 9 supported them.

Just looked at their repo - while they use tree-sitter they do not use the tree-sitter-c-sharp grammar - they have their own as far as I can tell https://github.com/emacs-csharp/csharp-mode/

csharp-mode uses this grammar, but it uses an external library to leverage it. That repo is pinned to 70fd2cb, which is from march. It should absolutely be updated, and is an issue with emacs-tree-sitter, not csharp-mode or tree-sitter-c-sharp :)

We definitely supported top-level statements in C#9/Roslyn style back in the 0.19 release #115 so not sure why it isn't working over there.

I'm not sure either. Found some time to test it now, and I get this
This is not an issue, as far as I can tell.

Seems to fontify properly as well :)

compilation_unit:
  global_statement:
    for_each_statement:
      identifier:
      identifier:
      invocation_expression:
        member_access_expression:
          identifier:
          identifier:
        argument_list:
          argument:
            string_literal:
      block:
        expression_statement:
          assignment_expression:
            identifier:
            assignment_operator:
            cast_expression:
              predefined_type:
              invocation_expression:
                member_access_expression:
                  identifier:
                  identifier:
                argument_list:
                  argument:
                    string_literal:
        expression_statement:
          assignment_expression:
            identifier:
            assignment_operator:
            interpolated_string_expression:
              interpolated_string_text:
              interpolation:
                identifier:
              interpolated_string_text:
        if_statement:
          boolean_literal:
          block:
            expression_statement:
              assignment_expression:
                identifier:
                assignment_operator:
                interpolated_string_expression:
                  interpolated_string_text:
                  interpolation:
                    identifier:
                  interpolated_string_text:

I did a clean install of emacs 27.2, and have in my init:

(use-package tree-sitter :ensure t)
(use-package tree-sitter-langs :ensure t)
(use-package tree-sitter-indent :ensure t)

(use-package csharp-mode
    :ensure t
    :config
    (add-to-list 'auto-mode-alist '("\\.cs\\'" . csharp-tree-sitter-mode))
    (add-to-list 'auto-mode-alist '("\\.csx\\'" . csharp-tree-sitter-mode))
)

And I'm getting a good parse tree and highlighting now. So the issue was not being up to date. And refreshing packages hadn't fixed it because I had originally installed tree-sitter manually.

Thank you for your help.

That's good! Glad it was so simple :)