Skip to content

Commit 3a25a6a

Browse files
committed
Reduce size of errors.
This improves performance significantly without costing too much in error reporting ability. Reviewed upstream in https://phabricator.services.mozilla.com/D320989 This is of course a breaking change, but it also allows most parsing routines to have simpler signatures.
1 parent a49dafa commit 3a25a6a

11 files changed

Lines changed: 355 additions & 388 deletions

File tree

color/lib.rs

Lines changed: 47 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,13 @@ where
4242

4343
/// Parse a CSS color using the specified [`ColorParser`] and return a new color
4444
/// value on success.
45-
pub fn parse_color_with<'i, 't, P>(
45+
pub fn parse_color_with<'i, P>(
4646
color_parser: &P,
47-
input: &mut Parser<'i, 't>,
48-
) -> Result<P::Output, ParseError<'i, P::Error>>
47+
input: &mut Parser<'i, '_>,
48+
) -> Result<P::Output, ParseError<P::Error>>
4949
where
5050
P: ColorParser<'i>,
5151
{
52-
let location = input.current_source_location();
5352
let token = input.next()?;
5453
match *token {
5554
Token::Hash(ref value) | Token::IDHash(ref value) => {
@@ -64,16 +63,16 @@ where
6463
}
6564
_ => Err(()),
6665
}
67-
.map_err(|()| location.new_unexpected_token_error(token.clone()))
66+
.map_err(|()| ParseError::unexpected_token())
6867
}
6968

7069
/// Parse one of the color functions: rgba(), lab(), color(), etc.
7170
#[inline]
72-
fn parse_color_function<'i, 't, P>(
71+
fn parse_color_function<'i, P>(
7372
color_parser: &P,
7473
name: CowRcStr<'i>,
75-
arguments: &mut Parser<'i, 't>,
76-
) -> Result<P::Output, ParseError<'i, P::Error>>
74+
arguments: &mut Parser,
75+
) -> Result<P::Output, ParseError<P::Error>>
7776
where
7877
P: ColorParser<'i>,
7978
{
@@ -102,7 +101,7 @@ where
102101

103102
"color" => parse_color_with_color_space(color_parser, arguments),
104103

105-
_ => return Err(arguments.new_unexpected_token_error(Token::Ident(name))),
104+
_ => return Err(ParseError::unexpected_token()),
106105
}?;
107106

