Pull rule_type into ParserContext

Absorb `rule_type` into the `ParserContext` so that it's easier to pass down to
deeper levels of the parser.

MozReview-Commit-ID: DjBNytLxGKX
This commit is contained in:
J. Ryan Stinnett 2017-04-10 09:24:32 +08:00
parent a093b0a087
commit 4574cd8ea6
35 changed files with 125 additions and 97 deletions

View file

@ -128,7 +128,8 @@ impl Keyframe {
let error_reporter = MemoryHoleReporter;
let context = ParserContext::new(parent_stylesheet.origin,
&parent_stylesheet.url_data,
&error_reporter);
&error_reporter,
Some(CssRuleType::Keyframe));
let mut input = Parser::new(css);
let mut rule_parser = KeyframeListParser {
@ -364,8 +365,9 @@ impl<'a> QualifiedRuleParser for KeyframeListParser<'a> {
fn parse_block(&mut self, prelude: Self::Prelude, input: &mut Parser)
-> Result<Self::QualifiedRule, ()> {
let context = ParserContext::new_with_rule_type(self.context, Some(CssRuleType::Keyframe));
let parser = KeyframeDeclarationParser {
context: self.context,
context: &context,
};
let mut iter = DeclarationListParser::new(input, parser);
let mut block = PropertyDeclarationBlock::new();
@ -376,7 +378,7 @@ impl<'a> QualifiedRuleParser for KeyframeListParser<'a> {
let pos = range.start;
let message = format!("Unsupported keyframe property declaration: '{}'",
iter.input.slice(range));
log_css_error(iter.input, pos, &*message, self.context);
log_css_error(iter.input, pos, &*message, &context);
}
}
// `parse_important` is not called here, `!important` is not allowed in keyframe blocks.
@ -403,7 +405,7 @@ impl<'a, 'b> DeclarationParser for KeyframeDeclarationParser<'a, 'b> {
fn parse_value(&mut self, name: &str, input: &mut Parser) -> Result<ParsedDeclaration, ()> {
let id = try!(PropertyId::parse(name.into()));
match ParsedDeclaration::parse(id, self.context, input, true, CssRuleType::Keyframe) {
match ParsedDeclaration::parse(id, self.context, input, true) {
Ok(parsed) => {
// In case there is still unparsed text in the declaration, we should roll back.
if !input.is_exhausted() {

View file

@ -9,7 +9,7 @@
use cssparser::{Parser, SourcePosition, UnicodeRange};
use error_reporting::ParseErrorReporter;
use style_traits::OneOrMoreCommaSeparated;
use stylesheets::{Origin, UrlExtraData};
use stylesheets::{CssRuleType, Origin, UrlExtraData};
/// The data that the parser needs from outside in order to parse a stylesheet.
pub struct ParserContext<'a> {
@ -20,26 +20,46 @@ pub struct ParserContext<'a> {
pub url_data: &'a UrlExtraData,
/// An error reporter to report syntax errors.
pub error_reporter: &'a ParseErrorReporter,
/// The current rule type, if any.
pub rule_type: Option<CssRuleType>,
}
impl<'a> ParserContext<'a> {
/// Create a parser context.
pub fn new(stylesheet_origin: Origin,
url_data: &'a UrlExtraData,
error_reporter: &'a ParseErrorReporter)
error_reporter: &'a ParseErrorReporter,
rule_type: Option<CssRuleType>)
-> ParserContext<'a> {
ParserContext {
stylesheet_origin: stylesheet_origin,
url_data: url_data,
error_reporter: error_reporter,
rule_type: rule_type,
}
}
/// Create a parser context for on-the-fly parsing in CSSOM
pub fn new_for_cssom(url_data: &'a UrlExtraData,
error_reporter: &'a ParseErrorReporter)
error_reporter: &'a ParseErrorReporter,
rule_type: Option<CssRuleType>)
-> ParserContext<'a> {
Self::new(Origin::Author, url_data, error_reporter)
Self::new(Origin::Author, url_data, error_reporter, rule_type)
}
/// Create a parser context based on a previous context, but with a modified rule type.
pub fn new_with_rule_type(context: &'a ParserContext,
rule_type: Option<CssRuleType>)
-> ParserContext<'a> {
Self::new(context.stylesheet_origin,
context.url_data,
context.error_reporter,
rule_type)
}
/// Get the rule type, which assumes that one is available.
pub fn rule_type(&self) -> CssRuleType {
self.rule_type.expect("Rule type expected, but none was found.")
}
}

View file

@ -612,8 +612,8 @@ pub fn parse_style_attribute(input: &str,
url_data: &UrlExtraData,
error_reporter: &ParseErrorReporter)
-> PropertyDeclarationBlock {
let context = ParserContext::new(Origin::Author, url_data, error_reporter);
parse_property_declaration_list(&context, &mut Parser::new(input), CssRuleType::Style)
let context = ParserContext::new(Origin::Author, url_data, error_reporter, Some(CssRuleType::Style));
parse_property_declaration_list(&context, &mut Parser::new(input))
}
/// Parse a given property declaration. Can result in multiple
@ -626,9 +626,9 @@ pub fn parse_one_declaration(id: PropertyId,
url_data: &UrlExtraData,
error_reporter: &ParseErrorReporter)
-> Result<ParsedDeclaration, ()> {
let context = ParserContext::new(Origin::Author, url_data, error_reporter);
let context = ParserContext::new(Origin::Author, url_data, error_reporter, Some(CssRuleType::Style));
Parser::new(input).parse_entirely(|parser| {
ParsedDeclaration::parse(id, &context, parser, false, CssRuleType::Style)
ParsedDeclaration::parse(id, &context, parser, false)
.map_err(|_| ())
})
}
@ -636,7 +636,6 @@ pub fn parse_one_declaration(id: PropertyId,
/// A struct to parse property declarations.
struct PropertyDeclarationParser<'a, 'b: 'a> {
context: &'a ParserContext<'b>,
rule_type: CssRuleType,
}
@ -654,7 +653,7 @@ impl<'a, 'b> DeclarationParser for PropertyDeclarationParser<'a, 'b> {
-> Result<(ParsedDeclaration, Importance), ()> {
let id = try!(PropertyId::parse(name.into()));
let parsed = input.parse_until_before(Delimiter::Bang, |input| {
ParsedDeclaration::parse(id, self.context, input, false, self.rule_type)
ParsedDeclaration::parse(id, self.context, input, false)
.map_err(|_| ())
})?;
let importance = match input.try(parse_important) {
@ -673,13 +672,11 @@ impl<'a, 'b> DeclarationParser for PropertyDeclarationParser<'a, 'b> {
/// Parse a list of property declarations and return a property declaration
/// block.
pub fn parse_property_declaration_list(context: &ParserContext,
input: &mut Parser,
rule_type: CssRuleType)
input: &mut Parser)
-> PropertyDeclarationBlock {
let mut block = PropertyDeclarationBlock::new();
let parser = PropertyDeclarationParser {
context: context,
rule_type: rule_type,
};
let mut iter = DeclarationListParser::new(input, parser);
while let Some(declaration) = iter.next() {

View file

@ -330,7 +330,7 @@ impl PropertyDeclarationIdSet {
//
// FIXME(pcwalton): Cloning the error reporter is slow! But so are custom
// properties, so whatever...
let context = ParserContext::new(Origin::Author, url_data, error_reporter);
let context = ParserContext::new(Origin::Author, url_data, error_reporter, None);
Parser::new(&css).parse_entirely(|input| {
match from_shorthand {
None => {
@ -977,8 +977,9 @@ impl ParsedDeclaration {
/// to Importance::Normal. Parsing Importance values is the job of PropertyDeclarationParser,
/// we only set them here so that we don't have to reallocate
pub fn parse(id: PropertyId, context: &ParserContext, input: &mut Parser,
in_keyframe_block: bool, rule_type: CssRuleType)
in_keyframe_block: bool)
-> Result<ParsedDeclaration, PropertyDeclarationParseError> {
let rule_type = context.rule_type();
debug_assert!(rule_type == CssRuleType::Keyframe ||
rule_type == CssRuleType::Page ||
rule_type == CssRuleType::Style,

View file

@ -387,7 +387,8 @@ impl CssRule {
let mut namespaces = parent_stylesheet.namespaces.write();
let context = ParserContext::new(parent_stylesheet.origin,
&parent_stylesheet.url_data,
&error_reporter);
&error_reporter,
None);
let mut input = Parser::new(css);
// nested rules are in the body state
@ -658,7 +659,7 @@ impl Stylesheet {
namespaces: namespaces,
shared_lock: shared_lock,
loader: stylesheet_loader,
context: ParserContext::new(origin, url_data, error_reporter),
context: ParserContext::new(origin, url_data, error_reporter, None),
state: Cell::new(State::Start),
};
@ -1115,7 +1116,8 @@ impl<'a, 'b> AtRuleParser for NestedRuleParser<'a, 'b> {
}))))
}
AtRulePrelude::Page => {
let declarations = parse_property_declaration_list(self.context, input, CssRuleType::Page);
let context = ParserContext::new_with_rule_type(self.context, Some(CssRuleType::Page));
let declarations = parse_property_declaration_list(&context, input);
Ok(CssRule::Page(Arc::new(self.shared_lock.wrap(PageRule(
Arc::new(self.shared_lock.wrap(declarations))
)))))
@ -1138,7 +1140,8 @@ impl<'a, 'b> QualifiedRuleParser for NestedRuleParser<'a, 'b> {
fn parse_block(&mut self, prelude: SelectorList<SelectorImpl>, input: &mut Parser)
-> Result<CssRule, ()> {
let declarations = parse_property_declaration_list(self.context, input, CssRuleType::Style);
let context = ParserContext::new_with_rule_type(self.context, Some(CssRuleType::Style));
let declarations = parse_property_declaration_list(&context, input);
Ok(CssRule::Style(Arc::new(self.shared_lock.wrap(StyleRule {
selectors: prelude,
block: Arc::new(self.shared_lock.wrap(declarations))

View file

@ -212,7 +212,8 @@ impl Declaration {
return false
};
let mut input = Parser::new(&self.val);
let res = ParsedDeclaration::parse(id, cx, &mut input, /* in_keyframe */ false, CssRuleType::Style);
let context = ParserContext::new_with_rule_type(cx, Some(CssRuleType::Style));
let res = ParsedDeclaration::parse(id, &context, &mut input, /* in_keyframe */ false);
let _ = input.try(parse_important);
res.is_ok() && input.is_exhausted()
}