Select Git revision
lib.rs 7.74 KiB
#![feature(proc_macro)]
extern crate proc_macro;
extern crate proc_macro2 as pm2;
#[macro_use]
extern crate quote;
#[macro_use]
extern crate syn;
use proc_macro::TokenStream;
use syn::spanned::Spanned;
use syn::synom::Synom;
use syn::{Ident, LitInt};
use quote::ToTokens;
use std::convert::From;
use std::error::Error;
/// Procedural macro `lit`
/// Parsing a `LitInt` with compile time range checking
#[proc_macro]
pub fn lit(input: TokenStream) -> TokenStream {
let v: LitInt = syn::parse(input).unwrap();
let value = v.value();
if !(10 <= value && value < 100) {
v.span()
.unstable()
.error(format!("expected literal 10 <= x < 100, got {}", value,))
.emit();
}
From::from(v.into_tokens())
}
/// Procedural macro `lite`
/// Parsing a `LitInt` with compile time range checking
#[proc_macro]
pub fn lite(input: TokenStream) -> TokenStream {
match syn::parse::<LitInt>(input) {
Ok(v) => {
let value = v.value();
if !(10 <= value && value < 100) {
v.span()
.unstable()
.error(format!("expected literal 10 <= x < 100, got {}", value,))
.emit();
}
From::from(v.into_tokens())
}
Err(err) => {
let desc = err.description();
let tokens = quote! {
compile_error!(#desc)
};
return tokens.into();
}
}
}
enum AppKeys {
Device,
Resources,
Interrupts,
Tasks,
Idle,
Init,
}
fn to_app_keys(id: Ident) -> Option<AppKeys> {
match id.as_ref() {
"device" => Some(AppKeys::Device),