Skip to content

Commit 65d6c3e

Browse files
author
bors-servo
authored
Auto merge of #265 - servo:those-without-syn, r=nox
Add support for attributes on match arms in match_ignore_ascii_case!
2 parents 5188d5e + d4af92c commit 65d6c3e

File tree

5 files changed

+26
-76
lines changed

5 files changed

+26
-76
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cssparser"
3-
version = "0.27.0"
3+
version = "0.27.1"
44
authors = [ "Simon Sapin <[email protected]>" ]
55

66
description = "Rust implementation of CSS Syntax Level 3"
@@ -20,7 +20,7 @@ difference = "2.0"
2020
encoding_rs = "0.8"
2121

2222
[dependencies]
23-
cssparser-macros = {path = "./macros", version = "0.5"}
23+
cssparser-macros = {path = "./macros", version = "0.6"}
2424
dtoa-short = "0.3"
2525
itoa = "0.4"
2626
matches = "0.1"

macros/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cssparser-macros"
3-
version = "0.5.0"
3+
version = "0.6.0"
44
authors = ["Simon Sapin <[email protected]>"]
55
description = "Procedural macros for cssparser"
66
documentation = "https://docs.rs/cssparser-macros/"

macros/lib.rs

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ extern crate proc_macro;
66

77
use proc_macro::TokenStream;
88

