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

Use raw identifiers for identifiers which collide with Rust keywords #476

Merged
merged 8 commits into from
Apr 21, 2021

Conversation

SciStarter
Copy link
Contributor

Rust keywords are valid identifier names in templates, so allow the template expansions to use those identifiers safely, by turning them into raw identifiers during the parse stage.

@vallentin
Copy link
Collaborator

vallentin commented Apr 19, 2021

Unrelated, but why are you posting as @SciStarter, but committing as @djarb?

@SciStarter
Copy link
Contributor Author

SciStarter commented Apr 19, 2021 via email

@vallentin vallentin requested a review from djc April 19, 2021 18:10
@djc
Copy link
Owner

djc commented Apr 19, 2021

Why do this in the parser? It would make much more sense to me in the code generator.

@SciStarter
Copy link
Contributor Author

SciStarter commented Apr 19, 2021 via email

@djc
Copy link
Owner

djc commented Apr 19, 2021

I'm sorry, but that doesn't make much sense to me. The parser is supposed to parse text into an AST that represents the template as parsed in a way that is easier to handle. The exact name of the variables only becomes a problem when they're used in the generated Rust code -- so that's where they should be renamed. I'm pretty sure there's only a few places where the code generator generates code for variable references; you could start by looking here.

(Since we're generating code into a buffer here, I don't think we need parallel arrays with r#-prefixed keywords. Just one array of keywords is enough, and then the code generator should take care of pushing the prefix into the buffer where needed.)

@SciStarter
Copy link
Contributor Author

SciStarter commented Apr 19, 2021

Okay, I've moved the identifier normalization into the code generator. Is this more to your liking?

@djc
Copy link
Owner

djc commented Apr 20, 2021

This is much better, thanks! As I understand it, though, we don't actually need to store the raw form in the generator's data structures -- we only need to put the raw form in the generated code, right? That should simplify this some more and also make it easier to follow through on my suggestion from the last comment that we don't need the two separate arrays.

Also, please put the constants and helpers near the bottom of the module as per the convention in this project.

@SciStarter
Copy link
Contributor Author

Unfortunately, that doesn't work due to the way expressions are handled (specifically, because they're rendered into a returned string instead of directly into the output buffer). I'd have to make extensive changes to the expression processing.

@SciStarter
Copy link
Contributor Author

However, due to the return value actually being a String, I can actually do something reasonable along those lines after all.

@vallentin
Copy link
Collaborator

Ah, I see what the issue is. It's because the key in locals: MapChain<...> is a &str and not a String. A workaround could then be to change the key to a String or Cow<'a, str>. Then it would be possible for normalize_identifier() to return Cow<'a, str> instead.

The question now ofc is, whether the price of dynamically allocated keys is better or worse, than the increased binary size, by having the "duplicate" arrays.

@SciStarter
Copy link
Contributor Author

As a rule of thumb, I prefer increased size over slower execution, but that's just a general opinion. Do you have a preference in this instance?

@djc
Copy link
Owner

djc commented Apr 20, 2021

I think I prefer the constants, but let's make it one array of tuples rather than parallel arrays (and let rustfmt do its thing on it).

@djc djc merged commit 451ef35 into djc:main Apr 21, 2021
@djc
Copy link
Owner

djc commented Apr 21, 2021

Thanks for doing the work on this!

@SciStarter
Copy link
Contributor Author

Thanks for being open to the idea :)

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.

None yet

4 participants