Skip to content

Commit 22dfc44

Browse files
author
bors-servo
authored
Auto merge of servo#226 - upsuper:source-location, r=SimonSapin
Include source location in param of rule parsers <!-- Reviewable:start --> This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-cssparser/226) <!-- Reviewable:end -->
2 parents 1cca84f + 901eb07 commit 22dfc44

File tree

3 files changed

+73
-24
lines changed

3 files changed

+73
-24
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cssparser"
3-
version = "0.23.10"
3+
version = "0.24.0"
44
authors = [ "Simon Sapin <simon.sapin@exyr.org>" ]
55

66
description = "Rust implementation of CSS Syntax Level 3"

src/rules_and_declarations.rs

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
use cow_rc_str::CowRcStr;
88
use 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
}

src/tests.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -785,13 +785,21 @@ impl<'i> AtRuleParser<'i> for JsonParser {
785785
}
786786
}
787787

788-
fn rule_without_block(&mut self, mut prelude: Vec<Json>) -> Json {
788+
fn rule_without_block(
789+
&mut self,
790+
mut prelude: Vec<Json>,
791+
_location: SourceLocation,
792+
) -> Json {
789793
prelude.push(Json::Null);
790794
Json::Array(prelude)
791795
}
792796

793-
fn parse_block<'t>(&mut self, mut prelude: Vec<Json>, input: &mut Parser<'i, 't>)
794-
-> Result<Json, ParseError<'i, ()>> {
797+
fn parse_block<'t>(
798+
&mut self,
799+
mut prelude: Vec<Json>,
800+
_location: SourceLocation,
801+
input: &mut Parser<'i, 't>,
802+
) -> Result<Json, ParseError<'i, ()>> {
795803
prelude.push(Json::Array(component_values_to_json(input)));
796804
Ok(Json::Array(prelude))
797805
}
@@ -806,8 +814,12 @@ impl<'i> QualifiedRuleParser<'i> for JsonParser {
806814
Ok(component_values_to_json(input))
807815
}
808816

809-
fn parse_block<'t>(&mut self, prelude: Vec<Json>, input: &mut Parser<'i, 't>)
810-
-> Result<Json, ParseError<'i, ()>> {
817+
fn parse_block<'t>(
818+
&mut self,
819+
prelude: Vec<Json>,
820+
_location: SourceLocation,
821+
input: &mut Parser<'i, 't>,
822+
) -> Result<Json, ParseError<'i, ()>> {
811823
Ok(JArray![
812824
"qualified rule",
813825
prelude,

0 commit comments

Comments
 (0)