@@ -18,22 +18,6 @@ pub fn parse_important<'i, 't>(input: &mut Parser<'i, 't>) -> Result<(), BasicPa
1818 input. expect_ident_matching ( "important" )
1919}
2020
21- /// The return value for `AtRuleParser::parse_prelude`.
22- /// Indicates whether the at-rule is expected to have a `{ /* ... */ }` block
23- /// or end with a `;` semicolon.
24- pub enum AtRuleType < P , PB > {
25- /// The at-rule is expected to end with a `;` semicolon. Example: `@import`.
26- ///
27- /// The value is the representation of all data of the rule which would be
28- /// handled in rule_without_block.
29- WithoutBlock ( P ) ,
30-
31- /// The at-rule is expected to have a a `{ /* ... */ }` block. Example: `@media`
32- ///
33- /// The value is the representation of the "prelude" part of the rule.
34- WithBlock ( PB ) ,
35- }
36-
3721/// A trait to provide various parsing of declaration values.
3822///
3923/// For example, there could be different implementations for property declarations in style rules
@@ -79,11 +63,8 @@ pub trait DeclarationParser<'i> {
7963/// so that `impl AtRuleParser<(), ()> for ... {}` can be used
8064/// for using `DeclarationListParser` to parse a declarations list with only qualified rules.
8165pub trait AtRuleParser < ' i > {
82- /// The intermediate representation of prelude of an at-rule without block;
83- type PreludeNoBlock ;
84-
85- /// The intermediate representation of prelude of an at-rule with block;
86- type PreludeBlock ;
66+ /// The intermediate representation of prelude of an at-rule.
67+ type Prelude ;
8768
8869 /// The finished representation of an at-rule.
8970 type AtRule ;
@@ -96,8 +77,6 @@ pub trait AtRuleParser<'i> {
9677 /// Return the representation of the prelude and the type of at-rule,
9778 /// or `Err(())` to ignore the entire at-rule as invalid.
9879 ///
99- /// See `AtRuleType`’s documentation for the return value.
100- ///
10180 /// The prelude is the part after the at-keyword
10281 /// and before the `;` semicolon or `{ /* ... */ }` block.
10382 ///
@@ -112,8 +91,7 @@ pub trait AtRuleParser<'i> {
11291 & mut self ,
11392 name : CowRcStr < ' i > ,
11493 input : & mut Parser < ' i , ' t > ,
115- ) -> Result < AtRuleType < Self :: PreludeNoBlock , Self :: PreludeBlock > , ParseError < ' i , Self :: Error > >
116- {
94+ ) -> Result < Self :: Prelude , ParseError < ' i , Self :: Error > > {
11795 let _ = name;
11896 let _ = input;
11997 Err ( input. new_error ( BasicParseErrorKind :: AtRuleInvalid ( name) ) )
@@ -129,15 +107,12 @@ pub trait AtRuleParser<'i> {
129107 /// the end of the input.
130108 fn rule_without_block (
131109 & mut self ,
132- prelude : Self :: PreludeNoBlock ,
110+ prelude : Self :: Prelude ,
133111 start : & ParserState ,
134- ) -> Self :: AtRule {
112+ ) -> Result < Self :: AtRule , ( ) > {
135113 let _ = prelude;
136114 let _ = start;
137- panic ! (
138- "The `AtRuleParser::rule_without_block` method must be overriden \
139- if `AtRuleParser::parse_prelude` ever returns `AtRuleType::WithoutBlock`."
140- )
115+ Err ( ( ) )
141116 }
142117
143118 /// Parse the content of a `{ /* ... */ }` block for the body of the at-rule.
@@ -152,7 +127,7 @@ pub trait AtRuleParser<'i> {
152127 /// was indeed found following the prelude.
153128 fn parse_block < ' t > (
154129 & mut self ,
155- prelude : Self :: PreludeBlock ,
130+ prelude : Self :: Prelude ,
156131 start : & ParserState ,
157132 input : & mut Parser < ' i , ' t > ,
158133 ) -> Result < Self :: AtRule , ParseError < ' i , Self :: Error > > {
@@ -468,39 +443,30 @@ where
468443 let callback = |input : & mut Parser < ' i , ' _ > | parser. parse_prelude ( name, input) ;
469444 let result = parse_until_before ( input, delimiters, callback) ;
470445 match result {
471- Ok ( AtRuleType :: WithoutBlock ( prelude) ) => match input. next ( ) {
472- Ok ( & Token :: Semicolon ) | Err ( _) => Ok ( parser. rule_without_block ( prelude, start) ) ,
473- Ok ( & Token :: CurlyBracketBlock ) => Err ( (
474- input. new_unexpected_token_error ( Token :: CurlyBracketBlock ) ,
475- input. slice_from ( start. position ( ) ) ,
476- ) ) ,
477- Ok ( _) => unreachable ! ( ) ,
478- } ,
479- Ok ( AtRuleType :: WithBlock ( prelude) ) => {
480- match input. next ( ) {
446+ Ok ( prelude) => {
447+ let result = match input. next ( ) {
448+ Ok ( & Token :: Semicolon ) | Err ( _) => {
449+ parser. rule_without_block ( prelude, start)
450+ . map_err ( |( ) | input. new_unexpected_token_error ( Token :: Semicolon ) )
451+ } ,
481452 Ok ( & Token :: CurlyBracketBlock ) => {
482453 // FIXME: https://github.com/servo/rust-cssparser/issues/254
483454 let callback =
484455 |input : & mut Parser < ' i , ' _ > | parser. parse_block ( prelude, start, input) ;
485456 parse_nested_block ( input, callback)
486- . map_err ( |e| ( e, input. slice_from ( start. position ( ) ) ) )
487- }
488- Ok ( & Token :: Semicolon ) => Err ( (
489- input. new_unexpected_token_error ( Token :: Semicolon ) ,
490- input. slice_from ( start. position ( ) ) ,
491- ) ) ,
492- Err ( e) => Err ( ( e. into ( ) , input. slice_from ( start. position ( ) ) ) ) ,
457+ } ,
493458 Ok ( _) => unreachable ! ( ) ,
494- }
495- }
459+ } ;
460+ result. map_err ( |e| ( e, input. slice_from ( start. position ( ) ) ) )
461+ } ,
496462 Err ( error) => {
497463 let end_position = input. position ( ) ;
498464 match input. next ( ) {
499465 Ok ( & Token :: CurlyBracketBlock ) | Ok ( & Token :: Semicolon ) | Err ( _) => { }
500466 _ => unreachable ! ( ) ,
501467 } ;
502468 Err ( ( error, input. slice ( start. position ( ) ..end_position) ) )
503- }
469+ } ,
504470 }
505471}
506472
0 commit comments