@@ -150,6 +150,7 @@ impl Color {
150
150
/// FIXME(#2) Deprecated CSS2 System Colors are not supported yet.
151
151
pub fn parse < ' i , ' t > ( input : & mut Parser < ' i , ' t > ) -> Result < Color , BasicParseError < ' i > > {
152
152
// FIXME: remove clone() when lifetimes are non-lexical
153
+ let location = input. current_source_location ( ) ;
153
154
let token = input. next ( ) ?. clone ( ) ;
154
155
match token {
155
156
Token :: Hash ( ref value) | Token :: IDHash ( ref value) => {
@@ -158,12 +159,11 @@ impl Color {
158
159
Token :: Ident ( ref value) => parse_color_keyword ( & * value) ,
159
160
Token :: Function ( ref name) => {
160
161
return input. parse_nested_block ( |arguments| {
161
- parse_color_function ( & * name, arguments)
162
- . map_err ( |e| ParseError :: Basic ( e) )
162
+ parse_color_function ( & * name, arguments) . map_err ( |e| e. into ( ) )
163
163
} ) . map_err ( ParseError :: < ( ) > :: basic) ;
164
164
}
165
165
_ => Err ( ( ) )
166
- } . map_err ( |( ) | BasicParseError :: UnexpectedToken ( token) )
166
+ } . map_err ( |( ) | location . new_basic_unexpected_token_error ( token) )
167
167
}
168
168
169
169
/// Parse a color hash, without the leading '#' character.
@@ -424,7 +424,7 @@ fn parse_color_function<'i, 't>(name: &str, arguments: &mut Parser<'i, 't>) -> R
424
424
let ( red, green, blue, uses_commas) = match_ignore_ascii_case ! { name,
425
425
"rgb" | "rgba" => parse_rgb_components_rgb( arguments) ?,
426
426
"hsl" | "hsla" => parse_rgb_components_hsl( arguments) ?,
427
- _ => return Err ( BasicParseError :: UnexpectedToken ( Token :: Ident ( name. to_owned( ) . into( ) ) ) ) ,
427
+ _ => return Err ( arguments . new_basic_unexpected_token_error ( Token :: Ident ( name. to_owned( ) . into( ) ) ) ) ,
428
428
} ;
429
429
430
430
let alpha = if !arguments. is_exhausted ( ) {
@@ -433,6 +433,7 @@ fn parse_color_function<'i, 't>(name: &str, arguments: &mut Parser<'i, 't>) -> R
433
433
} else {
434
434
arguments. expect_delim ( '/' ) ?;
435
435
} ;
436
+ let location = arguments. current_source_location ( ) ;
436
437
match * arguments. next ( ) ? {
437
438
Token :: Number { value : v, .. } => {
438
439
clamp_unit_f32 ( v)
@@ -441,7 +442,7 @@ fn parse_color_function<'i, 't>(name: &str, arguments: &mut Parser<'i, 't>) -> R
441
442
clamp_unit_f32 ( v)
442
443
}
443
444
ref t => {
444
- return Err ( BasicParseError :: UnexpectedToken ( t. clone ( ) ) )
445
+ return Err ( location . new_basic_unexpected_token_error ( t. clone ( ) ) )
445
446
}
446
447
}
447
448
} else {
@@ -462,6 +463,8 @@ fn parse_rgb_components_rgb<'i, 't>(arguments: &mut Parser<'i, 't>) -> Result<(u
462
463
463
464
// Either integers or percentages, but all the same type.
464
465
// https://drafts.csswg.org/css-color/#rgb-functions
466
+ // FIXME: remove .clone() when lifetimes are non-lexical.
467
+ let location = arguments. current_source_location ( ) ;
465
468
match arguments. next ( ) ?. clone ( ) {
466
469
Token :: Number { value : v, .. } => {
467
470
red = clamp_floor_256_f32 ( v) ;
@@ -471,7 +474,7 @@ fn parse_rgb_components_rgb<'i, 't>(arguments: &mut Parser<'i, 't>) -> Result<(u
471
474
uses_commas = true ;
472
475
arguments. expect_number ( ) ?
473
476
}
474
- t => return Err ( BasicParseError :: UnexpectedToken ( t) )
477
+ t => return Err ( location . new_basic_unexpected_token_error ( t) )
475
478
} ) ;
476
479
if uses_commas {
477
480
arguments. expect_comma ( ) ?;
@@ -486,14 +489,14 @@ fn parse_rgb_components_rgb<'i, 't>(arguments: &mut Parser<'i, 't>) -> Result<(u
486
489
uses_commas = true ;
487
490
arguments. expect_percentage ( ) ?
488
491
}
489
- t => return Err ( BasicParseError :: UnexpectedToken ( t) )
492
+ t => return Err ( location . new_basic_unexpected_token_error ( t) )
490
493
} ) ;
491
494
if uses_commas {
492
495
arguments. expect_comma ( ) ?;
493
496
}
494
497
blue = clamp_unit_f32 ( arguments. expect_percentage ( ) ?) ;
495
498
}
496
- t => return Err ( BasicParseError :: UnexpectedToken ( t) )
499
+ t => return Err ( location . new_basic_unexpected_token_error ( t) )
497
500
} ;
498
501
return Ok ( ( red, green, blue, uses_commas) ) ;
499
502
}
@@ -503,6 +506,7 @@ fn parse_rgb_components_hsl<'i, 't>(arguments: &mut Parser<'i, 't>) -> Result<(u
503
506
let mut uses_commas = false ;
504
507
// Hue given as an angle
505
508
// https://drafts.csswg.org/css-values/#angles
509
+ let location = arguments. current_source_location ( ) ;
506
510
let hue_degrees = match * arguments. next ( ) ? {
507
511
Token :: Number { value : v, .. } => v,
508
512
Token :: Dimension { value : v, ref unit, .. } => {
@@ -511,24 +515,25 @@ fn parse_rgb_components_hsl<'i, 't>(arguments: &mut Parser<'i, 't>) -> Result<(u
511
515
"grad" => v * 360. / 400. ,
512
516
"rad" => v * 360. / ( 2. * PI ) ,
513
517
"turn" => v * 360. ,
514
- _ => return Err ( BasicParseError :: UnexpectedToken ( Token :: Ident ( unit. clone( ) ) ) ) ,
518
+ _ => return Err ( location . new_basic_unexpected_token_error ( Token :: Ident ( unit. clone( ) ) ) ) ,
515
519
}
516
520
}
517
- ref t => return Err ( BasicParseError :: UnexpectedToken ( t. clone ( ) ) )
521
+ ref t => return Err ( location . new_basic_unexpected_token_error ( t. clone ( ) ) )
518
522
} ;
519
523
// Subtract an integer before rounding, to avoid some rounding errors:
520
524
let hue_normalized_degrees = hue_degrees - 360. * ( hue_degrees / 360. ) . floor ( ) ;
521
525
let hue = hue_normalized_degrees / 360. ;
522
526
523
527
// Saturation and lightness are clamped to 0% ... 100%
524
528
// https://drafts.csswg.org/css-color/#the-hsl-notation
529
+ let location = arguments. current_source_location ( ) ;
525
530
let saturation = match arguments. next ( ) ?. clone ( ) {
526
531
Token :: Percentage { unit_value, .. } => unit_value,
527
532
Token :: Comma => {
528
533
uses_commas = true ;
529
534
arguments. expect_percentage ( ) ?
530
535
}
531
- t => return Err ( BasicParseError :: UnexpectedToken ( t) )
536
+ t => return Err ( location . new_basic_unexpected_token_error ( t) )
532
537
} ;
533
538
let saturation = saturation. max ( 0. ) . min ( 1. ) ;
534
539
0 commit comments