Skip to content

Upgrade to latest Rust. #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Upgrade to latest Rust.
  • Loading branch information
metajack committed Jan 9, 2014
commit b359bf168e55068e37e35e3d521fc20febd9eb32
16 changes: 8 additions & 8 deletions ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,23 @@ impl ToStr for SyntaxError {
}


pub trait SkipWhitespaceIterable<'self> {
fn skip_whitespace(self) -> SkipWhitespaceIterator<'self>;
pub trait SkipWhitespaceIterable<'a> {
fn skip_whitespace(self) -> SkipWhitespaceIterator<'a>;
}

impl<'self> SkipWhitespaceIterable<'self> for &'self [ComponentValue] {
fn skip_whitespace(self) -> SkipWhitespaceIterator<'self> {
impl<'a> SkipWhitespaceIterable<'a> for &'a [ComponentValue] {
fn skip_whitespace(self) -> SkipWhitespaceIterator<'a> {
SkipWhitespaceIterator{ iter_with_whitespace: self.iter() }
}
}

#[deriving(Clone)]
pub struct SkipWhitespaceIterator<'self> {
iter_with_whitespace: vec::VecIterator<'self, ComponentValue>,
pub struct SkipWhitespaceIterator<'a> {
iter_with_whitespace: vec::VecIterator<'a, ComponentValue>,
}

impl<'self> Iterator<&'self ComponentValue> for SkipWhitespaceIterator<'self> {
fn next(&mut self) -> Option<&'self ComponentValue> {
impl<'a> Iterator<&'a ComponentValue> for SkipWhitespaceIterator<'a> {
fn next(&mut self) -> Option<&'a ComponentValue> {
for component_value in self.iter_with_whitespace {
if component_value != &WhiteSpace { return Some(component_value) }
}
Expand Down
3 changes: 1 addition & 2 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +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/. */

#[link(name = "cssparser", vers = "0.1")];

#[crate_id = "github.com/mozilla-servo/rust-cssparser#cssparser:0.1"];
#[feature(globs, macro_rules)];

