Skip to content

Commit

Permalink
Bug 1469957: Move the error reporter into ParserContext. r=xidorn
Browse files Browse the repository at this point in the history
Summary:
This should make it easier to report errors, and also reduce codesize.

The reason this was so generic is that error reporting was unconditionally
enabled and was super-hot, but now that's no longer the case after bug 1452143,
so we can afford the virtual call in the "error reporting enabled" case.

This opens the possibility of simplifying a lot the error setup as well, though
this patch doesn't do it.

Test Plan: No behavior change, so no new tests.

Reviewers: xidorn

Bug #: 1469957

Differential Revision: https://phabricator.services.mozilla.com/D1734

MozReview-Commit-ID: F3wTdhX9MB5
  • Loading branch information
emilio committed Jun 21, 2018
1 parent 5fefdd9 commit 93d5425
Show file tree
Hide file tree
Showing 16 changed files with 201 additions and 246 deletions.
16 changes: 6 additions & 10 deletions servo/components/style/counter_style/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use Atom;
use cssparser::{AtRuleParser, DeclarationListParser, DeclarationParser};
use cssparser::{CowRcStr, Parser, SourceLocation, Token};
use error_reporting::{ContextualParseError, ParseErrorReporter};
use parser::{Parse, ParserContext, ParserErrorContext};
use error_reporting::ContextualParseError;
use parser::{Parse, ParserContext};
use selectors::parser::SelectorParseErrorKind;
use shared_lock::{SharedRwLockReadGuard, ToCssWithGuard};
use std::fmt::{self, Write};
Expand Down Expand Up @@ -73,16 +73,12 @@ pub fn parse_counter_style_name_definition<'i, 't>(
}

/// Parse the body (inside `{}`) of an @counter-style rule
pub fn parse_counter_style_body<'i, 't, R>(
pub fn parse_counter_style_body<'i, 't>(
name: CustomIdent,
context: &ParserContext,
error_context: &ParserErrorContext<R>,
input: &mut Parser<'i, 't>,
location: SourceLocation,
) -> Result<CounterStyleRuleData, ParseError<'i>>
where
R: ParseErrorReporter,
{
) -> Result<CounterStyleRuleData, ParseError<'i>> {
let start = input.current_source_location();
let mut rule = CounterStyleRuleData::empty(name, location);
{
Expand All @@ -98,7 +94,7 @@ where
slice,
error,
);
context.log_css_error(error_context, location, error)
context.log_css_error(location, error)
}
}
}
Expand Down Expand Up @@ -134,7 +130,7 @@ where
_ => None,
};
if let Some(error) = error {
context.log_css_error(error_context, start, error);
context.log_css_error(start, error);
Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError))
} else {
Ok(rule)
Expand Down
14 changes: 0 additions & 14 deletions servo/components/style/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,17 +249,3 @@ impl ParseErrorReporter for RustLogReporter {
}
}
}

/// Error reporter which silently forgets errors
pub struct NullReporter;

