Skip to content

Commit

Permalink
Use the ParserContext along with Parser in the parse function
Browse files Browse the repository at this point in the history
  • Loading branch information
wafflespeanut committed Nov 27, 2016
1 parent c4f87f4 commit dee1a65
Show file tree
Hide file tree
Showing 30 changed files with 318 additions and 285 deletions.
4 changes: 2 additions & 2 deletions components/style/custom_properties.rs
Expand Up @@ -8,7 +8,7 @@

use Atom;
use cssparser::{Delimiter, Parser, SourcePosition, Token, TokenSerializationType};
use parser::Parse;
use parser::{Parse, ParserContext};
use properties::DeclaredValue;
use std::ascii::AsciiExt;
use std::borrow::Cow;
Expand Down Expand Up @@ -113,7 +113,7 @@ impl ComputedValue {
}

impl Parse for SpecifiedValue {
fn parse(input: &mut Parser) -> Result<Self, ()> {
fn parse(_context: &ParserContext, 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 {
Expand Down
4 changes: 1 addition & 3 deletions components/style/parser.rs
Expand Up @@ -69,8 +69,6 @@ pub fn log_css_error(input: &mut Parser, position: SourcePosition, message: &str

// 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.
// There may need to be two traits -- one for parsing with context, and one
// for parsing without
pub trait Parse {
fn parse(input: &mut Parser) -> Result<Self, ()> where Self: Sized;
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> where Self: Sized;
}
16 changes: 11 additions & 5 deletions components/style/properties/helpers.mako.rs
Expand Up @@ -16,7 +16,7 @@
</%call>
</%def>

<%def name="predefined_type(name, type, initial_value, parse_method='parse', needs_context=False, **kwargs)">
<%def name="predefined_type(name, type, initial_value, parse_method='parse', needs_context=True, **kwargs)">
<%call expr="longhand(name, predefined_type=type, **kwargs)">
#[allow(unused_imports)]
use app_units::Au;
Expand Down Expand Up @@ -283,7 +283,7 @@
% if not property.derived_from:
pub fn parse_declared(context: &ParserContext, input: &mut Parser)
-> Result<DeclaredValue<SpecifiedValue>, ()> {
match input.try(CSSWideKeyword::parse) {
match input.try(|i| CSSWideKeyword::parse(context, i)) {
Ok(CSSWideKeyword::InheritKeyword) => Ok(DeclaredValue::Inherit),
Ok(CSSWideKeyword::InitialKeyword) => Ok(DeclaredValue::Initial),
Ok(CSSWideKeyword::UnsetKeyword) => Ok(DeclaredValue::${
Expand Down Expand Up @@ -515,7 +515,7 @@
% endif
</%def>

<%def name="four_sides_shorthand(name, sub_property_pattern, parser_function)">
<%def name="four_sides_shorthand(name, sub_property_pattern, parser_function, needs_context=True)">
<%self:shorthand name="${name}" sub_properties="${
' '.join(sub_property_pattern % side
for side in ['top', 'right', 'bottom', 'left'])}">
Expand All @@ -524,8 +524,14 @@
use super::parse_four_sides;
use values::specified;

pub fn parse_value(_: &ParserContext, input: &mut Parser) -> Result<Longhands, ()> {
let (top, right, bottom, left) = try!(parse_four_sides(input, ${parser_function}));
pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result<Longhands, ()> {
let (top, right, bottom, left) =
% if needs_context:
try!(parse_four_sides(input, |i| ${parser_function}(context, i)));
% else:
try!(parse_four_sides(input, ${parser_function}));
let _unused = context;
% endif
Ok(Longhands {
% for side in ["top", "right", "bottom", "left"]:
${to_rust_ident(sub_property_pattern % side)}: Some(${side}),
Expand Down
10 changes: 5 additions & 5 deletions components/style/properties/longhand/background.mako.rs
Expand Up @@ -120,9 +120,9 @@ ${helpers.predefined_type("background-color", "CSSColor",
}
}

pub fn parse(_context: &ParserContext, input: &mut Parser)
pub fn parse(context: &ParserContext, input: &mut Parser)
-> Result<SpecifiedValue, ()> {
Ok(try!(Position::parse(input)))
Ok(try!(Position::parse(context, input)))
}
</%helpers:vector_longhand>

Expand Down Expand Up @@ -302,7 +302,7 @@ ${helpers.single_keyword("background-origin",
})
}

pub fn parse(_: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue,()> {
pub fn parse(context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue,()> {
let width;
if let Ok(value) = input.try(|input| {
match input.next() {
Expand All @@ -318,7 +318,7 @@ ${helpers.single_keyword("background-origin",
}) {
return Ok(value)
} else {
width = try!(specified::LengthOrPercentageOrAuto::parse(input))
width = try!(specified::LengthOrPercentageOrAuto::parse(context, input))
}

let height;
Expand All @@ -330,7 +330,7 @@ ${helpers.single_keyword("background-origin",
}) {
height = value
} else {
height = try!(specified::LengthOrPercentageOrAuto::parse(input));
height = try!(specified::LengthOrPercentageOrAuto::parse(context, input));
}

Ok(SpecifiedValue::Explicit(ExplicitSize {
Expand Down
23 changes: 12 additions & 11 deletions components/style/properties/longhand/border.mako.rs
Expand Up @@ -18,7 +18,8 @@
% for side in ALL_SIDES:
${helpers.predefined_type("border-%s-style" % side[0], "BorderStyle",
"specified::BorderStyle::none",
need_clone=True, animatable=False, logical = side[1])}
needs_context=False, need_clone=True,
animatable=False, logical = side[1])}
% endfor

% for side in ALL_SIDES:
Expand All @@ -32,9 +33,9 @@
pub type SpecifiedValue = BorderWidth;

#[inline]
pub fn parse(_context: &ParserContext, input: &mut Parser)
pub fn parse(context: &ParserContext, input: &mut Parser)
-> Result<SpecifiedValue, ()> {
BorderWidth::parse(input)
BorderWidth::parse(context, input)
}

pub mod computed_value {
Expand Down Expand Up @@ -503,12 +504,12 @@ ${helpers.single_keyword("-moz-float-edge", "content-box margin-box",
}

impl Parse for SingleSpecifiedValue {
fn parse(input: &mut Parser) -> Result<Self, ()> {
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
if input.try(|input| input.expect_ident_matching("auto")).is_ok() {
return Ok(SingleSpecifiedValue::Auto);
}

if let Ok(len) = input.try(|input| LengthOrPercentage::parse(input)) {
if let Ok(len) = input.try(|input| LengthOrPercentage::parse(context, input)) {
return Ok(SingleSpecifiedValue::LengthOrPercentage(len));
}

Expand All @@ -517,10 +518,10 @@ ${helpers.single_keyword("-moz-float-edge", "content-box margin-box",
}
}

pub fn parse(_context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
pub fn parse(context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
let mut values = vec![];
for _ in 0..4 {
let value = input.try(|input| SingleSpecifiedValue::parse(input));
let value = input.try(|input| SingleSpecifiedValue::parse(context, input));
match value {
Ok(val) => values.push(val),
Err(_) => break,
Expand Down Expand Up @@ -709,8 +710,8 @@ ${helpers.single_keyword("-moz-float-edge", "content-box margin-box",
}

impl Parse for PercentageOrNumber {
fn parse(input: &mut Parser) -> Result<Self, ()> {
if let Ok(per) = input.try(|input| Percentage::parse(input)) {
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
if let Ok(per) = input.try(|input| Percentage::parse(context, input)) {
return Ok(PercentageOrNumber::Percentage(per));
}

Expand All @@ -719,12 +720,12 @@ ${helpers.single_keyword("-moz-float-edge", "content-box margin-box",
}
}

pub fn parse(_context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
pub fn parse(context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
let mut fill = input.try(|input| input.expect_ident_matching("fill")).is_ok();

let mut values = vec![];
for _ in 0..4 {
let value = input.try(|input| PercentageOrNumber::parse(input));
let value = input.try(|input| PercentageOrNumber::parse(context, input));
match value {
Ok(val) => values.push(val),
Err(_) => break,
Expand Down

0 comments on commit dee1a65

Please sign in to comment.