|
2 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | 4 |
|
5 | | -#[macro_use] |
6 | | -extern crate procedural_masquerade; |
7 | | -extern crate phf_codegen; |
8 | 5 | extern crate proc_macro; |
9 | | -extern crate proc_macro2; |
10 | | -#[macro_use] |
11 | | -extern crate quote; |
12 | | -extern crate syn; |
13 | 6 |
|
14 | | -use proc_macro2::{TokenStream, TokenTree}; |
15 | | -use quote::TokenStreamExt; |
16 | | -use std::iter; |
| 7 | +use proc_macro::TokenStream; |
17 | 8 |
|
18 | | -define_proc_macros! { |
19 | | - /// Input: the arms of a `match` expression. |
20 | | - /// |
21 | | - /// Output: a `MAX_LENGTH` constant with the length of the longest string pattern. |
22 | | - /// |
23 | | - /// Panic if the arms contain non-string patterns, |
24 | | - /// or string patterns that contains ASCII uppercase letters. |
25 | | - #[allow(non_snake_case)] |
26 | | - pub fn cssparser_internal__assert_ascii_lowercase__max_len(input: &str) -> String { |
27 | | - let expr = syn::parse_str(&format!("match x {{ {} }}", input)).unwrap(); |
28 | | - let arms = match expr { |
29 | | - syn::Expr::Match(syn::ExprMatch { arms, .. }) => arms, |
30 | | - _ => panic!("expected a match expression, got {:?}", expr) |
31 | | - }; |
32 | | - max_len(arms.into_iter().flat_map(|ref arm| { |
33 | | - match arm.pat { |
34 | | - syn::Pat::Or(ref p) => p.cases.iter().cloned().collect(), |
35 | | - ref p => vec![p.clone()] |
36 | | - } |
37 | | - }).filter_map(|pattern| { |
| 9 | +/// Input: a `match` expression. |
| 10 | +/// |
| 11 | +/// Output: a `MAX_LENGTH` constant with the length of the longest string pattern. |
| 12 | +/// |
| 13 | +/// Panic if the arms contain non-string patterns, |
| 14 | +/// or string patterns that contains ASCII uppercase letters. |
| 15 | +#[allow(non_snake_case)] |
| 16 | +#[proc_macro] |
| 17 | +pub fn cssparser_internal__assert_ascii_lowercase__max_len(input: TokenStream) -> TokenStream { |
| 18 | + let expr: syn::ExprMatch = syn::parse_macro_input!(input); |
| 19 | + let strings = expr |
| 20 | + .arms |
| 21 | + .iter() |
| 22 | + .flat_map(|arm| match arm.pat { |
| 23 | + syn::Pat::Or(ref p) => p.cases.iter().collect(), |
| 24 | + ref p => vec![p], |
| 25 | + }) |
| 26 | + .filter_map(|pattern| { |
38 | 27 | let expr = match pattern { |
39 | 28 | syn::Pat::Lit(expr) => expr, |
40 | 29 | syn::Pat::Wild(_) => return None, |
41 | | - _ => panic!("expected string or wildcard pattern, got {:?}", pattern) |
| 30 | + _ => panic!("expected string or wildcard pattern, got {:?}", pattern), |
42 | 31 | }; |
43 | 32 | match *expr.expr { |
44 | | - syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Str(ref lit), .. }) => { |
45 | | - assert_eq!(lit.value(), lit.value().to_ascii_lowercase(), |
46 | | - "string patterns must be given in ASCII lowercase"); |
47 | | - Some(lit.value().len()) |
| 33 | + syn::Expr::Lit(syn::ExprLit { |
| 34 | + lit: syn::Lit::Str(ref lit), |
| 35 | + .. |
| 36 | + }) => { |
| 37 | + assert_eq!( |
| 38 | + lit.value(), |
| 39 | + lit.value().to_ascii_lowercase(), |
| 40 | + "string patterns must be given in ASCII lowercase" |
| 41 | + ); |
| 42 | + Some(lit) |
48 | 43 | } |
49 | | - _ => panic!("expected string pattern, got {:?}", expr) |
| 44 | + _ => panic!("expected string pattern, got {:?}", expr), |
50 | 45 | } |
51 | | - })) |
52 | | - } |
53 | | - |
54 | | - /// Input: string literals with no separator |
55 | | - /// |
56 | | - /// Output: a `MAX_LENGTH` constant with the length of the longest string. |
57 | | - #[allow(non_snake_case)] |
58 | | - pub fn cssparser_internal__max_len(input: &str) -> String { |
59 | | - max_len(syn::parse_str::<TokenStream>(input).unwrap().into_iter().map(|tt| string_literal(&tt).len())) |
60 | | - } |
| 46 | + }); |
| 47 | + max_len(strings) |
| 48 | +} |
61 | 49 |
|
62 | | - /// Input: parsed as token trees. The first TT is a type. (Can be wrapped in parens.) |
63 | | - /// following TTs are grouped in pairs, each pair being a key as a string literal |
64 | | - /// and the corresponding value as a const expression. |
65 | | - /// |
66 | | - /// Output: a rust-phf map, with keys ASCII-lowercased: |
67 | | - /// ```text |
68 | | - /// static MAP: &'static ::cssparser::phf::Map<&'static str, $ValueType> = …; |
69 | | - /// ``` |
70 | | - #[allow(non_snake_case)] |
71 | | - pub fn cssparser_internal__phf_map(input: &str) -> String { |
72 | | - let token_trees: Vec<TokenTree> = syn::parse_str::<TokenStream>(input).unwrap().into_iter().collect(); |
73 | | - let value_type = &token_trees[0]; |
74 | | - let pairs: Vec<_> = token_trees[1..].chunks(2).map(|chunk| { |
75 | | - let key = string_literal(&chunk[0]); |
76 | | - let value = &chunk[1]; |
77 | | - (key.to_ascii_lowercase(), quote!(#value).to_string()) |
78 | | - }).collect(); |
| 50 | +/// Input: string literals with no separator |
| 51 | +/// |
| 52 | +/// Output: a `MAX_LENGTH` constant with the length of the longest string. |
| 53 | +#[allow(non_snake_case)] |
| 54 | +#[proc_macro] |
| 55 | +pub fn cssparser_internal__max_len(input: TokenStream) -> TokenStream { |
| 56 | + struct Input(Vec<syn::LitStr>); |
79 | 57 |
|
80 | | - let mut map = phf_codegen::Map::new(); |
81 | | - map.phf_path("::cssparser::_internal__phf"); |
82 | | - for &(ref key, ref value) in &pairs { |
83 | | - map.entry(&**key, &**value); |
| 58 | + impl syn::parse::Parse for Input { |
| 59 | + fn parse(input: syn::parse::ParseStream) -> syn::parse::Result<Self> { |
| 60 | + let mut strings = Vec::new(); |
| 61 | + while !input.is_empty() { |
| 62 | + strings.push(input.parse()?) |
| 63 | + } |
| 64 | + Ok(Self(strings)) |
84 | 65 | } |
85 | | - |
86 | | - let mut tokens = quote! { |
87 | | - static MAP: ::cssparser::_internal__phf::Map<&'static str, #value_type> = |
88 | | - }; |
89 | | - tokens.append_all(syn::parse_str::<proc_macro2::TokenStream>(&map.build().to_string())); |
90 | | - tokens.append_all(quote!(;)); |
91 | | - tokens.to_string() |
92 | 66 | } |
| 67 | + |
| 68 | + let strings: Input = syn::parse_macro_input!(input); |
| 69 | + max_len(strings.0.iter()) |
93 | 70 | } |
94 | 71 |
|
95 | | -fn max_len<I: Iterator<Item = usize>>(lengths: I) -> String { |
96 | | - let max_length = lengths.max().expect("expected at least one string"); |
97 | | - quote!( const MAX_LENGTH: usize = #max_length; ).to_string() |
| 72 | +fn max_len<'a, I: Iterator<Item = &'a syn::LitStr>>(strings: I) -> TokenStream { |
| 73 | + let max_length = strings |
| 74 | + .map(|s| s.value().len()) |
| 75 | + .max() |
| 76 | + .expect("expected at least one string"); |
| 77 | + quote::quote!( pub(super) const MAX_LENGTH: usize = #max_length; ).into() |
98 | 78 | } |
99 | 79 |
|
100 | | -fn string_literal(token: &TokenTree) -> String { |
101 | | - let lit: syn::LitStr = syn::parse2(iter::once(token.clone()).collect()) |
102 | | - .expect(&format!("expected string literal, got {:?}", token)); |
103 | | - lit.value() |
| 80 | +/// Input: A type, followed by pairs of string literal keys and expression values. No separator. |
| 81 | +/// |
| 82 | +/// Output: a rust-phf map, with keys ASCII-lowercased: |
| 83 | +/// ```text |
| 84 | +/// static MAP: &'static ::cssparser::phf::Map<&'static str, $ValueType> = …; |
| 85 | +/// ``` |
| 86 | +#[allow(non_snake_case)] |
| 87 | +#[proc_macro] |
| 88 | +pub fn cssparser_internal__phf_map(input: TokenStream) -> TokenStream { |
| 89 | + struct Input { |
| 90 | + value_type: syn::Type, |
| 91 | + keys: Vec<syn::LitStr>, |
| 92 | + values: Vec<syn::Expr>, |
| 93 | + } |
| 94 | + |
| 95 | + impl syn::parse::Parse for Input { |
| 96 | + fn parse(input: syn::parse::ParseStream) -> syn::parse::Result<Self> { |
| 97 | + let mut keys = Vec::new(); |
| 98 | + let mut values = Vec::new(); |
| 99 | + let value_type = input.parse()?; |
| 100 | + while !input.is_empty() { |
| 101 | + keys.push(input.parse()?); |
| 102 | + values.push(input.parse()?); |
| 103 | + } |
| 104 | + Ok(Input { |
| 105 | + value_type, |
| 106 | + keys, |
| 107 | + values, |
| 108 | + }) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + let Input { |
| 113 | + value_type, |
| 114 | + keys, |
| 115 | + values, |
| 116 | + } = syn::parse_macro_input!(input); |
| 117 | + let keys = keys |
| 118 | + .iter() |
| 119 | + .map(|s| syn::LitStr::new(&s.value().to_ascii_lowercase(), s.span())); |
| 120 | + |
| 121 | + quote::quote!( |
| 122 | + pub(super) static MAP: Map<&'static str, #value_type> = phf_map! { |
| 123 | + #( |
| 124 | + #keys => #values, |
| 125 | + )* |
| 126 | + }; |
| 127 | + ) |
| 128 | + .into() |
104 | 129 | } |
0 commit comments