77use cow_rc_str:: CowRcStr ;
88use parser:: { parse_until_before, parse_until_after, parse_nested_block, ParserState } ;
99#[ allow( unused_imports) ] use std:: ascii:: AsciiExt ;
10- use super :: { Token , Parser , Delimiter , ParseError , BasicParseError , BasicParseErrorKind } ;
10+ use super :: { BasicParseError , BasicParseErrorKind , Delimiter } ;
11+ use super :: { ParseError , Parser , SourceLocation , Token } ;
1112
1213/// Parse `!important`.
1314///
@@ -116,26 +117,40 @@ pub trait AtRuleParser<'i> {
116117 /// End an at-rule which doesn't have block. Return the finished
117118 /// representation of the at-rule.
118119 ///
120+ /// The location passed in is source location of the start of the prelude.
121+ ///
119122 /// This is only called when `parse_prelude` returned `WithoutBlock`, and
120123 /// either the `;` semicolon indeed follows the prelude, or parser is at
121124 /// the end of the input.
122- fn rule_without_block ( & mut self , prelude : Self :: PreludeNoBlock ) -> Self :: AtRule {
125+ fn rule_without_block (
126+ & mut self ,
127+ prelude : Self :: PreludeNoBlock ,
128+ location : SourceLocation ,
129+ ) -> Self :: AtRule {
123130 let _ = prelude;
131+ let _ = location;
124132 panic ! ( "The `AtRuleParser::rule_without_block` method must be overriden \
125133 if `AtRuleParser::parse_prelude` ever returns `AtRuleType::WithoutBlock`.")
126134 }
127135
128136 /// Parse the content of a `{ /* ... */ }` block for the body of the at-rule.
129137 ///
138+ /// The location passed in is source location of the start of the prelude.
139+ ///
130140 /// Return the finished representation of the at-rule
131141 /// as returned by `RuleListParser::next` or `DeclarationListParser::next`,
132142 /// or `Err(())` to ignore the entire at-rule as invalid.
133143 ///
134144 /// This is only called when `parse_prelude` returned `WithBlock`, and a block
135145 /// was indeed found following the prelude.
136- fn parse_block < ' t > ( & mut self , prelude : Self :: PreludeBlock , input : & mut Parser < ' i , ' t > )
137- -> Result < Self :: AtRule , ParseError < ' i , Self :: Error > > {
146+ fn parse_block < ' t > (
147+ & mut self ,
148+ prelude : Self :: PreludeBlock ,
149+ location : SourceLocation ,
150+ input : & mut Parser < ' i , ' t > ,
151+ ) -> Result < Self :: AtRule , ParseError < ' i , Self :: Error > > {
138152 let _ = prelude;
153+ let _ = location;
139154 let _ = input;
140155 Err ( input. new_error ( BasicParseErrorKind :: AtRuleBodyInvalid ) )
141156 }
@@ -178,12 +193,19 @@ pub trait QualifiedRuleParser<'i> {
178193
179194 /// Parse the content of a `{ /* ... */ }` block for the body of the qualified rule.
180195 ///
196+ /// The location passed in is source location of the start of the prelude.
197+ ///
181198 /// Return the finished representation of the qualified rule
182199 /// as returned by `RuleListParser::next`,
183200 /// or `Err(())` to ignore the entire at-rule as invalid.
184- fn parse_block < ' t > ( & mut self , prelude : Self :: Prelude , input : & mut Parser < ' i , ' t > )
185- -> Result < Self :: QualifiedRule , ParseError < ' i , Self :: Error > > {
201+ fn parse_block < ' t > (
202+ & mut self ,
203+ prelude : Self :: Prelude ,
204+ location : SourceLocation ,
205+ input : & mut Parser < ' i , ' t > ,
206+ ) -> Result < Self :: QualifiedRule , ParseError < ' i , Self :: Error > > {
186207 let _ = prelude;
208+ let _ = location;
187209 let _ = input;
188210 Err ( input. new_error ( BasicParseErrorKind :: QualifiedRuleInvalid ) )
189211 }
@@ -421,11 +443,16 @@ where P: QualifiedRuleParser<'i, QualifiedRule = R, Error = E> +
421443 } )
422444}
423445
424- fn parse_at_rule < ' i : ' t , ' t , P , E > ( start : & ParserState , name : CowRcStr < ' i > ,
425- input : & mut Parser < ' i , ' t > , parser : & mut P )
426- -> Result < <P as AtRuleParser < ' i > >:: AtRule ,
427- ( ParseError < ' i , E > , & ' i str ) >
428- where P : AtRuleParser < ' i , Error = E > {
446+ fn parse_at_rule < ' i : ' t , ' t , P , E > (
447+ start : & ParserState ,
448+ name : CowRcStr < ' i > ,
449+ input : & mut Parser < ' i , ' t > ,
450+ parser : & mut P ,
451+ ) -> Result < <P as AtRuleParser < ' i > >:: AtRule , ( ParseError < ' i , E > , & ' i str ) >
452+ where
453+ P : AtRuleParser < ' i , Error = E >
454+ {
455+ let location = input. current_source_location ( ) ;
429456 let delimiters = Delimiter :: Semicolon | Delimiter :: CurlyBracketBlock ;
430457 // FIXME: https://github.com/rust-lang/rust/issues/42508
431458 let result = parse_until_before :: < ' i , ' t , _ , _ , _ > ( input, delimiters, |input| {
@@ -434,7 +461,7 @@ fn parse_at_rule<'i: 't, 't, P, E>(start: &ParserState, name: CowRcStr<'i>,
434461 match result {
435462 Ok ( AtRuleType :: WithoutBlock ( prelude) ) => {
436463 match input. next ( ) {
437- Ok ( & Token :: Semicolon ) | Err ( _) => Ok ( parser. rule_without_block ( prelude) ) ,
464+ Ok ( & Token :: Semicolon ) | Err ( _) => Ok ( parser. rule_without_block ( prelude, location ) ) ,
438465 Ok ( & Token :: CurlyBracketBlock ) => Err ( (
439466 input. new_unexpected_token_error ( Token :: CurlyBracketBlock ) ,
440467 input. slice_from ( start. position ( ) ) ,
@@ -446,8 +473,10 @@ fn parse_at_rule<'i: 't, 't, P, E>(start: &ParserState, name: CowRcStr<'i>,
446473 match input. next ( ) {
447474 Ok ( & Token :: CurlyBracketBlock ) => {
448475 // FIXME: https://github.com/rust-lang/rust/issues/42508
449- parse_nested_block :: < ' i , ' t , _ , _ , _ > ( input, move |input| parser. parse_block ( prelude, input) )
450- . map_err ( |e| ( e, input. slice_from ( start. position ( ) ) ) )
476+ parse_nested_block :: < ' i , ' t , _ , _ , _ > (
477+ input,
478+ move |input| parser. parse_block ( prelude, location, input)
479+ ) . map_err ( |e| ( e, input. slice_from ( start. position ( ) ) ) )
451480 }
452481 Ok ( & Token :: Semicolon ) => Err ( (
453482 input. new_unexpected_token_error ( Token :: Semicolon ) ,
@@ -469,9 +498,14 @@ fn parse_at_rule<'i: 't, 't, P, E>(start: &ParserState, name: CowRcStr<'i>,
469498}
470499
471500
472- fn parse_qualified_rule < ' i , ' t , P , E > ( input : & mut Parser < ' i , ' t > , parser : & mut P )
473- -> Result < <P as QualifiedRuleParser < ' i > >:: QualifiedRule , ParseError < ' i , E > >
474- where P : QualifiedRuleParser < ' i , Error = E > {
501+ fn parse_qualified_rule < ' i , ' t , P , E > (
502+ input : & mut Parser < ' i , ' t > ,
503+ parser : & mut P ,
504+ ) -> Result < <P as QualifiedRuleParser < ' i > >:: QualifiedRule , ParseError < ' i , E > >
505+ where
506+ P : QualifiedRuleParser < ' i , Error = E >
507+ {
508+ let location = input. current_source_location ( ) ;
475509 // FIXME: https://github.com/rust-lang/rust/issues/42508
476510 let prelude = parse_until_before :: < ' i , ' t , _ , _ , _ > ( input, Delimiter :: CurlyBracketBlock , |input| {
477511 parser. parse_prelude ( input)
@@ -481,7 +515,10 @@ fn parse_qualified_rule<'i, 't, P, E>(input: &mut Parser<'i, 't>, parser: &mut P
481515 // Do this here so that we consume the `{` even if the prelude is `Err`.
482516 let prelude = prelude?;
483517 // FIXME: https://github.com/rust-lang/rust/issues/42508
484- parse_nested_block :: < ' i , ' t , _ , _ , _ > ( input, move |input| parser. parse_block ( prelude, input) )
518+ parse_nested_block :: < ' i , ' t , _ , _ , _ > (
519+ input,
520+ move |input| parser. parse_block ( prelude, location, input) ,
521+ )
485522 }
486523 _ => unreachable ! ( )
487524 }
0 commit comments