Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Oct 9, 2015
1 parent 3fbebc6 commit 29f757d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
24 changes: 13 additions & 11 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ impl<'i, 't> Parser<'i, 't> {
/// This can help tell e.g. `color: green;` from `color: green 4px;`
#[inline]
pub fn parse_entirely<F, T>(&mut self, parse: F) -> Result<T, ()>
where F: FnOnce(&mut Parser) -> Result<T, ()> {
where F: FnOnce(&mut Parser<'i, 't>) -> Result<T, ()> {
let result = parse(self);
try!(self.expect_exhausted());
result
Expand Down Expand Up @@ -374,7 +374,9 @@ impl<'i, 't> Parser<'i, 't> {
/// The result is overridden to `Err(())` if the closure leaves some input before that point.
#[inline]
pub fn parse_nested_block<F, T>(&mut self, parse: F) -> Result <T, ()>
where F: FnOnce(&mut Parser) -> Result<T, ()> {
where for<'tt> F: FnOnce(&mut Parser<'i, 'tt>) -> Result<T, ()> {
// where 'tt: 't, F: FnOnce(&mut Parser<'i, 'tt>) -> Result<T, ()> {
// where F: FnOnce(&mut Parser) -> Result<T, ()> {
let block_type = self.at_start_of.take().expect("\
A nested parser can only be created when a Function, \
ParenthesisBlock, SquareBracketBlock, or CurlyBracketBlock \
Expand Down Expand Up @@ -412,7 +414,7 @@ impl<'i, 't> Parser<'i, 't> {
#[inline]
pub fn parse_until_before<F, T>(&mut self, delimiters: Delimiters, parse: F)
-> Result <T, ()>
where F: FnOnce(&mut Parser) -> Result<T, ()> {
where for<'tt> F: FnOnce(&mut Parser<'i, 'tt>) -> Result<T, ()> {
let delimiters = self.stop_before | delimiters;
let result;
// Introduce a new scope to limit duration of nested_parser’s borrow
Expand Down Expand Up @@ -451,7 +453,7 @@ impl<'i, 't> Parser<'i, 't> {
#[inline]
pub fn parse_until_after<F, T>(&mut self, delimiters: Delimiters, parse: F)
-> Result <T, ()>
where F: FnOnce(&mut Parser) -> Result<T, ()> {
where for<'tt> F: FnOnce(&mut Parser<'i, 'tt>) -> Result<T, ()> {
let result = self.parse_until_before(delimiters, parse);
let next_byte = self.tokenizer.next_byte();
if next_byte.is_some() && !self.stop_before.contains(Delimiters::from_byte(next_byte)) {
Expand Down Expand Up @@ -481,7 +483,7 @@ impl<'i, 't> Parser<'i, 't> {

/// Parse a <ident-token> whose unescaped value is an ASCII-insensitive match for the given value.
#[inline]
pub fn expect_ident_matching<'a>(&mut self, expected_value: &str) -> Result<(), ()> {
pub fn expect_ident_matching(&mut self, expected_value: &str) -> Result<(), ()> {
match try!(self.next()) {
Token::Ident(ref value) if value.eq_ignore_ascii_case(expected_value) => Ok(()),
_ => Err(())
Expand Down Expand Up @@ -512,9 +514,9 @@ impl<'i, 't> Parser<'i, 't> {
pub fn expect_url(&mut self) -> Result<Cow<'i, str>, ()> {
match try!(self.next()) {
Token::UnquotedUrl(value) => Ok(value),
Token::Function(ref name) if name.eq_ignore_ascii_case("url") => {
self.parse_nested_block(|input| input.expect_string())
},
// Token::Function(ref name) if name.eq_ignore_ascii_case("url") => {
// self.parse_nested_block(|input| input.expect_string())
// },
_ => Err(())
}
}
Expand All @@ -525,9 +527,9 @@ impl<'i, 't> Parser<'i, 't> {
match try!(self.next()) {
Token::UnquotedUrl(value) => Ok(value),
Token::QuotedString(value) => Ok(value),
Token::Function(ref name) if name.eq_ignore_ascii_case("url") => {
self.parse_nested_block(|input| input.expect_string())
},
// Token::Function(ref name) if name.eq_ignore_ascii_case("url") => {
// self.parse_nested_block(|input| input.expect_string())
// },
_ => Err(())
}
}
Expand Down
25 changes: 19 additions & 6 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* 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/. */

use std::borrow::Cow::Borrowed;
use std::borrow::Cow::{self, Borrowed};
use std::fs::File;
use std::io::{self, Write};
use std::path::Path;
Expand Down Expand Up @@ -247,11 +247,16 @@ fn expect_no_error_token() {
/// https://github.com/servo/rust-cssparser/issues/71
#[test]
fn outer_block_end_consumed() {
let mut input = Parser::new("(calc(true))");
assert!(input.expect_parenthesis_block().is_ok());
assert!(input.parse_nested_block(|input| input.expect_function_matching("calc")).is_ok());
println!("{:?}", input.position());
assert_eq!(input.next(), Err(()));
Parser::new("").parse_nested_block(|input| input.expect_exhausted());


// let mut input = Parser::new("(calc(true))");
// assert!(input.expect_parenthesis_block().is_ok());
// let r =
// input.parse_nested_block(|input| input.expect_function_matching("calc"));
// assert!(r.is_ok());
// println!("{:?}", input.position());
// assert_eq!(input.next(), Err(()));
}

#[test]
Expand All @@ -274,6 +279,14 @@ fn unquoted_url_escaping() {
assert_eq!(Parser::new(&serialized).next(), Ok(token))
}

#[test]
fn test_expect_url() {
fn parse(s: &str) -> Result<Cow<str>, ()> {
Parser::new(s).expect_url()
}
assert_eq!(parse("url()").unwrap(), "");
}


fn run_color_tests<F: Fn(Result<Color, ()>) -> Json>(json_data: &str, to_json: F) {
run_json_tests(json_data, |input| {
Expand Down

0 comments on commit 29f757d

Please sign in to comment.