servo: Merge #14178 - Parse trait functions (from impowski:parse_trait_functions); r=Wafflespeanut

<!-- Please describe your changes on the following line: -->
Moved parse functions to a Parse trait in style. r? @Manishearth

---
<!-- 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
- [X] These changes fix #14101 (github issue number if applicable).

<!-- Either: -->
- [x] There are tests for these changes OR
- [X] These changes do not require tests because new functionalities weren't added

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

Source-Repo: https://github.com/servo/servo
Source-Revision: 6adae66f9c7ff58e0e31f3913471e575910a5861
This commit is contained in:
Artem Biryukov 2016-11-11 21:35:48 -06:00
Родитель 7fa6f2be7f
Коммит 8e1d17f6f3
14 изменённых файлов: 149 добавлений и 114 удалений

Просмотреть файл

@ -8,6 +8,7 @@
use Atom;
use cssparser::{Delimiter, Parser, SourcePosition, Token, TokenSerializationType};
use parser::Parse;
use properties::DeclaredValue;
use std::ascii::AsciiExt;
use std::borrow::Cow;
@ -111,15 +112,17 @@ impl ComputedValue {
}
}
pub fn parse(input: &mut Parser) -> Result<SpecifiedValue, ()> {
let mut references = Some(HashSet::new());
let (first, css, last) = try!(parse_self_contained_declaration_value(input, &mut references));
Ok(SpecifiedValue {
css: css.into_owned(),
first_token_type: first,
last_token_type: last,
references: references.unwrap(),
})
impl Parse for SpecifiedValue {
fn parse(input: &mut Parser) -> Result<Self, ()> {
let mut references = Some(HashSet::new());
let (first, css, last) = try!(parse_self_contained_declaration_value(input, &mut references));
Ok(SpecifiedValue {
css: css.into_owned(),
first_token_type: first,
last_token_type: last,
references: references.unwrap(),
})
}
}
/// Parse the value of a non-custom property that contains `var()` references.

Просмотреть файл

@ -67,7 +67,7 @@
pub mod single_value {
use cssparser::Parser;
use parser::{ParserContext, ParserContextExtraData};
use parser::{Parse, ParserContext, ParserContextExtraData};
use properties::{CSSWideKeyword, DeclaredValue, Shorthand};
use values::computed::{Context, ToComputedValue};
use values::{computed, specified};
@ -175,13 +175,12 @@
#![allow(unused_imports)]
% if not property.derived_from:
use cssparser::Parser;
use parser::{ParserContext, ParserContextExtraData};
use parser::{Parse, ParserContext, ParserContextExtraData};
use properties::{CSSWideKeyword, DeclaredValue, Shorthand};
% endif
use values::{Auto, Either, None_, Normal};
use cascade_info::CascadeInfo;
use error_reporting::ParseErrorReporter;
use parser::Parse;
use properties::longhands;
use properties::property_bit_field::PropertyBitField;
use properties::{ComputedValues, PropertyDeclaration};

Просмотреть файл

@ -15,7 +15,6 @@ ${helpers.predefined_type("opacity",
<%helpers:vector_longhand name="box-shadow" allow_empty="True" animatable="True">
use cssparser;
use std::fmt;
use parser::Parse;
use style_traits::ToCss;
use values::HasViewportPercentage;
@ -355,7 +354,6 @@ ${helpers.predefined_type("opacity",
pub fn parse(_context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
use app_units::Au;
use parser::Parse;
use std::ascii::AsciiExt;
use values::specified::Length;
@ -677,7 +675,6 @@ pub struct OriginParseResult {
}
pub fn parse_origin(_: &ParserContext, input: &mut Parser) -> Result<OriginParseResult,()> {
use parser::Parse;
use values::specified::{LengthOrPercentage, Percentage};
let (mut horizontal, mut vertical, mut depth) = (None, None, None);
loop {

Просмотреть файл

@ -496,6 +496,7 @@ ${helpers.single_keyword("font-variant-position",
pub mod computed_value {
use cssparser::Parser;
use parser::Parse;
use std::fmt;
use style_traits::ToCss;
@ -542,10 +543,10 @@ ${helpers.single_keyword("font-variant-position",
}
}
impl FeatureTagValue {
impl Parse for FeatureTagValue {
/// https://www.w3.org/TR/css-fonts-3/#propdef-font-feature-settings
/// <string> [ on | off | <integer> ]
pub fn parse(input: &mut Parser) -> Result<FeatureTagValue, ()> {
fn parse(input: &mut Parser) -> Result<Self, ()> {
let tag = try!(input.expect_string());
// allowed strings of length 4 containing chars: <U+20, U+7E>

Просмотреть файл

@ -27,7 +27,7 @@ use euclid::size::Size2D;
use computed_values;
#[cfg(feature = "servo")] use logical_geometry::{LogicalMargin, PhysicalSide};
use logical_geometry::WritingMode;
use parser::{ParserContext, ParserContextExtraData};
use parser::{Parse, ParserContext, ParserContextExtraData};
use style_traits::ToCss;
use stylesheets::Origin;
#[cfg(feature = "servo")] use values::Either;
@ -49,7 +49,7 @@ pub mod declaration_block;
pub mod longhands {
use cssparser::Parser;
use parser::ParserContext;
use parser::{Parse, ParserContext};
use values::specified;
<%include file="/longhand/background.mako.rs" />
@ -79,7 +79,7 @@ pub mod longhands {
pub mod shorthands {
use cssparser::Parser;
use parser::ParserContext;
use parser::{Parse, ParserContext};
use values::specified;
pub fn parse_four_sides<F, T>(input: &mut Parser, parse_one: F) -> Result<(T, T, T, T), ()>
@ -344,8 +344,8 @@ pub enum CSSWideKeyword {
UnsetKeyword,
}
impl CSSWideKeyword {
pub fn parse(input: &mut Parser) -> Result<CSSWideKeyword, ()> {
impl Parse for CSSWideKeyword {
fn parse(input: &mut Parser) -> Result<Self, ()> {
match_ignore_ascii_case! { try!(input.expect_ident()),
"initial" => Ok(CSSWideKeyword::InitialKeyword),
"inherit" => Ok(CSSWideKeyword::InheritKeyword),
@ -736,7 +736,7 @@ impl PropertyDeclaration {
Ok(CSSWideKeyword::UnsetKeyword) | // Custom properties are alawys inherited
Ok(CSSWideKeyword::InheritKeyword) => DeclaredValue::Inherit,
Ok(CSSWideKeyword::InitialKeyword) => DeclaredValue::Initial,
Err(()) => match ::custom_properties::parse(input) {
Err(()) => match ::custom_properties::SpecifiedValue::parse(input) {
Ok(value) => DeclaredValue::Value(value),
Err(()) => return PropertyDeclarationParseResult::InvalidValue,
}

Просмотреть файл

@ -149,6 +149,7 @@ pub fn parse_border(context: &ParserContext, input: &mut Parser)
for corner in ['top-left', 'top-right', 'bottom-right', 'bottom-left']
)}">
use values::specified::basic_shape::BorderRadius;
use parser::Parse;
pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result<Longhands, ()> {
let _ignored = context;

Просмотреть файл

@ -7,6 +7,7 @@
<%helpers:shorthand name="outline" sub_properties="outline-color outline-style outline-width">
use properties::longhands::outline_width;
use values::specified;
use parser::Parse;
pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result<Longhands, ()> {
let _unused = context;

Просмотреть файл

@ -56,6 +56,7 @@
// https://drafts.csswg.org/css-flexbox/#flex-property
<%helpers:shorthand name="flex" sub_properties="flex-grow flex-shrink flex-basis">
use parser::Parse;
use app_units::Au;
use values::specified::{Number, Length, LengthOrPercentageOrAutoOrContent};

Просмотреть файл

@ -7,7 +7,6 @@
//! [values]: https://drafts.csswg.org/css-values/
pub use cssparser::{RGBA, Parser};
use parser::Parse;
use std::fmt::{self, Debug};
use style_traits::ToCss;

Просмотреть файл

@ -213,14 +213,6 @@ pub struct InsetRect {
}
impl InsetRect {
pub fn parse(input: &mut Parser) -> Result<InsetRect, ()> {
match_ignore_ascii_case! { try!(input.expect_function()),
"inset" => {
Ok(try!(input.parse_nested_block(InsetRect::parse_function_arguments)))
},
_ => Err(())
}
}
pub fn parse_function_arguments(input: &mut Parser) -> Result<InsetRect, ()> {
let (t, r, b, l) = try!(parse_four_sides(input, LengthOrPercentage::parse));
let mut rect = InsetRect {
@ -237,6 +229,17 @@ impl InsetRect {
}
}
impl Parse for InsetRect {
fn parse(input: &mut Parser) -> Result<Self, ()> {
match_ignore_ascii_case! { try!(input.expect_function()),
"inset" => {
Ok(try!(input.parse_nested_block(InsetRect::parse_function_arguments)))
},
_ => Err(())
}
}
}
impl ToCss for InsetRect {
// XXXManishearth again, we should try to reduce the number of values printed here
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
@ -374,14 +377,6 @@ pub struct Circle {
}
impl Circle {
pub fn parse(input: &mut Parser) -> Result<Circle, ()> {
match_ignore_ascii_case! { try!(input.expect_function()),
"circle" => {
Ok(try!(input.parse_nested_block(Circle::parse_function_arguments)))
},
_ => Err(())
}
}
pub fn parse_function_arguments(input: &mut Parser) -> Result<Circle, ()> {
let radius = input.try(ShapeRadius::parse).ok().unwrap_or_else(Default::default);
let position = if let Ok(_) = input.try(|input| input.expect_ident_matching("at")) {
@ -402,6 +397,17 @@ impl Circle {
}
}
impl Parse for Circle {
fn parse(input: &mut Parser) -> Result<Self, ()> {
match_ignore_ascii_case! { try!(input.expect_function()),
"circle" => {
Ok(try!(input.parse_nested_block(Circle::parse_function_arguments)))
},
_ => Err(())
}
}
}
impl ToCss for Circle {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(dest.write_str("circle("));
@ -446,14 +452,6 @@ pub struct Ellipse {
impl Ellipse {
pub fn parse(input: &mut Parser) -> Result<Ellipse, ()> {
match_ignore_ascii_case! { try!(input.expect_function()),
"ellipse" => {
Ok(try!(input.parse_nested_block(Ellipse::parse_function_arguments)))
},
_ => Err(())
}
}
pub fn parse_function_arguments(input: &mut Parser) -> Result<Ellipse, ()> {
let (a, b) = input.try(|input| -> Result<_, ()> {
Ok((try!(ShapeRadius::parse(input)), try!(ShapeRadius::parse(input))))
@ -477,6 +475,17 @@ impl Ellipse {
}
}
impl Parse for Ellipse {
fn parse(input: &mut Parser) -> Result<Self, ()> {
match_ignore_ascii_case! { try!(input.expect_function()),
"ellipse" => {
Ok(try!(input.parse_nested_block(Ellipse::parse_function_arguments)))
},
_ => Err(())
}
}
}
impl ToCss for Ellipse {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(dest.write_str("ellipse("));
@ -524,14 +533,6 @@ pub struct Polygon {
}
impl Polygon {
pub fn parse(input: &mut Parser) -> Result<Polygon, ()> {
match_ignore_ascii_case! { try!(input.expect_function()),
"polygon" => {
Ok(try!(input.parse_nested_block(Polygon::parse_function_arguments)))
},
_ => Err(())
}
}
pub fn parse_function_arguments(input: &mut Parser) -> Result<Polygon, ()> {
let fill = input.try(|input| {
let fill = FillRule::parse(input);
@ -550,6 +551,17 @@ impl Polygon {
}
}
impl Parse for Polygon {
fn parse(input: &mut Parser) -> Result<Self, ()> {
match_ignore_ascii_case! { try!(input.expect_function()),
"polygon" => {
Ok(try!(input.parse_nested_block(Polygon::parse_function_arguments)))
},
_ => Err(())
}
}
}
impl ToCss for Polygon {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(dest.write_str("polygon("));
@ -616,8 +628,8 @@ impl Default for ShapeRadius {
}
}
impl ShapeRadius {
pub fn parse(input: &mut Parser) -> Result<ShapeRadius, ()> {
impl Parse for ShapeRadius {
fn parse(input: &mut Parser) -> Result<Self, ()> {
input.try(LengthOrPercentage::parse).map(ShapeRadius::Length)
.or_else(|_| {
match_ignore_ascii_case! { try!(input.expect_ident()),
@ -703,8 +715,8 @@ impl ToCss for BorderRadius {
}
}
impl BorderRadius {
pub fn parse(input: &mut Parser) -> Result<BorderRadius, ()> {
impl Parse for BorderRadius {
fn parse(input: &mut Parser) -> Result<Self, ()> {
let widths = try!(parse_one_set_of_border_values(input));
let heights = if input.try(|input| input.expect_delim('/')).is_ok() {
try!(parse_one_set_of_border_values(input))
@ -781,8 +793,8 @@ pub enum FillRule {
impl ComputedValueAsSpecified for FillRule {}
impl FillRule {
pub fn parse(input: &mut Parser) -> Result<FillRule, ()> {
impl Parse for FillRule {
fn parse(input: &mut Parser) -> Result<FillRule, ()> {
match_ignore_ascii_case! { try!(input.expect_ident()),
"nonzero" => Ok(FillRule::NonZero),
"evenodd" => Ok(FillRule::EvenOdd),

Просмотреть файл

@ -840,15 +840,18 @@ impl LengthOrPercentageOrAuto {
}
}
#[inline]
pub fn parse(input: &mut Parser) -> Result<LengthOrPercentageOrAuto, ()> {
LengthOrPercentageOrAuto::parse_internal(input, AllowedNumericType::All)
}
#[inline]
pub fn parse_non_negative(input: &mut Parser) -> Result<LengthOrPercentageOrAuto, ()> {
LengthOrPercentageOrAuto::parse_internal(input, AllowedNumericType::NonNegative)
}
}
impl Parse for LengthOrPercentageOrAuto {
#[inline]
fn parse(input: &mut Parser) -> Result<Self, ()> {
LengthOrPercentageOrAuto::parse_internal(input, AllowedNumericType::All)
}
}
#[derive(Clone, PartialEq, Copy, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub enum LengthOrPercentageOrNone {
@ -899,15 +902,18 @@ impl LengthOrPercentageOrNone {
}
}
#[inline]
pub fn parse(input: &mut Parser) -> Result<LengthOrPercentageOrNone, ()> {
LengthOrPercentageOrNone::parse_internal(input, AllowedNumericType::All)
}
#[inline]
pub fn parse_non_negative(input: &mut Parser) -> Result<LengthOrPercentageOrNone, ()> {
LengthOrPercentageOrNone::parse_internal(input, AllowedNumericType::NonNegative)
}
}
impl Parse for LengthOrPercentageOrNone {
#[inline]
fn parse(input: &mut Parser) -> Result<Self, ()> {
LengthOrPercentageOrNone::parse_internal(input, AllowedNumericType::All)
}
}
pub type LengthOrNone = Either<Length, None_>;
impl LengthOrNone {
@ -949,8 +955,8 @@ impl ToCss for LengthOrPercentageOrAutoOrContent {
}
}
impl LengthOrPercentageOrAutoOrContent {
pub fn parse(input: &mut Parser) -> Result<LengthOrPercentageOrAutoOrContent, ()> {
impl Parse for LengthOrPercentageOrAutoOrContent {
fn parse(input: &mut Parser) -> Result<Self, ()> {
let context = AllowedNumericType::NonNegative;
match try!(input.next()) {
Token::Dimension(ref value, ref unit) if context.is_ok(value.value) =>

Просмотреть файл

@ -5,7 +5,7 @@
use app_units::Au;
use cssparser::{self, Parser, Token};
use euclid::size::Size2D;
use parser::ParserContext;
use parser::{Parse, ParserContext};
use self::url::SpecifiedUrl;
use std::ascii::AsciiExt;
use std::f32::consts::PI;
@ -36,8 +36,9 @@ pub struct CSSColor {
pub parsed: cssparser::Color,
pub authored: Option<String>,
}
impl CSSColor {
pub fn parse(input: &mut Parser) -> Result<CSSColor, ()> {
impl Parse for CSSColor {
fn parse(input: &mut Parser) -> Result<Self, ()> {
let start_position = input.position();
let authored = match input.next() {
Ok(Token::Ident(s)) => Some(s.into_owned()),
@ -192,9 +193,11 @@ impl BorderRadiusSize {
pub fn circle(radius: LengthOrPercentage) -> BorderRadiusSize {
BorderRadiusSize(Size2D::new(radius, radius))
}
}
impl Parse for BorderRadiusSize {
#[inline]
pub fn parse(input: &mut Parser) -> Result<BorderRadiusSize, ()> {
fn parse(input: &mut Parser) -> Result<Self, ()> {
let first = try!(LengthOrPercentage::parse_non_negative(input));
let second = input.try(LengthOrPercentage::parse_non_negative).unwrap_or(first);
Ok(BorderRadiusSize(Size2D::new(first, second)))
@ -236,9 +239,9 @@ const RAD_PER_DEG: CSSFloat = PI / 180.0;
const RAD_PER_GRAD: CSSFloat = PI / 200.0;
const RAD_PER_TURN: CSSFloat = PI * 2.0;
impl Angle {
impl Parse for Angle {
/// Parses an angle according to CSS-VALUES § 6.1.
pub fn parse(input: &mut Parser) -> Result<Angle, ()> {
fn parse(input: &mut Parser) -> Result<Self, ()> {
match try!(input.next()) {
Token::Dimension(ref value, ref unit) => Angle::parse_dimension(value.value, unit),
Token::Number(ref value) if value.value == 0. => Ok(Angle(0.)),
@ -248,7 +251,9 @@ impl Angle {
_ => Err(())
}
}
}
impl Angle {
pub fn parse_dimension(value: CSSFloat, unit: &str) -> Result<Angle, ()> {
match_ignore_ascii_case! { unit,
"deg" => Ok(Angle(value * RAD_PER_DEG)),
@ -399,8 +404,12 @@ impl Time {
Err(())
}
}
}
pub fn parse(input: &mut Parser) -> Result<Time, ()> {
impl ComputedValueAsSpecified for Time {}
impl Parse for Time {
fn parse(input: &mut Parser) -> Result<Self, ()> {
match input.next() {
Ok(Token::Dimension(ref value, ref unit)) => {
Time::parse_dimension(value.value, &unit)
@ -413,8 +422,6 @@ impl Time {
}
}
impl ComputedValueAsSpecified for Time {}
impl ToCss for Time {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
write!(dest, "{}s", self.0)
@ -427,11 +434,13 @@ pub struct Number(pub CSSFloat);
impl NoViewportPercentage for Number {}
impl Number {
pub fn parse(input: &mut Parser) -> Result<Number, ()> {
impl Parse for Number {
fn parse(input: &mut Parser) -> Result<Self, ()> {
parse_number(input).map(Number)
}
}
impl Number {
fn parse_with_minimum(input: &mut Parser, min: CSSFloat) -> Result<Number, ()> {
match parse_number(input) {
Ok(value) if value < min => Err(()),
@ -472,8 +481,8 @@ pub struct Opacity(pub CSSFloat);
impl NoViewportPercentage for Opacity {}
impl Opacity {
pub fn parse(input: &mut Parser) -> Result<Opacity, ()> {
impl Parse for Opacity {
fn parse(input: &mut Parser) -> Result<Self, ()> {
parse_number(input).map(Opacity)
}
}

Просмотреть файл

@ -169,7 +169,18 @@ impl Position {
})
}
pub fn parse(input: &mut Parser) -> Result<Position, ()> {
pub fn center() -> Position {
Position {
horiz_keyword: Some(Keyword::Center),
horiz_position: None,
vert_keyword: Some(Keyword::Center),
vert_position: None,
}
}
}
impl Parse for Position {
fn parse(input: &mut Parser) -> Result<Self, ()> {
let first = try!(PositionComponent::parse(input));
let second = input.try(PositionComponent::parse)
.unwrap_or(PositionComponent::Keyword(Keyword::Center));
@ -216,15 +227,6 @@ impl Position {
}
}
}
pub fn center() -> Position {
Position {
horiz_keyword: Some(Keyword::Center),
horiz_position: None,
vert_keyword: Some(Keyword::Center),
vert_position: None,
}
}
}
impl Keyword {
@ -362,25 +364,6 @@ impl HasViewportPercentage for PositionComponent {
}
impl PositionComponent {
pub fn parse(input: &mut Parser) -> Result<PositionComponent, ()> {
input.try(LengthOrPercentage::parse)
.map(PositionComponent::Length)
.or_else(|()| {
match try!(input.next()) {
Token::Ident(value) => {
match_ignore_ascii_case! { value,
"center" => Ok(PositionComponent::Keyword(Keyword::Center)),
"left" => Ok(PositionComponent::Keyword(Keyword::Left)),
"right" => Ok(PositionComponent::Keyword(Keyword::Right)),
"top" => Ok(PositionComponent::Keyword(Keyword::Top)),
"bottom" => Ok(PositionComponent::Keyword(Keyword::Bottom)),
_ => Err(())
}
},
_ => Err(())
}
})
}
#[inline]
pub fn to_length_or_percentage(self) -> LengthOrPercentage {
match self {
@ -389,3 +372,25 @@ impl PositionComponent {
}
}
}
impl Parse for PositionComponent {
fn parse(input: &mut Parser) -> Result<Self, ()> {
input.try(LengthOrPercentage::parse)
.map(PositionComponent::Length)
.or_else(|()| {
match try!(input.next()) {
Token::Ident(value) => {
match_ignore_ascii_case! { value,
"center" => Ok(PositionComponent::Keyword(Keyword::Center)),
"left" => Ok(PositionComponent::Keyword(Keyword::Left)),
"right" => Ok(PositionComponent::Keyword(Keyword::Right)),
"top" => Ok(PositionComponent::Keyword(Keyword::Top)),
"bottom" => Ok(PositionComponent::Keyword(Keyword::Bottom)),
_ => Err(())
}
},
_ => Err(())
}
})
}
}

Просмотреть файл

@ -3,6 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use parsing::parse;
use style::parser::Parse;
use style::values::specified::position::*;
use style_traits::ToCss;