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

don't put NoSourceNode into a *FileNode #38

Merged
merged 1 commit into from
Sep 20, 2022
Merged

Conversation

jhump
Copy link
Member

@jhump jhump commented Sep 20, 2022

The buf repo is using an earlier revision of protocompile, under the moniker github.com/jhump/protocompile, for formatting.

While integrating protocompile into the buf build command, I was also removing this old dependency and updating the format command to use the latest version of this repo (the one under the bufbuild organization).

While debugging some discrepancies, I encountered a bug in this repo: an empty file resulted in an AST that would cause a panic when trying to visit/walk the AST. The problem is that it would store a NoSourceNode into the empty file node -- but that is a special placeholder node that means there is no AST, so it is (intentionally) not handled by the visit/walk functionality.

This fixes it. An empty file now looks like a normal file, but with no declarations. The singular token in the AST is now just the EOF token. (The reason a NoSourceNode was previously used was just to have some singular token, because the implementation of compositeNode gets cranky if it has zero children.) I also updated the grammar to produce a valid AST for an empty file (previously, it was only handled in a special check after the parser completed). That allows the AST for an empty file to still retain information about comments and whitespace (so "empty file" really meaning no tokens/lexical elements, not necessarily no bytes).


return &FileNode{
compositeNode: compositeNode{
children: []Node{NewNoSourceNode(filename)},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the issue: adding a NoSourceNode to a FileNode was a poor choice. Also, it wasn't even necessary as the replacement code (to just delegate to NewFileNode) works and is much more clear.

@jhump jhump requested a review from pkwarren September 20, 2022 00:49
@jhump jhump merged commit c45d057 into main Sep 20, 2022
@jhump jhump deleted the jh/fix-empty-file-ast branch September 20, 2022 13:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants