UserNobody14 / tree-sitter-dart

Attempt to make a tree-sitter grammar for dart
MIT License
62 stars 40 forks source link

Ambiguous matching #51

Closed wurikiji closed 1 year ago

wurikiji commented 1 year ago
@Hypen()
myFunction() {}

For above code,

(annotation) @annotation

should capture an annotation.

But, tree-sitter parses @Hypen as a (marker_annotation) and () as a record type definition.

Are there any solutions to capture this pattern?

dalugm commented 1 year ago

Same problems here

@TypedGoRoute<LoginRoute>(path: LoginRoute.path)
class LoginRoute extends GoRouteData {
  const LoginRoute({this.fromPage});

  final String? fromPage;

  @override
  Widget build(BuildContext context, GoRouterState state) =>
      LoginScreen(from: fromPage);
}

In the above example, @TypedGoRoute is parsed as marker_annotation, which is correct. But the rest is parsed as function_signature.

 (marker_annotation @ name: (identifier))
 (function_signature name: (identifier)
  (type_parameters <
   (type_parameter (type_identifier))
   >)
  (formal_parameter_list (
   (ERROR
    (formal_parameter name: (identifier))
    : (type_identifier) . (type_identifier)
    (ERROR ) class (identifier) (identifier))
    (identifier))
   (optional_formal_parameters {
    (ERROR (const_builtin)
     (function_type (type_identifier) Function
      (parameter_type_list (
       (optional_parameter_types
        (named_parameter_types {
         (typed_identifier (type_identifier) . (type_identifier) (identifier))
         }))
       )))
     ;
     (final_builtin final)
     (type_identifier)
     (nullable_type ?)
     (identifier) ;)
    (formal_parameter
     name: (marker_annotation @ name: (identifier))
     (ERROR (type_identifier) (identifier)
      (formal_parameter_list (
       (formal_parameter name: (type_identifier) (identifier))
       ,
       (formal_parameter name: (type_identifier) (identifier))
       ))
      => (identifier) ()
     (identifier))
    : (identifier) })
   )))
TimWhiting commented 1 year ago

Fixed the issues in the latest commit https://github.com/UserNobody14/tree-sitter-dart/commit/b55b90fe7d504b324d0357d69dc7eed631a3996b

Also renamed marker_annotation to annotation, not sure why it was named the former in the first place

dalugm commented 1 year ago

It works for me now, thx for the quick fix! @TimWhiting

But does these lines duplicated?

https://github.com/UserNobody14/tree-sitter-dart/blob/b55b90fe7d504b324d0357d69dc7eed631a3996b/queries/highlights.scm#L12-L15

TimWhiting commented 1 year ago

Thanks for the catch, I just deduplicated those highlights. I think neovim maintains a separate highlights with neovim specific things though.

dalugm commented 1 year ago

Oh, I don't know it's actually effect since I don't use neovim, I just thought that is for the test 😂

Anyway, thanks for the work!