108107
arguments.expect_exhausted()?;
@@ -115,8 +114,8 @@ where
115114
#[inline]
116115
fn parse_alpha_component<'i, 't, P>(
117116
color_parser: &P,
118-
arguments: &mut Parser<'i, 't>,
119-
) -> Result<f32, ParseError<'i, P::Error>>
117+
arguments: &mut Parser,
118+
) -> Result<f32, ParseError<P::Error>>
120119
where
121120
P: ColorParser<'i>,
122121
{
@@ -128,8 +127,8 @@ where
128127

129128
fn parse_legacy_alpha<'i, 't, P>(
130129
color_parser: &P,
131-
arguments: &mut Parser<'i, 't>,
132-
) -> Result<f32, ParseError<'i, P::Error>>
130+
arguments: &mut Parser,
131+
) -> Result<f32, ParseError<P::Error>>
133132
where
134133
P: ColorParser<'i>,
135134
{
@@ -143,8 +142,8 @@ where
143142

144143
fn parse_modern_alpha<'i, 't, P>(
145144
color_parser: &P,
146-
arguments: &mut Parser<'i, 't>,
147-
) -> Result<Option<f32>, ParseError<'i, P::Error>>
145+
arguments: &mut Parser,
146+
) -> Result<Option<f32>, ParseError<P::Error>>
148147
where
149148
P: ColorParser<'i>,
150149
{
@@ -159,8 +158,8 @@ where
159158
#[inline]
160159
fn parse_rgb<'i, 't, P>(
161160
color_parser: &P,
162-
arguments: &mut Parser<'i, 't>,
163-
) -> Result<P::Output, ParseError<'i, P::Error>>
161+
arguments: &mut Parser,
162+
) -> Result<P::Output, ParseError<P::Error>>
164163
where
165164
P: ColorParser<'i>,
166165
{
@@ -225,8 +224,8 @@ where
225224
#[inline]
226225
fn parse_hsl<'i, 't, P>(
227226
color_parser: &P,
228-
arguments: &mut Parser<'i, 't>,
229-
) -> Result<P::Output, ParseError<'i, P::Error>>
227+
arguments: &mut Parser,
228+
) -> Result<P::Output, ParseError<P::Error>>
230229
where
231230
P: ColorParser<'i>,
232231
{
@@ -264,8 +263,8 @@ where
264263
#[inline]
265264
fn parse_hwb<'i, 't, P>(
266265
color_parser: &P,
267-
arguments: &mut Parser<'i, 't>,
268-
) -> Result<P::Output, ParseError<'i, P::Error>>
266+
arguments: &mut Parser,
267+
) -> Result<P::Output, ParseError<P::Error>>
269268
where
270269
P: ColorParser<'i>,
271270
{
@@ -343,11 +342,11 @@ type IntoColorFn<Output> =
343342
#[inline]
344343
fn parse_lab_like<'i, 't, P>(
345344
color_parser: &P,
346-
arguments: &mut Parser<'i, 't>,
345+
arguments: &mut Parser,
347346
lightness_range: f32,
348347
a_b_range: f32,
349348
into_color: IntoColorFn<P::Output>,
350-
) -> Result<P::Output, ParseError<'i, P::Error>>
349+
) -> Result<P::Output, ParseError<P::Error>>
351350
where
352351
P: ColorParser<'i>,
353352
{
@@ -369,11 +368,11 @@ where
369368
#[inline]
370369
fn parse_lch_like<'i, 't, P>(
371370
color_parser: &P,
372-
arguments: &mut Parser<'i, 't>,
371+
arguments: &mut Parser,
373372
lightness_range: f32,
374373
chroma_range: f32,
375374
into_color: IntoColorFn<P::Output>,
376-
) -> Result<P::Output, ParseError<'i, P::Error>>
375+
) -> Result<P::Output, ParseError<P::Error>>
377376
where
378377
P: ColorParser<'i>,
379378
{
@@ -396,8 +395,8 @@ where
396395
#[inline]
397396
fn parse_color_with_color_space<'i, 't, P>(
398397
color_parser: &P,
399-
arguments: &mut Parser<'i, 't>,
400-
) -> Result<P::Output, ParseError<'i, P::Error>>
398+
arguments: &mut Parser,
399+
) -> Result<P::Output, ParseError<P::Error>>
401400
where
402401
P: ColorParser<'i>,
403402
{
@@ -424,22 +423,22 @@ where
424423
))
425424
}
426425

427-
type ComponentParseResult<'i, R1, R2, R3, Error> =
428-
Result<(Option<R1>, Option<R2>, Option<R3>, Option<f32>), ParseError<'i, Error>>;
426+
type ComponentParseResult<R1, R2, R3, Error> =
427+
Result<(Option<R1>, Option<R2>, Option<R3>, Option<f32>), ParseError<Error>>;
429428

430429
/// Parse the color components and alpha with the modern [color-4] syntax.
431430
pub fn parse_components<'i, 't, P, F1, F2, F3, R1, R2, R3>(
432431
color_parser: &P,
433-
input: &mut Parser<'i, 't>,
432+
input: &mut Parser,
434433
f1: F1,
435434
f2: F2,
436435
f3: F3,
437-
) -> ComponentParseResult<'i, R1, R2, R3, P::Error>
436+
) -> ComponentParseResult<R1, R2, R3, P::Error>
438437
where
439438
P: ColorParser<'i>,
440-
F1: FnOnce(&P, &mut Parser<'i, 't>) -> Result<R1, ParseError<'i, P::Error>>,
441-
F2: FnOnce(&P, &mut Parser<'i, 't>) -> Result<R2, ParseError<'i, P::Error>>,
442-
F3: FnOnce(&P, &mut Parser<'i, 't>) -> Result<R3, ParseError<'i, P::Error>>,
439+
F1: FnOnce(&P, &mut Parser) -> Result<R1, ParseError<P::Error>>,
440+
F2: FnOnce(&P, &mut Parser) -> Result<R2, ParseError<P::Error>>,
441+
F3: FnOnce(&P, &mut Parser) -> Result<R3, ParseError<P::Error>>,
443442
{
444443
let r1 = parse_none_or(input, |p| f1(color_parser, p))?;
445444
let r2 = parse_none_or(input, |p| f2(color_parser, p))?;
@@ -450,9 +449,9 @@ where
450449
Ok((r1, r2, r3, alpha))
451450
}
452451

453-
fn parse_none_or<'i, 't, F, T, E>(input: &mut Parser<'i, 't>, thing: F) -> Result<Option<T>, E>
452+
fn parse_none_or<'i, 't, F, T, E>(input: &mut Parser, thing: F) -> Result<Option<T>, E>
454453
where
455-
F: FnOnce(&mut Parser<'i, 't>) -> Result<T, E>,
454+
F: FnOnce(&mut Parser) -> Result<T, E>,
456455
{
457456
match input.try_parse(|p| p.expect_ident_matching("none")) {
458457
Ok(_) => Ok(None),
@@ -980,11 +979,10 @@ pub trait ColorParser<'i> {
980979
/// Parse an `<angle>` or `<number>`.
981980
///
982981
/// Returns the result in degrees.
983-
fn parse_angle_or_number<'t>(
982+
fn parse_angle_or_number(
984983
&self,
985-
input: &mut Parser<'i, 't>,
986-
) -> Result<AngleOrNumber, ParseError<'i, Self::Error>> {
987-
let location = input.current_source_location();
984+
input: &mut Parser,
985+
) -> Result<AngleOrNumber, ParseError<Self::Error>> {
988986
Ok(match *input.next()? {
989987
Token::Number { value, .. } => AngleOrNumber::Number { value },
990988
Token::Dimension {
@@ -995,45 +993,36 @@ pub trait ColorParser<'i> {
995993
"grad" => v * 360. / 400.,
996994
"rad" => v * 360. / (2. * PI),
997995
"turn" => v * 360.,
998-
_ => {
999-
return Err(location.new_unexpected_token_error(Token::Ident(unit.clone())))
1000-
}
996+
_ => return Err(ParseError::unexpected_token()),
1001997
};
1002998

1003999
AngleOrNumber::Angle { degrees }
10041000
}
1005-
ref t => return Err(location.new_unexpected_token_error(t.clone())),
1001+
_ => return Err(ParseError::unexpected_token()),
10061002
})
10071003
}
10081004

10091005
/// Parse a `<percentage>` value.
10101006
///
10111007
/// Returns the result in a number from 0.0 to 1.0.
1012-
fn parse_percentage<'t>(
1013-
&self,
1014-
input: &mut Parser<'i, 't>,
1015-
) -> Result<f32, ParseError<'i, Self::Error>> {
1008+
fn parse_percentage(&self, input: &mut Parser) -> Result<f32, ParseError<Self::Error>> {
10161009
input.expect_percentage().map_err(From::from)
10171010
}
10181011

10191012
/// Parse a `<number>` value.
1020-
fn parse_number<'t>(
1021-
&self,
1022-
input: &mut Parser<'i, 't>,
1023-
) -> Result<f32, ParseError<'i, Self::Error>> {
1013+
fn parse_number(&self, input: &mut Parser) -> Result<f32, ParseError<Self::Error>> {
10241014
input.expect_number().map_err(From::from)
10251015
}
10261016

10271017
/// Parse a `<number>` value or a `<percentage>` value.
1028-
fn parse_number_or_percentage<'t>(
1018+
fn parse_number_or_percentage(
10291019
&self,
1030-
input: &mut Parser<'i, 't>,
1031-
) -> Result<NumberOrPercentage, ParseError<'i, Self::Error>> {
1032-
let location = input.current_source_location();
1020+
input: &mut Parser,
1021+
) -> Result<NumberOrPercentage, ParseError<Self::Error>> {
10331022
Ok(match *input.next()? {
10341023
Token::Number { value, .. } => NumberOrPercentage::Number { value },
10351024
Token::Percentage { unit_value, .. } => NumberOrPercentage::Percentage { unit_value },
1036-
ref t => return Err(location.new_unexpected_token_error(t.clone())),
1025+
_ => return Err(ParseError::unexpected_token()),
10371026
})
10381027
}
10391028
}
@@ -1050,7 +1039,7 @@ impl Color {
10501039
/// Parse a <color> value, per CSS Color Module Level 3.
10511040
///
10521041
/// FIXME(#2) Deprecated CSS2 System Colors are not supported yet.
1053-
pub fn parse<'i>(input: &mut Parser<'i, '_>) -> Result<Color, ParseError<'i, ()>> {
1042+
pub fn parse(input: &mut Parser) -> Result<Color, ParseError<()>> {
10541043
parse_color_with(&DefaultColorParser, input)
10551044
}
10561045
}

fuzz/fuzz_targets/cssparser.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn parse_and_serialize(input: &str, preserving_comments: bool) -> String {
2222
}
2323

2424
fn do_parse_and_serialize<'i>(
25-
input: &mut Parser<'i, '_>,
25+
input: &mut Parser,
2626
preserving_comments: bool,
2727
mut previous_token_type: TokenSerializationType,
2828
serialization: &mut String,
@@ -46,7 +46,7 @@ fn do_parse_and_serialize<'i>(
4646
}
4747
if token.is_parse_error() {
4848
let token = token.clone();
49-
return Err(input.new_unexpected_token_error(token))
49+
return Err(input.new_unexpected_token_error(token));
5050
}
5151
let token_type = token.serialization_type();
5252
if previous_token_type.needs_separator_when_before(token_type) {
@@ -62,7 +62,13 @@ fn do_parse_and_serialize<'i>(
6262
};
6363

6464
input.parse_nested_block(|input| -> Result<_, ParseError<()>> {
65-
do_parse_and_serialize(input, preserving_comments, previous_token_type, serialization, indent_level + 1)
65+
do_parse_and_serialize(
66+
input,
67+
preserving_comments,
68+
previous_token_type,
69+
serialization,
70+
indent_level + 1,
71+
)
6672
})?;
6773

6874
closing_token.to_css(serialization).unwrap();

src/color.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/// The opaque alpha value of 1.0.
1515
pub const OPAQUE: f32 = 1.0;
1616

17-
use crate::{BasicParseError, Parser, ToCss, Token};
17+
use crate::{BasicParseError, Parser, ToCss};
1818
use std::fmt;
1919

2020
/// Clamp a 0..1 number to a 0..255 range to u8.
@@ -101,9 +101,7 @@ pub enum PredefinedColorSpace {
101101

102102
impl PredefinedColorSpace {
103103
/// Parse a PredefinedColorSpace from the given input.
104-
pub fn parse<'i>(input: &mut Parser<'i, '_>) -> Result<Self, BasicParseError<'i>> {
105-
let location = input.current_source_location();
106-
104+
pub fn parse(input: &mut Parser) -> Result<Self, BasicParseError> {
107105
let ident = input.expect_ident()?;
108106
Ok(match_ignore_ascii_case! { ident,
109107
"srgb" => Self::Srgb,
@@ -115,7 +113,7 @@ impl PredefinedColorSpace {
115113
"rec2020" => Self::Rec2020,
116114
"xyz-d50" => Self::XyzD50,
117115
"xyz" | "xyz-d65" => Self::XyzD65,
118-
_ => return Err(location.new_basic_unexpected_token_error(Token::Ident(ident.clone()))),
116+
_ => return Err(BasicParseError::unexpected_token()),
119117
})
120118
}
121119
}

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ fn parse_border_spacing(_context: &ParserContext, input: &mut Parser)
7070
pub use crate::cow_rc_str::CowRcStr;
7171
pub use crate::from_bytes::{stylesheet_encoding, EncodingSupport};
7272
#[doc(hidden)]
73-
pub use crate::macros::_cssparser_internal_to_lowercase;
73+
pub use crate::macros::{
74+
_cssparser_internal_create_uninit_array, _cssparser_internal_to_lowercase,
75+
};
7476
pub use crate::nth::parse_nth;
7577
pub use crate::parser::{BasicParseError, BasicParseErrorKind, ParseError, ParseErrorKind};
7678
pub use crate::parser::{Delimiter, Delimiters, Parser, ParserInput, ParserState};

0 commit comments

Comments
 (0)