-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes annotation, sort of #143
Conversation
In Scala 3 this was relaxed to allow zero, one or more parameter lists based on whether the parameter list has type ascriptions (so |
oo interesting. Thanks for the hint. Considering:
I guess we should think of |
Problem ------- 1. class constructor should accept annotation, but it doesn't. 2. Apparently Scala 3 does allow zero, one, or multiple parameter list to an annotation, and differentiated between the class parameter by its lack of type ascription. Solution -------- 1. Add optional annotation for constructor. At this point, we can at least parse the code with annotation on the ctor. 2. Unfortunatley, however, tree-sitter doesn't make enough lookahead to distinguish postfix expr or ascription expression `(x: Int)`, for now the class parameters will end up being parsed as arguments to the annotation.
d19de26
to
0b7fd00
Compare
Just to make sure we're on the same page
I sort of actually see this as a regression in the class parameter parsing this. Do you feel like this is an improvement enough in handling annotations to move forward, or no? I don't have strong feelings on it, and honestly I never even knew you could have multiple parameter groups in annotations (mainly because I've just never used it). |
The status quo on master is that a legal constructor with an attribute fails to parse, and its methods would leak out: $ cat examples/C.scala
// TODO: The last argument should become class_parameters
class A @Inject()(x: Int, y: Int) {
def x = 1
}
$ tree-sitter parse examples/C.scala
(compilation_unit [0, 0] - [5, 0]
(comment [0, 0] - [0, 57])
(class_definition [1, 0] - [1, 7]
name: (identifier [1, 6] - [1, 7]))
(function_definition [1, 8] - [2, 11]
(annotation [1, 8] - [1, 33]
name: (type_identifier [1, 9] - [1, 15])
arguments: (arguments [1, 15] - [1, 17])
arguments: (arguments [1, 17] - [1, 33]
(ascription_expression [1, 18] - [1, 24]
(identifier [1, 18] - [1, 19])
(type_identifier [1, 21] - [1, 24]))
(ascription_expression [1, 26] - [1, 32]
(identifier [1, 26] - [1, 27])
(type_identifier [1, 29] - [1, 32]))))
(ERROR [1, 34] - [2, 2])
name: (identifier [2, 6] - [2, 7])
body: (integer_literal [2, 10] - [2, 11]))
(ERROR [3, 0] - [5, 0]))
examples/C.scala 0 ms (ERROR [1, 34] - [2, 2]) With this PR, it does the same $ tree-sitter parse examples/C.scala
(compilation_unit [0, 0] - [5, 0]
(comment [0, 0] - [0, 57])
(class_definition [1, 0] - [3, 1]
name: (identifier [1, 6] - [1, 7])
(annotation [1, 8] - [1, 33]
name: (type_identifier [1, 9] - [1, 15])
arguments: (arguments [1, 15] - [1, 17])
arguments: (arguments [1, 17] - [1, 33]
(ascription_expression [1, 18] - [1, 24]
(identifier [1, 18] - [1, 19])
(type_identifier [1, 21] - [1, 24]))
(ascription_expression [1, 26] - [1, 32]
(identifier [1, 26] - [1, 27])
(type_identifier [1, 29] - [1, 32]))))
body: (template_body [1, 34] - [3, 1]
(function_definition [2, 2] - [2, 11]
name: (identifier [2, 6] - [2, 7])
body: (integer_literal [2, 10] - [2, 11]))))) |
Ah, agree. Thanks for clarifying. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
I think behaving like scala 2 for now would give you the most amount of code parsed correctly considering no one knows the new scala 3 behavior exists |
Yea. I thought about throwing Scala 3 under the bus as well, but it's a fairly common thing to have parameter-less attribute, so we'd still have issues misinterpreting attributes unless we make a special case for constructor attributes only? |
Assuming that constructor attribute in general is fairly uncommon, I think this is ok for now. |
I think people usually annotate constructors with java annotations where you always have exactly one parameter list. |
Fixes #41
Problem
to an annotation, and differentiated between the class parameter by
its lack of type ascription.
Solution
least parse the code with annotation on the ctor.
distinguish postfix expr or ascription expression
(x: Int)
,for now the class parameters will end up being parsed as arguments to
the annotation.