From 1bb3903e681cf4b3df0be4fb3805e3ff2ce4fdb3 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Mon, 17 Oct 2016 16:33:12 +0200 Subject: [PATCH] Update to latest libsyntax --- Cargo.toml | 2 +- macros/Cargo.toml | 2 +- macros/src/lib.rs | 7 ++++-- macros/src/match_token.rs | 49 ++++++++++++++++++++------------------- macros/src/pre_expand.rs | 31 ++++++++++++++----------- 5 files changed, 50 insertions(+), 41 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b353ff97..27517e55 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "html5ever" -version = "0.5.4" +version = "0.5.5" authors = [ "The html5ever Project Developers" ] license = "MIT / Apache-2.0" repository = "https://github.com/servo/html5ever" diff --git a/macros/Cargo.toml b/macros/Cargo.toml index da6a9e25..02e9f406 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "html5ever_macros" -version = "0.2.6" +version = "0.2.7" authors = [ "The html5ever Project Developers" ] license = "MIT / Apache-2.0" repository = "https://github.com/servo/html5ever" diff --git a/macros/src/lib.rs b/macros/src/lib.rs index f374f28f..bd976011 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -18,10 +18,13 @@ extern crate mac; // See https://github.com/rust-lang/rust/pull/23857 macro_rules! panictry { ($e:expr) => ({ - use syntax::diagnostic::FatalError; + use syntax::errors::FatalError; match $e { Ok(e) => e, - Err(FatalError) => panic!(FatalError) + Err(mut e) => { + e.emit(); + panic!(FatalError); + } } }) } diff --git a/macros/src/match_token.rs b/macros/src/match_token.rs index 0e9a3237..9271dbaf 100644 --- a/macros/src/match_token.rs +++ b/macros/src/match_token.rs @@ -100,20 +100,21 @@ matching, by enforcing the following restrictions on its input: use std::collections::{HashSet, HashMap}; use std::collections::hash_map::Entry::{Occupied, Vacant}; -use syntax::diagnostic::FatalError; -use syntax::ptr::P; -use syntax::codemap::{Span, Spanned, spanned}; use syntax::ast; -use syntax::parse::parser::{Parser, Restrictions}; -use syntax::parse::{token, parser, classify}; -use syntax::parse; +use syntax::codemap::{Span, Spanned, spanned}; +use syntax::errors::DiagnosticBuilder; use syntax::ext::base::{ExtCtxt, MacResult, MacEager}; +use syntax::parse; +use syntax::parse::{token, parser, classify}; +use syntax::parse::parser::{Parser, Restrictions}; +use syntax::ptr::P; +use syntax::tokenstream::TokenTree; use self::TagKind::{StartTag, EndTag}; use self::LHS::{Pat, Tags}; use self::RHS::{Else, Expr}; -type Tokens = Vec; +type Tokens = Vec; // FIXME: duplicated in src/tokenizer/interface.rs #[derive(PartialEq, Eq, Hash, Copy, Clone, Debug)] @@ -170,22 +171,22 @@ fn push_all(lhs: &mut Vec, rhs: Vec) { lhs.extend(rhs.into_iter()); } -fn parse_spanned_ident(parser: &mut Parser) -> Result { +fn parse_spanned_ident<'a>(parser: &mut Parser<'a>) -> Result> { let lo = parser.span.lo; let ident = try!(parser.parse_ident()); let hi = parser.last_span.hi; Ok(spanned(lo, hi, ident)) } -fn parse_tag(parser: &mut Parser) -> Result, FatalError> { +fn parse_tag<'a>(parser: &mut Parser<'a>) -> Result, DiagnosticBuilder<'a>> { let lo = parser.span.lo; try!(parser.expect(&token::Lt)); - let kind = match try!(parser.eat(&token::BinOp(token::Slash))) { + let kind = match parser.eat(&token::BinOp(token::Slash)) { true => EndTag, false => StartTag, }; - let name = match try!(parser.eat(&token::Underscore)) { + let name = match parser.eat(&token::Underscore) { true => None, false => Some((*try!(parser.parse_ident()).name.as_str()).to_owned()), }; @@ -198,18 +199,18 @@ fn parse_tag(parser: &mut Parser) -> Result, FatalError> { } /// Parse a `match_token!` invocation into the little AST defined above. -fn parse(cx: &mut ExtCtxt, toks: &[ast::TokenTree]) -> Result { +fn parse<'a>(cx: &'a mut ExtCtxt, toks: &[TokenTree]) -> Result> { let mut parser = parse::new_parser_from_tts(cx.parse_sess(), cx.cfg(), toks.to_vec()); - let discriminant = try!(parser.parse_expr_res(Restrictions::RESTRICTION_NO_STRUCT_LITERAL)); - try!(parser.commit_expr_expecting(&*discriminant, token::OpenDelim(token::Brace))); + let discriminant = try!(parser.parse_expr_res(Restrictions::RESTRICTION_NO_STRUCT_LITERAL, None)); + try!(parser.expect(&token::OpenDelim(token::Brace))); let mut arms: Vec = Vec::new(); while parser.token != token::CloseDelim(token::Brace) { let mut binding = None; if parser.look_ahead(1, |t| *t == token::At) { binding = Some(try!(parse_spanned_ident(&mut parser))); - try!(parser.bump()); // Consume the @ + parser.bump(); // Consume the @ } let lhs_lo = parser.span.lo; @@ -230,11 +231,11 @@ fn parse(cx: &mut ExtCtxt, toks: &[ast::TokenTree]) -> Result let rhs_lo = parser.span.lo; let mut rhs_hi = parser.span.hi; - let rhs = if try!(parser.eat_keyword(token::keywords::Else)) { + let rhs = if parser.eat_keyword(token::keywords::Else) { try!(parser.expect(&token::Comma)); Else } else { - let expr = try!(parser.parse_expr_res(Restrictions::RESTRICTION_STMT_EXPR)); + let expr = try!(parser.parse_expr_res(Restrictions::RESTRICTION_STMT_EXPR, None)); rhs_hi = parser.last_span.hi; let require_comma = @@ -242,10 +243,10 @@ fn parse(cx: &mut ExtCtxt, toks: &[ast::TokenTree]) -> Result && parser.token != token::CloseDelim(token::Brace); if require_comma { - try!(parser.commit_expr( - &*expr, &[token::Comma], &[token::CloseDelim(token::Brace)])); + try!(parser.expect_one_of( + &[token::Comma], &[token::CloseDelim(token::Brace)])); } else { - try!(parser.eat(&token::Comma)); + parser.eat(&token::Comma); } Expr(expr) @@ -259,7 +260,7 @@ fn parse(cx: &mut ExtCtxt, toks: &[ast::TokenTree]) -> Result } // Consume the closing brace - try!(parser.bump()); + parser.bump(); Ok(Match { discriminant: discriminant, @@ -300,8 +301,8 @@ macro_rules! ext_err_if { } /// Expand the `match_token!` macro. -pub fn expand_to_tokens(cx: &mut ExtCtxt, span: Span, toks: &[ast::TokenTree]) - -> Result, (Span, &'static str)> { +pub fn expand_to_tokens(cx: &mut ExtCtxt, span: Span, toks: &[TokenTree]) + -> Result, (Span, &'static str)> { let Match { discriminant, mut arms } = panictry!(parse(cx, toks)); // Handle the last arm specially at the end. @@ -436,7 +437,7 @@ pub fn expand_to_tokens(cx: &mut ExtCtxt, span: Span, toks: &[ast::TokenTree]) (None, Tags(_), _) => ext_err!(lhs.span, "the last arm cannot have tag patterns"), (None, _, Else) => ext_err!(rhs.span, "the last arm cannot use 'else'"), (None, Pat(p), Expr(e)) => match p.node { - ast::PatWild | ast::PatIdent(..) => (p, e), + ast::PatKind::Wild | ast::PatKind::Ident(..) => (p, e), _ => ext_err!(lhs.span, "the last arm must have a wildcard or ident pattern"), }, }; diff --git a/macros/src/pre_expand.rs b/macros/src/pre_expand.rs index a63d881b..873fc4ef 100644 --- a/macros/src/pre_expand.rs +++ b/macros/src/pre_expand.rs @@ -9,12 +9,14 @@ use match_token; use std::fs::File; -use std::hash::{Hash, Hasher, SipHasher}; +use std::hash::{Hash, Hasher}; use std::io::{Read, Write}; use std::path::Path; use std::rc::Rc; -use syntax::{ast, codemap, ext, parse, print}; +use syntax::{codemap, ext, parse, print}; +use syntax::ext::base::DummyResolver; use syntax::parse::token; +use syntax::tokenstream::{Delimited, TokenTree}; pub fn pre_expand(from: &Path, to: &Path) { let mut source = String::new(); @@ -25,13 +27,13 @@ pub fn pre_expand(from: &Path, to: &Path) { write_header(&from, &source, &mut file_to); let sess = parse::ParseSess::new(); - let mut feature_gated_cfgs = Vec::new(); + let mut resolver = DummyResolver; let mut cx = ext::base::ExtCtxt::new(&sess, vec![], ext::expand::ExpansionConfig::default("".to_owned()), - &mut feature_gated_cfgs); + &mut resolver); let from = from.to_string_lossy().into_owned(); - let tts = parse::parse_tts_from_source_str(from, source, vec![], &sess); + let tts = panictry!(parse::parse_tts_from_source_str(from, source, vec![], &sess)); let tts = find_and_expand_match_token(&mut cx, tts); let tts = pretty(&mut cx, tts); @@ -39,22 +41,22 @@ pub fn pre_expand(from: &Path, to: &Path) { file_to.write_all(expanded.as_bytes()).unwrap(); } -fn find_and_expand_match_token(cx: &mut ext::base::ExtCtxt, tts: Vec) - -> Vec { +fn find_and_expand_match_token(cx: &mut ext::base::ExtCtxt, tts: Vec) + -> Vec { let mut expanded = Vec::new(); let mut tts = tts.into_iter().peekable(); while let Some(tt) = tts.next() { match tt { - ast::TokenTree::Token(span, token::Token::Ident(ident, token::IdentStyle::Plain)) + TokenTree::Token(span, token::Token::Ident(ident)) if ident.name.as_str() == "match_token" => { // `!` - if !matches!(tts.next(), Some(ast::TokenTree::Token(_, token::Token::Not))) { + if !matches!(tts.next(), Some(TokenTree::Token(_, token::Token::Not))) { expanded.push(tt); continue } match tts.next() { - Some(ast::TokenTree::Delimited(_, block)) => { + Some(TokenTree::Delimited(_, block)) => { cx.bt_push(expn_info(span)); expanded.extend( match match_token::expand_to_tokens(cx, span, &block.tts) { @@ -69,10 +71,10 @@ fn find_and_expand_match_token(cx: &mut ext::base::ExtCtxt, tts: Vec panic!("expected a block after {:?}", span) } } - ast::TokenTree::Delimited(span, mut block) => { + TokenTree::Delimited(span, mut block) => { Rc::make_mut(&mut block); let block = Rc::try_unwrap(block).unwrap(); - expanded.push(ast::TokenTree::Delimited(span, Rc::new(ast::Delimited { + expanded.push(TokenTree::Delimited(span, Rc::new(Delimited { delim: block.delim, open_span: block.open_span, tts: find_and_expand_match_token(cx, block.tts), @@ -97,7 +99,7 @@ fn expn_info(span: codemap::Span) -> codemap::ExpnInfo { } /// Somehow, going through a parser and back to tokens gives nicer whitespace. -fn pretty(cx: &mut ext::base::ExtCtxt, tts: Vec) -> Vec { +fn pretty(cx: &mut ext::base::ExtCtxt, tts: Vec) -> Vec { let mut parser = parse::new_parser_from_tts(cx.parse_sess(), cx.cfg(), tts); let start_span = parser.span; let mut items = Vec::new(); @@ -109,7 +111,10 @@ fn pretty(cx: &mut ext::base::ExtCtxt, tts: Vec) -> Vec