UserNobody14 / tree-sitter-dart

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

Ambiguous matching #51

Closed wurikiji closed 10 months ago

wurikiji commented 11 months 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 10 months 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 10 months 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 10 months 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 10 months ago

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

dalugm commented 10 months 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!