extern mod extra;
Expand Down
4 changes: 2 additions & 2 deletions nth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub fn parse_nth(input: &[ComponentValue]) -> Option<(i32, i32)> {


type Nth = Option<(i32, i32)>;
type Iter<'self> = SkipWhitespaceIterator<'self>;
type Iter<'a> = SkipWhitespaceIterator<'a>;

fn parse_b(iter: &mut Iter, a: i32) -> Nth {
match iter.next() {
Expand Down Expand Up @@ -105,7 +105,7 @@ fn parse_end(iter: &mut Iter, a: i32, b: i32) -> Nth {
fn parse_n_dash_digits(string: &str) -> Option<i32> {
if string.len() >= 3
&& string.starts_with("n-")
&& string.slice_from(2).iter().all(|c| match c { '0'..'9' => true, _ => false })
&& string.slice_from(2).chars().all(|c| match c { '0'..'9' => true, _ => false })
{
let result = from_str(string.slice_from(1)); // Include the minus sign
assert!(result.is_some());
Expand Down
40 changes: 20 additions & 20 deletions serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl ast::ComponentValue {
},
Hash(ref value) => {
css.push_char('#');
for c in value.iter() {
for c in value.chars() {
serialize_char(c, css, /* is_identifier_start = */ false);
}
},
Expand All @@ -49,7 +49,7 @@ impl ast::ComponentValue {
let unit = unit.as_slice();
if unit == "e" || unit == "E" || unit.starts_with("e-") || unit.starts_with("E-") {
css.push_str("\\65 ");
for c in unit.slice_from(1).iter() {
for c in unit.slice_from(1).chars() {
serialize_char(c, css, /* is_identifier_start = */ false);
}
} else {
Expand Down Expand Up @@ -111,7 +111,7 @@ impl ast::ComponentValue {

pub fn serialize_identifier(value: &str, css: &mut ~str) {
// TODO: avoid decoding/re-encoding UTF-8?
let mut iter = value.iter();
let mut iter = value.chars();
let mut c = iter.next().unwrap();
if c == '-' {
c = match iter.next() {
Expand Down Expand Up @@ -144,7 +144,7 @@ fn serialize_char(c: char, css: &mut ~str, is_identifier_start: bool) {
pub fn serialize_string(value: &str, css: &mut ~str) {
css.push_char('"');
// TODO: avoid decoding/re-encoding UTF-8?
for c in value.iter() {
for c in value.chars() {
match c {
'"' => css.push_str("\\\""),
'\\' => css.push_str("\\\\"),
Expand All @@ -169,7 +169,7 @@ pub trait ToCss {
}


impl<'self, I: Iterator<&'self ComponentValue>> ToCss for I {
impl<'a, I: Iterator<&'a ComponentValue>> ToCss for I {
fn to_css_push(&mut self, css: &mut ~str) {
let mut previous = match self.next() {
None => return,
Expand All @@ -184,29 +184,29 @@ impl<'self, I: Iterator<&'self ComponentValue>> ToCss for I {
loop { match self.next() { None => break, Some(component_value) => {
let (a, b) = (previous, component_value);
if (
matches!(*a, Ident(*) | AtKeyword(*) | Hash(*) | IDHash(*) |
Dimension(*) | Delim('#') | Delim('-') | Number(*)) &&
matches!(*b, Ident(*) | Function(*) | URL(*) | BadURL(*) |
Number(*) | Percentage(*) | Dimension(*) | UnicodeRange(*))
matches!(*a, Ident(..) | AtKeyword(..) | Hash(..) | IDHash(..) |
Dimension(..) | Delim('#') | Delim('-') | Number(..)) &&
matches!(*b, Ident(..) | Function(..) | URL(..) | BadURL(..) |
Number(..) | Percentage(..) | Dimension(..) | UnicodeRange(..))
) || (
matches!(*a, Ident(*)) &&
matches!(*b, ParenthesisBlock(*))
matches!(*a, Ident(..)) &&
matches!(*b, ParenthesisBlock(..))
) || (
matches!(*a, Ident(*) | AtKeyword(*) | Hash(*) | IDHash(*) | Dimension(*)) &&
matches!(*a, Ident(..) | AtKeyword(..) | Hash(..) | IDHash(..) | Dimension(..)) &&
matches!(*b, Delim('-') | CDC)
) || (
matches!(*a, Delim('#') | Delim('-') | Number(*) | Delim('@')) &&
matches!(*b, Ident(*) | Function(*) | URL(*) | BadURL(*))
matches!(*a, Delim('#') | Delim('-') | Number(..) | Delim('@')) &&
matches!(*b, Ident(..) | Function(..) | URL(..) | BadURL(..))
) || (
matches!(*a, Delim('@')) &&
matches!(*b, Ident(*) | Function(*) | URL(*) | BadURL(*) |
UnicodeRange(*) | Delim('-'))
matches!(*b, Ident(..) | Function(..) | URL(..) | BadURL(..) |
UnicodeRange(..) | Delim('-'))
) || (
matches!(*a, UnicodeRange(*) | Delim('.') | Delim('+')) &&
matches!(*b, Number(*) | Percentage(*) | Dimension(*))
matches!(*a, UnicodeRange(..) | Delim('.') | Delim('+')) &&
matches!(*b, Number(..) | Percentage(..) | Dimension(..))
) || (
matches!(*a, UnicodeRange(*)) &&
matches!(*b, Ident(*) | Function(*) | Delim('?'))
matches!(*a, UnicodeRange(..)) &&
matches!(*b, Ident(..) | Function(..) | Delim('?'))
) || (match (a, b) { (&Delim(a), &Delim(b)) => matches!((a, b),
('#', '-') |
('$', '=') |
Expand Down
76 changes: 38 additions & 38 deletions tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::{str, run, task};
use std::rt::io;
use std::rt::io::Writer;
use std::io;
use std::io::{File, Writer};
use extra::{tempfile, json};
use extra::json::ToJson;
use extra::test;
Expand All @@ -16,7 +16,7 @@ use ast::*;


fn write_whole_file(path: &Path, data: &str) {
match io::file::open(path, io::Create, io::Write) {
match File::open_mode(path, io::Open, io::Write) {
Some(mut writer) => writer.write(data.as_bytes()),
None => fail!("could not open file"),
}
Expand All @@ -42,7 +42,7 @@ fn assert_json_eq(results: json::Json, expected: json::Json, message: ~str) {
let temp = tempfile::TempDir::new("rust-cssparser-tests").unwrap();
let results = results.to_pretty_str() + "\n";
let expected = expected.to_pretty_str() + "\n";
do task::try {
task::try(proc() {
let mut result_path = temp.path().clone();
result_path.push("results.json");
let mut expected_path = temp.path().clone();
Expand All @@ -51,14 +51,14 @@ fn assert_json_eq(results: json::Json, expected: json::Json, message: ~str) {
write_whole_file(&expected_path, expected);
run::process_status("colordiff", [~"-u1000", result_path.display().to_str(),
expected_path.display().to_str()]);
};
});

fail!(message)
}
}


fn run_raw_json_tests(json_data: &str, run: &fn (json::Json, json::Json)) {
fn run_raw_json_tests(json_data: &str, run: |json::Json, json::Json|) {
let items = match json::from_str(json_data) {
Ok(json::List(items)) => items,
_ => fail!("Invalid JSON")
Expand All @@ -77,86 +77,86 @@ fn run_raw_json_tests(json_data: &str, run: &fn (json::Json, json::Json)) {
}


fn run_json_tests<T: ToJson>(json_data: &str, parse: &fn (input: ~str) -> T) {
do run_raw_json_tests(json_data) |input, expected| {
fn run_json_tests<T: ToJson>(json_data: &str, parse: |input: ~str| -> T) {
run_raw_json_tests(json_data, |input, expected| {
match input {
json::String(input) => {
let result = parse(input.to_owned()).to_json();
assert_json_eq(result, expected, input);
},
_ => fail!("Unexpected JSON")
}
}
});
}


#[test]
fn component_value_list() {
do run_json_tests(include_str!("css-parsing-tests/component_value_list.json")) |input| {
run_json_tests(include_str!("css-parsing-tests/component_value_list.json"), |input| {
tokenize(input).map(|(c, _)| c).to_owned_vec()
}
});
}


#[test]
fn one_component_value() {
do run_json_tests(include_str!("css-parsing-tests/one_component_value.json")) |input| {
run_json_tests(include_str!("css-parsing-tests/one_component_value.json"), |input| {
parse_one_component_value(tokenize(input))
}
});
}


#[test]
fn declaration_list() {
do run_json_tests(include_str!("css-parsing-tests/declaration_list.json")) |input| {
run_json_tests(include_str!("css-parsing-tests/declaration_list.json"), |input| {
parse_declaration_list(tokenize(input)).to_owned_vec()
}
});
}


#[test]
fn one_declaration() {
do run_json_tests(include_str!("css-parsing-tests/one_declaration.json")) |input| {
run_json_tests(include_str!("css-parsing-tests/one_declaration.json"), |input| {
parse_one_declaration(tokenize(input))
}
});
}


#[test]
fn rule_list() {
do run_json_tests(include_str!("css-parsing-tests/rule_list.json")) |input| {
run_json_tests(include_str!("css-parsing-tests/rule_list.json"), |input| {
parse_rule_list(tokenize(input)).to_owned_vec()
}
});
}


#[test]
fn stylesheet() {
do run_json_tests(include_str!("css-parsing-tests/stylesheet.json")) |input| {
run_json_tests(include_str!("css-parsing-tests/stylesheet.json"), |input| {
parse_stylesheet_rules(tokenize(input)).to_owned_vec()
}
});
}


#[test]
fn one_rule() {
do run_json_tests(include_str!("css-parsing-tests/one_rule.json")) |input| {
run_json_tests(include_str!("css-parsing-tests/one_rule.json"), |input| {
parse_one_rule(tokenize(input))
}
});
}


#[test]
fn stylesheet_from_bytes() {
do run_raw_json_tests(include_str!("css-parsing-tests/stylesheet_bytes.json"))
run_raw_json_tests(include_str!("css-parsing-tests/stylesheet_bytes.json"),
|input, expected| {
let map = match input {
json::Object(map) => map,
_ => fail!("Unexpected JSON")
};

let result = {
let css = get_string(map, &~"css_bytes").unwrap().iter().map(|c| {
let css = get_string(map, &~"css_bytes").unwrap().chars().map(|c| {
assert!(c as u32 <= 0xFF);
c as u8
}).to_owned_vec();
Expand All @@ -170,7 +170,7 @@ fn stylesheet_from_bytes() {
(rules.to_owned_vec(), used_encoding.name().to_owned()).to_json()
};
assert_json_eq(result, expected, json::Object(map).to_str());
}
});

fn get_string<'a>(map: &'a json::Object, key: &~str) -> Option<&'a str> {
match map.find(key) {
Expand All @@ -183,13 +183,13 @@ fn stylesheet_from_bytes() {
}


fn run_color_tests(json_data: &str, to_json: &fn(result: Option<Color>) -> json::Json) {
do run_json_tests(json_data) |input| {
fn run_color_tests(json_data: &str, to_json: |result: Option<Color>| -> json::Json) {
run_json_tests(json_data, |input| {
match parse_one_component_value(tokenize(input)) {
Ok(component_value) => to_json(Color::parse(&component_value)),
Err(_reason) => json::Null,
}
}
});
}


Expand All @@ -208,14 +208,14 @@ fn color3_hsl() {
/// color3_keywords.json is different: R, G and B are in 0..255 rather than 0..1
#[test]
fn color3_keywords() {
do run_color_tests(include_str!("css-parsing-tests/color3_keywords.json")) |c| {
run_color_tests(include_str!("css-parsing-tests/color3_keywords.json"), |c| {
match c {
Some(RGBA(RGBA { red: r, green: g, blue: b, alpha: a }))
=> (~[r * 255., g * 255., b * 255., a]).to_json(),
Some(CurrentColor) => json::String(~"currentColor"),
None => json::Null,
}
}
});
}


Expand All @@ -242,19 +242,19 @@ fn bench_color_lookup_fail(b: &mut test::BenchHarness) {

#[test]
fn nth() {
do run_json_tests(include_str!("css-parsing-tests/An+B.json")) |input| {
run_json_tests(include_str!("css-parsing-tests/An+B.json"), |input| {
parse_nth(tokenize(input).map(|(c, _)| c).to_owned_vec())
}
});
}


#[test]
fn serializer() {
do run_json_tests(include_str!("css-parsing-tests/component_value_list.json")) |input| {
run_json_tests(include_str!("css-parsing-tests/component_value_list.json"), |input| {
let component_values = tokenize(input).map(|(c, _)| c).to_owned_vec();
let serialized = component_values.iter().to_css();
tokenize(serialized).map(|(c, _)| c).to_owned_vec()
}
});
}


Expand Down Expand Up @@ -351,7 +351,7 @@ fn list_to_json(list: &~[(ComponentValue, SourceLocation)]) -> ~[json::Json] {
impl ToJson for AtRule {
fn to_json(&self) -> json::Json {
match *self {
AtRule{name: ref name, prelude: ref prelude, block: ref block, _}
AtRule{name: ref name, prelude: ref prelude, block: ref block, ..}
=> json::List(~[json::String(~"at-rule"), name.to_json(),
prelude.to_json(), block.as_ref().map(list_to_json).to_json()])
}
Expand All @@ -362,7 +362,7 @@ impl ToJson for AtRule {
impl ToJson for QualifiedRule {
fn to_json(&self) -> json::Json {
match *self {
QualifiedRule{prelude: ref prelude, block: ref block, _}
QualifiedRule{prelude: ref prelude, block: ref block, ..}
=> json::List(~[json::String(~"qualified rule"),
prelude.to_json(), json::List(list_to_json(block))])
}
Expand All @@ -373,7 +373,7 @@ impl ToJson for QualifiedRule {
impl ToJson for Declaration {
fn to_json(&self) -> json::Json {
match *self {
Declaration{name: ref name, value: ref value, important: ref important, _}
Declaration{name: ref name, value: ref value, important: ref important, ..}
=> json::List(~[json::String(~"declaration"), name.to_json(),
value.to_json(), important.to_json()])
}
Expand Down
Loading