Skip to content

Commit

Permalink
generator: Replace manual fn to_tokens with quote::ToTokens trait impl
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed Nov 24, 2020
1 parent 4aadcf7 commit fdc5910
Showing 1 changed file with 36 additions and 52 deletions.
88 changes: 36 additions & 52 deletions generator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ pub enum CType {
Bool32,
}

impl CType {
fn to_tokens(self) -> Tokens {
impl quote::ToTokens for CType {
fn to_tokens(&self, tokens: &mut Tokens) {
let term = match self {
CType::USize => Term::intern("usize"),
CType::U32 => Term::intern("u32"),
CType::U64 => Term::intern("u64"),
CType::Float => Term::intern("f32"),
CType::Bool32 => Term::intern("Bool32"),
};
quote! {#term}
term.to_tokens(tokens);
}
}

Expand Down Expand Up @@ -385,7 +385,6 @@ impl ConstVal {
pub trait ConstantExt {
fn constant(&self) -> Constant;
fn variant_ident(&self, enum_name: &str) -> Ident;
fn to_tokens(&self) -> Tokens;
fn notation(&self) -> Option<&str>;
}

Expand All @@ -396,9 +395,6 @@ impl ConstantExt for vkxml::ExtensionEnum {
fn variant_ident(&self, enum_name: &str) -> Ident {
variant_ident(enum_name, &self.name)
}
fn to_tokens(&self) -> Tokens {
Constant::from_extension_enum(self).expect("").to_tokens()
}
fn notation(&self) -> Option<&str> {
self.notation.as_deref()
}
Expand All @@ -411,9 +407,6 @@ impl ConstantExt for vkxml::Constant {
fn variant_ident(&self, enum_name: &str) -> Ident {
variant_ident(enum_name, &self.name)
}
fn to_tokens(&self) -> Tokens {
Constant::from_constant(self).to_tokens()
}
fn notation(&self) -> Option<&str> {
self.notation.as_deref()
}
Expand All @@ -429,6 +422,36 @@ pub enum Constant {
Alias(Ident, Ident),
}

impl quote::ToTokens for Constant {
fn to_tokens(&self, tokens: &mut Tokens) {
match *self {
Constant::Number(n) => {
let number = interleave_number('_', 3, &n.to_string());
let term = Term::intern(&number);
term.to_tokens(tokens);
}
Constant::Hex(ref s) => {
let number = interleave_number('_', 4, s);
let term = Term::intern(&format!("0x{}", number));
term.to_tokens(tokens);
}
Constant::Text(ref text) => text.to_tokens(tokens),
Constant::CExpr(ref expr) => {
let (_, (_, rexpr)) = cexpr(expr).expect("Unable to parse cexpr");
tokens.append_all(rexpr.parse::<TokenStream>());
}
Constant::BitPos(pos) => {
let value = 1 << pos;
let bit_string = format!("{:b}", value);
let bit_string = interleave_number('_', 4, &bit_string);
let term = Term::intern(&format!("0b{}", bit_string));
term.to_tokens(tokens);
}
Constant::Alias(ref base, ref value) => tokens.append_all(quote!(#base::#value)),
}
}
}

impl quote::ToTokens for ConstVal {
fn to_tokens(&self, tokens: &mut Tokens) {
match self {
Expand Down Expand Up @@ -475,39 +498,6 @@ impl Constant {
}
}

pub fn to_tokens(&self) -> Tokens {
match *self {
Constant::Number(n) => {
let number = interleave_number('_', 3, &n.to_string());
let term = Term::intern(&number);
quote! {#term}
}
Constant::Hex(ref s) => {
let number = interleave_number('_', 4, s);
let term = Term::intern(&format!("0x{}", number));
quote! {#term}
}
Constant::Text(ref text) => {
quote! {#text}
}
Constant::CExpr(ref expr) => {
let (_, (_, rexpr)) = cexpr(expr).expect("Unable to parse cexpr");
let term = Term::intern(rexpr.as_str());
quote! {#term}
}
Constant::BitPos(pos) => {
let value = 1 << pos;
let bit_string = format!("{:b}", value);
let bit_string = interleave_number('_', 4, &bit_string);
let term = Term::intern(&format!("0b{}", bit_string));
quote! {#term}
}
Constant::Alias(ref base, ref value) => {
quote! {#base::#value}
}
}
}

pub fn from_extension_enum(constant: &vkxml::ExtensionEnum) -> Option<Self> {
let number = constant.number.map(Constant::Number);
let hex = constant.hex.as_ref().map(|hex| Constant::Hex(hex.clone()));
Expand Down Expand Up @@ -1010,9 +1000,6 @@ impl<'a> ConstantExt for ExtensionConstant<'a> {
fn variant_ident(&self, enum_name: &str) -> Ident {
variant_ident(enum_name, self.name)
}
fn to_tokens(&self) -> Tokens {
self.constant.to_tokens()
}
fn notation(&self) -> Option<&str> {
None
}
Expand Down Expand Up @@ -1331,11 +1318,10 @@ pub fn bitflags_impl_block(
.map(|constant| {
let variant_ident = constant.variant_ident(enum_name);
let constant = constant.constant();
let tokens = constant.to_tokens();
let tokens = if let Constant::Alias(_, _) = &constant {
tokens
quote!(#constant)
} else {
quote!(Self(#tokens))
quote!(Self(#constant))
};
(variant_ident, tokens)
})
Expand Down Expand Up @@ -2234,15 +2220,13 @@ pub fn generate_constant<'a>(
let c = Constant::from_constant(constant);
let name = constant_name(&constant.name);
let ident = Ident::from(name.as_str());
let value = c.to_tokens();
let ty = if name == "TRUE" || name == "FALSE" {
CType::Bool32
} else {
c.ty()
};
let ty = ty.to_tokens();
quote! {
pub const #ident: #ty = #value;
pub const #ident: #ty = #c;
}
}

Expand Down

0 comments on commit fdc5910

Please sign in to comment.