Skip to content

Commit 40d86b0

Browse files
committed
Make parse error types extensible.
1 parent 04bef8c commit 40d86b0

File tree

8 files changed

+240
-170
lines changed

8 files changed

+240
-170
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.13.5"
4+
version = "0.14.0"
55
authors = [ "Simon Sapin <simon.sapin@exyr.org>" ]
66

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

src/color.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use std::fmt;
66
use std::f32::consts::PI;
77

8-
use super::{Token, Parser, ToCss, ParseError};
8+
use super::{Token, Parser, ToCss, ParseError, BasicParseError};
99
use tokenizer::NumericValue;
1010

1111
#[cfg(feature = "serde")]
@@ -141,7 +141,7 @@ impl Color {
141141
/// Parse a <color> value, per CSS Color Module Level 3.
142142
///
143143
/// FIXME(#2) Deprecated CSS2 System Colors are not supported yet.
144-
pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Color, ParseError<'i>> {
144+
pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Color, BasicParseError<'i>> {
145145
let token = try!(input.next());
146146
match token {
147147
Token::Hash(ref value) | Token::IDHash(ref value) => {
@@ -151,10 +151,11 @@ impl Color {
151151
Token::Function(ref name) => {
152152
return input.parse_nested_block(|arguments| {
153153
parse_color_function(&*name, arguments)
154-
});
154+
.map_err(|e| ParseError::Basic(e))
155+
}).map_err(ParseError::<()>::basic);
155156
}
156157
_ => Err(())
157-
}.map_err(|()| ParseError::UnexpectedToken(token))
158+
}.map_err(|()| BasicParseError::UnexpectedToken(token))
158159
}
159160

160161
/// Parse a color hash, without the leading '#' character.
@@ -411,11 +412,11 @@ fn clamp_floor_256_f32(val: f32) -> u8 {
411412
}
412413

413414
#[inline]
414-
fn parse_color_function<'i, 't>(name: &str, arguments: &mut Parser<'i, 't>) -> Result<Color, ParseError<'i>> {
415+
fn parse_color_function<'i, 't>(name: &str, arguments: &mut Parser<'i, 't>) -> Result<Color, BasicParseError<'i>> {
415416
let (red, green, blue, uses_commas) = match_ignore_ascii_case! { name,
416417
"rgb" | "rgba" => parse_rgb_components_rgb(arguments)?,
417418
"hsl" | "hsla" => parse_rgb_components_hsl(arguments)?,
418-
_ => return Err(ParseError::UnexpectedToken(Token::Ident(name.to_owned().into()))),
419+
_ => return Err(BasicParseError::UnexpectedToken(Token::Ident(name.to_owned().into()))),
419420
};
420421

421422
let alpha = if !arguments.is_exhausted() {
@@ -424,7 +425,7 @@ fn parse_color_function<'i, 't>(name: &str, arguments: &mut Parser<'i, 't>) -> R
424425
} else {
425426
match try!(arguments.next()) {
426427
Token::Delim('/') => {},
427-
t => return Err(ParseError::UnexpectedToken(t)),
428+
t => return Err(BasicParseError::UnexpectedToken(t)),
428429
};
429430
};
430431
let token = try!(arguments.next());
@@ -436,7 +437,7 @@ fn parse_color_function<'i, 't>(name: &str, arguments: &mut Parser<'i, 't>) -> R
436437
clamp_unit_f32(v.unit_value)
437438
}
438439
t => {
439-
return Err(ParseError::UnexpectedToken(t))
440+
return Err(BasicParseError::UnexpectedToken(t))
440441
}
441442
}
442443
} else {
@@ -449,7 +450,7 @@ fn parse_color_function<'i, 't>(name: &str, arguments: &mut Parser<'i, 't>) -> R
449450

450451

451452
#[inline]
452-
fn parse_rgb_components_rgb<'i, 't>(arguments: &mut Parser<'i, 't>) -> Result<(u8, u8, u8, bool), ParseError<'i>> {
453+
fn parse_rgb_components_rgb<'i, 't>(arguments: &mut Parser<'i, 't>) -> Result<(u8, u8, u8, bool), BasicParseError<'i>> {
453454
let red: u8;
454455
let green: u8;
455456
let blue: u8;
@@ -466,7 +467,7 @@ fn parse_rgb_components_rgb<'i, 't>(arguments: &mut Parser<'i, 't>) -> Result<(u
466467
uses_commas = true;
467468
try!(arguments.expect_number())
468469
}
469-
t => return Err(ParseError::UnexpectedToken(t))
470+
t => return Err(BasicParseError::UnexpectedToken(t))
470471
});
471472
if uses_commas {
472473
try!(arguments.expect_comma());
@@ -481,20 +482,20 @@ fn parse_rgb_components_rgb<'i, 't>(arguments: &mut Parser<'i, 't>) -> Result<(u
481482
uses_commas = true;
482483
try!(arguments.expect_percentage())
483484
}
484-
t => return Err(ParseError::UnexpectedToken(t))
485+
t => return Err(BasicParseError::UnexpectedToken(t))
485486
});
486487
if uses_commas {
487488
try!(arguments.expect_comma());
488489
}
489490
blue = clamp_unit_f32(try!(arguments.expect_percentage()));
490491
}
491-
t => return Err(ParseError::UnexpectedToken(t))
492+
t => return Err(BasicParseError::UnexpectedToken(t))
492493
};
493494
return Ok((red, green, blue, uses_commas));
494495
}
495496

496497
#[inline]
497-
fn parse_rgb_components_hsl<'i, 't>(arguments: &mut Parser<'i, 't>) -> Result<(u8, u8, u8, bool), ParseError<'i>> {
498+
fn parse_rgb_components_hsl<'i, 't>(arguments: &mut Parser<'i, 't>) -> Result<(u8, u8, u8, bool), BasicParseError<'i>> {
498499
let mut uses_commas = false;
499500
// Hue given as an angle
500501
// https://drafts.csswg.org/css-values/#angles
@@ -510,9 +511,9 @@ fn parse_rgb_components_hsl<'i, 't>(arguments: &mut Parser<'i, 't>) -> Result<(u
510511
_ => Err(()),
511512
}
512513
}
513-
t => return Err(ParseError::UnexpectedToken(t))
514+
t => return Err(BasicParseError::UnexpectedToken(t))
514515
};
515-
let hue_degrees = try!(hue_degrees.map_err(|()| ParseError::UnexpectedToken(token)));
516+
let hue_degrees = try!(hue_degrees.map_err(|()| BasicParseError::UnexpectedToken(token)));
516517
// Subtract an integer before rounding, to avoid some rounding errors:
517518
let hue_normalized_degrees = hue_degrees - 360. * (hue_degrees / 360.).floor();
518519
let hue = hue_normalized_degrees / 360.;
@@ -525,7 +526,7 @@ fn parse_rgb_components_hsl<'i, 't>(arguments: &mut Parser<'i, 't>) -> Result<(u
525526
uses_commas = true;
526527
try!(arguments.expect_percentage())
527528
}
528-
t => return Err(ParseError::UnexpectedToken(t))
529+
t => return Err(BasicParseError::UnexpectedToken(t))
529530
};
530531
let saturation = saturation.max(0.).min(1.);
531532

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub use from_bytes::{stylesheet_encoding, EncodingSupport};
8989
pub use color::{RGBA, Color, parse_color_keyword};
9090
pub use nth::parse_nth;
9191
pub use serializer::{ToCss, CssStringWriter, serialize_identifier, serialize_string, TokenSerializationType};
92-
pub use parser::{Parser, Delimiter, Delimiters, SourcePosition, ParseError};
92+
pub use parser::{Parser, Delimiter, Delimiters, SourcePosition, ParseError, BasicParseError};
9393
pub use unicode_range::UnicodeRange;
9494

9595
// For macros

src/nth.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
use std::ascii::AsciiExt;
66

7-
use super::{Token, Parser, ParseError};
7+
use super::{Token, Parser, BasicParseError};
88

99

1010
/// Parse the *An+B* notation, as found in the `:nth-child()` selector.
1111
/// The input is typically the arguments of a function,
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.
14-
pub fn parse_nth<'i, 't>(input: &mut Parser<'i, 't>) -> Result<(i32, i32), ParseError<'i>> {
14+
pub fn parse_nth<'i, 't>(input: &mut Parser<'i, 't>) -> Result<(i32, i32), BasicParseError<'i>> {
1515
let token = try!(input.next());
1616
match token {
1717
Token::Number(ref value) => {
@@ -59,14 +59,14 @@ pub fn parse_nth<'i, 't>(input: &mut Parser<'i, 't>) -> Result<(i32, i32), Parse
5959
_ => parse_n_dash_digits(&*value).map(|v| (1, v))
6060
}
6161
}
62-
t => return Err(ParseError::UnexpectedToken(t)),
62+
t => return Err(BasicParseError::UnexpectedToken(t)),
6363
},
6464
_ => Err(()),
65-
}.map_err(|()| ParseError::UnexpectedToken(token))
65+
}.map_err(|()| BasicParseError::UnexpectedToken(token))
6666
}
6767

6868

69-
fn parse_b<'i, 't>(input: &mut Parser<'i, 't>, a: i32) -> Result<(i32, i32), ParseError<'i>> {
69+
fn parse_b<'i, 't>(input: &mut Parser<'i, 't>, a: i32) -> Result<(i32, i32), BasicParseError<'i>> {
7070
let start_position = input.position();
7171
let token = input.next();
7272
match token {
@@ -82,10 +82,10 @@ fn parse_b<'i, 't>(input: &mut Parser<'i, 't>, a: i32) -> Result<(i32, i32), Par
8282
input.reset(start_position);
8383
Ok((a, 0))
8484
}
85-
}.map_err(|()| ParseError::UnexpectedToken(token.unwrap()))
85+
}.map_err(|()| BasicParseError::UnexpectedToken(token.unwrap()))
8686
}
8787

88-
fn parse_signless_b<'i, 't>(input: &mut Parser<'i, 't>, a: i32, b_sign: i32) -> Result<(i32, i32), ParseError<'i>> {
88+
fn parse_signless_b<'i, 't>(input: &mut Parser<'i, 't>, a: i32, b_sign: i32) -> Result<(i32, i32), BasicParseError<'i>> {
8989
let token = try!(input.next());
9090
match token {
9191
Token::Number(ref value) if !value.has_sign => {
@@ -95,7 +95,7 @@ fn parse_signless_b<'i, 't>(input: &mut Parser<'i, 't>, a: i32, b_sign: i32) ->
9595
}
9696
}
9797
_ => Err(())
98-
}.map_err(|()| ParseError::UnexpectedToken(token))
98+
}.map_err(|()| BasicParseError::UnexpectedToken(token))
9999
}
100100

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

0 commit comments

Comments
 (0)