Rename Palette to FRAME (#4182)

* palette -> frame

* PALETTE, Palette -> FRAME

* Move folder pallete -> frame

* Update docs/Structure.adoc

Co-Authored-By: Benjamin Kampmann <ben.kampmann@googlemail.com>

* Update docs/README.adoc

Co-Authored-By: Benjamin Kampmann <ben.kampmann@googlemail.com>

* Update README.adoc
This commit is contained in:
Shawn Tabrizi
2019-11-22 19:21:25 +01:00
committed by GitHub
parent 68351da29b
commit c9175b59ff
206 changed files with 485 additions and 483 deletions
@@ -0,0 +1,12 @@
[package]
name = "frame-support-procedural-tools"
version = "2.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[dependencies]
frame-support-procedural-tools-derive = { package = "frame-support-procedural-tools-derive", path = "./derive" }
proc-macro2 = "1.0.6"
quote = "1.0.2"
syn = { version = "1.0.7", features = ["full"] }
proc-macro-crate = "0.1.4"
@@ -0,0 +1,13 @@
[package]
name = "frame-support-procedural-tools-derive"
version = "2.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[lib]
proc-macro = true
[dependencies]
proc-macro2 = "1.0.6"
quote = { version = "1.0.2", features = ["proc-macro"] }
syn = { version = "1.0.7", features = ["proc-macro" ,"full", "extra-traits", "parsing"] }
@@ -0,0 +1,243 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
// tag::description[]
//! Use to derive parsing for parsing struct.
// end::description[]
#![recursion_limit = "128"]
extern crate proc_macro;
use proc_macro::TokenStream;
use proc_macro2::Span;
use syn::parse_macro_input;
use quote::quote;
pub(crate) fn fields_idents(
fields: impl Iterator<Item = syn::Field>,
) -> impl Iterator<Item = proc_macro2::TokenStream> {
fields.enumerate().map(|(ix, field)| {
field.ident.clone().map(|i| quote!{#i}).unwrap_or_else(|| {
let f_ix: syn::Ident = syn::Ident::new(&format!("f_{}", ix), Span::call_site());
quote!( #f_ix )
})
})
}
pub(crate) fn fields_access(
fields: impl Iterator<Item = syn::Field>,
) -> impl Iterator<Item = proc_macro2::TokenStream> {
fields.enumerate().map(|(ix, field)| {
field.ident.clone().map(|i| quote!( #i )).unwrap_or_else(|| {
let f_ix: syn::Index = syn::Index {
index: ix as u32,
span: Span::call_site(),
};
quote!( #f_ix )
})
})
}
/// self defined parsing struct or enum.
/// not meant for any struct/enum, just for fast
/// parse implementation.
/// For enums:
/// variant are tested in order of definition.
/// Empty variant is always true.
/// Please use carefully, this will fully parse successful variant twice.
#[proc_macro_derive(Parse)]
pub fn derive_parse(input: TokenStream) -> TokenStream {
let item = parse_macro_input!(input as syn::Item);
match item {
syn::Item::Enum(input) => derive_parse_enum(input),
syn::Item::Struct(input) => derive_parse_struct(input),
_ => TokenStream::new(), // ignore
}
}
fn derive_parse_struct(input: syn::ItemStruct) -> TokenStream {
let syn::ItemStruct {
ident,
generics,
fields,
..
} = input;
let field_names = {
let name = fields_idents(fields.iter().map(Clone::clone));
quote!{
#(
#name,
)*
}
};
let field = fields_idents(fields.iter().map(Clone::clone));
let tokens = quote! {
impl #generics syn::parse::Parse for #ident #generics {
fn parse(input: syn::parse::ParseStream) -> syn::parse::Result<Self> {
#(
let #field = input.parse()?;
)*
Ok(Self {
#field_names
})
}
}
};
tokens.into()
}
fn derive_parse_enum(input: syn::ItemEnum) -> TokenStream {
let syn::ItemEnum {
ident,
generics,
variants,
..
} = input;
let variants = variants.iter().map(|v| {
let variant_ident = v.ident.clone();
let fields_build = if v.fields.iter().count() > 0 {
let fields_id = fields_idents(v.fields.iter().map(Clone::clone));
quote!( (#(#fields_id), *) )
} else {
quote!()
};
let fields_procs = fields_idents(v.fields.iter().map(Clone::clone))
.map(|fident| {
quote!{
let mut #fident = match fork.parse() {
Ok(r) => r,
Err(_e) => break,
};
}
});
let fields_procs_again = fields_idents(v.fields.iter().map(Clone::clone))
.map(|fident| {
quote!{
#fident = input.parse().expect("was parsed just before");
}
});
// double parse to update input cursor position
// next syn crate version should be checked for a way
// to copy position/state from a fork
quote!{
let mut fork = input.fork();
loop {
#(#fields_procs)*
#(#fields_procs_again)*
return Ok(#ident::#variant_ident#fields_build);
}
}
});
let tokens = quote! {
impl #generics syn::parse::Parse for #ident #generics {
fn parse(input: syn::parse::ParseStream) -> syn::parse::Result<Self> {
#(
#variants
)*
// no early return from any variants
Err(
syn::parse::Error::new(
proc_macro2::Span::call_site(),
"derived enum no matching variants"
)
)
}
}
};
tokens.into()
}
/// self defined parsing struct or enum.
/// not meant for any struct/enum, just for fast
/// parse implementation.
/// For enum:
/// it only output fields (empty field act as a None).
#[proc_macro_derive(ToTokens)]
pub fn derive_totokens(input: TokenStream) -> TokenStream {
let item = parse_macro_input!(input as syn::Item);
match item {
syn::Item::Enum(input) => derive_totokens_enum(input),
syn::Item::Struct(input) => derive_totokens_struct(input),
_ => TokenStream::new(), // ignore
}
}
fn derive_totokens_struct(input: syn::ItemStruct) -> TokenStream {
let syn::ItemStruct {
ident,
generics,
fields,
..
} = input;
let fields = fields_access(fields.iter().map(Clone::clone));
let tokens = quote! {
impl #generics quote::ToTokens for #ident #generics {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
#(
self.#fields.to_tokens(tokens);
)*
}
}
};
tokens.into()
}
fn derive_totokens_enum(input: syn::ItemEnum) -> TokenStream {
let syn::ItemEnum {
ident,
generics,
variants,
..
} = input;
let variants = variants.iter().map(|v| {
let v_ident = v.ident.clone();
let fields_build = if v.fields.iter().count() > 0 {
let fields_id = fields_idents(v.fields.iter().map(Clone::clone));
quote!( (#(#fields_id), *) )
} else {
quote!()
};
let field = fields_idents(v.fields.iter().map(Clone::clone));
quote! {
#ident::#v_ident#fields_build => {
#(
#field.to_tokens(tokens);
)*
},
}
});
let tokens = quote! {
impl #generics quote::ToTokens for #ident #generics {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
match self {
#(
#variants
)*
}
}
}
};
tokens.into()
}
@@ -0,0 +1,91 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
// tag::description[]
//! Proc macro helpers for procedural macros
// end::description[]
// reexport proc macros
pub use frame_support_procedural_tools_derive::*;
use proc_macro_crate::crate_name;
use syn::parse::Error;
use quote::quote;
pub mod syn_ext;
// FIXME #1569, remove the following functions, which are copied from sr-api-macros
use proc_macro2::{TokenStream, Span};
use syn::Ident;
fn generate_hidden_includes_mod_name(unique_id: &str) -> Ident {
Ident::new(&format!("sr_api_hidden_includes_{}", unique_id), Span::call_site())
}
/// Generates the access to the `frame-support` crate.
pub fn generate_crate_access(unique_id: &str, def_crate: &str) -> TokenStream {
if std::env::var("CARGO_PKG_NAME").unwrap() == def_crate {
quote::quote!( frame_support )
} else {
let mod_name = generate_hidden_includes_mod_name(unique_id);
quote::quote!( self::#mod_name::hidden_include )
}
}
/// Generates the hidden includes that are required to make the macro independent from its scope.
pub fn generate_hidden_includes(unique_id: &str, def_crate: &str) -> TokenStream {
if ::std::env::var("CARGO_PKG_NAME").unwrap() == def_crate {
TokenStream::new()
} else {
let mod_name = generate_hidden_includes_mod_name(unique_id);
match crate_name(def_crate) {
Ok(name) => {
let name = Ident::new(&name, Span::call_site());
quote::quote!(
#[doc(hidden)]
mod #mod_name {
pub extern crate #name as hidden_include;
}
)
},
Err(e) => {
let err = Error::new(Span::call_site(), &e).to_compile_error();
quote!( #err )
}
}
}
}
// fn to remove white spaces around string types
// (basically whitespaces around tokens)
pub fn clean_type_string(input: &str) -> String {
input
.replace(" ::", "::")
.replace(":: ", "::")
.replace(" ,", ",")
.replace(" ;", ";")
.replace(" [", "[")
.replace("[ ", "[")
.replace(" ]", "]")
.replace(" (", "(")
.replace("( ", "(")
.replace(" )", ")")
.replace(" <", "<")
.replace("< ", "<")
.replace(" >", ">")
}
@@ -0,0 +1,252 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
// tag::description[]
//! Extension to syn types, mainly for parsing
// end::description[]
use syn::{visit::{Visit, self}, parse::{Parse, ParseStream, Result}, Ident};
use proc_macro2::{TokenStream, TokenTree};
use quote::ToTokens;
use std::iter::once;
use frame_support_procedural_tools_derive::{ToTokens, Parse};
/// stop parsing here getting remaining token as content
/// Warn duplicate stream (part of)
#[derive(Parse, ToTokens, Debug)]
pub struct StopParse {
pub inner: TokenStream,
}
// inner macro really dependant on syn naming convention, do not export
macro_rules! groups_impl {
($name:ident, $tok:ident, $deli:ident, $parse:ident) => {
#[derive(Debug)]
pub struct $name<P> {
pub token: syn::token::$tok,
pub content: P,
}
impl<P: Parse> Parse for $name<P> {
fn parse(input: ParseStream) -> Result<Self> {
let syn::group::$name { token, content } = syn::group::$parse(input)?;
let content = content.parse()?;
Ok($name { token, content, })
}
}
impl<P: ToTokens> ToTokens for $name<P> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let mut inner_stream = TokenStream::new();
self.content.to_tokens(&mut inner_stream);
let token_tree: proc_macro2::TokenTree =
proc_macro2::Group::new(proc_macro2::Delimiter::$deli, inner_stream).into();
tokens.extend(once(token_tree));
}
}
}
}
groups_impl!(Braces, Brace, Brace, parse_braces);
groups_impl!(Brackets, Bracket, Bracket, parse_brackets);
groups_impl!(Parens, Paren, Parenthesis, parse_parens);
#[derive(Debug)]
pub struct PunctuatedInner<P,T,V> {
pub inner: syn::punctuated::Punctuated<P,T>,
pub variant: V,
}
#[derive(Debug)]
pub struct NoTrailing;
#[derive(Debug)]
pub struct Trailing;
pub type Punctuated<P,T> = PunctuatedInner<P,T,NoTrailing>;
pub type PunctuatedTrailing<P,T> = PunctuatedInner<P,T,Trailing>;
impl<P: Parse, T: Parse + syn::token::Token> Parse for PunctuatedInner<P,T,Trailing> {
fn parse(input: ParseStream) -> Result<Self> {
Ok(PunctuatedInner {
inner: syn::punctuated::Punctuated::parse_separated_nonempty(input)?,
variant: Trailing,
})
}
}
impl<P: Parse, T: Parse> Parse for PunctuatedInner<P,T,NoTrailing> {
fn parse(input: ParseStream) -> Result<Self> {
Ok(PunctuatedInner {
inner: syn::punctuated::Punctuated::parse_terminated(input)?,
variant: NoTrailing,
})
}
}
impl<P: ToTokens, T: ToTokens, V> ToTokens for PunctuatedInner<P,T,V> {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.inner.to_tokens(tokens)
}
}
/// Note that syn Meta is almost fine for use case (lacks only `ToToken`)
#[derive(Debug, Clone)]
pub struct Meta {
pub inner: syn::Meta,
}
impl Parse for Meta {
fn parse(input: ParseStream) -> Result<Self> {
Ok(Meta {
inner: syn::Meta::parse(input)?,
})
}
}
impl ToTokens for Meta {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self.inner {
syn::Meta::Path(ref path) => path.to_tokens(tokens),
syn::Meta::List(ref l) => l.to_tokens(tokens),
syn::Meta::NameValue(ref n) => n.to_tokens(tokens),
}
}
}
#[derive(Debug)]
pub struct OuterAttributes {
pub inner: Vec<syn::Attribute>,
}
impl Parse for OuterAttributes {
fn parse(input: ParseStream) -> Result<Self> {
let inner = syn::Attribute::parse_outer(input)?;
Ok(OuterAttributes {
inner,
})
}
}
impl ToTokens for OuterAttributes {
fn to_tokens(&self, tokens: &mut TokenStream) {
for att in self.inner.iter() {
att.to_tokens(tokens);
}
}
}
#[derive(Debug)]
pub struct Opt<P> {
pub inner: Option<P>,
}
impl<P: Parse> Parse for Opt<P> {
// Note that it cost a double parsing (same as enum derive)
fn parse(input: ParseStream) -> Result<Self> {
let inner = match input.fork().parse::<P>() {
Ok(_item) => Some(input.parse().expect("Same parsing ran before")),
Err(_e) => None,
};
Ok(Opt { inner })
}
}
impl<P: ToTokens> ToTokens for Opt<P> {
fn to_tokens(&self, tokens: &mut TokenStream) {
if let Some(ref p) = self.inner {
p.to_tokens(tokens);
}
}
}
pub fn extract_type_option(typ: &syn::Type) -> Option<syn::Type> {
if let syn::Type::Path(ref path) = typ {
let v = path.path.segments.last()?;
if v.ident == "Option" {
// Option has only one type argument in angle bracket.
if let syn::PathArguments::AngleBracketed(a) = &v.arguments {
if let syn::GenericArgument::Type(typ) = a.args.last()? {
return Some(typ.clone())
}
}
}
}
None
}
/// Auxialary structure to check if a given `Ident` is contained in an ast.
struct ContainsIdent<'a> {
ident: &'a Ident,
result: bool,
}
impl<'ast> ContainsIdent<'ast> {
fn visit_tokenstream(&mut self, stream: TokenStream) {
stream.into_iter().for_each(|tt|
match tt {
TokenTree::Ident(id) => self.visit_ident(&id),
TokenTree::Group(ref group) => self.visit_tokenstream(group.stream()),
_ => {}
}
)
}
fn visit_ident(&mut self, ident: &Ident) {
if ident == self.ident {
self.result = true;
}
}
}
impl<'ast> Visit<'ast> for ContainsIdent<'ast> {
fn visit_ident(&mut self, input: &'ast Ident) {
self.visit_ident(input);
}
fn visit_macro(&mut self, input: &'ast syn::Macro) {
self.visit_tokenstream(input.tokens.clone());
visit::visit_macro(self, input);
}
}
/// Check if a `Type` contains the given `Ident`.
pub fn type_contains_ident(typ: &syn::Type, ident: &Ident) -> bool {
let mut visit = ContainsIdent {
result: false,
ident,
};
visit::visit_type(&mut visit, typ);
visit.result
}
/// Check if a `Expr` contains the given `Ident`.
pub fn expr_contains_ident(expr: &syn::Expr, ident: &Ident) -> bool {
let mut visit = ContainsIdent {
result: false,
ident,
};
visit::visit_expr(&mut visit, expr);
visit.result
}