-
Notifications
You must be signed in to change notification settings - Fork 277
cleanup AST builder functionality #1350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
use std::str; | ||
|
||
use itertools::intersperse; | ||
use proc_macro2::{Literal, Punct, Spacing, Span, TokenStream, TokenTree}; | ||
use std::default::Default; | ||
use std::iter::FromIterator; | ||
|
@@ -161,6 +162,16 @@ fn punct_box<T, P: Default>(x: Vec<Box<T>>) -> Punctuated<T, P> { | |
Punctuated::from_iter(x.into_iter().map(|x| *x)) | ||
} | ||
|
||
fn comma_separated<I, T>(items: I) -> TokenStream | ||
where | ||
I: Iterator<Item = T>, | ||
T: ToTokens + Clone, | ||
{ | ||
let items = items.map(|items| items.to_token_stream()); | ||
let comma = TokenTree::Punct(Punct::new(',', Spacing::Alone)).into_token_stream(); | ||
intersperse(items, comma).collect() | ||
} | ||
|
||
pub trait Make<T> { | ||
fn make(self, mk: &Builder) -> T; | ||
} | ||
|
@@ -208,28 +219,6 @@ impl<'a> Make<Path> for &'a str { | |
} | ||
} | ||
|
||
impl<'a> Make<Visibility> for &'a str { | ||
fn make(self, mk_: &Builder) -> Visibility { | ||
match self { | ||
"pub" => Visibility::Public(Token), | ||
"priv" | "" | "inherit" => Visibility::Inherited, | ||
"pub(crate)" => Visibility::Restricted(VisRestricted { | ||
pub_token: Token, | ||
paren_token: token::Paren(mk_.span), | ||
in_token: None, | ||
path: Box::new(mk().path("crate")), | ||
}), | ||
"pub(super)" => Visibility::Restricted(VisRestricted { | ||
pub_token: Token, | ||
paren_token: token::Paren(mk_.span), | ||
in_token: None, | ||
path: Box::new(mk().path("super")), | ||
}), | ||
_ => panic!("unrecognized string for Visibility: {:?}", self), | ||
} | ||
} | ||
} | ||
|
||
impl<'a> Make<Abi> for &'a str { | ||
fn make(self, mk: &Builder) -> Abi { | ||
Abi { | ||
|
@@ -252,47 +241,6 @@ impl Make<Extern> for Abi { | |
} | ||
} | ||
|
||
impl<'a> Make<Mutability> for &'a str { | ||
fn make(self, _mk: &Builder) -> Mutability { | ||
match self { | ||
"" | "imm" | "immut" | "immutable" => Mutability::Immutable, | ||
"mut" | "mutable" => Mutability::Mutable, | ||
_ => panic!("unrecognized string for Mutability: {:?}", self), | ||
} | ||
} | ||
} | ||
|
||
impl<'a> Make<Unsafety> for &'a str { | ||
fn make(self, _mk: &Builder) -> Unsafety { | ||
match self { | ||
"" | "safe" | "normal" => Unsafety::Normal, | ||
"unsafe" => Unsafety::Unsafe, | ||
_ => panic!("unrecognized string for Unsafety: {:?}", self), | ||
} | ||
} | ||
} | ||
|
||
impl<'a> Make<Constness> for &'a str { | ||
fn make(self, _mk: &Builder) -> Constness { | ||
match self { | ||
"" | "normal" | "not-const" => Constness::NotConst, | ||
"const" => Constness::Const, | ||
_ => panic!("unrecognized string for Constness: {:?}", self), | ||
} | ||
} | ||
} | ||
|
||
impl<'a> Make<UnOp> for &'a str { | ||
fn make(self, _mk: &Builder) -> UnOp { | ||
match self { | ||
"deref" | "*" => UnOp::Deref(Default::default()), | ||
"not" | "!" => UnOp::Not(Default::default()), | ||
"neg" | "-" => UnOp::Neg(Default::default()), | ||
Comment on lines
-288
to
-290
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure getting rid of all of these is helpful. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think is more readable for all unary_expr() usage in transpile to just use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe the original motivation for this was to isolate the actual types of the syntax library from the code using the builder API as much as possible. This was nice when we changed from using the rustc syntax tree to |
||
_ => panic!("unrecognized string for UnOp: {:?}", self), | ||
} | ||
} | ||
} | ||
|
||
impl<I: Make<Ident>> Make<Lifetime> for I { | ||
fn make(self, mk: &Builder) -> Lifetime { | ||
Lifetime { | ||
|
@@ -330,82 +278,21 @@ impl Make<TokenStream> for Vec<TokenTree> { | |
} | ||
} | ||
|
||
impl Make<TokenStream> for Vec<String> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With an iterator-based impl Make<TokenStream> for Vec<&str> {
fn make(self, _mk: &Builder) -> TokenStream {
comma_separated(self.iter().map(|&s| Ident::new(s, Span::call_site())))
}
}
impl Make<TokenStream> for Vec<u64> {
fn make(self, _mk: &Builder) -> TokenStream {
comma_separated(self.iter().map(|&s| Literal::u64_unsuffixed(s)))
}
}
impl Make<TokenStream> for Vec<Meta> {
fn make(self, _mk: &Builder) -> TokenStream {
comma_separated(self.iter())
}
} |
||
fn make(self, _mk: &Builder) -> TokenStream { | ||
let mut tokens = vec![]; | ||
let mut first = true; | ||
|
||
for s in self { | ||
if !first { | ||
tokens.push(TokenTree::Punct(Punct::new(',', Spacing::Alone))); | ||
} else { | ||
first = false; | ||
} | ||
|
||
tokens.push(TokenTree::Ident(Ident::new(&s, Span::call_site()))); | ||
} | ||
|
||
tokens.into_iter().collect::<TokenStream>() | ||
} | ||
} | ||
|
||
impl Make<TokenStream> for Vec<&str> { | ||
fn make(self, _mk: &Builder) -> TokenStream { | ||
let mut tokens = vec![]; | ||
let mut first = true; | ||
|
||
for s in self { | ||
if !first { | ||
tokens.push(TokenTree::Punct(Punct::new(',', Spacing::Alone))); | ||
} else { | ||
first = false; | ||
} | ||
|
||
tokens.push(TokenTree::Ident(Ident::new(s, Span::call_site()))); | ||
} | ||
|
||
tokens.into_iter().collect::<TokenStream>() | ||
comma_separated(self.iter().map(|&s| Ident::new(s, Span::call_site()))) | ||
} | ||
} | ||
|
||
impl Make<TokenStream> for Vec<u64> { | ||
fn make(self, _mk: &Builder) -> TokenStream { | ||
let mut tokens = vec![]; | ||
let mut first = true; | ||
|
||
for s in self { | ||
if !first { | ||
tokens.push(TokenTree::Punct(Punct::new(',', Spacing::Alone))); | ||
} else { | ||
first = false; | ||
} | ||
|
||
tokens.push(TokenTree::Literal(Literal::u64_unsuffixed(s))); | ||
} | ||
|
||
tokens.into_iter().collect::<TokenStream>() | ||
comma_separated(self.iter().map(|&s| Literal::u64_unsuffixed(s))) | ||
} | ||
} | ||
|
||
impl Make<TokenStream> for Vec<Meta> { | ||
fn make(self, _mk: &Builder) -> TokenStream { | ||
let mut tokens = TokenStream::new(); | ||
|
||
let mut first = true; | ||
|
||
for meta in self { | ||
if !first { | ||
let tt = TokenTree::Punct(Punct::new(',', Spacing::Alone)); | ||
|
||
tokens.extend(vec![tt]); | ||
} else { | ||
first = false; | ||
} | ||
|
||
meta.to_tokens(&mut tokens); | ||
} | ||
|
||
tokens | ||
comma_separated(self.iter()) | ||
} | ||
} | ||
|
||
bungcip marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
@@ -415,12 +302,6 @@ impl Make<PathArguments> for AngleBracketedGenericArguments { | |
} | ||
} | ||
|
||
impl Make<PathArguments> for ParenthesizedGenericArguments { | ||
fn make(self, _mk: &Builder) -> PathArguments { | ||
PathArguments::Parenthesized(self) | ||
} | ||
} | ||
|
||
impl Make<GenericArgument> for Box<Type> { | ||
fn make(self, _mk: &Builder) -> GenericArgument { | ||
GenericArgument::Type(*self) | ||
|
@@ -661,14 +542,6 @@ impl Builder { | |
} | ||
} | ||
|
||
pub fn parenthesized_args(self, tys: Vec<Box<Type>>) -> ParenthesizedGenericArguments { | ||
ParenthesizedGenericArguments { | ||
paren_token: token::Paren(self.span), | ||
inputs: punct_box(tys), | ||
output: ReturnType::Default, | ||
} | ||
} | ||
|
||
pub fn angle_bracketed_args<A>(self, args: Vec<A>) -> AngleBracketedGenericArguments | ||
where | ||
A: Make<GenericArgument>, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -667,12 +667,13 @@ impl StructureState { | |
/// * Negating something of the form `!<expr>` produces `<expr>` | ||
/// | ||
fn not(bool_expr: &Expr) -> Box<Expr> { | ||
use syn::UnOp; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this can be hoisted to the top-level, that'd be preferable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we can create a new module, lets call it |
||
match *bool_expr { | ||
Expr::Unary(ExprUnary { | ||
op: syn::UnOp::Not(_), | ||
op: UnOp::Not(_), | ||
ref expr, | ||
.. | ||
}) => Box::new(unparen(expr).clone()), | ||
_ => mk().unary_expr("!", Box::new(bool_expr.clone())), | ||
_ => mk().unary_expr(UnOp::Not(Default::default()), Box::new(bool_expr.clone())), | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.