diff --git a/Cargo.toml b/Cargo.toml index 6f2b8578..79ac0a62 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,9 @@ git = "https://github.com/servo/string-cache" [dependencies.string_cache_plugin] git = "https://github.com/servo/string-cache" +[dependencies.mac] +git = "https://github.com/reem/rust-mac" + [dependencies.html5ever_macros] path = "macros" diff --git a/benches/tokenizer.rs b/benches/tokenizer.rs index 73bf263e..4cb117f5 100644 --- a/benches/tokenizer.rs +++ b/benches/tokenizer.rs @@ -7,7 +7,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![feature(box_syntax, core, env, io, path, std_misc, start, test)] +#![feature(box_syntax, core, env, old_io, old_path, std_misc, start, test)] extern crate test; extern crate html5ever; @@ -56,7 +56,7 @@ impl Bench { // Break the input into chunks of 1024 chars (= a few kB). // This simulates reading from the network. let mut input = vec![]; - let mut total = 0us; + let mut total = 0usize; while total < size { // The by_ref() call is important, otherwise we get wrong results! // See rust-lang/rust#18045. diff --git a/examples/noop-tokenize.rs b/examples/noop-tokenize.rs index cb3db552..cc846039 100644 --- a/examples/noop-tokenize.rs +++ b/examples/noop-tokenize.rs @@ -9,13 +9,13 @@ // Run a single benchmark once. For use with profiling tools. -#![feature(core, env, old_io, test, old_path)] +#![feature(core, env, test, io, path, fs)] extern crate test; extern crate html5ever; -use std::old_io as io; -use std::env; +use std::{fs, env}; +use std::io::prelude::*; use std::default::Default; use test::black_box; @@ -34,12 +34,13 @@ impl TokenSink for Sink { } fn main() { - let mut path = env::current_exe().ok().expect("can't get exe path"); + let mut path = env::current_exe().unwrap(); path.push("../data/bench/"); path.push(env::args().nth(1).unwrap().as_slice()); - let mut file = io::File::open(&path).ok().expect("can't open file"); - let file_input = file.read_to_string().ok().expect("can't read file"); + let mut file = fs::File::open(&path).unwrap(); + let mut file_input = String::new(); + file.read_to_string(&mut file_input).unwrap(); tokenize_to(Sink, one_input(file_input), TokenizerOpts { profile: true, diff --git a/macros/Cargo.toml b/macros/Cargo.toml index 6edc57be..9885db95 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -8,3 +8,6 @@ authors = [ "The html5ever Project Developers" ] name = "html5ever_macros" plugin = true + +[dependencies.mac] +git = "https://github.com/reem/rust-mac" diff --git a/macros/src/internal.rs b/macros/src/internal.rs deleted file mode 100644 index 58da1dce..00000000 --- a/macros/src/internal.rs +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2014 The html5ever Project Developers. See the -// COPYRIGHT file at the top-level directory of this distribution. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Macros for use in defining other macros. Not exported. - -macro_rules! bail ( ($cx:expr, $sp:expr, $msg:expr) => ({ - $cx.span_err($sp, $msg); - return ::syntax::ext::base::DummyResult::any($sp); -})); - -macro_rules! bail_if ( ($e:expr, $cx:expr, $sp:expr, $msg:expr) => ( - if $e { bail!($cx, $sp, $msg) } -)); - -macro_rules! expect ( ($cx:expr, $sp:expr, $e:expr, $msg:expr) => ( - match $e { - Some(x) => x, - None => bail!($cx, $sp, $msg), - } -)); - diff --git a/macros/src/lib.rs b/macros/src/lib.rs index e9569bb3..9e761108 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -18,11 +18,10 @@ extern crate syntax; extern crate rustc; extern crate "serialize" as rustc_serialize; -use rustc::plugin::Registry; - -// Internal macros for use in defining other macros. #[macro_use] -mod internal; +extern crate mac; + +use rustc::plugin::Registry; // Make these public so that rustdoc will generate documentation for them. pub mod named_entities; diff --git a/macros/src/match_token.rs b/macros/src/match_token.rs index 9469b272..746e9dfb 100644 --- a/macros/src/match_token.rs +++ b/macros/src/match_token.rs @@ -297,7 +297,7 @@ pub fn expand(cx: &mut ExtCtxt, span: Span, toks: &[ast::TokenTree]) -> Box x, - None => bail!(cx, span, "need at least one match arm"), + None => ext_bail!(cx, span, "need at least one match arm"), }; // Code for the arms other than the last one. @@ -322,11 +322,11 @@ pub fn expand(cx: &mut ExtCtxt, span: Span, toks: &[ast::TokenTree]) -> Box bail!(cx, rhs.span, "'else' may not appear with an ordinary pattern"), + => ext_bail!(cx, rhs.span, "'else' may not appear with an ordinary pattern"), // ordinary pattern => expression (Pat(pat), Expr(expr)) => { - bail_if!(!wildcards.is_empty(), cx, lhs.span, + ext_bail_if!(!wildcards.is_empty(), cx, lhs.span, "ordinary patterns may not appear after wildcard tags"); push_all(&mut arm_code, quote_tokens!(&mut *cx, $binding $pat => $expr,)); } @@ -334,8 +334,8 @@ pub fn expand(cx: &mut ExtCtxt, span: Span, toks: &[ast::TokenTree]) -> Box ... => else (Tags(tags), Else) => { for Spanned { span, node: tag } in tags.into_iter() { - bail_if!(!seen_tags.insert(tag.clone()), cx, span, "duplicate tag"); - bail_if!(tag.name.is_none(), cx, rhs.span, + ext_bail_if!(!seen_tags.insert(tag.clone()), cx, span, "duplicate tag"); + ext_bail_if!(tag.name.is_none(), cx, rhs.span, "'else' may not appear with a wildcard tag"); match wild_excluded.entry(tag.kind) { Occupied(e) => { e.into_mut().push(tag.clone()); } @@ -351,15 +351,15 @@ pub fn expand(cx: &mut ExtCtxt, span: Span, toks: &[ast::TokenTree]) -> Box Some(_) => { - bail_if!(!wildcards.is_empty(), cx, lhs.span, + ext_bail_if!(!wildcards.is_empty(), cx, lhs.span, "specific tags may not appear after wildcard tags"); - bail_if!(wildcard == Some(true), cx, span, + ext_bail_if!(wildcard == Some(true), cx, span, "wildcard tags must appear alone"); if wildcard.is_some() { @@ -373,7 +373,7 @@ pub fn expand(cx: &mut ExtCtxt, span: Span, toks: &[ast::TokenTree]) -> Box None => { - bail_if!(wildcard.is_some(), cx, span, + ext_bail_if!(wildcard.is_some(), cx, span, "wildcard tags must appear alone"); wildcard = Some(true); wildcards.push(WildcardArm { @@ -386,7 +386,7 @@ pub fn expand(cx: &mut ExtCtxt, span: Span, toks: &[ast::TokenTree]) -> Box bail!(cx, lhs.span, "[internal macro error] tag arm with no tags"), + None => ext_bail!(cx, lhs.span, "[internal macro error] tag arm with no tags"), Some(false) => { push_all(&mut arm_code, quote_tokens!(&mut *cx, => $expr,)); } @@ -422,12 +422,12 @@ pub fn expand(cx: &mut ExtCtxt, span: Span, toks: &[ast::TokenTree]) -> Box bail!(cx, id.span, "the last arm cannot have an @-binding"), - (None, Tags(_), _) => bail!(cx, lhs.span, "the last arm cannot have tag patterns"), - (None, _, Else) => bail!(cx, rhs.span, "the last arm cannot use 'else'"), + (Some(id), _, _) => ext_bail!(cx, id.span, "the last arm cannot have an @-binding"), + (None, Tags(_), _) => ext_bail!(cx, lhs.span, "the last arm cannot have tag patterns"), + (None, _, Else) => ext_bail!(cx, rhs.span, "the last arm cannot use 'else'"), (None, Pat(p), Expr(e)) => match p.node { ast::PatWild(ast::PatWildSingle) | ast::PatIdent(..) => (p, e), - _ => bail!(cx, lhs.span, "the last arm must have a wildcard or ident pattern"), + _ => ext_bail!(cx, lhs.span, "the last arm must have a wildcard or ident pattern"), }, }; diff --git a/macros/src/named_entities.rs b/macros/src/named_entities.rs index bddabca0..e7be7145 100644 --- a/macros/src/named_entities.rs +++ b/macros/src/named_entities.rs @@ -79,12 +79,12 @@ pub fn expand(cx: &mut ExtCtxt, sp: Span, tt: &[TokenTree]) -> Box s.as_str().to_string(), - _ => bail!(cx, sp, usage), + _ => ext_bail!(cx, sp, usage), }; // Get the result of calling file!() in the same place as our macro. // This would be a lot nicer if @-patterns were still supported. - let mod_filename = expect!(cx, sp, match expand_file(cx, sp, &[]).make_expr() { + let mod_filename = ext_expect!(cx, sp, match expand_file(cx, sp, &[]).make_expr() { Some(e) => match e.node { ExprLit(ref s) => match s.node { Lit_::LitStr(ref s, _) => Some(s.to_string()), @@ -96,16 +96,16 @@ pub fn expand(cx: &mut ExtCtxt, sp: Span, tt: &[TokenTree]) -> Box ( - match $opt { - None => $else_block, - Some(x) => x, +macro_rules! unwrap_or_else { + ($opt:expr, $else_block:block) => { + match $opt { + None => $else_block, + Some(x) => x, + } } -)); - -macro_rules! unwrap_or_return ( ($opt:expr, $retval:expr) => ( - unwrap_or_else!($opt, { return $retval }) -)); - -macro_rules! test_eq ( ($name:ident, $left:expr, $right:expr) => ( - #[test] - fn $name() { - assert_eq!($left, $right); - } -)); - -/// Make a tuple of the addresses of some of a struct's fields. -macro_rules! addrs_of ( ($obj:expr => $($field:ident),+) => ( - ( // make a tuple - $( - unsafe { - ::core::mem::transmute::<_, usize>(&$obj.$field) - } - ),+ - ) -)); - -// No format!() without libstd... just use the static message. -#[cfg(for_c)] -macro_rules! format_if ( ($pred:expr, $msg_static:expr, $msg_fmt:expr, $($arg:expr),*) => ( - ::std::borrow::Cow::Borrowed($msg_static) -)); +} -#[cfg(not(for_c))] -macro_rules! format_if ( ($pred:expr, $msg_static:expr, $msg_fmt:expr, $($arg:expr),*) => ( - if $pred { - ::std::borrow::Cow::Owned(format!($msg_fmt, $($arg),*)) - } else { - ::std::borrow::Cow::Borrowed($msg_static) +macro_rules! unwrap_or_return { + ($opt:expr, $retval:expr) => { + unwrap_or_else!($opt, { return $retval }) } -)); - -macro_rules! time ( ($e:expr) => ({ - let t0 = ::time::precise_time_ns(); - let result = $e; - let dt = ::time::precise_time_ns() - t0; - (result, dt) -})); +} -/// FIXME(rust-lang/rust#16806): copied from libcollections/macros.rs -#[cfg(for_c)] -macro_rules! vec( - ($($e:expr),*) => ({ - // leading _ to allow empty construction without a warning. - let mut _temp = ::collections::vec::Vec::new(); - $(_temp.push($e);)* - _temp - }); - ($($e:expr),+,) => (vec!($($e),+)) -); +macro_rules! time { + ($e:expr) => {{ + let t0 = ::time::precise_time_ns(); + let result = $e; + let dt = ::time::precise_time_ns() - t0; + (result, dt) + }} +} // Disable logging when building without the runtime. #[cfg(for_c)] diff --git a/src/tree_builder/actions.rs b/src/tree_builder/actions.rs index 8dbd7641..66333147 100644 --- a/src/tree_builder/actions.rs +++ b/src/tree_builder/actions.rs @@ -22,10 +22,7 @@ use tree_builder::rules::TreeBuilderStep; use tokenizer::{Attribute, Tag, EndTag}; use tokenizer::states::{RawData, RawKind}; -use util::str::AsciiExt; - -#[cfg(not(for_c))] -use util::str::to_escaped_string; +use util::str::{AsciiExt, to_escaped_string}; use core::mem::replace; use core::iter::{Rev, Enumerate}; diff --git a/src/util/str.rs b/src/util/str.rs index 59cdd1cc..ec6a4f9f 100644 --- a/src/util/str.rs +++ b/src/util/str.rs @@ -9,14 +9,10 @@ use core::prelude::*; -use core::str::CharEq; use collections::vec::Vec; use collections::string::String; - -#[cfg(not(for_c))] use core::fmt::Debug; -#[cfg(not(for_c))] pub fn to_escaped_string(x: &T) -> String { use collections::str::StrExt; use core::fmt::Write; @@ -194,12 +190,14 @@ pub fn is_ascii_whitespace(c: char) -> bool { /// and also return whether they match. /// /// Returns `None` on an empty string. -pub fn char_run(mut pred: Pred, buf: &str) -> Option<(usize, bool)> { +pub fn char_run(mut pred: Pred, buf: &str) -> Option<(usize, bool)> + where Pred: FnMut(char) -> bool, +{ let (first, rest) = unwrap_or_return!(buf.slice_shift_char(), None); - let matches = pred.matches(first); + let matches = pred(first); for (idx, ch) in rest.char_indices() { - if matches != pred.matches(ch) { + if matches != pred(ch) { return Some((idx + first.len_utf8(), matches)); } }