Skip to content

Commit f936495

Browse files
committed
Use const fn rather than proc_macro for MAX_LENGTH
Signed-off-by: Nico Burns <nico@nicoburns.com>
1 parent 5f517e7 commit f936495

5 files changed

Lines changed: 41 additions & 51 deletions

File tree

macros/lib.rs

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,6 @@ extern crate proc_macro;
66

77
use proc_macro::TokenStream;
88

9-
#[proc_macro]
10-
pub fn _cssparser_internal_max_len(input: TokenStream) -> TokenStream {
11-
struct Input {
12-
max_length: usize,
13-
}
14-
15-
impl syn::parse::Parse for Input {
16-
fn parse(input: syn::parse::ParseStream) -> syn::parse::Result<Self> {
17-
let mut max_length = 0;
18-
while !input.is_empty() {
19-
if input.peek(syn::Token![_]) {
20-
input.parse::<syn::Token![_]>().unwrap();
21-
continue;
22-
}
23-
let lit: syn::LitStr = input.parse()?;
24-
let value = lit.value();
25-
if value.to_ascii_lowercase() != value {
26-
return Err(syn::Error::new(lit.span(), "must be ASCII-lowercase"));
27-
}
28-
max_length = max_length.max(value.len());
29-
}
30-
Ok(Input { max_length })
31-
}
32-
}
33-
34-
let Input { max_length } = syn::parse_macro_input!(input);
35-
quote::quote!(
36-
pub(super) const MAX_LENGTH: usize = #max_length;
37-
)
38-
.into()
39-
}
40-
419
fn get_byte_from_lit(lit: &syn::Lit) -> u8 {
4210
if let syn::Lit::Byte(ref byte) = *lit {
4311
byte.value()

src/color.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ impl PredefinedColorSpace {
114114
"prophoto-rgb" => Self::ProphotoRgb,
115115
"rec2020" => Self::Rec2020,
116116
"xyz-d50" => Self::XyzD50,
117-
"xyz" | "xyz-d65" => Self::XyzD65,
117+
"xyz" => Self::XyzD65,
118+
"xyz-d65" => Self::XyzD65,
118119
_ => return Err(location.new_basic_unexpected_token_error(Token::Ident(ident.clone()))),
119120
})
120121
}

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ pub use crate::serializer::{serialize_identifier, serialize_name, serialize_stri
8484
pub use crate::serializer::{CssStringWriter, ToCss, TokenSerializationType};
8585
pub use crate::tokenizer::{SourceLocation, SourcePosition, Token};
8686
pub use crate::unicode_range::UnicodeRange;
87-
pub use cssparser_macros::_cssparser_internal_max_len;
8887

8988
#[cfg(feature = "fast_match_byte")]
9089
pub use cssparser_macros::match_byte;

src/macros.rs

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,40 @@ macro_rules! match_ignore_ascii_case {
3434
( $input:expr,
3535
$(
3636
$( #[$meta: meta] )*
37-
$( $pattern: pat )|+ $( if $guard: expr )? => $then: expr
37+
$pattern: literal $( if $guard: expr )? => $then: expr
3838
),+
39+
$(,_ => $fallback:expr)?
3940
$(,)?
4041
) => {
4142
{
42-
// This dummy module works around the feature gate
43-
// `error[E0658]: procedural macros cannot be expanded to statements`
44-
// by forcing the macro to be in an item context
45-
// rather than expression/statement context,
46-
// even though the macro only expands to items.
47-
mod cssparser_internal {
48-
$crate::_cssparser_internal_max_len! {
49-
$( $( $pattern )+ )+
43+
#[inline(always)]
44+
const fn const_usize_max(a: usize, b: usize) -> usize {
45+
if a > b {
46+
a
47+
} else {
48+
b
5049
}
5150
}
52-
$crate::_cssparser_internal_to_lowercase!($input, cssparser_internal::MAX_LENGTH => lowercase);
51+
52+
const MAX_LENGTH : usize = {
53+
let mut maxlen : usize = 0;
54+
$(
55+
$( #[$meta] )*
56+
// {} is necessary to work around "[E0658]: attributes on expressions are experimental"
57+
{ maxlen = const_usize_max(maxlen, $pattern.len()); }
58+
)+
59+
maxlen
60+
};
61+
62+
$crate::_cssparser_internal_to_lowercase!($input, MAX_LENGTH => lowercase);
5363
// "A" is a short string that we know is different for every string pattern,
5464
// since we’ve verified that none of them include ASCII upper case letters.
5565
match lowercase.unwrap_or("A") {
5666
$(
5767
$( #[$meta] )*
58-
$( $pattern )|+ $( if $guard )? => $then,
68+
$pattern $( if $guard )? => $then,
5969
)+
70+
$(_ => $fallback,)?
6071
}
6172
}
6273
};
@@ -95,13 +106,24 @@ macro_rules! ascii_case_insensitive_phf_map {
95106
($name: ident -> $ValueType: ty = { $( $key: tt => $value: expr, )+ }) => {
96107
use $crate::_cssparser_internal_phf as phf;
97108

98-
// See macro above for context.
99-
mod cssparser_internal {
100-
$crate::_cssparser_internal_max_len! {
101-
$( $key )+
109+
#[inline(always)]
110+
const fn const_usize_max(a: usize, b: usize) -> usize {
111+
if a > b {
112+
a
113+
} else {
114+
b
102115
}
103116
}
104117

118+
const MAX_LENGTH : usize = {
119+
let mut maxlen : usize = 0;
120+
$(
121+
// {} is necessary to work around "[E0658]: attributes on expressions are experimental"
122+
{ maxlen = const_usize_max(maxlen, ($key).len()); }
123+
)+
124+
maxlen
125+
};
126+
105127
static MAP: phf::Map<&'static str, $ValueType> = phf::phf_map! {
106128
$(
107129
$key => $value,
@@ -122,7 +144,7 @@ macro_rules! ascii_case_insensitive_phf_map {
122144
}
123145

124146
fn get(input: &str) -> Option<&'static $ValueType> {
125-
$crate::_cssparser_internal_to_lowercase!(input, cssparser_internal::MAX_LENGTH => lowercase);
147+
$crate::_cssparser_internal_to_lowercase!(input, MAX_LENGTH => lowercase);
126148
MAP.get(lowercase?)
127149
}
128150
}

src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1333,7 +1333,7 @@ fn utf16_columns() {
13331333
#[test]
13341334
fn servo_define_css_keyword_enum() {
13351335
macro_rules! define_css_keyword_enum {
1336-
(pub enum $name:ident { $($variant:ident = $css:pat,)+ }) => {
1336+
(pub enum $name:ident { $($variant:ident = $css:literal,)+ }) => {
13371337
#[derive(PartialEq, Debug)]
13381338
pub enum $name {
13391339
$($variant),+

0 commit comments

Comments
 (0)