Skip to content

Add support for attributes on match arms in match_ignore_ascii_case! #265

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 2 commits into from
Oct 22, 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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cssparser"
version = "0.27.0"
version = "0.27.1"
authors = [ "Simon Sapin <[email protected]>" ]

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

[dependencies]
cssparser-macros = {path = "./macros", version = "0.5"}
cssparser-macros = {path = "./macros", version = "0.6"}
dtoa-short = "0.3"
itoa = "0.4"
matches = "0.1"
Expand Down
2 changes: 1 addition & 1 deletion macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cssparser-macros"
version = "0.5.0"
version = "0.6.0"
authors = ["Simon Sapin <[email protected]>"]
description = "Procedural macros for cssparser"
documentation = "https://docs.rs/cssparser-macros/"
Expand Down
59 changes: 1 addition & 58 deletions macros/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ extern crate proc_macro;

use proc_macro::TokenStream;

/// Implementation detail of the `match_ignore_ascii_case!` macro
#[allow(non_snake_case)]
#[proc_macro]
pub fn cssparser_internal__match_ignore_ascii_case__support(input: TokenStream) -> TokenStream {
pub fn _cssparser_internal_max_len(input: TokenStream) -> TokenStream {
struct Input {
max_length: usize,
}
Expand Down Expand Up @@ -39,58 +37,3 @@ pub fn cssparser_internal__match_ignore_ascii_case__support(input: TokenStream)
)
.into()
}

/// Implementation detail of the `ascii_case_insensitive_phf_map!` macro
#[allow(non_snake_case)]
#[proc_macro]
pub fn cssparser_internal__ascii_case_insensitive_phf_map__support(
input: TokenStream,
) -> TokenStream {
struct Input {
value_type: syn::Type,
max_key_length: usize,
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 value_type = input.parse()?;
let mut max_key_length = 0;
let mut keys = Vec::new();
let mut values = Vec::new();
while !input.is_empty() {
let key: syn::LitStr = input.parse()?;
let key_value = key.value();
max_key_length = max_key_length.max(key_value.len());
keys.push(syn::LitStr::new(
&key_value.to_ascii_lowercase(),
key.span(),
));
values.push(input.parse()?);
}
Ok(Input {
value_type,
max_key_length,
keys,
values,
})
}
}

let Input {
value_type,
max_key_length,
keys,
values,
} = syn::parse_macro_input!(input);
quote::quote!(
pub(super) const MAX_LENGTH: usize = #max_key_length;
pub(super) static MAP: Map<&'static str, #value_type> = phf_map! {
#(
#keys => #values,
)*
};
)
.into()
}
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub use crate::color::{
pub use crate::cow_rc_str::CowRcStr;
pub use crate::from_bytes::{stylesheet_encoding, EncodingSupport};
#[doc(hidden)]
pub use crate::macros::_internal__to_lowercase;
pub use crate::macros::_cssparser_internal_to_lowercase;
pub use crate::nth::parse_nth;
pub use crate::parser::{BasicParseError, BasicParseErrorKind, ParseError, ParseErrorKind};
pub use crate::parser::{Delimiter, Delimiters, Parser, ParserInput, ParserState};
Expand All @@ -87,7 +87,7 @@ pub use crate::tokenizer::{SourceLocation, SourcePosition, Token};
pub use crate::unicode_range::UnicodeRange;
pub use cssparser_macros::*;
#[doc(hidden)]
pub use phf as _internal__phf;
pub use phf as _cssparser_internal_phf;

#[macro_use]
mod macros;
Expand Down
33 changes: 20 additions & 13 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::mem::MaybeUninit;
/// # fn dummy(function_name: &String) { let _ =
/// match_ignore_ascii_case! { &function_name,
/// "rgb" => parse_rgb(..),
/// # #[cfg(not(something))]
/// "rgba" => parse_rgba(..),
/// "hsl" => parse_hsl(..),
/// "hsla" => parse_hsla(..),
Expand All @@ -35,6 +36,7 @@ use std::mem::MaybeUninit;
macro_rules! match_ignore_ascii_case {
( $input:expr,
$(
$( #[$meta: meta] )*
$( $pattern: pat )|+ $( if $guard: expr )? => $then: expr
),+
$(,)?
Expand All @@ -46,15 +48,16 @@ macro_rules! match_ignore_ascii_case {
// rather than expression/statement context,
// even though the macro only expands to items.
mod cssparser_internal {
$crate::cssparser_internal__match_ignore_ascii_case__support! {
$crate::_cssparser_internal_max_len! {
$( $( $pattern )+ )+
}
}
cssparser_internal__to_lowercase!($input, cssparser_internal::MAX_LENGTH => lowercase);
_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") {
$(
$( #[$meta] )*
$( $pattern )|+ $( if $guard )? => $then,
)+
}
Expand Down Expand Up @@ -95,15 +98,19 @@ macro_rules! ascii_case_insensitive_phf_map {
pub fn $name(input: &str) -> Option<&'static $ValueType> {
// 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::*;
$crate::cssparser_internal__ascii_case_insensitive_phf_map__support! {
$ValueType $( $key $value )+
mod _cssparser_internal {
$crate::_cssparser_internal_max_len! {
$( $key )+
}
}
cssparser_internal__to_lowercase!(input, cssparser_internal::MAX_LENGTH => lowercase);
lowercase.and_then(|s| cssparser_internal::MAP.get(s))
use $crate::_cssparser_internal_phf as phf;
static MAP: phf::Map<&'static str, $ValueType> = phf::phf_map! {
$(
$key => $value,
)*
};
_cssparser_internal_to_lowercase!(input, _cssparser_internal::MAX_LENGTH => lowercase);
lowercase.and_then(|s| MAP.get(s))
}
}
}
Expand All @@ -113,19 +120,19 @@ macro_rules! ascii_case_insensitive_phf_map {
/// **This macro is not part of the public API. It can change or be removed between any versions.**
///
/// Define a local variable named `$output`
/// and assign it the result of calling `_internal__to_lowercase`
/// and assign it the result of calling `_cssparser_internal_to_lowercase`
/// with a stack-allocated buffer of length `$BUFFER_SIZE`.
#[macro_export]
#[doc(hidden)]
macro_rules! cssparser_internal__to_lowercase {
macro_rules! _cssparser_internal_to_lowercase {
($input: expr, $BUFFER_SIZE: expr => $output: ident) => {
#[allow(unsafe_code)]
let mut buffer = unsafe {
::std::mem::MaybeUninit::<[::std::mem::MaybeUninit<u8>; $BUFFER_SIZE]>::uninit()
.assume_init()
};
let input: &str = $input;
let $output = $crate::_internal__to_lowercase(&mut buffer, input);
let $output = $crate::_cssparser_internal_to_lowercase(&mut buffer, input);
};
}

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