Skip to content

Commit 0c78da0

Browse files
committedJan 19, 2016
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
1 parent b844896 commit 0c78da0

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed
 

‎Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22

33
name = "cssparser"
4-
version = "0.4.0"
4+
version = "0.5.0"
55
authors = [ "Simon Sapin <simon.sapin@exyr.org>" ]
66

77
description = "Rust implementation of CSS Syntax Level 3"

‎src/color.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ pub fn parse_color_keyword(ident: &str) -> Result<Color, ()> {
258258
"yellowgreen" => rgb(154., 205., 50.),
259259

260260
"transparent" => Ok(Color::RGBA(RGBA { red: 0., green: 0., blue: 0., alpha: 0. })),
261-
"currentcolor" => Ok(Color::CurrentColor)
261+
"currentcolor" => Ok(Color::CurrentColor),
262262
_ => Err(())
263263
}
264264
}
@@ -312,7 +312,7 @@ fn parse_color_function(name: &str, arguments: &mut Parser) -> Result<Color, ()>
312312
"rgba" => (true, true),
313313
"rgb" => (true, false),
314314
"hsl" => (false, false),
315-
"hsla" => (false, true)
315+
"hsla" => (false, true),
316316
_ => return Err(())
317317
};
318318

‎src/lib.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ fn parse_border_spacing(_context: &ParserContext, input: &mut Parser)
6969
7070
*/
7171

72+
#![recursion_limit="200"] // For color::parse_color_keyword
73+
7274
extern crate encoding;
7375
#[macro_use] extern crate matches;
7476
#[cfg(test)] extern crate tempdir;
@@ -99,21 +101,29 @@ Usage example:
99101
match_ignore_ascii_case! { string,
100102
"foo" => Some(Foo),
101103
"bar" => Some(Bar),
102-
"baz" => Some(Baz)
104+
"baz" => Some(Baz),
103105
_ => None
104106
}
105107
```
106108
107109
The macro also takes a slice of the value,
108110
so that a `String` or `CowString` could be passed directly instead of a `&str`.
109111
110-
Note that because of `macro_rules` ambiguity resolutions quirks,
111-
each arm except the fallback and the one before it must end with a comma.
112-
113112
*/
114113
#[macro_export]
115114
macro_rules! match_ignore_ascii_case {
116-
( $value: expr, $( $string: expr => $result: expr ),+ _ => $fallback: expr ) => {
115+
// parse the last case plus the fallback
116+
(@inner $value:expr, ($string:expr => $result:expr, _ => $fallback:expr) -> ($($parsed:tt)*) ) => {
117+
match_ignore_ascii_case!(@inner $value, () -> ($($parsed)* ($string => $result)) $fallback)
118+
};
119+
120+
// parse a case (not the last one)
121+
(@inner $value:expr, ($string:expr => $result:expr, $($rest:tt)*) -> ($($parsed:tt)*) ) => {
122+
match_ignore_ascii_case!(@inner $value, ($($rest)*) -> ($($parsed)* ($string => $result)))
123+
};
124+
125+
// finished parsing
126+
(@inner $value:expr, () -> ($(($string:expr => $result:expr))*) $fallback:expr ) => {
117127
{
118128
use std::ascii::AsciiExt;
119129
match &$value[..] {
@@ -124,8 +134,12 @@ macro_rules! match_ignore_ascii_case {
124134
}
125135
}
126136
};
127-
}
128137

138+
// entry point, start parsing
139+
( $value:expr, $($rest:tt)* ) => {
140+
match_ignore_ascii_case!(@inner $value, ($($rest)*) -> ())
141+
};
142+
}
129143

130144
mod rules_and_declarations;
131145
mod tokenizer;

‎src/nth.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub fn parse_nth(input: &mut Parser) -> Result<(i32, i32), ()> {
1818
let a = try!(value.int_value.ok_or(())) as i32;
1919
match_ignore_ascii_case! { unit,
2020
"n" => parse_b(input, a),
21-
"n-" => parse_signless_b(input, a, -1)
21+
"n-" => parse_signless_b(input, a, -1),
2222
_ => Ok((a, try!(parse_n_dash_digits(&*unit))))
2323
}
2424
}
@@ -29,7 +29,7 @@ pub fn parse_nth(input: &mut Parser) -> Result<(i32, i32), ()> {
2929
"n" => parse_b(input, 1),
3030
"-n" => parse_b(input, -1),
3131
"n-" => parse_signless_b(input, 1, -1),
32-
"-n-" => parse_signless_b(input, -1, -1)
32+
"-n-" => parse_signless_b(input, -1, -1),
3333
_ => if value.starts_with("-") {
3434
Ok((-1, try!(parse_n_dash_digits(&value[1..]))))
3535
} else {
@@ -41,7 +41,7 @@ pub fn parse_nth(input: &mut Parser) -> Result<(i32, i32), ()> {
4141
Token::Ident(value) => {
4242
match_ignore_ascii_case! { value,
4343
"n" => parse_b(input, 1),
44-
"n-" => parse_signless_b(input, 1, -1)
44+
"n-" => parse_signless_b(input, 1, -1),
4545
_ => Ok((1, try!(parse_n_dash_digits(&*value))))
4646
}
4747
}

0 commit comments

Comments
 (0)
Please sign in to comment.