Git Product home page Git Product logo

Comments (6)

wycats avatar wycats commented on May 24, 2024

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?

from litrs.

LukasKalbertodt avatar LukasKalbertodt commented on May 24, 2024

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.

from litrs.

wycats avatar wycats commented on May 24, 2024

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

from litrs.

LukasKalbertodt avatar LukasKalbertodt commented on May 24, 2024

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?

from litrs.

LukasKalbertodt avatar LukasKalbertodt commented on May 24, 2024

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

from litrs.

LukasKalbertodt avatar LukasKalbertodt commented on May 24, 2024

Those changes are now released as 0.3.0

from litrs.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.