openrewrite / rewrite

Automated mass refactoring of source code.
https://docs.openrewrite.org
Apache License 2.0
2.19k stars 329 forks source link

HCL Parser does not accept percent sign (%) in String literals #4601

Open mccartney opened 1 day ago

mccartney commented 1 day ago

What is the smallest, simplest way to reproduce the problem?

    @Test
    void percentSignInStringLiteral() {
        rewriteRun(
          hcl(
            """
              locals {
                hello = "no alcohol %"
              }
              """
          )
        );
    }

What is the full stack trace of any errors you encountered?

[...]
Caused by: org.openrewrite.hcl.HclParsingException: Syntax error in file.tf at line 2:24 token recognition error at: '\n'.
    at org.openrewrite.hcl.HclParser$ForwardingErrorListener.syntaxError(HclParser.java:105)
    ... 132 more
Caused by: LexerNoViableAltException('\n')
    at org.antlr.v4.runtime.atn.LexerATNSimulator.failOrAccept(LexerATNSimulator.java:309)
    at org.antlr.v4.runtime.atn.LexerATNSimulator.execATN(LexerATNSimulator.java:230)
    at org.antlr.v4.runtime.atn.LexerATNSimulator.match(LexerATNSimulator.java:114)
    at org.antlr.v4.runtime.Lexer.nextToken(Lexer.java:141)
    ... 129 more

Are you interested in contributing a fix to OpenRewrite?

No opinion yet. I haven't looked at the possible fix yet.

timtebeek commented 1 day ago

Thanks for the report @mccartney ! That's sure surprising. 🤔 Appreciate the runnable example. No idea why from a quick look at the ANTLR spec, although I'm certainly no expert there. Did you explore a cause/fix?

timtebeek commented 1 day ago

Looking at the spec it seems only %{ would be special, and escaped as %%{. Then % by itself in a string ought to be fine. https://github.com/hashicorp/hcl/blob/main/hclsyntax/spec.md Not sure we already cover that in out ANTLR spec though.