Comments (6)
Could quote_attribute!
do compile-time parsing while keeping the generality of "Anything that implements ToTokens
can be used with #foo
interpolation" ?
Anyway, just to write down the idea, the traits I mentioned before could look like this:
pub trait Parse: Sized {
fn parse<I: ToSourceString>(input: I) -> Result<Self, String>;
}
// This replaces parse_expr()
impl Parse for Expr {
fn parse<I: ToSourceString>(input: I) -> Result<Self, String> {
unwrap("expression", expr::parsing::expr, &input.to_source_string())
}
}
// Other impls for various AST types…
pub trait ToSourceString {
fn to_source_string(&self) -> Cow<str>;
}
impl ToSourceString for str {
fn to_source_string(&self) -> Cow<str> {
// Remove leading and trailing whitespace.
// Nom parsing functions tend to trip on the latter particularly
self.trim().into()
}
}
impl ToSourceString for String {
fn to_source_string(&self) -> Cow<str> {
(**self).to_source_string()
}
}
impl ToSourceString for quote::Tokens {
fn to_source_string(&self) -> Cow<str> {
self.to_string().to_source_string().into_owned().into()
}
}
impl ToSourceString for [TokenTree] {
fn to_source_string(&self) -> Cow<str> {
let mut tokens = quote::Tokens::new();
tokens.append_all(self);
tokens.to_string().to_source_string().into_owned().into()
}
}
impl ToSourceString for Vec<TokenTree> {
fn to_source_string(&self) -> Cow<str> {
(**self).to_source_string()
}
}
from syn.
Rather than a large number of macros for each type, maybe a method available on each type would be a nicer API?
let attr = Attribute::parse(quote!(#[derive(Debug, Clone, #a, #b)])).unwrap();
These methods could also replace the ad-hoc top-level parse_*
functions, if they accept a generic parameter with some trait implemented by both quote::Tokens
and &str
(and maybe &[syn::TonkenTree]
/ Vec<TokenTree>
/ TokenTree
? For dealing with expression/items macros.)
from syn.
I like it but I have two concerns:
- I think
Attribute::parse
puts too much emphasis on the fact that parsing is happening, which is not what I want the user and readers of the user's code to focus on in these cases. Everyone knows parsing is something that can fail, so you need to unwrap ortry!(Attribute::parse(...))
. But if the thing you are parsing is right there inside aquote!(...)
macro, you should write it in a way that you know will parse i.e. anything that doesn't parse is a bug in your program, not an expected failure that you can handle at runtime. I thinkquote_attribute!(...)
captures this distinction better. - Once procedural macros are stable, the
quote_attribute!
approach allows us to do the parsing at compile time rather than run time in most cases. As I mentioned in another ticket I am not worried about performance here so that is not why I am excited about this. Instead, parsing at compile time would let us validate the thing you are quoting, and fail to compile your program if it looks wrong. We can't ever do this with theAttribute::parse(quote!(...))
approach.
I am not settled on the quote_attribute!
approach yet but these are some of the advantages.
I do like Attribute::parse
as an alternative to the current style of syn::parse_attribute
, will think about the tradeoffs there.
from syn.
This seems like it should be doable with an external crate, which may be desirable to keep syn small.
from syn.
I added a parse_quote!
macro in 01cc020 that can return any Synom implementation.
// Add a bound `T: HeapSize` to every type parameter T.
fn add_trait_bounds(mut generics: Generics) -> Generics {
for param in &mut generics.params {
if let GenericParam::Type(ref mut type_param) = *param {
type_param.bounds.push(parse_quote!(HeapSize));
}
}
generics
}
from syn.
@dtolnay Unfortunately this doesn't handle the case of things like Attribute which don't implement Synom
, because the have specific parse_inner
and parse_outer
methods which are distinct.
Do you have any idea how we could go about handling that case? I think Attribute
is particularly relevant (especially considering that it's the one you mention in the first comment!)
from syn.
Related Issues (20)
- Parse precise capturing syntax HOT 1
- Scope when parsing delimited group content does not necessarily belong to the right Group token
- Parsing function using `parse` referencing enum fails HOT 1
- Parse attributes on where-predicates
- Deny keyword lifetimes pre-expansion
- Parse unsafe attributes
- Parse unnamed C varargs within function pointer types HOT 1
- [Feature Request] Add support for incomplete expression and statement HOT 1
- FieldMutability is missing Parse and ToTokens HOT 1
- Breaking change to `Generics::lifetimes` in v2.0.73 HOT 8
- ExprPath to_tokens() output can't be parsed as an expression due to missing turbofish
- A
- Documentation discrepancy between `parse` and `parse2`
- Generics::split_for_impl can cause clippy::multiple_bound_locations HOT 2
- Error parsing `Option<()>` and `Result<(), ()>` literals in `syn::parse_macro_input`: `unexpected end of input, expected an expression` HOT 1
- test/debug directory is .gitignored by cargo vendor causing friction when placing vendored crates in git HOT 3
- Parse raw lifetimes
- Can't parse Stmt::Expr
- Parsing invalid source code results in stack overflow HOT 5
- Potential false-positive of rustdoc::broken_intra_doc_links lint
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 syn.