#![feature(proc_macro, proc_macro_lib)]
extern crate proc_macro;
pub use proc_macro::TokenStream;
extern crate syn;
#[allow(unused_imports)]
pub use syn::{
Lit,
StrStyle,
IntTy,
FloatTy,
Ty,
Lifetime,
ConstExpr
};
pub mod meta;
#[allow(unused_imports)]
pub use meta::HashTag;
pub mod bodykind;
#[allow(unused_imports)]
use bodykind::{
BodyKind,
Enumerator,
Variant,
Field,
Fields,
Structure
};
macro_rules! is_thing {
($name:ident, $ma: pat) => {
#[inline(always)]
#[allow(dead_code)]
pub fn $name(&self) -> bool {
match self {
$ma => true,
_ => false
}
}
}
}
#[derive(Clone,Debug)]
#[allow(dead_code)]
pub struct MacroInput {
pub attr: Vec<HashTag>,
pub id: String,
pub body: BodyKind
}
impl MacroInput {
pub fn from_tree(input: TokenStream) -> Result<MacroInput,String> {
let s = input.to_string();
MacroInput::from_str(&s)
}
#[inline]
pub fn from_str(s: &str) -> Result<MacroInput,String> {
use std::mem;
let ast = syn::parse_macro_input(s)?;
let mut ret_val: MacroInput = unsafe{ mem::uninitialized() };
ret_val.id = ast.ident.to_string();
ret_val.attr = HashTag::complete( &ast.attrs);
Ok(ret_val)
}
}
#[derive(Clone,Debug,PartialEq,Eq)]
pub enum FromLiteral {
Str(String),
ByteStr(Vec<u8>),
Byte(u8),
Int(u64),
Float(String),
Bool(bool),
Char(char)
}
impl From<Lit> for FromLiteral {
fn from(x: Lit) -> FromLiteral {
match x {
Lit::Str(w,_) => FromLiteral::Str(w),
Lit::ByteStr(w,_) => FromLiteral::ByteStr(w),
Lit::Byte(w) => FromLiteral::Byte(w),
Lit::Int(w,_) => FromLiteral::Int(w),
Lit::Char(w) => FromLiteral::Char(w),
Lit::Bool(w) => FromLiteral::Bool(w),
Lit::Float(w,_) => FromLiteral::Float(w)
}
}
}
impl<'a> From<&'a Lit> for FromLiteral {
fn from(x: &'a Lit) -> FromLiteral {
match x {
&Lit::Str(ref w,_) => FromLiteral::Str(w.clone()),
&Lit::ByteStr(ref w,_) => FromLiteral::ByteStr(w.clone()),
&Lit::Byte(ref w) => FromLiteral::Byte(w.clone()),
&Lit::Int(ref w,_) => FromLiteral::Int(w.clone()),
&Lit::Char(ref w) => FromLiteral::Char(w.clone()),
&Lit::Bool(ref w) => FromLiteral::Bool(w.clone()),
&Lit::Float(ref w,_) => FromLiteral::Float(w.clone())
}
}
}
impl FromLiteral {
is_thing!(is_str,&FromLiteral::Str(_));
is_thing!(is_bytestr,&FromLiteral::ByteStr(_));
is_thing!(is_byte,&FromLiteral::Byte(_));
is_thing!(is_int,&FromLiteral::Int(_));
is_thing!(is_float,&FromLiteral::Float(_));
is_thing!(is_bool,&FromLiteral::Bool(_));
is_thing!(is_char,&FromLiteral::Char(_));
}