Skip to content

Commit 4734033

Browse files
committed
Pass rule start to parser rather than just a location.
This is: * More generic, as the state has more information like the source index. * Slightly cheaper to compute, as the source location requires math to compute, which the parser state doesn't I need this to improve CSS sanitization in Gecko. https://bugzilla.mozilla.org/show_bug.cgi?id=1680084
1 parent 5ce9919 commit 4734033

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

src/rules_and_declarations.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// https://drafts.csswg.org/css-syntax/#parsing
66

77
use super::{BasicParseError, BasicParseErrorKind, Delimiter};
8-
use super::{ParseError, Parser, SourceLocation, Token};
8+
use super::{ParseError, Parser, Token};
99
use crate::cow_rc_str::CowRcStr;
1010
use crate::parser::{parse_nested_block, parse_until_after, parse_until_before, ParserState};
1111

@@ -130,10 +130,10 @@ pub trait AtRuleParser<'i> {
130130
fn rule_without_block(
131131
&mut self,
132132
prelude: Self::PreludeNoBlock,
133-
location: SourceLocation,
133+
start: &ParserState,
134134
) -> Self::AtRule {
135135
let _ = prelude;
136-
let _ = location;
136+
let _ = start;
137137
panic!(
138138
"The `AtRuleParser::rule_without_block` method must be overriden \
139139
if `AtRuleParser::parse_prelude` ever returns `AtRuleType::WithoutBlock`."
@@ -153,11 +153,11 @@ pub trait AtRuleParser<'i> {
153153
fn parse_block<'t>(
154154
&mut self,
155155
prelude: Self::PreludeBlock,
156-
location: SourceLocation,
156+
start: &ParserState,
157157
input: &mut Parser<'i, 't>,
158158
) -> Result<Self::AtRule, ParseError<'i, Self::Error>> {
159159
let _ = prelude;
160-
let _ = location;
160+
let _ = start;
161161
let _ = input;
162162
Err(input.new_error(BasicParseErrorKind::AtRuleBodyInvalid))
163163
}
@@ -210,11 +210,11 @@ pub trait QualifiedRuleParser<'i> {
210210
fn parse_block<'t>(
211211
&mut self,
212212
prelude: Self::Prelude,
213-
location: SourceLocation,
213+
start: &ParserState,
214214
input: &mut Parser<'i, 't>,
215215
) -> Result<Self::QualifiedRule, ParseError<'i, Self::Error>> {
216216
let _ = prelude;
217-
let _ = location;
217+
let _ = start;
218218
let _ = input;
219219
Err(input.new_error(BasicParseErrorKind::QualifiedRuleInvalid))
220220
}
@@ -463,14 +463,13 @@ fn parse_at_rule<'i, 't, P, E>(
463463
where
464464
P: AtRuleParser<'i, Error = E>,
465465
{
466-
let location = input.current_source_location();
467466
let delimiters = Delimiter::Semicolon | Delimiter::CurlyBracketBlock;
468467
// FIXME: https://github.com/servo/rust-cssparser/issues/254
469468
let callback = |input: &mut Parser<'i, '_>| parser.parse_prelude(name, input);
470469
let result = parse_until_before(input, delimiters, callback);
471470
match result {
472471
Ok(AtRuleType::WithoutBlock(prelude)) => match input.next() {
473-
Ok(&Token::Semicolon) | Err(_) => Ok(parser.rule_without_block(prelude, location)),
472+
Ok(&Token::Semicolon) | Err(_) => Ok(parser.rule_without_block(prelude, start)),
474473
Ok(&Token::CurlyBracketBlock) => Err((
475474
input.new_unexpected_token_error(Token::CurlyBracketBlock),
476475
input.slice_from(start.position()),
@@ -482,7 +481,7 @@ where
482481
Ok(&Token::CurlyBracketBlock) => {
483482
// FIXME: https://github.com/servo/rust-cssparser/issues/254
484483
let callback =
485-
|input: &mut Parser<'i, '_>| parser.parse_block(prelude, location, input);
484+
|input: &mut Parser<'i, '_>| parser.parse_block(prelude, start, input);
486485
parse_nested_block(input, callback)
487486
.map_err(|e| (e, input.slice_from(start.position())))
488487
}
@@ -512,7 +511,7 @@ fn parse_qualified_rule<'i, 't, P, E>(
512511
where
513512
P: QualifiedRuleParser<'i, Error = E>,
514513
{
515-
let location = input.current_source_location();
514+
let start = input.state();
516515
// FIXME: https://github.com/servo/rust-cssparser/issues/254
517516
let callback = |input: &mut Parser<'i, '_>| parser.parse_prelude(input);
518517
let prelude = parse_until_before(input, Delimiter::CurlyBracketBlock, callback);
@@ -521,8 +520,7 @@ where
521520
// Do this here so that we consume the `{` even if the prelude is `Err`.
522521
let prelude = prelude?;
523522
// FIXME: https://github.com/servo/rust-cssparser/issues/254
524-
let callback =
525-
|input: &mut Parser<'i, '_>| parser.parse_block(prelude, location, input);
523+
let callback = |input: &mut Parser<'i, '_>| parser.parse_block(prelude, &start, input);
526524
parse_nested_block(input, callback)
527525
}
528526
_ => unreachable!(),

src/tests.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use super::{
1616
parse_important, parse_nth, parse_one_declaration, parse_one_rule, stylesheet_encoding,
1717
AtRuleParser, AtRuleType, BasicParseError, BasicParseErrorKind, Color, CowRcStr,
1818
DeclarationListParser, DeclarationParser, Delimiter, EncodingSupport, ParseError,
19-
ParseErrorKind, Parser, ParserInput, QualifiedRuleParser, RuleListParser, SourceLocation,
20-
ToCss, Token, TokenSerializationType, UnicodeRange, RGBA,
19+
ParseErrorKind, Parser, ParserInput, ParserState, QualifiedRuleParser, RuleListParser,
20+
SourceLocation, ToCss, Token, TokenSerializationType, UnicodeRange, RGBA,
2121
};
2222

2323
macro_rules! JArray {
@@ -946,15 +946,15 @@ impl<'i> AtRuleParser<'i> for JsonParser {
946946
}
947947
}
948948

949-
fn rule_without_block(&mut self, mut prelude: Vec<Value>, _location: SourceLocation) -> Value {
949+
fn rule_without_block(&mut self, mut prelude: Vec<Value>, _: &ParserState) -> Value {
950950
prelude.push(Value::Null);
951951
Value::Array(prelude)
952952
}
953953

954954
fn parse_block<'t>(
955955
&mut self,
956956
mut prelude: Vec<Value>,
957-
_location: SourceLocation,
957+
_: &ParserState,
958958
input: &mut Parser<'i, 't>,
959959
) -> Result<Value, ParseError<'i, ()>> {
960960
prelude.push(Value::Array(component_values_to_json(input)));
@@ -977,7 +977,7 @@ impl<'i> QualifiedRuleParser<'i> for JsonParser {
977977
fn parse_block<'t>(
978978
&mut self,
979979
prelude: Vec<Value>,
980-
_location: SourceLocation,
980+
_: &ParserState,
981981
input: &mut Parser<'i, 't>,
982982
) -> Result<Value, ParseError<'i, ()>> {
983983
Ok(JArray![

0 commit comments

Comments
 (0)