8000 CST: Serialize and print EOF trivia properly by JohnnyMorganz · Pull Request #247 · luau-lang/lute · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

CST: Serialize and print EOF trivia properly #247

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

Merged
merged 1 commit into from
May 4, 2025
Merged

Conversation

JohnnyMorganz
Copy link
Collaborator

The CST works by attaching trivia between tokens as trailing trivia of the previous token or leading trivia of the currently processed token. However, right now the CST does not serialize any trailing trivia of the file properly (e.g. whitespace such as newline at EOF, or files composed solely of comments). This is because we do not process a final token to attach leading trivia to.

This PR introduces a final "EOF" token that is composed solely of leading trivia at the end of the file. This is not a concept in the Luau AST itself, so we need to extend our parse result to carry the "eof" token separately, independent of the AstStatBlock.

An alternative approach could be to attach all trivia at the end of the file as trailing trivia of the last processed token. However, this ends up special casing this particular token and breaks the guarantee that the trailing trivia will only consist of trivia up to and including a single newline character. As such, downstream tooling would potentially need to handle final statement / expressions in files separately to all other statements / expressions if they cared about trivia processing (e.g., formatters). Also, this would not work for files consisting solely of trivia, as there is no token present.

return {
parse = parse,
parseexpr = parseexpr,
parsefile = parsefile,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not 100% on this API name. It was unclear if this should've just replaced "parse", or if we still wanted the ability to parse a block independently of whether it was a whole file. Happy to change it if there's suggestions, or can do it in a followup when we standardise the whole API after it's complete.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this is a fine name for now, i suppose

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

cool, will leave it alone for now, and revisit it afterwards

@aatxe aatxe merged commit 7f0501d into primary May 4, 2025
6 checks passed
@aatxe aatxe deleted the johnny/cst/eof branch May 4, 2025 03:23
green-real pushed a commit to green-real/lute that referenced this pull request May 19, 2025
The CST works by attaching trivia between tokens as trailing trivia of
the previous token or leading trivia of the currently processed token.
However, right now the CST does not serialize any trailing trivia of the
file properly (e.g. whitespace such as newline at EOF, or files composed
solely of comments). This is because we do not process a final token to
attach leading trivia to.

This PR introduces a final "EOF" token that is composed solely of
leading trivia at the end of the file. This is not a concept in the Luau
AST itself, so we need to extend our parse result to carry the "eof"
token separately, independent of the AstStatBlock.

An alternative approach could be to attach all trivia at the end of the
file as trailing trivia of the last processed token. However, this ends
up special casing this particular token and breaks the guarantee that
the trailing trivia will only consist of trivia up to and including a
single newline character. As such, downstream tooling would potentially
need to handle final statement / expressions in files separately to all
other statements / expressions if they cared about trivia processing
(e.g., formatters). Also, this would not work for files consisting
solely of trivia, as there is no token present.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0