7
7
use cow_rc_str:: CowRcStr ;
8
8
use parser:: { parse_until_before, parse_until_after, parse_nested_block, ParserState } ;
9
9
#[ 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 } ;
11
12
12
13
/// Parse `!important`.
13
14
///
@@ -116,26 +117,40 @@ pub trait AtRuleParser<'i> {
116
117
/// End an at-rule which doesn't have block. Return the finished
117
118
/// representation of the at-rule.
118
119
///
120
+ /// The location passed in is source location of the start of the prelude.
121
+ ///
119
122
/// This is only called when `parse_prelude` returned `WithoutBlock`, and
120
123
/// either the `;` semicolon indeed follows the prelude, or parser is at
121
124
/// 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 {
123
130
let _ = prelude;
131
+ let _ = location;
124
132
panic ! ( "The `AtRuleParser::rule_without_block` method must be overriden \
125
133
if `AtRuleParser::parse_prelude` ever returns `AtRuleType::WithoutBlock`.")
126
134
}
127
135
128
136
/// Parse the content of a `{ /* ... */ }` block for the body of the at-rule.
129
137
///
138
+ /// The location passed in is source location of the start of the prelude.
139
+ ///
130
140
/// Return the finished representation of the at-rule
131
141
/// as returned by `RuleListParser::next` or `DeclarationListParser::next`,
132
142
/// or `Err(())` to ignore the entire at-rule as invalid.
133
143
///
134
144
/// This is only called when `parse_prelude` returned `WithBlock`, and a block
135
145
/// 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 > > {
138
152
let _ = prelude;
153
+ let _ = location;
139
154
let _ = input;
140
155
Err ( input. new_error ( BasicParseErrorKind :: AtRuleBodyInvalid ) )
141
156
}
@@ -178,12 +193,19 @@ pub trait QualifiedRuleParser<'i> {
178
193
179
194
/// Parse the content of a `{ /* ... */ }` block for the body of the qualified rule.
180
195
///
196
+ /// The location passed in is source location of the start of the prelude.
197
+ ///
181
198
/// Return the finished representation of the qualified rule
182
199
/// as returned by `RuleListParser::next`,
183
200
/// 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 > > {
186
207
let _ = prelude;
208
+ let _ = location;
187
209
let _ = input;
188
210
Err ( input. new_error ( BasicParseErrorKind :: QualifiedRuleInvalid ) )
189
211
}
@@ -421,11 +443,16 @@ where P: QualifiedRuleParser<'i, QualifiedRule = R, Error = E> +
421
443
} )
422
444
}
423
445
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 ( ) ;
429
456
let delimiters = Delimiter :: Semicolon | Delimiter :: CurlyBracketBlock ;
430
457
// FIXME: https://github.com/rust-lang/rust/issues/42508
431
458
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>,
434
461
match result {
435
462
Ok ( AtRuleType :: WithoutBlock ( prelude) ) => {
436
463
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 ) ) ,
438
465
Ok ( & Token :: CurlyBracketBlock ) => Err ( (
439
466
input. new_unexpected_token_error ( Token :: CurlyBracketBlock ) ,
440
467
input. slice_from ( start. position ( ) ) ,
@@ -446,8 +473,10 @@ fn parse_at_rule<'i: 't, 't, P, E>(start: &ParserState, name: CowRcStr<'i>,
446
473
match input. next ( ) {
447
474
Ok ( & Token :: CurlyBracketBlock ) => {
448
475
// 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 ( ) ) ) )
451
480
}
452
481
Ok ( & Token :: Semicolon ) => Err ( (
453
482
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>,
469
498
}
470
499
471
500
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 ( ) ;
475
509
// FIXME: https://github.com/rust-lang/rust/issues/42508
476
510
let prelude = parse_until_before :: < ' i , ' t , _ , _ , _ > ( input, Delimiter :: CurlyBracketBlock , |input| {
477
511
parser. parse_prelude ( input)
@@ -481,7 +515,10 @@ fn parse_qualified_rule<'i, 't, P, E>(input: &mut Parser<'i, 't>, parser: &mut P
481
515
// Do this here so that we consume the `{` even if the prelude is `Err`.
482
516
let prelude = prelude?;
483
517
// 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
+ )
485
522
}
486
523
_ => unreachable ! ( )
487
524
}
0 commit comments