Skip to content

Commit

Permalink
Auto merge of #91 - servo:macro, r=SimonSapin
Browse files Browse the repository at this point in the history
Make match_ignore_ascii_case! future-proof.

Change the usage syntax to require a comma on the last non-fallback arm, which is a [breaking-change].

The old definition has started to emit a warning in recent nightlies and is likely to be an error in future versions: rust-lang/rfcs#1384

The new definition is recursive to resolve ambiguities. Unfortunately this makes error reporting terrible: rust-lang/rust#31022

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/rust-cssparser/91)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo committed Jan 19, 2016
2 parents b844896 + 0c78da0 commit 6b46e72
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
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

0 comments on commit 6b46e72

Please sign in to comment.