-
Notifications
You must be signed in to change notification settings - Fork 601
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
Parsed hclsyntax.Body
has slightly different range depending on what type of comment is at the top
#702
Comments
I guess this is the same thing as #551...? 🤔 Noticed it while trying to do LSP stuff just like hashicorp/terraform-ls#1052. |
The "peeker" component is, when doing a normal parse, configured to filter out all comments (the boolean argument here is Line 22 in 3883feb
This then interacts with a quirk of the grammar: the two single-line comment styles absorb their trailing newline because that's the marker that indicates the token has ended, but the multi-line comment style ends with Lines 68 to 80 in 3883feb
However, because newlines are sometimes significant in parts of the grammar (any time we're parsing the series of items in a body), the peeker has a special case where instead of dropping the single-line tokens entirely it quietly replaces them with Lines 85 to 102 in 3883feb
With all of that in mind, if we remove the comment tokens in the same way the peeker would before entry into the parser, the six different inputs as observed by the parser are:
This is a somewhat-confusing outcome because it suggests that examples 1, 2, and 3 should be treated the same. However, I suspect that that the trick is in the comment in one of the code snippets I included above: Lines 91 to 98 in 3883feb
In examples 2 and 3, the initial
Assuming all of the above is correct -- I've only read the code and imagined how it would behave with each input, not tried to validate this by executing it -- one possible way to make this work would be to decide that, despite what I wrote in that comment above, the complexity is justified to work to calculate the true location of the I guess the need for hashicorp/terraform-ls#1052 is to ensure that the root body resulting from a A different answer then is to change the rules for Lines 65 to 71 in 3883feb
The Instead then, perhaps the This implies changing the signature of I hope that's all useful! |
Thank you for your detailed analysis, @apparentlymart. I did notice this tidbit when stepping into the code. I wondered if the public API offered a |
The parser is not designed to accept comment tokens in arbitrary places, so I don't think that simply pushing this problem down into the parser is a viable solution, but I've not tried it. |
I would expect this to print the same thing six times but the
/**/
seems to behave differently if it's on the first line.The text was updated successfully, but these errors were encountered: