1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use std::ascii::AsciiExt;
use super::{Token, Parser, BasicParseError};
pub fn parse_nth<'i, 't>(input: &mut Parser<'i, 't>) -> Result<(i32, i32), BasicParseError<'i>> {
let token = input.next()?;
match token {
Token::Number { int_value: Some(b), .. } => {
Ok((0, b))
}
Token::Dimension { int_value: Some(a), ref unit, .. } => {
match_ignore_ascii_case! {
&unit,
"n" => Ok(try!(parse_b(input, a))),
"n-" => Ok(try!(parse_signless_b(input, a, -1))),
_ => {
parse_n_dash_digits(&*unit).map(|val| (a, val))
}
}
}
Token::Ident(ref value) => {
match_ignore_ascii_case! { &value,
"even" => Ok((2, 0)),
"odd" => Ok((2, 1)),
"n" => Ok(try!(parse_b(input, 1))),
"-n" => Ok(try!(parse_b(input, -1))),
"n-" => Ok(try!(parse_signless_b(input, 1, -1))),
"-n-" => Ok(try!(parse_signless_b(input, -1, -1))),
_ => if value.starts_with("-") {
parse_n_dash_digits(&value[1..]).map(|v| (-1, v))
} else {
parse_n_dash_digits(&*value).map(|v| (1, v))
}
}
}
Token::Delim('+') => match input.next_including_whitespace()? {
Token::Ident(value) => {
match_ignore_ascii_case! { &value,
"n" => Ok(try!(parse_b(input, 1))),
"n-" => Ok(try!(parse_signless_b(input, 1, -1))),
_ => parse_n_dash_digits(&*value).map(|v| (1, v))
}
}
t => return Err(BasicParseError::UnexpectedToken(t)),
},
_ => Err(()),
}.map_err(|()| BasicParseError::UnexpectedToken(token))
}
fn parse_b<'i, 't>(input: &mut Parser<'i, 't>, a: i32) -> Result<(i32, i32), BasicParseError<'i>> {
let start_position = input.position();
let token = input.next();
match token {
Ok(Token::Delim('+')) => Ok(parse_signless_b(input, a, 1)?),
Ok(Token::Delim('-')) => Ok(parse_signless_b(input, a, -1)?),
Ok(Token::Number { has_sign: true, int_value: Some(b), .. }) => Ok((a, b)),
_ => {
input.reset(start_position);
Ok((a, 0))
}
}.map_err(|()| BasicParseError::UnexpectedToken(token.unwrap()))
}
fn parse_signless_b<'i, 't>(input: &mut Parser<'i, 't>, a: i32, b_sign: i32) -> Result<(i32, i32), BasicParseError<'i>> {
let token = input.next()?;
match token {
Token::Number { has_sign: false, int_value: Some(b), .. } => Ok((a, b_sign * b)),
_ => Err(())
}.map_err(|()| BasicParseError::UnexpectedToken(token))
}
fn parse_n_dash_digits(string: &str) -> Result<i32, ()> {
if string.len() >= 3
&& string[..2].eq_ignore_ascii_case("n-")
&& string[2..].chars().all(|c| matches!(c, '0'...'9'))
{
Ok(string[1..].parse().unwrap())
} else {
Err(())
}
}