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 nested string literals #4405

Open
levelrin opened this issue Feb 2, 2025 · 0 comments
Open

Dart2 grammar fails to parse nested string literals #4405

levelrin opened this issue Feb 2, 2025 · 0 comments

Comments

@levelrin
Copy link

levelrin commented Feb 2, 2025

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

The grammar could not parse the following code:

void main() {
  String param = '';
  String text = '''
  ${param.isEmpty? '''one
  ''':'''$param'''}two
  ''';
  print(text);
}

Here is the error message:

line 4:22 no viable alternative at input 'voidmain(){Stringparam='';Stringtext='''\n  ${param.isEmpty? '''one'

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 = """
            void main() {
              String param = '';
              String text = '''
              ${param.isEmpty? '''one
              ''':'''$param'''}two
              ''';
              print(text);
            }
            """;
        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);
    }

}
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

1 participant