Skip to content

Remove use of procedural-masquerade #263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ dtoa-short = "0.3"
heapsize = {version = ">= 0.3, < 0.5", optional = true}
itoa = "0.4"
matches = "0.1"
phf = "0.8"
procedural-masquerade = {path = "./procedural-masquerade", version = "0.1"}
phf = {version = "0.8", features = ["macros"]}
serde = {version = "1.0", optional = true}
smallvec = "0.6"

Expand Down
4 changes: 1 addition & 3 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ description = "Procedural macros for cssparser"
documentation = "https://docs.rs/cssparser-macros/"
repository = "https://github.com/servo/rust-cssparser"
license = "MPL-2.0"
edition = "2018"

[lib]
path = "lib.rs"
proc-macro = true

[dependencies]
procedural-masquerade = {path = "../procedural-masquerade", version = "0.1"}
phf_codegen = "0.8"
quote = "1"
syn = {version = "1", features = ["full", "extra-traits"]}
proc-macro2 = "1"
187 changes: 106 additions & 81 deletions macros/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,103 +2,128 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#[macro_use]
extern crate procedural_masquerade;
extern crate phf_codegen;
extern crate proc_macro;
extern crate proc_macro2;
#[macro_use]
extern crate quote;
extern crate syn;

use proc_macro2::{TokenStream, TokenTree};
use quote::TokenStreamExt;
use std::iter;
use proc_macro::TokenStream;

define_proc_macros! {
/// Input: the arms of a `match` expression.
///
/// Output: a `MAX_LENGTH` constant with the length of the longest string pattern.
///
/// Panic if the arms contain non-string patterns,
/// or string patterns that contains ASCII uppercase letters.
#[allow(non_snake_case)]
pub fn cssparser_internal__assert_ascii_lowercase__max_len(input: &str) -> String {
let expr = syn::parse_str(&format!("match x {{ {} }}", input)).unwrap();
let arms = match expr {
syn::Expr::Match(syn::ExprMatch { arms, .. }) => arms,
_ => panic!("expected a match expression, got {:?}", expr)
};
max_len(arms.into_iter().flat_map(|ref arm| {
match arm.pat {
syn::Pat::Or(ref p) => p.cases.iter().cloned().collect(),
ref p => vec![p.clone()]
}
}).filter_map(|pattern| {
/// Input: a `match` expression.
///
/// Output: a `MAX_LENGTH` constant with the length of the longest string pattern.
///
/// Panic if the arms contain non-string patterns,
/// or string patterns that contains ASCII uppercase letters.
#[allow(non_snake_case)]
#[proc_macro]
pub fn cssparser_internal__assert_ascii_lowercase__max_len(input: TokenStream) -> TokenStream {
let expr: syn::ExprMatch = syn::parse_macro_input!(input);
let strings = expr
.arms
.iter()
.flat_map(|arm| match arm.pat {
syn::Pat::Or(ref p) => p.cases.iter().collect(),
ref p => vec![p],
})
.filter_map(|pattern| {
let expr = match pattern {
syn::Pat::Lit(expr) => expr,
syn::Pat::Wild(_) => return None,
_ => panic!("expected string or wildcard pattern, got {:?}", pattern)
_ => panic!("expected string or wildcard pattern, got {:?}", pattern),
};
match *expr.expr {
syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Str(ref lit), .. }) => {
assert_eq!(lit.value(), lit.value().to_ascii_lowercase(),
"string patterns must be given in ASCII lowercase");
Some(lit.value().len())
syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(ref lit),
..
}) => {
assert_eq!(
lit.value(),
lit.value().to_ascii_lowercase(),
"string patterns must be given in ASCII lowercase"
);
Some(lit)
}
_ => panic!("expected string pattern, got {:?}", expr)
_ => panic!("expected string pattern, got {:?}", expr),
}
}))
}

/// Input: string literals with no separator
///
/// Output: a `MAX_LENGTH` constant with the length of the longest string.
#[allow(non_snake_case)]
pub fn cssparser_internal__max_len(input: &str) -> String {
max_len(syn::parse_str::<TokenStream>(input).unwrap().into_iter().map(|tt| string_literal(&tt).len()))
}
});
max_len(strings)
}

