Skip to content

Fix visitPreprocessedAst bug when ifdef directives followed by comments#59

Merged
AndrewRayCode merged 1 commit intoShaderFrog:mainfrom
06wj:fix/visit
Sep 5, 2025
Merged

Fix visitPreprocessedAst bug when ifdef directives followed by comments#59
AndrewRayCode merged 1 commit intoShaderFrog:mainfrom
06wj:fix/visit

Conversation

@06wj
Copy link
Contributor

@06wj 06wj commented Aug 26, 2025

No description provided.

const res = visitNode(child, node, path, nodeKey, i - offset);
if (res?.removed) {
offset += 1;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I see that child here becomes [ '// test', null ] and then '// test' which it tries to dive into and causes the error.

At a glance I wonder if something is screwy with the parse tree. It's weird there's a null here. And I'm not sure the code should be getting here at all. What makes me think this, is this codepath is used for all visiting, and there are plenty of other cases where lines end with comments.

Either way this is a definite improvement, thank you!

Copy link
Collaborator

Choose a reason for hiding this comment

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

I found a different way to address this - the change I added here https://github.com/ShaderFrog/glsl-parser/pull/60/files#diff-b78ac5f8fa2dcd8ef492d5c8cb4837b1f3489873eb1e130b1be66e1bdc3eedaaR33

In the parse tree, most nodes have a "wsEnd" key, representing the whitespace at the end of the lexeme, which can include comments. The wsEnd value is always supposed to be a string. The bug in the grammar was that it was not properly producing the string // test - the convoluted array logic in the parser was leaving it as ['// test', null]. THe parser update linked above properly produces a string for the ending whitespace including the comment.

If the value of any node key is a string, it's not visited, so this code path isn't hit.

@AndrewRayCode AndrewRayCode merged commit ab96553 into ShaderFrog:main Sep 5, 2025
1 check passed
@AndrewRayCode AndrewRayCode mentioned this pull request Sep 5, 2025
@AndrewRayCode
Copy link
Collaborator

Released in @shaderfrog/glsl-parser@6.1.0

@06wj 06wj deleted the fix/visit branch September 5, 2025 08:49
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