9-
/// Implementation detail of the `match_ignore_ascii_case!` macro
10-
#[allow(non_snake_case)]
119
#[proc_macro]
12-
pub fn cssparser_internal__match_ignore_ascii_case__support(input: TokenStream) -> TokenStream {
10+
pub fn _cssparser_internal_max_len(input: TokenStream) -> TokenStream {
1311
struct Input {
1412
max_length: usize,
1513
}
@@ -39,58 +37,3 @@ pub fn cssparser_internal__match_ignore_ascii_case__support(input: TokenStream)
3937
)
4038
.into()
4139
}
42-
43-
/// Implementation detail of the `ascii_case_insensitive_phf_map!` macro
44-
#[allow(non_snake_case)]
45-
#[proc_macro]
46-
pub fn cssparser_internal__ascii_case_insensitive_phf_map__support(
47-
input: TokenStream,
48-
) -> TokenStream {
49-
struct Input {
50-
value_type: syn::Type,
51-
max_key_length: usize,
52-
keys: Vec<syn::LitStr>,
53-
values: Vec<syn::Expr>,
54-
}
55-
56-
impl syn::parse::Parse for Input {
57-
fn parse(input: syn::parse::ParseStream) -> syn::parse::Result<Self> {
58-
let value_type = input.parse()?;
59-
let mut max_key_length = 0;
60-
let mut keys = Vec::new();
61-
let mut values = Vec::new();
62-
while !input.is_empty() {
63-
let key: syn::LitStr = input.parse()?;
64-
let key_value = key.value();
65-
max_key_length = max_key_length.max(key_value.len());
66-
keys.push(syn::LitStr::new(
67-
&key_value.to_ascii_lowercase(),
68-
key.span(),
69-
));
70-
values.push(input.parse()?);
71-
}
72-
Ok(Input {
73-
value_type,
74-
max_key_length,
75-
keys,
76-
values,
77-
})
78-
}
79-
}
80-
81-
let Input {
82-
value_type,
83-
max_key_length,
84-
keys,
85-
values,
86-
} = syn::parse_macro_input!(input);
87-
quote::quote!(
88-
pub(super) const MAX_LENGTH: usize = #max_key_length;
89-
pub(super) static MAP: Map<&'static str, #value_type> = phf_map! {
90-
#(
91-
#keys => #values,
92-
)*
93-
};
94-
)
95-
.into()
96-
}

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub use crate::color::{
7373
pub use crate::cow_rc_str::CowRcStr;
7474
pub use crate::from_bytes::{stylesheet_encoding, EncodingSupport};
7575
#[doc(hidden)]
76-
pub use crate::macros::_internal__to_lowercase;
76+
pub use crate::macros::_cssparser_internal_to_lowercase;
7777
pub use crate::nth::parse_nth;
7878
pub use crate::parser::{BasicParseError, BasicParseErrorKind, ParseError, ParseErrorKind};
7979
pub use crate::parser::{Delimiter, Delimiters, Parser, ParserInput, ParserState};
@@ -87,7 +87,7 @@ pub use crate::tokenizer::{SourceLocation, SourcePosition, Token};
8787
pub use crate::unicode_range::UnicodeRange;
8888
pub use cssparser_macros::*;
8989
#[doc(hidden)]
90-
pub use phf as _internal__phf;
90+
pub use phf as _cssparser_internal_phf;
9191

9292
#[macro_use]
9393
mod macros;

src/macros.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use std::mem::MaybeUninit;
1919
/// # fn dummy(function_name: &String) { let _ =
2020
/// match_ignore_ascii_case! { &function_name,
2121
/// "rgb" => parse_rgb(..),
22+
/// # #[cfg(not(something))]
2223
/// "rgba" => parse_rgba(..),
2324
/// "hsl" => parse_hsl(..),
2425
/// "hsla" => parse_hsla(..),
@@ -35,6 +36,7 @@ use std::mem::MaybeUninit;
3536
macro_rules! match_ignore_ascii_case {
3637
( $input:expr,
3738
$(
39+
$( #[$meta: meta] )*
3840
$( $pattern: pat )|+ $( if $guard: expr )? => $then: expr
3941
),+
4042
$(,)?
@@ -46,15 +48,16 @@ macro_rules! match_ignore_ascii_case {
4648
// rather than expression/statement context,
4749
// even though the macro only expands to items.
4850
mod cssparser_internal {
49-
$crate::cssparser_internal__match_ignore_ascii_case__support! {
51+
$crate::_cssparser_internal_max_len! {
5052
$( $( $pattern )+ )+
5153
}
5254
}
53-
cssparser_internal__to_lowercase!($input, cssparser_internal::MAX_LENGTH => lowercase);
55+
_cssparser_internal_to_lowercase!($input, cssparser_internal::MAX_LENGTH => lowercase);
5456
// "A" is a short string that we know is different for every string pattern,
5557
// since we’ve verified that none of them include ASCII upper case letters.
5658
match lowercase.unwrap_or("A") {
5759
$(
60+
$( #[$meta] )*
5861
$( $pattern )|+ $( if $guard )? => $then,
5962
)+
6063
}
@@ -95,15 +98,19 @@ macro_rules! ascii_case_insensitive_phf_map {
9598
pub fn $name(input: &str) -> Option<&'static $ValueType> {
9699
// This dummy module works around a feature gate,
97100
// see comment on the similar module in `match_ignore_ascii_case!` above.
98-
mod cssparser_internal {
99-
use $crate::_internal__phf::{Map, phf_map};
100-
#[allow(unused)] use super::*;
101-
$crate::cssparser_internal__ascii_case_insensitive_phf_map__support! {
102-
$ValueType $( $key $value )+
101+
mod _cssparser_internal {
102+
$crate::_cssparser_internal_max_len! {
103+
$( $key )+
103104
}
104105
}
105-
cssparser_internal__to_lowercase!(input, cssparser_internal::MAX_LENGTH => lowercase);
106-
lowercase.and_then(|s| cssparser_internal::MAP.get(s))
106+
use $crate::_cssparser_internal_phf as phf;
107+
static MAP: phf::Map<&'static str, $ValueType> = phf::phf_map! {
108+
$(
109+
$key => $value,
110+
)*
111+
};
112+
_cssparser_internal_to_lowercase!(input, _cssparser_internal::MAX_LENGTH => lowercase);
113+
lowercase.and_then(|s| MAP.get(s))
107114
}
108115
}
109116
}
@@ -113,19 +120,19 @@ macro_rules! ascii_case_insensitive_phf_map {
113120
/// **This macro is not part of the public API. It can change or be removed between any versions.**
114121
///
115122
/// Define a local variable named `$output`
116-
/// and assign it the result of calling `_internal__to_lowercase`
123+
/// and assign it the result of calling `_cssparser_internal_to_lowercase`
117124
/// with a stack-allocated buffer of length `$BUFFER_SIZE`.
118125
#[macro_export]
119126
#[doc(hidden)]
120-
macro_rules! cssparser_internal__to_lowercase {
127+
macro_rules! _cssparser_internal_to_lowercase {
121128
($input: expr, $BUFFER_SIZE: expr => $output: ident) => {
122129
#[allow(unsafe_code)]
123130
let mut buffer = unsafe {
124131
::std::mem::MaybeUninit::<[::std::mem::MaybeUninit<u8>; $BUFFER_SIZE]>::uninit()
125132
.assume_init()
126133
};
127134
let input: &str = $input;
128-
let $output = $crate::_internal__to_lowercase(&mut buffer, input);
135+
let $output = $crate::_cssparser_internal_to_lowercase(&mut buffer, input);
129136
};
130137
}
131138

@@ -137,7 +144,7 @@ macro_rules! cssparser_internal__to_lowercase {
137144
/// Otherwise, return `input` ASCII-lowercased, using `buffer` as temporary space if necessary.
138145
#[doc(hidden)]
139146
#[allow(non_snake_case)]
140-
pub fn _internal__to_lowercase<'a>(
147+
pub fn _cssparser_internal_to_lowercase<'a>(
141148
buffer: &'a mut [MaybeUninit<u8>],
142149
input: &'a str,
143150
) -> Option<&'a str> {

0 commit comments

Comments
 (0)