Skip to content

Rust 20140224 #36

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

Closed
wants to merge 6 commits into from
Closed
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
2 changes: 1 addition & 1 deletion Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ RUST_SRC=$(shell find $(VPATH)/. -type f -name '*.rs')
all: libcssparser.dummy

libcssparser.dummy: lib.rs $(RUST_SRC)
$(RUSTC) $(RUSTFLAGS) $< --lib --out-dir .
$(RUSTC) $(RUSTFLAGS) $< --crate-type=lib --out-dir .
touch $@

cssparser-test: lib.rs $(RUST_SRC)
Expand Down
13 changes: 7 additions & 6 deletions ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +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::fmt;
use std::vec;


Expand Down Expand Up @@ -94,7 +95,7 @@ pub struct AtRule {
pub enum DeclarationListItem {
Declaration(Declaration),
// A better idea for a name that means "at-rule" but is not "AtRule"?
Decl_AtRule(AtRule),
DeclAtRule(AtRule),
}

#[deriving(Eq)]
Expand All @@ -119,9 +120,9 @@ pub enum ErrorReason {
// This is meant to be extended
}

impl ToStr for SyntaxError {
fn to_str(&self) -> ~str {
format!("{:u}:{:u} {:?}", self.location.line, self.location.column, self.reason)
impl fmt::Show for SyntaxError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f.buf, "{:u}:{:u} {:?}", self.location.line, self.location.column, self.reason)
}
}

Expand All @@ -138,7 +139,7 @@ impl<'a> SkipWhitespaceIterable<'a> for &'a [ComponentValue] {

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

impl<'a> Iterator<&'a ComponentValue> for SkipWhitespaceIterator<'a> {
Expand All @@ -162,7 +163,7 @@ impl MoveSkipWhitespaceIterable for ~[ComponentValue] {
}

pub struct MoveSkipWhitespaceIterator {
iter_with_whitespace: vec::MoveIterator<ComponentValue>,
iter_with_whitespace: vec::MoveItems<ComponentValue>,
}

