Comments (6)
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.
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.
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.
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 typesFrom<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.
@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.
Those changes are now released as 0.3.0
from litrs.
Related Issues (11)
- Community Feedback HOT 7
- Return `String` from `StringLit<String>::into_value`
- Remove `proc-macro2` from default features
- Floatlit fails with 27f32 HOT 4
- Possible to add value_as_str? HOT 5
- Update escape logic according to RFC 3349
- Support non-number suffixes HOT 2
- Example for proc macro HOT 2
- Pull out majority of parsing code into non-generic functions HOT 1
- Broken external link in documentation
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from litrs.