Skip to content

Commit a79e536

Browse files
committed
Make Parser::next return &Token instead of Token
1 parent 4705393 commit a79e536

File tree

6 files changed

+233
-226
lines changed

6 files changed

+233
-226
lines changed

src/color.rs

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ impl Color {
141141
///
142142
/// FIXME(#2) Deprecated CSS2 System Colors are not supported yet.
143143
pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Color, BasicParseError<'i>> {
144-
let token = input.next()?;
144+
let token = input.next()?.clone();
145145
match token {
146146
Token::Hash(ref value) | Token::IDHash(ref value) => {
147147
Color::parse_hash(value.as_bytes())
@@ -154,7 +154,7 @@ impl Color {
154154
}).map_err(ParseError::<()>::basic);
155155
}
156156
_ => Err(())
157-
}.map_err(|()| BasicParseError::UnexpectedToken(token))
157+
}.map_err(|()| BasicParseError::UnexpectedToken(token.clone()))
158158
}
159159

160160
/// Parse a color hash, without the leading '#' character.
@@ -422,21 +422,17 @@ fn parse_color_function<'i, 't>(name: &str, arguments: &mut Parser<'i, 't>) -> R
422422
if uses_commas {
423423
arguments.expect_comma()?;
424424
} else {
425-
match arguments.next()? {
426-
Token::Delim('/') => {},
427-
t => return Err(BasicParseError::UnexpectedToken(t)),
428-
};
425+
arguments.expect_delim('/')?;
429426
};
430-
let token = arguments.next()?;
431-
match token {
427+
match *arguments.next()? {
432428
Token::Number { value: v, .. } => {
433429
clamp_unit_f32(v)
434430
}
435431
Token::Percentage { unit_value: v, .. } => {
436432
clamp_unit_f32(v)
437433
}
438-
t => {
439-
return Err(BasicParseError::UnexpectedToken(t))
434+
ref t => {
435+
return Err(BasicParseError::UnexpectedToken(t.clone()))
440436
}
441437
}
442438
} else {
@@ -457,10 +453,10 @@ fn parse_rgb_components_rgb<'i, 't>(arguments: &mut Parser<'i, 't>) -> Result<(u
457453

458454
// Either integers or percentages, but all the same type.
459455
// https://drafts.csswg.org/css-color/#rgb-functions
460-
match arguments.next()? {
456+
match arguments.next()?.clone() {
461457
Token::Number { value: v, .. } => {
462458
red = clamp_floor_256_f32(v);
463-
green = clamp_floor_256_f32(match arguments.next()? {
459+
green = clamp_floor_256_f32(match arguments.next()?.clone() {
464460
Token::Number { value: v, .. } => v,
465461
Token::Comma => {
466462
uses_commas = true;
@@ -475,7 +471,7 @@ fn parse_rgb_components_rgb<'i, 't>(arguments: &mut Parser<'i, 't>) -> Result<(u
475471
}
476472
Token::Percentage { unit_value, .. } => {
477473
red = clamp_unit_f32(unit_value);
478-
green = clamp_unit_f32(match arguments.next()? {
474+
green = clamp_unit_f32(match arguments.next()?.clone() {
479475
Token::Percentage { unit_value, .. } => unit_value,
480476
Token::Comma => {
481477
uses_commas = true;
@@ -498,28 +494,26 @@ fn parse_rgb_components_hsl<'i, 't>(arguments: &mut Parser<'i, 't>) -> Result<(u
498494
let mut uses_commas = false;
499495
// Hue given as an angle
500496
// https://drafts.csswg.org/css-values/#angles
501-
let token = arguments.next()?;
502-
let hue_degrees = match token {
503-
Token::Number { value: v, .. } => Ok(v),
497+
let hue_degrees = match *arguments.next()? {
498+
Token::Number { value: v, .. } => v,
504499
Token::Dimension { value: v, ref unit, .. } => {
505500
match_ignore_ascii_case! { &*unit,
506-
"deg" => Ok(v),
507-
"grad" => Ok(v * 360. / 400.),
508-
"rad" => Ok(v * 360. / (2. * PI)),
509-
"turn" => Ok(v * 360.),
510-
_ => Err(()),
501+
"deg" => v,
502+
"grad" => v * 360. / 400.,
503+
"rad" => v * 360. / (2. * PI),
504+
"turn" => v * 360.,
505+
_ => return Err(BasicParseError::UnexpectedToken(Token::Ident(unit.clone()))),
511506
}
512507
}
513-
t => return Err(BasicParseError::UnexpectedToken(t))
508+
ref t => return Err(BasicParseError::UnexpectedToken(t.clone()))
514509
};
515-
let hue_degrees = hue_degrees.map_err(|()| BasicParseError::UnexpectedToken(token))?;
516510
// Subtract an integer before rounding, to avoid some rounding errors:
517511
let hue_normalized_degrees = hue_degrees - 360. * (hue_degrees / 360.).floor();
518512
let hue = hue_normalized_degrees / 360.;
519513

520514
// Saturation and lightness are clamped to 0% ... 100%
521515
// https://drafts.csswg.org/css-color/#the-hsl-notation
522-
let saturation = match arguments.next()? {
516+
let saturation = match arguments.next()?.clone() {
523517
Token::Percentage { unit_value, .. } => unit_value,
524518
Token::Comma => {
525519
uses_commas = true;

src/nth.rs

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,71 +12,80 @@ use super::{Token, Parser, BasicParseError};
1212
/// in which case the caller needs to check if the arguments’ parser is exhausted.
1313
/// Return `Ok((A, B))`, or `Err(())` for a syntax error.
1414
pub fn parse_nth<'i, 't>(input: &mut Parser<'i, 't>) -> Result<(i32, i32), BasicParseError<'i>> {
15-
let token = input.next()?;
16-
match token {
15+
// FIXME: remove .clone() when lifetimes are non-lexical.
16+
match input.next()?.clone() {
1717
Token::Number { int_value: Some(b), .. } => {
1818
Ok((0, b))
1919
}
20-
Token::Dimension { int_value: Some(a), ref unit, .. } => {
20+
Token::Dimension { int_value: Some(a), unit, .. } => {
2121
match_ignore_ascii_case! {
2222
&unit,
2323
"n" => Ok(try!(parse_b(input, a))),
2424
"n-" => Ok(try!(parse_signless_b(input, a, -1))),
25-
_ => {
26-
parse_n_dash_digits(&*unit).map(|val| (a, val))
25+
_ => match parse_n_dash_digits(&*unit) {
26+
Ok(b) => Ok((a, b)),
27+
Err(()) => Err(BasicParseError::UnexpectedToken(Token::Ident(unit.clone())))
2728
}
2829
}
2930
}
30-
Token::Ident(ref value) => {
31+
Token::Ident(value) => {
3132
match_ignore_ascii_case! { &value,
3233
"even" => Ok((2, 0)),
3334
"odd" => Ok((2, 1)),
3435
"n" => Ok(try!(parse_b(input, 1))),
3536
"-n" => Ok(try!(parse_b(input, -1))),
3637
"n-" => Ok(try!(parse_signless_b(input, 1, -1))),
3738
"-n-" => Ok(try!(parse_signless_b(input, -1, -1))),
38-
_ => if value.starts_with("-") {
39-
parse_n_dash_digits(&value[1..]).map(|v| (-1, v))
40-
} else {
41-
parse_n_dash_digits(&*value).map(|v| (1, v))
39+
_ => {
40+
let (slice, a) = if value.starts_with("-") {
41+
(&value[1..], -1)
42+
} else {
43+
(&*value, 1)
44+
};
45+
match parse_n_dash_digits(slice) {
46+
Ok(b) => Ok((a, b)),
47+
Err(()) => Err(BasicParseError::UnexpectedToken(Token::Ident(value.clone())))
48+
}
4249
}
4350
}
4451
}
45-
Token::Delim('+') => match input.next_including_whitespace()? {
52+
// FIXME: remove .clone() when lifetimes are non-lexical.
53+
Token::Delim('+') => match input.next_including_whitespace()?.clone() {
4654
Token::Ident(value) => {
4755
match_ignore_ascii_case! { &value,
48-
"n" => Ok(try!(parse_b(input, 1))),
49-
"n-" => Ok(try!(parse_signless_b(input, 1, -1))),
50-
_ => parse_n_dash_digits(&*value).map(|v| (1, v))
56+
"n" => parse_b(input, 1),
57+
"n-" => parse_signless_b(input, 1, -1),
58+
_ => match parse_n_dash_digits(&*value) {
59+
Ok(b) => Ok((1, b)),
60+
Err(()) => Err(BasicParseError::UnexpectedToken(Token::Ident(value.clone())))
61+
}
5162
}
5263
}
53-
t => return Err(BasicParseError::UnexpectedToken(t)),
64+
token => Err(BasicParseError::UnexpectedToken(token)),
5465
},
55-
_ => Err(()),
56-
}.map_err(|()| BasicParseError::UnexpectedToken(token))
66+
token => Err(BasicParseError::UnexpectedToken(token)),
67+
}
5768
}
5869

5970

6071
fn parse_b<'i, 't>(input: &mut Parser<'i, 't>, a: i32) -> Result<(i32, i32), BasicParseError<'i>> {
6172
let start_position = input.position();
62-
let token = input.next();
63-
match token {
64-
Ok(Token::Delim('+')) => Ok(parse_signless_b(input, a, 1)?),
65-
Ok(Token::Delim('-')) => Ok(parse_signless_b(input, a, -1)?),
66-
Ok(Token::Number { has_sign: true, int_value: Some(b), .. }) => Ok((a, b)),
73+
match input.next() {
74+
Ok(&Token::Delim('+')) => parse_signless_b(input, a, 1),
75+
Ok(&Token::Delim('-')) => parse_signless_b(input, a, -1),
76+
Ok(&Token::Number { has_sign: true, int_value: Some(b), .. }) => Ok((a, b)),
6777
_ => {
6878
input.reset(start_position);
6979
Ok((a, 0))
7080
}
71-
}.map_err(|()| BasicParseError::UnexpectedToken(token.unwrap()))
81+
}
7282
}
7383

7484
fn parse_signless_b<'i, 't>(input: &mut Parser<'i, 't>, a: i32, b_sign: i32) -> Result<(i32, i32), BasicParseError<'i>> {
75-
let token = input.next()?;
76-
match token {
85+
match *input.next()? {
7786
Token::Number { has_sign: false, int_value: Some(b), .. } => Ok((a, b_sign * b)),
78-
_ => Err(())
79-
}.map_err(|()| BasicParseError::UnexpectedToken(token))
87+
ref token => Err(BasicParseError::UnexpectedToken(token.clone()))
88+
}
8089
}
8190

8291
fn parse_n_dash_digits(string: &str) -> Result<i32, ()> {

0 commit comments

Comments
 (0)