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

Improve module, type and function definitions

Open savq opened this issue 2 years ago • 6 comments

Types and modules

  • Add bare module definitions.
  • Fix single line structs.
  • Split tests for structs and other types.

Functions

  • Add operators as names in function definitions.
  • Add zero method function definitions.
  • Add short form function definitions.
  • Add function signature rule (shared by block and short function definitions).
  • Add where clauses.
  • Refactor anonymous block functions (function() ... end). Consider them function definitions, not function expressions.
  • Split tests for function definitions into 4 sections.

Function parameters and scoped identifiers

  • Add anonymous typed parameters.
  • Add parameters to do_clause.
  • Rename spread_expression to slurp_expression (yes, that's the real name).
  • Allow _primary_expression and prefixed_string_literal as types in typed_parameter and return_type.
  • Allow scoped identifiers in:
    • function definitions
    • macro identifiers
    • type parameters

Misc

  • Split import rules into selected_import and _import_list. These cannot be used in the same import statement.
  • Remove 29 files from known-failures.

Caveats:

  • where is actually a binary operator. It can be used in places beyond function definitions. A new rule could be added in the future to handle more use cases.
  • I wanted to add a _parameter rule as suggested here, but this caused more conflicts than necessary.
  • I made the terminator after do_clause parameters mandatory.
  • The conflicts that were added are mostly due to the fact that assignment_expression still allows call_expression in the LHS.
  • The reason for calling ... a "slurp" comes from here: What does the ... operator do?

Once assignment patterns are fixed, these conflicts could be removed (and _parameter could finally be added), but I think that belongs in another PR.


Closes #26 Closes #41 Closes #42 Closes #36, but the implementation is different from #43 Closes #57

savq avatar Oct 08 '22 08:10 savq

Very nice, I will close https://github.com/tree-sitter/tree-sitter-julia/pull/43 in favor of getting this one through.

Could you also add support for empty functions, for example function f end. These are often used to add docstrings.

ChrHorn avatar Oct 10 '22 09:10 ChrHorn

This is awesome, thanks!

Just wanted to report a minor issue I found in which sometimes type assertions can be parsed as symbols, see this example image

ExpandingMan avatar Oct 11 '22 01:10 ExpandingMan

Another minor issue, the character in for loops is not being recognized (should be @keyword.operator to match in).

image

ExpandingMan avatar Oct 11 '22 01:10 ExpandingMan

Just wanted to report a minor issue I found in which sometimes type assertions can be parsed as symbols, see this example

@ExpandingMan The PR fixes this 👍

Another minor issue, the ∈ character in for loops is not being recognized (should be @keyword.operator to match in).

This can be fixed in either the grammar or the queries, and it's a bit unrelated to the rest of the PR. I'll add a fix for this in nvim-treesitter first.

savq avatar Oct 12 '22 02:10 savq

Ok. All the updated rules and their correspoding tests should be ready.

savq avatar Oct 12 '22 23:10 savq

Should this be merged, @maxbrunsfeld ? Seems like a significant improvement.

aviks avatar Oct 23 '22 19:10 aviks

Regarding slurp_expression, would splat_expression be more appropriate? I see ... described as the "splat operator" more than anything else. You'll also see it called the "splat operator" (but not the "slurp operator") in Python, Ruby, and other places.

tecosaur avatar Jan 16 '23 02:01 tecosaur

I wrote the wrong thing in the PR comment (I updated it). For future reference:

  • spread_parameter was renamed to slurp_parameter (here)
  • spread_expression was renamed to splat_expression (in #83)

That's the "correct" naming convention in Julia. See What does the ... operator do?

savq avatar Jan 16 '23 05:01 savq