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

Modifying the AST before printing? #201

Open
brikis98 opened this issue May 16, 2017 · 4 comments
Open

Modifying the AST before printing? #201

brikis98 opened this issue May 16, 2017 · 4 comments
Labels
v1 Relates to the v1 line of releases

Comments

@brikis98
Copy link

Is there a way to use this HCL library to:

  1. Parse a file that contains HCL (e.g. main.tf) to get back an AST.
  2. Modify the AST in memory.
  3. Write the result back out.

It seems like this should be easy, but it falls apart due to the logic in token.Pos. In particular, if you add a node somewhere to the AST, then all the line and offset values in the rest of the AST are wrong; and if you modify an existing node in the AST, then all the column and offset values in the rest of the AST are wrong. As a result, when you print the AST back out, you get all sorts of mangled code, especially related to comments.

Am I using this library wrong or is this just a current limitation?

@apparentlymart
Copy link
Contributor

Hi @brikis98!

The HCL printer code is primarily intended for pretty-printing a parsed, unmodified AST. I expect the mangling you saw was related to how comments get re-inserted into the stream in order to write out the result, since comments are not normally part of the main AST.

@brikis98
Copy link
Author

@apparentlymart Got it. I'll see if I can hack something up to properly update line/column/offset values for my use case.

@VladRassokhin
Copy link
Contributor

My suggestion is to add comments to AST so there would be no need to insert them back into the stream. And user could then even modify not only logic but comments too. Is it doable?

@sean-zou
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v1 Relates to the v1 line of releases
Projects
None yet
Development

No branches or pull requests

4 participants