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

HCL Parser does not accept a comment as the last line in the file #4611

Open
mccartney opened this issue Oct 26, 2024 · 7 comments
Open

HCL Parser does not accept a comment as the last line in the file #4611

mccartney opened this issue Oct 26, 2024 · 7 comments
Labels
bug Something isn't working parser-hcl test provided Already replicated with a unit test, using JUnit pioneer's ExpectedToFail

Comments

@mccartney
Copy link
Contributor

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

    @Test
    void commentAsTheLastLine() {
        rewriteRun(
          hcl(
            """
              locals {
                a = 3
              }
              # Nice code, right?
            """
          )
        );
    }

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

[...]
Caused by: org.openrewrite.hcl.HclParsingException: Syntax error in file.tf at line 4:0 token recognition error at: '# Nice code, right?'.
	at org.openrewrite.hcl.HclParser$ForwardingErrorListener.syntaxError(HclParser.java:105)
	... 130 more
Caused by: LexerNoViableAltException('#')
	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)
	... 127 more
@mccartney mccartney added the bug Something isn't working label Oct 26, 2024
@timtebeek
Copy link
Contributor

hi @mccartney ; thanks for the report! This seems very similar to

Let me see if the suggestion reported there also fixes this issue.

@timtebeek timtebeek moved this to Backlog in OpenRewrite Oct 26, 2024
@timtebeek timtebeek added the test provided Already replicated with a unit test, using JUnit pioneer's ExpectedToFail label Oct 26, 2024
@timtebeek
Copy link
Contributor

Sadly this needs work beyond what was done in #4616 ; Any help appreciated!

@rahul3509
Copy link

rahul3509 commented Oct 27, 2024

I can look into it. I have also seen similar issue. Will get some more details regarding this

@mccartney
Copy link
Contributor Author

mccartney commented Oct 29, 2024

I suspect this has something to do with the \n at the end of this rule:

LINE_COMMENT : ('//' | '#') ~[\r\n]* '\r'?'\n' -> channel(HIDDEN);

Other languages use NEWLINE or EOF:

: '//' .*? (NEWLINE | EOF) -> skip

@rahul3509
Copy link

I was debugging the issue looks like

).visitConfigFile(parser.configFile());

visitConfigFile function call is failing, Looking into it.

@rahul3509
Copy link

Screenshot 2024-11-06 at 3 08 50 PM Screenshot 2024-11-06 at 3 07 58 PM

I have tested the grammar is alright it is parsing and tokenizing contents of input properly.

@mccartney
Copy link
Contributor Author

Related discussion in Antlr group: https://groups.google.com/g/antlr-discussion/c/1mJ0n38Jf8c?pli=1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working parser-hcl test provided Already replicated with a unit test, using JUnit pioneer's ExpectedToFail
Projects
Status: Backlog
Development

No branches or pull requests

3 participants