impl ParseErrorReporter for NullReporter {
fn report_error(
&self,
_url: &UrlExtraData,
_location: SourceLocation,
_error: ContextualParseError,
) {
// do nothing
}
}
14 changes: 5 additions & 9 deletions servo/components/style/font_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use cssparser::{AtRuleParser, DeclarationListParser, DeclarationParser, Parser};
use cssparser::{CowRcStr, SourceLocation};
#[cfg(feature = "gecko")]
use cssparser::UnicodeRange;
use error_reporting::{ContextualParseError, ParseErrorReporter};
use parser::{Parse, ParserContext, ParserErrorContext};
use error_reporting::ContextualParseError;
use parser::{Parse, ParserContext};
#[cfg(feature = "gecko")]
use properties::longhands::font_language_override;
use selectors::parser::SelectorParseErrorKind;
Expand Down Expand Up @@ -186,15 +186,11 @@ impl ToCss for FontStyle {
/// Parse the block inside a `@font-face` rule.
///
/// Note that the prelude parsing code lives in the `stylesheets` module.
pub fn parse_font_face_block<R>(
pub fn parse_font_face_block(
context: &ParserContext,
error_context: &ParserErrorContext<R>,
input: &mut Parser,
location: SourceLocation,
) -> FontFaceRuleData
where
R: ParseErrorReporter,
{
) -> FontFaceRuleData {
let mut rule = FontFaceRuleData::empty(location);
{
let parser = FontFaceRuleParser {
Expand All @@ -206,7 +202,7 @@ where
if let Err((error, slice)) = declaration {
let location = error.location;
let error = ContextualParseError::UnsupportedFontFaceDescriptor(slice, error);
context.log_css_error(error_context, location, error)
context.log_css_error(location, error)
}
}
}
Expand Down
15 changes: 5 additions & 10 deletions servo/components/style/media_queries/media_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use context::QuirksMode;
use cssparser::{Delimiter, Parser};
use cssparser::{ParserInput, Token};
use error_reporting::{ContextualParseError, ParseErrorReporter};
use parser::{ParserContext, ParserErrorContext};
use error_reporting::ContextualParseError;
use parser::ParserContext;
use super::{Device, MediaQuery, Qualifier};

/// A type that encapsulates a media query list.
Expand All @@ -30,14 +30,10 @@ impl MediaList {
/// "not all", see:
///
/// <https://drafts.csswg.org/mediaqueries/#error-handling>
pub fn parse<R>(
pub fn parse(
context: &ParserContext,
input: &mut Parser,
error_reporter: &R,
) -> MediaList
where
R: ParseErrorReporter,
{
) -> Self {
if input.is_exhausted() {
return Self::empty();
}
Expand All @@ -54,8 +50,7 @@ impl MediaList {
let location = err.location;
let error =
ContextualParseError::InvalidMediaRule(input.slice_from(start_position), err);
let error_context = ParserErrorContext { error_reporter };
context.log_css_error(&error_context, location, error);
context.log_css_error(location, error);
},
}

Expand Down
31 changes: 14 additions & 17 deletions servo/components/style/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ pub fn assert_parsing_mode_match() {
}
}

/// The context required to report a parse error.
pub struct ParserErrorContext<'a, R: 'a> {
/// An error reporter to report syntax errors.
pub error_reporter: &'a R,
}

/// The data that the parser needs from outside in order to parse a stylesheet.
pub struct ParserContext<'a> {
/// The `Origin` of the stylesheet, whether it's a user, author or
Expand All @@ -55,6 +49,8 @@ pub struct ParserContext<'a> {
pub parsing_mode: ParsingMode,
/// The quirks mode of this stylesheet.
pub quirks_mode: QuirksMode,
/// The active error reporter, or none if error reporting is disabled.
error_reporter: Option<&'a ParseErrorReporter>,
/// The currently active namespaces.
pub namespaces: Option<&'a Namespaces>,
}
Expand All @@ -68,13 +64,15 @@ impl<'a> ParserContext<'a> {
rule_type: Option<CssRuleType>,
parsing_mode: ParsingMode,
quirks_mode: QuirksMode,
error_reporter: Option<&'a ParseErrorReporter>,
) -> Self {
ParserContext {
stylesheet_origin,
url_data,
rule_type,
parsing_mode,
quirks_mode,
error_reporter,
namespaces: None,
}
}
Expand All @@ -86,13 +84,15 @@ impl<'a> ParserContext<'a> {
rule_type: Option<CssRuleType>,
parsing_mode: ParsingMode,
quirks_mode: QuirksMode,
error_reporter: Option<&'a ParseErrorReporter>,
) -> Self {
Self::new(
Origin::Author,
url_data,
rule_type,
parsing_mode,
quirks_mode,
error_reporter,
)
}

Expand All @@ -110,6 +110,7 @@ impl<'a> ParserContext<'a> {
parsing_mode: context.parsing_mode,
quirks_mode: context.quirks_mode,
namespaces: Some(namespaces),
error_reporter: context.error_reporter,
}
}

Expand All @@ -127,21 +128,17 @@ impl<'a> ParserContext<'a> {
}

/// Record a CSS parse error with this context’s error reporting.
pub fn log_css_error<R>(
pub fn log_css_error(
&self,
context: &ParserErrorContext<R>,
location: SourceLocation,
error: ContextualParseError,
) where
R: ParseErrorReporter,
{
let location = SourceLocation {
line: location.line,
column: location.column,
) {
let error_reporter = match self.error_reporter {
Some(r) => r,
None => return,
};
context
.error_reporter
.report_error(self.url_data, location, error)

error_reporter.report_error(self.url_data, location, error)
}

/// Returns whether chrome-only rules should be parsed.
Expand Down
46 changes: 21 additions & 25 deletions servo/components/style/properties/declaration_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use cssparser::{DeclarationListParser, parse_important, ParserInput, CowRcStr};
use cssparser::{Parser, AtRuleParser, DeclarationParser, Delimiter, ParseErrorKind};
use custom_properties::CustomPropertiesBuilder;
use error_reporting::{ParseErrorReporter, ContextualParseError};
use parser::{ParserContext, ParserErrorContext};
use parser::ParserContext;
use properties::animated_properties::AnimationValue;
use shared_lock::Locked;
use smallbitvec::{self, SmallBitVec};
Expand Down Expand Up @@ -1077,50 +1077,49 @@ where

/// A helper to parse the style attribute of an element, in order for this to be
/// shared between Servo and Gecko.
pub fn parse_style_attribute<R>(
///
/// Inline because we call this cross-crate.
#[inline]
pub fn parse_style_attribute(
input: &str,
url_data: &UrlExtraData,
error_reporter: &R,
error_reporter: Option<&ParseErrorReporter>,
quirks_mode: QuirksMode,
) -> PropertyDeclarationBlock
where
R: ParseErrorReporter
{
) -> PropertyDeclarationBlock {
let context = ParserContext::new(
Origin::Author,
url_data,
Some(CssRuleType::Style),
ParsingMode::DEFAULT,
quirks_mode,
error_reporter,
);

let error_context = ParserErrorContext { error_reporter: error_reporter };
let mut input = ParserInput::new(input);
parse_property_declaration_list(&context, &error_context, &mut Parser::new(&mut input))
parse_property_declaration_list(&context, &mut Parser::new(&mut input))
}

/// Parse a given property declaration. Can result in multiple
/// `PropertyDeclaration`s when expanding a shorthand, for example.
///
/// This does not attempt to parse !important at all.
pub fn parse_one_declaration_into<R>(
#[inline]
pub fn parse_one_declaration_into(
declarations: &mut SourcePropertyDeclaration,
id: PropertyId,
input: &str,
url_data: &UrlExtraData,
error_reporter: &R,
error_reporter: Option<&ParseErrorReporter>,
parsing_mode: ParsingMode,
quirks_mode: QuirksMode
) -> Result<(), ()>
where
R: ParseErrorReporter
{
) -> Result<(), ()> {
let context = ParserContext::new(
Origin::Author,
url_data,
Some(CssRuleType::Style),
parsing_mode,
quirks_mode,
error_reporter,
);

let mut input = ParserInput::new(input);
Expand All @@ -1131,9 +1130,10 @@ where
}).map_err(|err| {
let location = err.location;
let error = ContextualParseError::UnsupportedPropertyDeclaration(
parser.slice_from(start_position), err);
let error_context = ParserErrorContext { error_reporter: error_reporter };
context.log_css_error(&error_context, location, error);
parser.slice_from(start_position),
err,
);
context.log_css_error(location, error);
})
}

Expand Down Expand Up @@ -1193,14 +1193,10 @@ impl<'a, 'b, 'i> DeclarationParser<'i> for PropertyDeclarationParser<'a, 'b> {

/// Parse a list of property declarations and return a property declaration
/// block.
pub fn parse_property_declaration_list<R>(
pub fn parse_property_declaration_list(
context: &ParserContext,
error_context: &ParserErrorContext<R>,
input: &mut Parser,
) -> PropertyDeclarationBlock
where
R: ParseErrorReporter
{
) -> PropertyDeclarationBlock {
let mut declarations = SourcePropertyDeclaration::new();
let mut block = PropertyDeclarationBlock::new();
let parser = PropertyDeclarationParser {
Expand Down Expand Up @@ -1228,7 +1224,7 @@ where

let location = error.location;
let error = ContextualParseError::UnsupportedPropertyDeclaration(slice, error);
context.log_css_error(error_context, location, error);
context.log_css_error(location, error);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions servo/components/style/properties/properties.mako.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,7 @@ impl UnparsedValue {
None,
ParsingMode::DEFAULT,
quirks_mode,
None,
);

let mut input = ParserInput::new(&css);
Expand Down
Loading

0 comments on commit 93d5425

Please sign in to comment.