gecko-dev/servo/components/style/parser.rs
Ravi Shankar f0120bad7b servo: Merge #14373 - Use the ParserContext along with the Parser (from Wafflespeanut:parse); r=emilio
<!-- Please describe your changes on the following line: -->

This changes the `parse` function's signature to include `ParserContext`, so that we don't introduce another trait just for the sake of the context. Instead, we can safely ignore the context whenever we don't need it.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors

<!-- Either: -->
- [x] These changes do not require tests because it's a refactor
<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

r? @SimonSapin or @emilio or @Manishearth

Source-Repo: https://github.com/servo/servo
Source-Revision: 4755cb7586ab4a89f35bbccf8b57c85ed2f428e7
2016-11-26 19:20:10 -08:00

75 lines
2.6 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! The context within which CSS code is parsed.
use cssparser::{Parser, SourcePosition};
use error_reporting::ParseErrorReporter;
#[cfg(feature = "gecko")]
use gecko_bindings::sugar::refptr::{GeckoArcPrincipal, GeckoArcURI};
use servo_url::ServoUrl;
use stylesheets::Origin;
#[cfg(not(feature = "gecko"))]
pub struct ParserContextExtraData;
#[cfg(feature = "gecko")]
pub struct ParserContextExtraData {
pub base: Option<GeckoArcURI>,
pub referrer: Option<GeckoArcURI>,
pub principal: Option<GeckoArcPrincipal>,
}
impl ParserContextExtraData {
#[cfg(not(feature = "gecko"))]
pub fn default() -> ParserContextExtraData {
ParserContextExtraData
}
#[cfg(feature = "gecko")]
pub fn default() -> ParserContextExtraData {
ParserContextExtraData { base: None, referrer: None, principal: None }
}
}
pub struct ParserContext<'a> {
pub stylesheet_origin: Origin,
pub base_url: &'a ServoUrl,
pub error_reporter: Box<ParseErrorReporter + Send>,
pub extra_data: ParserContextExtraData,
}
impl<'a> ParserContext<'a> {
pub fn new_with_extra_data(stylesheet_origin: Origin, base_url: &'a ServoUrl,
error_reporter: Box<ParseErrorReporter + Send>,
extra_data: ParserContextExtraData)
-> ParserContext<'a> {
ParserContext {
stylesheet_origin: stylesheet_origin,
base_url: base_url,
error_reporter: error_reporter,
extra_data: extra_data,
}
}
pub fn new(stylesheet_origin: Origin, base_url: &'a ServoUrl, error_reporter: Box<ParseErrorReporter + Send>)
-> ParserContext<'a> {
let extra_data = ParserContextExtraData::default();
ParserContext::new_with_extra_data(stylesheet_origin, base_url, error_reporter, extra_data)
}
}
/// Defaults to a no-op.
/// Set a `RUST_LOG=style::errors` environment variable
/// to log CSS parse errors to stderr.
pub fn log_css_error(input: &mut Parser, position: SourcePosition, message: &str, parsercontext: &ParserContext) {
parsercontext.error_reporter.report_error(input, position, message);
}
// XXXManishearth Replace all specified value parse impls with impls of this
// trait. This will make it easy to write more generic values in the future.
pub trait Parse {
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> where Self: Sized;
}