litrs icon indicating copy to clipboard operation
litrs copied to clipboard

Conversion to proc_macro2::Literal

Open wycats opened this issue 2 years ago • 5 comments

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).

wycats avatar Oct 04 '21 13:10 wycats

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?

wycats avatar Oct 04 '21 13:10 wycats

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.

LukasKalbertodt avatar Oct 08 '21 17:10 LukasKalbertodt

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 :)

wycats avatar Nov 15 '21 20:11 wycats

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 and into_raw_input for all non-bool literal types (this required changing the struct for IntegerLit and FloatLit)
  • 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?

LukasKalbertodt avatar Dec 30 '21 13:12 LukasKalbertodt

@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!

LukasKalbertodt avatar Apr 16 '22 08:04 LukasKalbertodt

Those changes are now released as 0.3.0

LukasKalbertodt avatar Dec 19 '22 14:12 LukasKalbertodt