tree-sitter / tree-sitter-julia

Julia grammar for Tree-sitter
MIT License
93 stars 32 forks source link

Error parsing property destructuring and implicit names from identifiers (leading semicolon `;`) #65

Closed wangl-cc closed 1 year ago

wangl-cc commented 1 year ago

I'm using tree-sitter with nvim, and I found there are some error when parse code with semicolon.

Construction:

nt = (; a, b) # equivalent to nt = (a = a, b = b)

Playground:

identifier [0, 0] - [0, 2]
ERROR [0, 3] - [1, 11]
  ERROR [0, 5] - [1, 3]
    parameter_list [0, 5] - [0, 13]
      keyword_parameters [0, 6] - [0, 12]
        identifier [0, 8] - [0, 9]
        identifier [0, 11] - [0, 12]

Destruction:

(;a, b) = nt  # equivalent to a = getproperty(nt, :a); b = getproperty(nt, :b)

Playground:

ERROR [0, 0] - [0, 9]
  parameter_list [0, 0] - [0, 7]
    keyword_parameters [0, 1] - [0, 6]
      identifier [0, 2] - [0, 3]
      identifier [0, 5] - [0, 6]
identifier [0, 10] - [0, 12]

Keyword arguments:

foo(; a, b) # equivalent to foo(; a = a, b = b)

Playground

call_expression [0, 0] - [0, 11]
  identifier [0, 0] - [0, 3]
  argument_list [0, 3] - [0, 11]
    ERROR [0, 4] - [0, 10]
      ERROR [0, 6] - [0, 8]
        identifier [0, 6] - [0, 7]
      identifier [0, 9] - [0, 10]
savq commented 1 year ago

Relevant docs: https://docs.julialang.org/en/v1/manual/functions/#Property-destructuring

wangl-cc commented 1 year ago

Docs for implicit names from identifiers in NamedTuple and keyword arguments: https://docs.julialang.org/en/v1/base/base/#Core.NamedTuple, https://docs.julialang.org/en/v1/manual/functions/#Keyword-Arguments.

When a bare identifier or dot expression occurs after a semicolon, the keyword argument name is implied by the identifier or field name. For example plot(x, y; width) is equivalent to plot(x, y; width=width) and plot(x, y; options.width) is equivalent to plot(x, y; width=options.width).

wangl-cc commented 1 year ago

I have also found leading semicolon will break name tuple construction without implicit names from identifiers:

nt = (; a = 1, b = 2)

Playground:

identifier [0, 0] - [0, 2]
ERROR [0, 3] - [0, 21]
  parameter_list [0, 5] - [0, 21]
    keyword_parameters [0, 6] - [0, 20]
      optional_parameter [0, 8] - [0, 13]
        identifier [0, 8] - [0, 9]
        integer_literal [0, 12] - [0, 13]
      optional_parameter [0, 15] - [0, 20]
        identifier [0, 15] - [0, 16]
        integer_literal [0, 19] - [0, 20]