litrs
litrs copied to clipboard
Conversion to proc_macro2::Literal
I've started writing a conversion from (litrs::Literal, Span)
to proc_macro2::Literal
, and was wondering if there was a trick for doing it (given that Literal
only has a ton of constructors for each sub-kind of numeric literal). If not, I was wondering if you'd take the conversion as a PR (behind a Cargo feature, of course).
Clarification: I think it's possible that I'm supposed to use proc_macro2::Literal
's FromStr
with litrs::Literal
's Display
, because the constructors don't offer any way to restore the base. Is that right?
If so, would it be useful to expose the buffer directly, so it could be re-parsed? Would it be useful to have a TokenTree
or Literal
buffer type, to cheapen the round trip?
Out of curiosity: what's your use case for this conversion? Thinking about it, I wonder if it makes sense to implement quote::ToTokens
instead?
But yeah, I guess re-parsing it is the only way to preserve all details, like the integer base or stuff like that. Regarding exposing the string buffer: litrs::Literal
(and all the specific literals) don't necessarily hold the whole input as string. For example, take a look at the IntegerLit
struct. So I cannot offer a method returning &str
.
I think that quote::ToTokens is better than nothing, and I'd happily accept it.
The use-case is that I'm passing a TokenStream and want to submatch on specific instances of Literal. Once I've matched a particular submatch, I sometime want to "pass through" the remaining literals to the output TokenStream.
For this use-case, ToTokens is fine. But sometimes I want to pass the token along to another function that handles literals, and I'd rather not need to pattern match TokenTree again.
fwiw, I'd also like to avoid re-parsing the literal, and in most cases I use a slightly enriched TokenTree enum that remembers the parsed Literal. I think my feeling is: why not support round-tripping? You can try to find a slam-dunk use-case, but round-tripping is a good algebraic property that sometimes just comes up :)
I took a look at this issue again. I decided against quote::ToTokens
as litrs
does not depend on quote
yet. However, i added the following things:
-
raw_input
andinto_raw_input
for all non-bool literal types (this required changing the struct forIntegerLit
andFloatLit
) -
From<*Lit> for proc_macro[2]::Literal
for all non-bool literal types -
From<BoolLit> for proc_macro[2]::Ident
Diff: https://github.com/LukasKalbertodt/litrs/compare/litrs-to-pm
Before I merge and release that: what do you think? Do you think these additions are useful? Are they sufficient for your use case? Can you think of any other related additions that should be included as well? Do you see any problems with these additions?
@wycats Did you get a chance to look at my changes? Any comments? If you don't have the time or lost interest in this, that's totally fine, of course!
Those changes are now released as 0.3.0