From b359bf168e55068e37e35e3d521fc20febd9eb32 Mon Sep 17 00:00:00 2001 From: Jack Moffitt Date: Fri, 20 Dec 2013 21:56:03 -0700 Subject: [PATCH] Upgrade to latest Rust. --- ast.rs | 16 +++++------ lib.rs | 3 +- nth.rs | 4 +-- serializer.rs | 40 +++++++++++++-------------- tests.rs | 76 +++++++++++++++++++++++++-------------------------- tokenizer.rs | 4 +-- 6 files changed, 71 insertions(+), 72 deletions(-) diff --git a/ast.rs b/ast.rs index 9c34d9ff..836b503b 100644 --- a/ast.rs +++ b/ast.rs @@ -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) } } diff --git a/lib.rs b/lib.rs index f71d4645..b5837726 100644 --- a/lib.rs +++ b/lib.rs @@ -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; diff --git a/nth.rs b/nth.rs index 1e4e9cf0..4108d466 100644 --- a/nth.rs +++ b/nth.rs @@ -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() { @@ -105,7 +105,7 @@ fn parse_end(iter: &mut Iter, a: i32, b: i32) -> Nth { fn parse_n_dash_digits(string: &str) -> Option { 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()); diff --git a/serializer.rs b/serializer.rs index d491c5c3..3a788506 100644 --- a/serializer.rs +++ b/serializer.rs @@ -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); } }, @@ -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 { @@ -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() { @@ -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("\\\\"), @@ -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, @@ -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), ('#', '-') | ('$', '=') | diff --git a/tests.rs b/tests.rs index 0cb36574..595b45c8 100644 --- a/tests.rs +++ b/tests.rs @@ -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; @@ -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"), } @@ -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(); @@ -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") @@ -77,8 +77,8 @@ fn run_raw_json_tests(json_data: &str, run: &fn (json::Json, json::Json)) { } -fn run_json_tests(json_data: &str, parse: &fn (input: ~str) -> T) { - do run_raw_json_tests(json_data) |input, expected| { +fn run_json_tests(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(); @@ -86,69 +86,69 @@ fn run_json_tests(json_data: &str, parse: &fn (input: ~str) -> T) { }, _ => 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, @@ -156,7 +156,7 @@ fn stylesheet_from_bytes() { }; 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(); @@ -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) { @@ -183,13 +183,13 @@ fn stylesheet_from_bytes() { } -fn run_color_tests(json_data: &str, to_json: &fn(result: Option) -> json::Json) { - do run_json_tests(json_data) |input| { +fn run_color_tests(json_data: &str, to_json: |result: Option| -> 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, } - } + }); } @@ -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, } - } + }); } @@ -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() - } + }); } @@ -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()]) } @@ -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))]) } @@ -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()]) } diff --git a/tokenizer.rs b/tokenizer.rs index 53112302..24154252 100644 --- a/tokenizer.rs +++ b/tokenizer.rs @@ -46,7 +46,7 @@ fn test_preprocess() { } -struct Tokenizer { +pub struct Tokenizer { priv input: ~str, priv length: uint, // All counted in bytes, not characters priv position: uint, // All counted in bytes, not characters @@ -101,7 +101,7 @@ fn next_component_value(tokenizer: &mut Tokenizer) -> Option { consume_comments(tokenizer); if tokenizer.is_eof() { if cfg!(test) { - assert!(tokenizer.line == tokenizer.input.split_iter('\n').len(), + assert!(tokenizer.line == tokenizer.input.split('\n').len(), "The tokenizer is missing a tokenizer.new_line() call somewhere.") } return None