Skip to content

Commit a8245f5

Browse files
committed
Add lots of static Symbols.
These will be used in the subsequent commits. Many of them are attributes. The commit also adds the ability to handle symbols that aren't identifiers (e.g. "proc-macro").
1 parent 1764b29 commit a8245f5

File tree

2 files changed

+415
-16
lines changed

2 files changed

+415
-16
lines changed

src/librustc_macros/src/symbols.rs

+31-12
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use quote::quote;
1111
#[allow(non_camel_case_types)]
1212
mod kw {
1313
syn::custom_keyword!(Keywords);
14-
syn::custom_keyword!(Other);
14+
syn::custom_keyword!(Symbols);
1515
}
1616

1717
struct Keyword {
@@ -33,14 +33,24 @@ impl Parse for Keyword {
3333
}
3434
}
3535

36-
struct Symbol(Ident);
36+
struct Symbol {
37+
name: Ident,
38+
value: Option<LitStr>,
39+
}
3740

3841
impl Parse for Symbol {
3942
fn parse(input: ParseStream<'_>) -> Result<Self> {
40-
let ident: Ident = input.parse()?;
43+
let name = input.parse()?;
44+
let value = match input.parse::<Token![:]>() {
45+
Ok(_) => Some(input.parse()?),
46+
Err(_) => None,
47+
};
4148
input.parse::<Token![,]>()?;
4249

43-
Ok(Symbol(ident))
50+
Ok(Symbol {
51+
name,
52+
value,
53+
})
4454
}
4555
}
4656

@@ -69,7 +79,7 @@ impl Parse for Input {
6979
braced!(content in input);
7080
let keywords = content.parse()?;
7181

72-
input.parse::<kw::Other>()?;
82+
input.parse::<kw::Symbols>()?;
7383
let content;
7484
braced!(content in input);
7585
let symbols = content.parse()?;
@@ -116,19 +126,22 @@ pub fn symbols(input: TokenStream) -> TokenStream {
116126
}
117127

118128
for symbol in &input.symbols.0 {
119-
let value = &symbol.0;
120-
let value_str = value.to_string();
121-
check_dup(&value_str);
129+
let name = &symbol.name;
130+
let value = match &symbol.value {
131+
Some(value) => value.value(),
132+
None => name.to_string(),
133+
};
134+
check_dup(&value);
122135
prefill_stream.extend(quote! {
123-
#value_str,
136+
#value,
124137
});
125138
symbols_stream.extend(quote! {
126-
pub const #value: Symbol = Symbol::new(#counter);
139+
pub const #name: Symbol = Symbol::new(#counter);
127140
});
128141
counter += 1;
129142
}
130143

131-
TokenStream::from(quote! {
144+
let tt = TokenStream::from(quote! {
132145
macro_rules! keywords {
133146
() => {
134147
#keyword_stream
@@ -159,5 +172,11 @@ pub fn symbols(input: TokenStream) -> TokenStream {
159172
])
160173
}
161174
}
162-
})
175+
});
176+
177+
// To see the generated code generated, uncomment this line, recompile, and
178+
// run the resulting output through `rustfmt`.
179+
//eprintln!("{}", tt);
180+
181+
tt
163182
}

0 commit comments

Comments
 (0)