impl Iterator<ComponentValue> for MoveSkipWhitespaceIterator {
Expand Down
9 changes: 5 additions & 4 deletions color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::ascii::StrAsciiExt;
use std::cmp;

use ast::*;

Expand Down Expand Up @@ -250,7 +251,7 @@ fn parse_color_function(name: &str, arguments: &[ComponentValue])

let mut iter = arguments.skip_whitespace();
macro_rules! expect_comma(
() => ( if iter.next() != Some(&Comma) { return None } );
() => ( match iter.next() { Some(&Comma) => {}, _ => { return None } } );
)
macro_rules! expect_percentage(
() => ( match iter.next() {
Expand Down Expand Up @@ -297,9 +298,9 @@ fn parse_color_function(name: &str, arguments: &[ComponentValue])
let hue = expect_number!() / 360.;
let hue = hue - hue.floor();
expect_comma!();
let saturation = (expect_percentage!() / 100.).max(&0.).min(&1.);
let saturation = cmp::min(cmp::max((expect_percentage!() / 100.), 0.), 1.);
expect_comma!();
let lightness = (expect_percentage!() / 100.).max(&0.).min(&1.);
let lightness = cmp::min(cmp::max((expect_percentage!() / 100.), 0.), 1.);

// http://www.w3.org/TR/css3-color/#hsl-color
fn hue_to_rgb(m1: f64, m2: f64, mut h: f64) -> f64 {
Expand All @@ -321,7 +322,7 @@ fn parse_color_function(name: &str, arguments: &[ComponentValue])

let alpha = if has_alpha {
expect_comma!();
(expect_number!()).max(&0.).min(&1.) as f32
cmp::min(cmp::max((expect_number!()), 0.), 1.) as f32
} else {
1.
};
Expand Down
3 changes: 2 additions & 1 deletion from_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +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::cmp;
use std::str;

use encoding::label::encoding_from_whatwg_label;
Expand Down Expand Up @@ -44,7 +45,7 @@ pub fn decode_stylesheet_bytes(css: &[u8], protocol_encoding_label: Option<&str>
if css.starts_with("@charset \"".as_bytes()) {
// 10 is "@charset \"".len()
// 100 is arbitrary so that no encoding label is more than 100-10 bytes.
match css.slice(10, css.len().min(&100)).position_elem(&('"' as u8)) {
match css.slice(10, cmp::min(css.len(), 100)).position_elem(&('"' as u8)) {
None => (),
Some(label_length)
=> if css.slice_from(10 + label_length).starts_with("\";".as_bytes()) {
Expand Down
7 changes: 5 additions & 2 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
#[crate_id = "github.com/mozilla-servo/rust-cssparser#cssparser:0.1"];
#[feature(globs, macro_rules)];

extern mod extra;
extern mod encoding; // https://github.com/lifthrasiir/rust-encoding
extern crate extra;
extern crate encoding; // https://github.com/lifthrasiir/rust-encoding

#[cfg(test)]
extern crate test;

pub use tokenizer::tokenize;
pub use parser::{parse_stylesheet_rules, parse_rule_list, parse_declaration_list,
Expand Down
8 changes: 4 additions & 4 deletions nth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn parse_nth(input: &[ComponentValue]) -> Option<(i32, i32)> {
match unit {
"n" => parse_b(iter, a as i32),
"n-" => parse_signless_b(iter, a as i32, -1),
_ => match(parse_n_dash_digits(unit)) {
_ => match parse_n_dash_digits(unit) {
Some(b) => parse_end(iter, a as i32, b),
_ => None
},
Expand All @@ -40,11 +40,11 @@ pub fn parse_nth(input: &[ComponentValue]) -> Option<(i32, i32)> {
"-n" => parse_b(iter, -1),
"n-" => parse_signless_b(iter, 1, -1),
"-n-" => parse_signless_b(iter, -1, -1),
_ if ident.starts_with("-") => match(parse_n_dash_digits(ident.slice_from(1))) {
_ if ident.starts_with("-") => match parse_n_dash_digits(ident.slice_from(1)) {
Some(b) => parse_end(iter, -1, b),
_ => None
},
_ => match(parse_n_dash_digits(ident)) {
_ => match parse_n_dash_digits(ident) {
Some(b) => parse_end(iter, 1, b),
_ => None
},
Expand All @@ -56,7 +56,7 @@ pub fn parse_nth(input: &[ComponentValue]) -> Option<(i32, i32)> {
match ident {
"n" => parse_b(iter, 1),
"n-" => parse_signless_b(iter, 1, -1),
_ => match(parse_n_dash_digits(ident)) {
_ => match parse_n_dash_digits(ident) {
Some(b) => parse_end(iter, 1, b),
_ => None
},
Expand Down
2 changes: 1 addition & 1 deletion parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ for DeclarationListParser<T> {
match component_value {
WhiteSpace | Semicolon => (),
AtKeyword(name)
=> return Some(Ok(Decl_AtRule(parse_at_rule(iter, name, location)))),
=> return Some(Ok(DeclAtRule(parse_at_rule(iter, name, location)))),
_ => return Some(match parse_declaration(iter, component_value, location) {
Ok(declaration) => Ok(Declaration(declaration)),
Err(e) => {
Expand Down
17 changes: 8 additions & 9 deletions tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
* 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::{str, run, task};
use std::io;
use std::io::{File, Writer};
use std::{io, str, task};
use std::io::{File, Process, Writer};
use extra::{tempfile, json};
use extra::json::ToJson;
use extra::test;
use test;

use encoding::label::encoding_from_whatwg_label;

Expand All @@ -17,8 +16,8 @@ use ast::*;

fn write_whole_file(path: &Path, data: &str) {
match File::open_mode(path, io::Open, io::Write) {
Some(mut writer) => writer.write(data.as_bytes()),
None => fail!("could not open file"),
Ok(mut writer) => { writer.write(data.as_bytes()); },
_ => fail!("could not open file"),
}
}

Expand Down Expand Up @@ -49,8 +48,8 @@ fn assert_json_eq(results: json::Json, expected: json::Json, message: ~str) {
expected_path.push("expected.json");
write_whole_file(&result_path, results);
write_whole_file(&expected_path, expected);
run::process_status("colordiff", [~"-u1000", result_path.display().to_str(),
expected_path.display().to_str()]);
Process::status("colordiff", [~"-u1000", result_path.display().to_str(),
expected_path.display().to_str()]);
});

fail!(message)
Expand Down Expand Up @@ -333,7 +332,7 @@ impl ToJson for DeclarationListItem {
fn to_json(&self) -> json::Json {
match *self {
Declaration(ref declaration) => declaration.to_json(),
Decl_AtRule(ref at_rule) => at_rule.to_json(),
DeclAtRule(ref at_rule) => at_rule.to_json(),
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,8 @@ fn next_component_value(tokenizer: &mut Tokenizer) -> Option<Node> {
}
},
'.' => {
if (
tokenizer.position + 1 < tokenizer.length
&& is_match!(tokenizer.char_at(1), '0'..'9')
if tokenizer.position + 1 < tokenizer.length
&& is_match!(tokenizer.char_at(1), '0'..'9'
) {
consume_numeric(tokenizer)
} else {
Expand Down