Comments support in unstable/Parser #860
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is the first part of completing issue #822. In this pull request, I have added functionality to the
unstable/Parser
to emitComment
nodes when instructed to do so using theKeepComments: true
option.I have also added a new method,
unstable/Parser.Shape()
, which provides the location (byte offset, column, line) of the beginning and end of eachNode
. Although some nodes are currently missing their location, I plan to fix that soon. This method should help retrieving whitespace when needed.To demonstrate the use of the new
KeepComments
option, I have included theparser_test.go/ExampleParser_comments
example. This function shows how to interpret comments in various positions within a document. Comments are emitted either as root expressions (when they are alone on their line) or as siblings of the element that precedes them. Within an array, groups of comments are represented as a single comment with multiple child comments.I purposely did not attach comments to any specific node, as I feel this is subject to interpretation and may vary from one application to another. Instead, the caller is free to decide which element a comment should be logically attached to, if at all.
@mikefarah @TomWright I welcome any feedback on this draft! Note that this pull request does not address how to encode the AST to TOML, but I plan to address that in a follow-up PR.