-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #293 from emilio/bitflags
Improve support for bitflags. r=eqrion
- Loading branch information
Showing
64 changed files
with
833 additions
and
289 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
use proc_macro2::TokenStream; | ||
use syn; | ||
use syn::parse::{Parse, ParseStream, Parser, Result as ParseResult}; | ||
|
||
// $(#[$outer:meta])* | ||
// ($($vis:tt)*) $BitFlags:ident: $T:ty { | ||
// $( | ||
// $(#[$inner:ident $($args:tt)*])* | ||
// const $Flag:ident = $value:expr; | ||
// )+ | ||
// } | ||
#[derive(Debug)] | ||
pub struct Bitflags { | ||
attrs: Vec<syn::Attribute>, | ||
vis: syn::Visibility, | ||
struct_token: Token![struct], | ||
name: syn::Ident, | ||
colon_token: Token![:], | ||
repr: syn::Type, | ||
flags: Flags, | ||
} | ||
|
||
impl Bitflags { | ||
pub fn expand(&self) -> (syn::ItemStruct, syn::ItemImpl) { | ||
let Bitflags { | ||
ref attrs, | ||
ref vis, | ||
ref name, | ||
ref repr, | ||
ref flags, | ||
.. | ||
} = *self; | ||
|
||
let struct_ = parse_quote! { | ||
#(#attrs)* | ||
#vis struct #name { | ||
bits: #repr, | ||
} | ||
}; | ||
|
||
let consts = flags.expand(name); | ||
let impl_ = parse_quote! { | ||
impl #name { | ||
#consts | ||
} | ||
}; | ||
|
||
(struct_, impl_) | ||
} | ||
} | ||
|
||
impl Parse for Bitflags { | ||
fn parse(input: ParseStream) -> ParseResult<Self> { | ||
Ok(Self { | ||
attrs: input.call(syn::Attribute::parse_outer)?, | ||
vis: input.parse()?, | ||
struct_token: input.parse()?, | ||
name: input.parse()?, | ||
colon_token: input.parse()?, | ||
repr: input.parse()?, | ||
flags: input.parse()?, | ||
}) | ||
} | ||
} | ||
|
||
// $(#[$inner:ident $($args:tt)*])* | ||
// const $Flag:ident = $value:expr; | ||
#[derive(Debug)] | ||
struct Flag { | ||
attrs: Vec<syn::Attribute>, | ||
const_token: Token![const], | ||
name: syn::Ident, | ||
equals_token: Token![=], | ||
value: syn::Expr, | ||
semicolon_token: Token![;], | ||
} | ||
|
||
impl Flag { | ||
fn expand(&self, struct_name: &syn::Ident) -> TokenStream { | ||
let Flag { | ||
ref attrs, | ||
ref name, | ||
ref value, | ||
.. | ||
} = *self; | ||
quote! { | ||
#(#attrs)* | ||
const #name : #struct_name = #struct_name { bits: #value }; | ||
} | ||
} | ||
} | ||
|
||
impl Parse for Flag { | ||
fn parse(input: ParseStream) -> ParseResult<Self> { | ||
Ok(Self { | ||
attrs: input.call(syn::Attribute::parse_outer)?, | ||
const_token: input.parse()?, | ||
name: input.parse()?, | ||
equals_token: input.parse()?, | ||
value: input.parse()?, | ||
semicolon_token: input.parse()?, | ||
}) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
struct Flags(Vec<Flag>); | ||
|
||
impl Parse for Flags { | ||
fn parse(input: ParseStream) -> ParseResult<Self> { | ||
let content; | ||
let _ = braced!(content in input); | ||
let mut flags = vec![]; | ||
while !content.is_empty() { | ||
flags.push(content.parse()?); | ||
} | ||
Ok(Flags(flags)) | ||
} | ||
} | ||
|
||
impl Flags { | ||
fn expand(&self, struct_name: &syn::Ident) -> TokenStream { | ||
let mut ts = quote! {}; | ||
for flag in &self.0 { | ||
ts.extend(flag.expand(struct_name)); | ||
} | ||
ts | ||
} | ||
} | ||
|
||
pub fn parse(tokens: TokenStream) -> ParseResult<Bitflags> { | ||
let parser = Bitflags::parse; | ||
parser.parse2(tokens) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.