Skip to content
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

Dart2 grammar fails to parse super parameters #4389

Open
levelrin opened this issue Jan 21, 2025 · 2 comments
Open

Dart2 grammar fails to parse super parameters #4389

levelrin opened this issue Jan 21, 2025 · 2 comments

Comments

@levelrin
Copy link

I'm using Dart2Parser.g4, Dart2Lexer.g4, and Dart2LexerBase.java.

The grammar could not parse the following code:

class Parent {
  final String? lastName;
  const Parent({
    this.lastName
  });
}

class Child extends Parent {
  final String firstName;
  const Child({
    required this.firstName,
    super.lastName,
  });
}

It failed at super.lastName.

Here is the error message:

line 12:4 no viable alternative at input ',super'
line 13:3 mismatched input ')' expecting {<EOF>, '@', 'abstract', 'as', 'async', 'await', 'class', 'const', 'covariant', 'deferred', 'dynamic', 'enum', 'export', 'extension', 'external', 'factory', 'final', 'Function', 'get', 'hide', 'implements', 'import', 'interface', 'late', 'library', 'mixin', 'native', 'of', 'on', 'operator', 'part', 'required', 'set', 'show', 'static', 'sync', 'typedef', 'var', 'void', 'yield', IDENTIFIER}

Here is the Java code to reproduce the issue:

package com.levelrin;

import com.levelrin.antlr.generated.Dart2Lexer;
import com.levelrin.antlr.generated.Dart2Parser;
import com.levelrin.antlr.generated.Dart2ParserBaseListener;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;

public final class Main {

    public static void main(final String... args) {
        final String source = """
            class Parent {
              final String? lastName;
              const Parent({
                this.lastName
              });
            }
            
            class Child extends Parent {
              final String firstName;
              const Child({
                required this.firstName,
                super.lastName,
              });
            }
            """;
        final CharStream charStream = CharStreams.fromString(source);
        final Dart2Lexer lexer = new Dart2Lexer(charStream);
        final CommonTokenStream tokens = new CommonTokenStream(lexer);
        final Dart2Parser parser = new Dart2Parser(tokens);
        final ParseTree tree = parser.compilationUnit();
        final Dart2ParserBaseListener listener = new Dart2ParserBaseListener();
        ParseTreeWalker.DEFAULT.walk(listener, tree);
    }

}
@kaby76
Copy link
Contributor

kaby76 commented Jan 21, 2025

"super" "." is not in either Dart2 or Dart3 in the spec:

⟨fieldFormalParameter⟩ ::=
⟨finalConstVarOrType⟩? this ‘.’ ⟨identifier⟩ (⟨formalParameterPart⟩ ‘?’?)?

(page 23, https://dart.dev/resources/language/spec/versions/DartLangSpec-v2.10.pdf)

It needs to be raised with Dart people. https://github.com/dart-lang/language eernstg

@levelrin
Copy link
Author

Since @kaby76 pointed out that the Dart spec does not have super parameters, I submitted a ticket to the Dart community.

By the way, here is a workaround I made in Dart2Parser.g4:

fieldFormalParameter
    // The original rule was: finalConstVarOrType? THIS_ D identifier (formalParameterPart QU?)?
    // Since Dart spec does not have super parameters, I include SUPER_ as a workaround.
    : finalConstVarOrType? (THIS_|SUPER_) D identifier (formalParameterPart QU?)?
    ;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants