|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +extern crate phf_codegen; |
| 6 | +extern crate proc_macro; |
| 7 | +#[macro_use] extern crate quote; |
| 8 | +extern crate syn; |
| 9 | + |
| 10 | +use std::ascii::AsciiExt; |
| 11 | + |
| 12 | +/// Find a `#[cssparser__assert_ascii_lowercase__data(string = "…", string = "…")]` attribute, |
| 13 | +/// and panic if any string contains ASCII uppercase letters. |
| 14 | +#[proc_macro_derive(cssparser__assert_ascii_lowercase, |
| 15 | + attributes(cssparser__assert_ascii_lowercase__data))] |
| 16 | +pub fn assert_ascii_lowercase(input: proc_macro::TokenStream) -> proc_macro::TokenStream { |
| 17 | + let input = syn::parse_macro_input(&input.to_string()).unwrap(); |
| 18 | + let data = list_attr(&input, "cssparser__assert_ascii_lowercase__data"); |
| 19 | + |
| 20 | + for sub_attr in data { |
| 21 | + let string = sub_attr_value(sub_attr, "string"); |
| 22 | + assert_eq!(*string, string.to_ascii_lowercase(), |
| 23 | + "the expected strings must be given in ASCII lowercase"); |
| 24 | + } |
| 25 | + |
| 26 | + "".parse().unwrap() |
| 27 | +} |
| 28 | + |
| 29 | +/// Find a `#[cssparser__max_len__data(string = "…", string = "…")]` attribute, |
| 30 | +/// panic if any string contains ASCII uppercase letters, |
| 31 | +/// emit a `MAX_LENGTH` constant with the length of the longest string. |
| 32 | +#[proc_macro_derive(cssparser__max_len, |
| 33 | + attributes(cssparser__max_len__data))] |
| 34 | +pub fn max_len(input: proc_macro::TokenStream) -> proc_macro::TokenStream { |
| 35 | + let input = syn::parse_macro_input(&input.to_string()).unwrap(); |
| 36 | + let data = list_attr(&input, "cssparser__max_len__data"); |
| 37 | + |
| 38 | + let lengths = data.iter().map(|sub_attr| sub_attr_value(sub_attr, "string").len()); |
| 39 | + let max_length = lengths.max().expect("expected at least one string"); |
| 40 | + |
| 41 | + let tokens = quote! { |
| 42 | + const MAX_LENGTH: usize = #max_length; |
| 43 | + }; |
| 44 | + |
| 45 | + tokens.as_str().parse().unwrap() |
| 46 | +} |
| 47 | + |
| 48 | +/// On `struct $Name($ValueType)`, add a new static method |
| 49 | +/// `fn map() -> &'static ::phf::Map<&'static str, $ValueType>`. |
| 50 | +/// The map’s content is given as: |
| 51 | +/// `#[cssparser__phf_map__kv_pairs(key = "…", value = "…", key = "…", value = "…")]`. |
| 52 | +/// Keys are ASCII-lowercased. |
| 53 | +#[proc_macro_derive(cssparser__phf_map, |
| 54 | + attributes(cssparser__phf_map__kv_pairs))] |
| 55 | +pub fn phf_map(input: proc_macro::TokenStream) -> proc_macro::TokenStream { |
| 56 | + let input = syn::parse_macro_input(&input.to_string()).unwrap(); |
| 57 | + let name = &input.ident; |
| 58 | + let value_type = match input.body { |
| 59 | + syn::Body::Struct(syn::VariantData::Tuple(ref fields)) if fields.len() == 1 => { |
| 60 | + &fields[0].ty |
| 61 | + } |
| 62 | + _ => panic!("expected tuple struct newtype, got {:?}", input.body) |
| 63 | + }; |
| 64 | + |
| 65 | + let pairs: Vec<_> = list_attr(&input, "cssparser__phf_map__kv_pairs").chunks(2).map(|chunk| { |
| 66 | + let key = sub_attr_value(&chunk[0], "key"); |
| 67 | + let value = sub_attr_value(&chunk[1], "value"); |
| 68 | + (key.to_ascii_lowercase(), value) |
| 69 | + }).collect(); |
| 70 | + |
| 71 | + let mut map = phf_codegen::Map::new(); |
| 72 | + for &(ref key, value) in &pairs { |
| 73 | + map.entry(&**key, value); |
| 74 | + } |
| 75 | + |
| 76 | + let mut initializer_bytes = Vec::<u8>::new(); |
| 77 | + let mut initializer_tokens = quote::Tokens::new(); |
| 78 | + map.build(&mut initializer_bytes).unwrap(); |
| 79 | + initializer_tokens.append(::std::str::from_utf8(&initializer_bytes).unwrap()); |
| 80 | + |
| 81 | + let tokens = quote! { |
| 82 | + impl #name { |
| 83 | + #[inline] |
| 84 | + fn map() -> &'static ::phf::Map<&'static str, #value_type> { |
| 85 | + static MAP: ::phf::Map<&'static str, #value_type> = #initializer_tokens; |
| 86 | + &MAP |
| 87 | + } |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + tokens.as_str().parse().unwrap() |
| 92 | +} |
| 93 | + |
| 94 | +/// Panic if the first attribute isn’t `#[foo(…)]` with the given name, |
| 95 | +/// or return the parameters. |
| 96 | +fn list_attr<'a>(input: &'a syn::DeriveInput, expected_name: &str) -> &'a [syn::NestedMetaItem] { |
| 97 | + for attr in &input.attrs { |
| 98 | + match attr.value { |
| 99 | + syn::MetaItem::List(ref name, ref nested) if name == expected_name => { |
| 100 | + return nested |
| 101 | + } |
| 102 | + _ => {} |
| 103 | + } |
| 104 | + } |
| 105 | + panic!("expected a {} attribute", expected_name) |
| 106 | +} |
| 107 | + |
| 108 | +/// Panic if `sub_attr` is not a name-value like `foo = "…"` with the given name, |
| 109 | +/// or return the value. |
| 110 | +fn sub_attr_value<'a>(sub_attr: &'a syn::NestedMetaItem, expected_name: &str) -> &'a str { |
| 111 | + match *sub_attr { |
| 112 | + syn::NestedMetaItem::MetaItem( |
| 113 | + syn::MetaItem::NameValue(ref name, syn::Lit::Str(ref value, _)) |
| 114 | + ) |
| 115 | + if name == expected_name => { |
| 116 | + value |
| 117 | + } |
| 118 | + _ => { |
| 119 | + panic!("expected a `{} = \"…\"` parameter to the attribute, got {:?}", |
| 120 | + expected_name, sub_attr) |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments