@@ -23,24 +23,17 @@ pub fn parse_important<'i, 't>(input: &mut Parser<'i, 't>) -> Result<(), BasicPa
2323/// The return value for `AtRuleParser::parse_prelude`.
2424/// Indicates whether the at-rule is expected to have a `{ /* ... */ }` block
2525/// or end with a `;` semicolon.
26- pub enum AtRuleType < P , R > {
26+ pub enum AtRuleType < P , PB > {
2727 /// The at-rule is expected to end with a `;` semicolon. Example: `@import`.
2828 ///
29- /// The value is the finished representation of an at- rule
30- /// as returned by `RuleListParser::next` or `DeclarationListParser::next` .
31- WithoutBlock ( R ) ,
29+ /// The value is the representation of all data of the rule which would be
30+ /// handled in rule_without_block .
31+ WithoutBlock ( P ) ,
3232
3333 /// The at-rule is expected to have a a `{ /* ... */ }` block. Example: `@media`
3434 ///
3535 /// The value is the representation of the "prelude" part of the rule.
36- WithBlock ( P ) ,
37-
38- /// The at-rule may either have a block or end with a semicolon.
39- ///
40- /// This is mostly for testing. As of this writing no real CSS at-rule behaves like this.
41- ///
42- /// The value is the representation of the "prelude" part of the rule.
43- OptionalBlock ( P ) ,
36+ WithBlock ( PB ) ,
4437}
4538
4639/// A trait to provide various parsing of declaration values.
@@ -85,8 +78,11 @@ pub trait DeclarationParser<'i> {
8578/// so that `impl AtRuleParser<(), ()> for ... {}` can be used
8679/// for using `DeclarationListParser` to parse a declartions list with only qualified rules.
8780pub trait AtRuleParser < ' i > {
88- /// The intermediate representation of an at-rule prelude.
89- type Prelude ;
81+ /// The intermediate representation of prelude of an at-rule without block;
82+ type PreludeNoBlock ;
83+
84+ /// The intermediate representation of prelude of an at-rule with block;
85+ type PreludeBlock ;
9086
9187 /// The finished representation of an at-rule.
9288 type AtRule ;
@@ -112,36 +108,39 @@ pub trait AtRuleParser<'i> {
112108 /// that ends wherever the prelude should end.
113109 /// (Before the next semicolon, the next `{`, or the end of the current block.)
114110 fn parse_prelude < ' t > ( & mut self , name : CowRcStr < ' i > , input : & mut Parser < ' i , ' t > )
115- -> Result < AtRuleType < Self :: Prelude , Self :: AtRule > , ParseError < ' i , Self :: Error > > {
111+ -> Result < AtRuleType < Self :: PreludeNoBlock , Self :: PreludeBlock > ,
112+ ParseError < ' i , Self :: Error > > {
116113 let _ = name;
117114 let _ = input;
118115 Err ( ParseError :: Basic ( BasicParseError :: AtRuleInvalid ( name) ) )
119116 }
120117
118+ /// End an at-rule which doesn't have block. Return the finished
119+ /// representation of the at-rule.
120+ ///
121+ /// This is only called when `parse_prelude` returned `WithoutBlock`, and
122+ /// either the `;` semicolon indeed follows the prelude, or parser is at
123+ /// the end of the input.
124+ fn rule_without_block ( & mut self , prelude : Self :: PreludeNoBlock ) -> Self :: AtRule {
125+ let _ = prelude;
126+ panic ! ( "The `AtRuleParser::rule_without_block` method must be overriden \
127+ if `AtRuleParser::parse_prelude` ever returns `AtRuleType::WithoutBlock`.")
128+ }
129+
121130 /// Parse the content of a `{ /* ... */ }` block for the body of the at-rule.
122131 ///
123132 /// Return the finished representation of the at-rule
124133 /// as returned by `RuleListParser::next` or `DeclarationListParser::next`,
125134 /// or `Err(())` to ignore the entire at-rule as invalid.
126135 ///
127- /// This is only called when `parse_prelude` returned `WithBlock` or `OptionalBlock`,
128- /// and a block was indeed found following the prelude.
129- fn parse_block < ' t > ( & mut self , prelude : Self :: Prelude , input : & mut Parser < ' i , ' t > )
136+ /// This is only called when `parse_prelude` returned `WithBlock`, and a block
137+ /// was indeed found following the prelude.
138+ fn parse_block < ' t > ( & mut self , prelude : Self :: PreludeBlock , input : & mut Parser < ' i , ' t > )
130139 -> Result < Self :: AtRule , ParseError < ' i , Self :: Error > > {
131140 let _ = prelude;
132141 let _ = input;
133142 Err ( ParseError :: Basic ( BasicParseError :: AtRuleBodyInvalid ) )
134143 }
135-
136- /// An `OptionalBlock` prelude was followed by `;`.
137- ///
138- /// Convert the prelude into the finished representation of the at-rule
139- /// as returned by `RuleListParser::next` or `DeclarationListParser::next`.
140- fn rule_without_block ( & mut self , prelude : Self :: Prelude ) -> Self :: AtRule {
141- let _ = prelude;
142- panic ! ( "The `AtRuleParser::rule_without_block` method must be overriden \
143- if `AtRuleParser::parse_prelude` ever returns `AtRuleType::OptionalBlock`.")
144- }
145144}
146145
147146/// A trait to provide various parsing of qualified rules.
@@ -460,9 +459,9 @@ fn parse_at_rule<'i: 't, 't, P, E>(start: &ParserState, name: CowRcStr<'i>,
460459 parser. parse_prelude ( name, input)
461460 } ) ;
462461 match result {
463- Ok ( AtRuleType :: WithoutBlock ( rule ) ) => {
462+ Ok ( AtRuleType :: WithoutBlock ( prelude ) ) => {
464463 match input. next ( ) {
465- Ok ( & Token :: Semicolon ) | Err ( _) => Ok ( rule ) ,
464+ Ok ( & Token :: Semicolon ) | Err ( _) => Ok ( parser . rule_without_block ( prelude ) ) ,
466465 Ok ( & Token :: CurlyBracketBlock ) => Err ( PreciseParseError {
467466 error : ParseError :: Basic ( BasicParseError :: UnexpectedToken ( Token :: CurlyBracketBlock ) ) ,
468467 slice : input. slice_from ( start. position ( ) ) ,
@@ -495,21 +494,6 @@ fn parse_at_rule<'i: 't, 't, P, E>(start: &ParserState, name: CowRcStr<'i>,
495494 Ok ( _) => unreachable ! ( )
496495 }
497496 }
498- Ok ( AtRuleType :: OptionalBlock ( prelude) ) => {
499- match input. next ( ) {
500- Ok ( & Token :: Semicolon ) | Err ( _) => Ok ( parser. rule_without_block ( prelude) ) ,
501- Ok ( & Token :: CurlyBracketBlock ) => {
502- // FIXME: https://github.com/rust-lang/rust/issues/42508
503- parse_nested_block :: < ' i , ' t , _ , _ , _ > ( input, move |input| parser. parse_block ( prelude, input) )
504- . map_err ( |e| PreciseParseError {
505- error : e,
506- slice : input. slice_from ( start. position ( ) ) ,
507- location : start. source_location ( ) ,
508- } )
509- }
510- _ => unreachable ! ( )
511- }
512- }
513497 Err ( error) => {
514498 let end_position = input. position ( ) ;
515499 match input. next ( ) {
0 commit comments