Skip to content
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

Make match_ignore_ascii_case! future-proof. #91

Merged
merged 1 commit into from
Jan 19, 2016
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "cssparser"
version = "0.4.0"
version = "0.5.0"
authors = [ "Simon Sapin <simon.sapin@exyr.org>" ]

description = "Rust implementation of CSS Syntax Level 3"
Expand Down
4 changes: 2 additions & 2 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ pub fn parse_color_keyword(ident: &str) -> Result<Color, ()> {
"yellowgreen" => rgb(154., 205., 50.),

"transparent" => Ok(Color::RGBA(RGBA { red: 0., green: 0., blue: 0., alpha: 0. })),
"currentcolor" => Ok(Color::CurrentColor)
"currentcolor" => Ok(Color::CurrentColor),
_ => Err(())
}
}
Expand Down Expand Up @@ -312,7 +312,7 @@ fn parse_color_function(name: &str, arguments: &mut Parser) -> Result<Color, ()>
"rgba" => (true, true),
"rgb" => (true, false),
"hsl" => (false, false),
"hsla" => (false, true)
"hsla" => (false, true),
_ => return Err(())
};

Expand Down
26 changes: 20 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ fn parse_border_spacing(_context: &ParserContext, input: &mut Parser)

*/

#![recursion_limit="200"] // For color::parse_color_keyword

extern crate encoding;
#[macro_use] extern crate matches;
#[cfg(test)] extern crate tempdir;
Expand Down Expand Up @@ -99,21 +101,29 @@ Usage example:
match_ignore_ascii_case! { string,
"foo" => Some(Foo),
"bar" => Some(Bar),
"baz" => Some(Baz)
"baz" => Some(Baz),
_ => None
}
```

The macro also takes a slice of the value,
so that a `String` or `CowString` could be passed directly instead of a `&str`.

Note that because of `macro_rules` ambiguity resolutions quirks,
each arm except the fallback and the one before it must end with a comma.

*/
#[macro_export]
macro_rules! match_ignore_ascii_case {
( $value: expr, $( $string: expr => $result: expr ),+ _ => $fallback: expr ) => {
// parse the last case plus the fallback
(@inner $value:expr, ($string:expr => $result:expr, _ => $fallback:expr) -> ($($parsed:tt)*) ) => {
match_ignore_ascii_case!(@inner $value, () -> ($($parsed)* ($string => $result)) $fallback)
};

// parse a case (not the last one)
(@inner $value:expr, ($string:expr => $result:expr, $($rest:tt)*) -> ($($parsed:tt)*) ) => {
match_ignore_ascii_case!(@inner $value, ($($rest)*) -> ($($parsed)* ($string => $result)))
};

// finished parsing
(@inner $value:expr, () -> ($(($string:expr => $result:expr))*) $fallback:expr ) => {
{
use std::ascii::AsciiExt;
match &$value[..] {
Expand All @@ -124,8 +134,12 @@ macro_rules! match_ignore_ascii_case {
}
}
};
}

// entry point, start parsing
( $value:expr, $($rest:tt)* ) => {
match_ignore_ascii_case!(@inner $value, ($($rest)*) -> ())
};
}

mod rules_and_declarations;
mod tokenizer;
Expand Down
6 changes: 3 additions & 3 deletions src/nth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn parse_nth(input: &mut Parser) -> Result<(i32, i32), ()> {
let a = try!(value.int_value.ok_or(())) as i32;
match_ignore_ascii_case! { unit,
"n" => parse_b(input, a),
"n-" => parse_signless_b(input, a, -1)
"n-" => parse_signless_b(input, a, -1),
_ => Ok((a, try!(parse_n_dash_digits(&*unit))))
}
}
Expand All @@ -29,7 +29,7 @@ pub fn parse_nth(input: &mut Parser) -> Result<(i32, i32), ()> {
"n" => parse_b(input, 1),
"-n" => parse_b(input, -1),
"n-" => parse_signless_b(input, 1, -1),
"-n-" => parse_signless_b(input, -1, -1)
"-n-" => parse_signless_b(input, -1, -1),
_ => if value.starts_with("-") {
Ok((-1, try!(parse_n_dash_digits(&value[1..]))))
} else {
Expand All @@ -41,7 +41,7 @@ pub fn parse_nth(input: &mut Parser) -> Result<(i32, i32), ()> {
Token::Ident(value) => {
match_ignore_ascii_case! { value,
"n" => parse_b(input, 1),
"n-" => parse_signless_b(input, 1, -1)
"n-" => parse_signless_b(input, 1, -1),
_ => Ok((1, try!(parse_n_dash_digits(&*value))))
}
}
Expand Down