/// Input: parsed as token trees. The first TT is a type. (Can be wrapped in parens.)
/// following TTs are grouped in pairs, each pair being a key as a string literal
/// and the corresponding value as a const expression.
///
/// Output: a rust-phf map, with keys ASCII-lowercased:
/// ```text
/// static MAP: &'static ::cssparser::phf::Map<&'static str, $ValueType> = …;
/// ```
#[allow(non_snake_case)]
pub fn cssparser_internal__phf_map(input: &str) -> String {
let token_trees: Vec<TokenTree> = syn::parse_str::<TokenStream>(input).unwrap().into_iter().collect();
let value_type = &token_trees[0];
let pairs: Vec<_> = token_trees[1..].chunks(2).map(|chunk| {
let key = string_literal(&chunk[0]);
let value = &chunk[1];
(key.to_ascii_lowercase(), quote!(#value).to_string())
}).collect();
/// Input: string literals with no separator
///
/// Output: a `MAX_LENGTH` constant with the length of the longest string.
#[allow(non_snake_case)]
#[proc_macro]
pub fn cssparser_internal__max_len(input: TokenStream) -> TokenStream {
struct Input(Vec<syn::LitStr>);

let mut map = phf_codegen::Map::new();
map.phf_path("::cssparser::_internal__phf");
for &(ref key, ref value) in &pairs {
map.entry(&**key, &**value);
impl syn::parse::Parse for Input {
fn parse(input: syn::parse::ParseStream) -> syn::parse::Result<Self> {
let mut strings = Vec::new();
while !input.is_empty() {
strings.push(input.parse()?)
}
Ok(Self(strings))
}

let mut tokens = quote! {
static MAP: ::cssparser::_internal__phf::Map<&'static str, #value_type> =
};
tokens.append_all(syn::parse_str::<proc_macro2::TokenStream>(&map.build().to_string()));
tokens.append_all(quote!(;));
tokens.to_string()
}

let strings: Input = syn::parse_macro_input!(input);
max_len(strings.0.iter())
}

fn max_len<I: Iterator<Item = usize>>(lengths: I) -> String {
let max_length = lengths.max().expect("expected at least one string");
quote!( const MAX_LENGTH: usize = #max_length; ).to_string()
fn max_len<'a, I: Iterator<Item = &'a syn::LitStr>>(strings: I) -> TokenStream {
let max_length = strings
.map(|s| s.value().len())
.max()
.expect("expected at least one string");
quote::quote!( pub(super) const MAX_LENGTH: usize = #max_length; ).into()
}

fn string_literal(token: &TokenTree) -> String {
let lit: syn::LitStr = syn::parse2(iter::once(token.clone()).collect())
.expect(&format!("expected string literal, got {:?}", token));
lit.value()
/// Input: A type, followed by pairs of string literal keys and expression values. No separator.
///
/// Output: a rust-phf map, with keys ASCII-lowercased:
/// ```text
/// static MAP: &'static ::cssparser::phf::Map<&'static str, $ValueType> = …;
/// ```
#[allow(non_snake_case)]
#[proc_macro]
pub fn cssparser_internal__phf_map(input: TokenStream) -> TokenStream {
struct Input {
value_type: syn::Type,
keys: Vec<syn::LitStr>,
values: Vec<syn::Expr>,
}

impl syn::parse::Parse for Input {
fn parse(input: syn::parse::ParseStream) -> syn::parse::Result<Self> {
let mut keys = Vec::new();
let mut values = Vec::new();
let value_type = input.parse()?;
while !input.is_empty() {
keys.push(input.parse()?);
values.push(input.parse()?);
}
Ok(Input {
value_type,
keys,
values,
})
}
}

let Input {
value_type,
keys,
values,
} = syn::parse_macro_input!(input);
let keys = keys
.iter()
.map(|s| syn::LitStr::new(&s.value().to_ascii_lowercase(), s.span()));

quote::quote!(
pub(super) static MAP: Map<&'static str, #value_type> = phf_map! {
#(
#keys => #values,
)*
};
)
.into()
}
6 changes: 2 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,16 @@ extern crate itoa;
extern crate cssparser_macros;
#[macro_use]
extern crate matches;
#[macro_use]
extern crate procedural_masquerade;
#[cfg(test)]
extern crate difference;
#[cfg(test)]
extern crate encoding_rs;
#[doc(hidden)]
pub extern crate phf as _internal__phf;
#[cfg(test)]
extern crate serde_json;
#[cfg(feature = "serde")]
extern crate serde;
#[cfg(test)]
extern crate serde_json;
#[cfg(feature = "heapsize")]
#[macro_use]
extern crate heapsize;
Expand Down
54 changes: 26 additions & 28 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

use std::mem::MaybeUninit;

// See docs of the `procedural-masquerade` crate.
define_invoke_proc_macro!(cssparser_internal__invoke_proc_macro);

/// Expands to a `match` expression with string patterns,
/// matching case-insensitively in the ASCII range.
///
Expand Down Expand Up @@ -37,19 +34,22 @@ define_invoke_proc_macro!(cssparser_internal__invoke_proc_macro);
macro_rules! match_ignore_ascii_case {
( $input:expr, $( $match_body:tt )* ) => {
{
cssparser_internal__invoke_proc_macro! {
cssparser_internal__assert_ascii_lowercase__max_len!( $( $match_body )* )
}

{
// MAX_LENGTH is generated by cssparser_internal__assert_ascii_lowercase__max_len
cssparser_internal__to_lowercase!($input, MAX_LENGTH => lowercase);
// "A" is a short string that we know is different for every string pattern,
// since we’ve verified that none of them include ASCII upper case letters.
match lowercase.unwrap_or("A") {
$( $match_body )*
// This dummy module works around the feature gate
// `error[E0658]: procedural macros cannot be expanded to statements`
// by forcing the macro to be in an item context
// rather than expression/statement context,
// even though the macro only expands to items.
mod cssparser_internal {
cssparser_internal__assert_ascii_lowercase__max_len! {
match x { $( $match_body )* }
}
}
cssparser_internal__to_lowercase!($input, cssparser_internal::MAX_LENGTH => lowercase);
// "A" is a short string that we know is different for every string pattern,
// since we’ve verified that none of them include ASCII upper case letters.
match lowercase.unwrap_or("A") {
$( $match_body )*
}
}
};
}
Expand Down Expand Up @@ -80,23 +80,21 @@ macro_rules! match_ignore_ascii_case {
/// }
#[macro_export]
macro_rules! ascii_case_insensitive_phf_map {
($name: ident -> $ValueType: ty = { $( $key: expr => $value: expr ),* }) => {
ascii_case_insensitive_phf_map!($name -> $ValueType = { $( $key => $value, )* })
($name: ident -> $ValueType: ty = { $( $key: tt => $value: expr ),+ }) => {
ascii_case_insensitive_phf_map!($name -> $ValueType = { $( $key => $value, )+ })
};
($name: ident -> $ValueType: ty = { $( $key: expr => $value: expr, )* }) => {
($name: ident -> $ValueType: ty = { $( $key: tt => $value: expr, )+ }) => {
pub fn $name(input: &str) -> Option<&'static $ValueType> {
cssparser_internal__invoke_proc_macro! {
cssparser_internal__phf_map!( ($ValueType) $( $key ($value) )+ )
}

{
cssparser_internal__invoke_proc_macro! {
cssparser_internal__max_len!( $( $key )+ )
}
// MAX_LENGTH is generated by cssparser_internal__max_len
cssparser_internal__to_lowercase!(input, MAX_LENGTH => lowercase);
lowercase.and_then(|s| MAP.get(s))
// This dummy module works around a feature gate,
// see comment on the similar module in `match_ignore_ascii_case!` above.
mod cssparser_internal {
use $crate::_internal__phf::{Map, phf_map};
#[allow(unused)] use super::*;
cssparser_internal__max_len!( $( $key )+ );
cssparser_internal__phf_map!( $ValueType $( $key $value )+ );
}
cssparser_internal__to_lowercase!(input, cssparser_internal::MAX_LENGTH => lowercase);
lowercase.and_then(|s| cssparser_internal::MAP.get(s))
}
}
}
Expand Down