Skip to content

Commit 4e26e2d

Browse files
Keegan McAllisteralexcrichton
Keegan McAllister
authored andcommitted
Use macros to implement syntax::ext::quote::ToSource
This code deserves a bigger refactor, but here's a local improvement.
1 parent a0ec902 commit 4e26e2d

File tree

1 file changed

+74
-125
lines changed

1 file changed

+74
-125
lines changed

src/libsyntax/ext/quote.rs

Lines changed: 74 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -68,72 +68,61 @@ pub mod rt {
6868
6969
*/
7070

71+
// FIXME: Move this trait to pprust and get rid of *_to_str?
7172
pub trait ToSource {
7273
// Takes a thing and generates a string containing rust code for it.
7374
fn to_source(&self) -> String;
7475
}
7576

76-
impl ToSource for ast::Ident {
77-
fn to_source(&self) -> String {
78-
token::get_ident(*self).get().to_string()
79-
}
80-
}
81-
82-
impl ToSource for Gc<ast::Item> {
83-
fn to_source(&self) -> String {
84-
pprust::item_to_str(&**self)
85-
}
86-
}
87-
88-
impl<'a> ToSource for &'a [Gc<ast::Item>] {
89-
fn to_source(&self) -> String {
90-
self.iter()
91-
.map(|i| i.to_source())
92-
.collect::<Vec<String>>()
93-
.connect("\n\n")
94-
.to_string()
95-
}
96-
}
97-
98-
impl ToSource for ast::Ty {
99-
fn to_source(&self) -> String {
100-
pprust::ty_to_str(self)
101-
}
102-
}
103-
104-
impl<'a> ToSource for &'a [ast::Ty] {
105-
fn to_source(&self) -> String {
106-
self.iter()
107-
.map(|i| i.to_source())
108-
.collect::<Vec<String>>()
109-
.connect(", ")
110-
.to_string()
111-
}
112-
}
77+
macro_rules! impl_to_source(
78+
(Gc<$t:ty>, $pp:ident) => (
79+
impl ToSource for Gc<$t> {
80+
fn to_source(&self) -> String {
81+
pprust::$pp(&**self)
82+
}
83+
}
84+
);
85+
($t:ty, $pp:ident) => (
86+
impl ToSource for $t {
87+
fn to_source(&self) -> String {
88+
pprust::$pp(self)
89+
}
90+
}
91+
);
92+
)
11393

114-
impl ToSource for Generics {
115-
fn to_source(&self) -> String {
116-
pprust::generics_to_str(self)
117-
}
94+
fn slice_to_source<'a, T: ToSource>(sep: &'static str, xs: &'a [T]) -> String {
95+
xs.iter()
96+
.map(|i| i.to_source())
97+
.collect::<Vec<String>>()
98+
.connect(sep)
99+
.to_string()
118100
}
119101

120-
impl ToSource for Gc<ast::Expr> {
121-
fn to_source(&self) -> String {
122-
pprust::expr_to_str(&**self)
123-
}
124-
}
102+
macro_rules! impl_to_source_slice(
103+
($t:ty, $sep:expr) => (
104+
impl<'a> ToSource for &'a [$t] {
105+
fn to_source(&self) -> String {
106+
slice_to_source($sep, *self)
107+
}
108+
}
109+
)
110+
)
125111

126-
impl ToSource for ast::Block {
112+
impl ToSource for ast::Ident {
127113
fn to_source(&self) -> String {
128-
pprust::block_to_str(self)
114+
token::get_ident(*self).get().to_string()
129115
}
130116
}
131117

132-
impl ToSource for ast::Arg {
133-
fn to_source(&self) -> String {
134-
pprust::arg_to_str(self)
135-
}
136-
}
118+
impl_to_source!(ast::Ty, ty_to_str)
119+
impl_to_source!(ast::Block, block_to_str)
120+
impl_to_source!(ast::Arg, arg_to_str)
121+
impl_to_source!(Generics, generics_to_str)
122+
impl_to_source!(Gc<ast::Item>, item_to_str)
123+
impl_to_source!(Gc<ast::Expr>, expr_to_str)
124+
impl_to_source_slice!(ast::Ty, ", ")
125+
impl_to_source_slice!(Gc<ast::Item>, "\n\n")
137126

138127
impl<'a> ToSource for &'a str {
139128
fn to_source(&self) -> String {
@@ -163,76 +152,36 @@ pub mod rt {
163152
}
164153
}
165154

166-
impl ToSource for int {
167-
fn to_source(&self) -> String {
168-
let lit = dummy_spanned(ast::LitInt(*self as i64, ast::TyI));
169-
pprust::lit_to_str(&lit)
170-
}
171-
}
172-
173-
impl ToSource for i8 {
174-
fn to_source(&self) -> String {
175-
let lit = dummy_spanned(ast::LitInt(*self as i64, ast::TyI8));
176-
pprust::lit_to_str(&lit)
177-
}
178-
}
179-
180-
impl ToSource for i16 {
181-
fn to_source(&self) -> String {
182-
let lit = dummy_spanned(ast::LitInt(*self as i64, ast::TyI16));
183-
pprust::lit_to_str(&lit)
184-
}
185-
}
186-
187-
188-
impl ToSource for i32 {
189-
fn to_source(&self) -> String {
190-
let lit = dummy_spanned(ast::LitInt(*self as i64, ast::TyI32));
191-
pprust::lit_to_str(&lit)
192-
}
193-
}
194-
195-
impl ToSource for i64 {
196-
fn to_source(&self) -> String {
197-
let lit = dummy_spanned(ast::LitInt(*self as i64, ast::TyI64));
198-
pprust::lit_to_str(&lit)
199-
}
200-
}
201-
202-
impl ToSource for uint {
203-
fn to_source(&self) -> String {
204-
let lit = dummy_spanned(ast::LitUint(*self as u64, ast::TyU));
205-
pprust::lit_to_str(&lit)
206-
}
207-
}
208-
209-
impl ToSource for u8 {
210-
fn to_source(&self) -> String {
211-
let lit = dummy_spanned(ast::LitUint(*self as u64, ast::TyU8));
212-
pprust::lit_to_str(&lit)
213-
}
214-
}
215-
216-
impl ToSource for u16 {
217-
fn to_source(&self) -> String {
218-
let lit = dummy_spanned(ast::LitUint(*self as u64, ast::TyU16));
219-
pprust::lit_to_str(&lit)
220-
}
221-
}
155+
macro_rules! impl_to_source_int(
156+
(signed, $t:ty, $tag:ident) => (
157+
impl ToSource for $t {
158+
fn to_source(&self) -> String {
159+
let lit = dummy_spanned(ast::LitInt(*self as i64, ast::$tag));
160+
pprust::lit_to_str(&lit)
161+
}
162+
}
163+
);
164+
(unsigned, $t:ty, $tag:ident) => (
165+
impl ToSource for $t {
166+
fn to_source(&self) -> String {
167+
let lit = dummy_spanned(ast::LitUint(*self as u64, ast::$tag));
168+
pprust::lit_to_str(&lit)
169+
}
170+
}
171+
);
172+
)
222173

223-
impl ToSource for u32 {
224-
fn to_source(&self) -> String {
225-
let lit = dummy_spanned(ast::LitUint(*self as u64, ast::TyU32));
226-
pprust::lit_to_str(&lit)
227-
}
228-
}
174+
impl_to_source_int!(signed, int, TyI)
175+
impl_to_source_int!(signed, i8, TyI8)
176+
impl_to_source_int!(signed, i16, TyI16)
177+
impl_to_source_int!(signed, i32, TyI32)
178+
impl_to_source_int!(signed, i64, TyI64)
229179

230-
impl ToSource for u64 {
231-
fn to_source(&self) -> String {
232-
let lit = dummy_spanned(ast::LitUint(*self as u64, ast::TyU64));
233-
pprust::lit_to_str(&lit)
234-
}
235-
}
180+
impl_to_source_int!(unsigned, uint, TyU)
181+
impl_to_source_int!(unsigned, u8, TyU8)
182+
impl_to_source_int!(unsigned, u16, TyU16)
183+
impl_to_source_int!(unsigned, u32, TyU32)
184+
impl_to_source_int!(unsigned, u64, TyU64)
236185

237186
// Alas ... we write these out instead. All redundant.
238187

@@ -246,7 +195,7 @@ pub mod rt {
246195
)
247196
)
248197

249-
macro_rules! impl_to_tokens_self(
198+
macro_rules! impl_to_tokens_lifetime(
250199
($t:ty) => (
251200
impl<'a> ToTokens for $t {
252201
fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
@@ -258,14 +207,14 @@ pub mod rt {
258207

259208
impl_to_tokens!(ast::Ident)
260209
impl_to_tokens!(Gc<ast::Item>)
261-
impl_to_tokens_self!(&'a [Gc<ast::Item>])
210+
impl_to_tokens_lifetime!(&'a [Gc<ast::Item>])
262211
impl_to_tokens!(ast::Ty)
263-
impl_to_tokens_self!(&'a [ast::Ty])
212+
impl_to_tokens_lifetime!(&'a [ast::Ty])
264213
impl_to_tokens!(Generics)
265214
impl_to_tokens!(Gc<ast::Expr>)
266215
impl_to_tokens!(ast::Block)
267216
impl_to_tokens!(ast::Arg)
268-
impl_to_tokens_self!(&'a str)
217+
impl_to_tokens_lifetime!(&'a str)
269218
impl_to_tokens!(())
270219
impl_to_tokens!(char)
271220
impl_to_tokens!(bool)

0 commit comments

Comments
 (0)