Skip to content

Commit

Permalink
Merge pull request dtolnay#248 from dtolnay/libsyntax
Browse files Browse the repository at this point in the history
Test with libsyntax instead of syntex
  • Loading branch information
mystor authored Nov 19, 2017
2 parents 091926c + a2a1a4a commit f3ec604
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 115 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,4 @@ unicode-xid = "0.1"
synom = { version = "0.11", path = "synom" }

[dev-dependencies]
syntex_pos = "0.59"
syntex_syntax = "0.59"
walkdir = "1.0.1"
3 changes: 3 additions & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#![allow(dead_code)]

extern crate walkdir;
extern crate syntax;

use std::env;
use std::path::Path;
use std::process::Command;
use std::u32;

use self::syntax::ast;

use self::walkdir::DirEntry;

macro_rules! errorf {
Expand Down
14 changes: 7 additions & 7 deletions tests/common/parse.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
extern crate proc_macro2;
extern crate syn;
extern crate synom;
extern crate syntex_syntax;
extern crate syntax;

use self::syntex_syntax::ast;
use self::syntex_syntax::ptr::P;
use self::syntex_syntax::parse::{self, ParseSess};
use self::syntex_syntax::codemap::FilePathMapping;
use self::syntax::ast;
use self::syntax::ptr::P;
use self::syntax::parse::{self, ParseSess};
use self::syntax::codemap::FilePathMapping;

use std::panic;

use self::synom::{Synom, SynomBuffer};

pub fn syntex_expr(input: &str) -> Option<P<ast::Expr>> {
pub fn libsyntax_expr(input: &str) -> Option<P<ast::Expr>> {
match panic::catch_unwind(|| {
let sess = ParseSess::new(FilePathMapping::empty());
sess.span_diagnostic.set_continue_after_error(false);
Expand All @@ -34,7 +34,7 @@ pub fn syntex_expr(input: &str) -> Option<P<ast::Expr>> {
None
}
Err(_) => {
errorf!("syntex paniced\n");
errorf!("libsyntax paniced\n");
None
}
}
Expand Down
131 changes: 61 additions & 70 deletions tests/common/respan.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
extern crate syntex_syntax;
extern crate syntex_pos;
extern crate syntax;
extern crate syntax_pos;

use std::rc::Rc;
use self::syntex_syntax::ast::{Attribute, Expr, ExprKind, Field, FnDecl, FunctionRetTy, ImplItem,
ImplItemKind, ItemKind, Mac, MetaItem, MetaItemKind, MethodSig,
NestedMetaItem, NestedMetaItemKind, TraitItem, TraitItemKind, TyParam,
Visibility};
use self::syntex_syntax::codemap::{self, Spanned};
use self::syntex_syntax::fold::{self, Folder};
use self::syntex_syntax::parse::token::{Lit, Token};
use self::syntex_syntax::ptr::P;
use self::syntex_syntax::symbol::Symbol;
use self::syntex_syntax::tokenstream::{Delimited, TokenTree};
use self::syntex_syntax::util::move_map::MoveMap;
use self::syntex_syntax::util::small_vector::SmallVector;

use self::syntex_pos::{Span, DUMMY_SP};
use self::syntex_syntax::ast;
use self::syntax::ast::{Attribute, Expr, ExprKind, Field, FnDecl, FunctionRetTy, ImplItem,
ImplItemKind, ItemKind, Mac, MetaItem, MetaItemKind, MethodSig,
NestedMetaItem, NestedMetaItemKind, TraitItem, TraitItemKind, TyParam,
Visibility, Item, WhereClause};
use self::syntax::codemap::{self, Spanned};
use self::syntax::fold::{self, Folder};
use self::syntax::parse::token::{Lit, Token};
use self::syntax::ptr::P;
use self::syntax::symbol::Symbol;
use self::syntax::tokenstream::{Delimited, TokenTree};
use self::syntax::util::move_map::MoveMap;
use self::syntax::util::small_vector::SmallVector;

use self::syntax_pos::{Span, DUMMY_SP};
use self::syntax::ast;

struct Respanner;

Expand Down Expand Up @@ -46,6 +46,14 @@ impl Folder for Respanner {
DUMMY_SP
}

fn fold_item(&mut self, i: P<Item>) -> SmallVector<P<Item>> {
let i = i.map(|mut i| {
i.tokens = None;
i
});
fold::noop_fold_item(i, self)
}

fn fold_item_kind(&mut self, i: ItemKind) -> ItemKind {
match i {
ItemKind::Fn(decl, unsafety, constness, abi, generics, body) => {
Expand Down Expand Up @@ -122,38 +130,38 @@ impl Folder for Respanner {
}
}

fn fold_trait_item(&mut self, i: TraitItem) -> SmallVector<TraitItem> {
fn fold_trait_item(&mut self, mut i: TraitItem) -> SmallVector<TraitItem> {
i.tokens = None;
let noop = fold::noop_fold_trait_item(i, self).expect_one("");
SmallVector::one(TraitItem {
node: match noop.node {
TraitItemKind::Method(sig, body) => {
node: match noop.node {
TraitItemKind::Method(sig, body) => {
TraitItemKind::Method(MethodSig {
constness: self.fold_spanned(sig.constness),
..sig
},
body)
constness: self.fold_spanned(sig.constness),
..sig
}, body)
}
node => node,
},
..noop
})
node => node,
},
..noop
})
}

fn fold_impl_item(&mut self, i: ImplItem) -> SmallVector<ImplItem> {
fn fold_impl_item(&mut self, mut i: ImplItem) -> SmallVector<ImplItem> {
i.tokens = None;
let noop = fold::noop_fold_impl_item(i, self).expect_one("");
SmallVector::one(ImplItem {
node: match noop.node {
ImplItemKind::Method(sig, body) => {
node: match noop.node {
ImplItemKind::Method(sig, body) => {
ImplItemKind::Method(MethodSig {
constness: self.fold_spanned(sig.constness),
..sig
},
body)
constness: self.fold_spanned(sig.constness),
..sig
}, body)
}
node => node,
},
..noop
})
node => node,
},
..noop
})
}

fn fold_attribute(&mut self, mut at: Attribute) -> Option<Attribute> {
Expand Down Expand Up @@ -192,48 +200,31 @@ impl Folder for Respanner {
}
}

// This folder is disabled by default.
fn fold_mac(&mut self, mac: Mac) -> Mac {
fold::noop_fold_mac(mac, self)
}

fn fold_tt(&mut self, tt: TokenTree) -> TokenTree {
match tt {
TokenTree::Token(span, ref tok) => {
TokenTree::Token(self.new_span(span), self.fold_token(tok.clone()))
}
TokenTree::Delimited(span, ref delimed) => {
TokenTree::Delimited(self.new_span(span),
Delimited {
delim: delimed.delim,
tts: self.fold_tts(delimed.tts.clone().into()).into(),
})
}
}
}

fn fold_token(&mut self, t: Token) -> Token {
match t {
fold::noop_fold_token(match t {
// default fold_token does not fold literals
Token::Literal(lit, repr) => Token::Literal(self.fold_lit(lit), repr),
Token::Ident(id) => Token::Ident(self.fold_ident(id)),
Token::Lifetime(id) => Token::Lifetime(self.fold_ident(id)),
Token::Interpolated(nt) => {
let nt = match Rc::try_unwrap(nt) {
Ok(nt) => nt,
Err(nt) => (*nt).clone(),
};
Token::Interpolated(Rc::new(self.fold_interpolated(nt)))
}
Token::SubstNt(ident) => Token::SubstNt(self.fold_ident(ident)),
_ => t,
}
}, self)
}

fn fold_vis(&mut self, vis: Visibility) -> Visibility {
match vis {
Visibility::Crate(span) => Visibility::Crate(self.new_span(span)),
_ => fold::noop_fold_vis(vis, self),
}
fold::noop_fold_vis(match vis {
Visibility::Crate(span, sugar) =>
Visibility::Crate(self.new_span(span), sugar),
_ => vis,
}, self)
}

// noop_fold_where_clause doesn't modify the span.
fn fold_where_clause(&mut self, mut clause: WhereClause) -> WhereClause {
clause.span = self.new_span(clause.span);
fold::noop_fold_where_clause(clause, self)
}
}

Expand Down
2 changes: 2 additions & 0 deletions tests/test_generics.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![cfg(feature = "extra-traits")]

#![feature(rustc_private)]

extern crate syn;
use syn::*;

Expand Down
2 changes: 2 additions & 0 deletions tests/test_grouping.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![cfg(all(feature = "extra-traits", feature = "full"))]

#![feature(rustc_private)]

extern crate syn;
use syn::{Expr, ExprKind, ExprGroup, ExprBinary, Lit, LitKind, BinOp};

Expand Down
54 changes: 28 additions & 26 deletions tests/test_precedence.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
#![cfg(all(feature = "full", feature = "fold"))]

#![feature(rustc_private)]

//! The tests in this module do the following:
//!
//! 1. Parse a given expression in both `syn` and `syntex`.
//! 1. Parse a given expression in both `syn` and `libsyntax`.
//! 2. Fold over the expression adding brackets around each subexpression (with
//! some complications - see the `syn_brackets` and `syntex_brackets`
//! some complications - see the `syn_brackets` and `libsyntax_brackets`
//! methods).
//! 3. Serialize the `syn` expression back into a string, and re-parse it with
//! `syntex`.
//! `libsyntax`.
//! 4. Respan all of the expressions, replacing the spans with the default spans.
//! 5. Compare the expressions with one another, if they are not equal fail.
#[macro_use]
extern crate quote;
extern crate syn;
extern crate synom;
extern crate syntex_syntax;
extern crate syntax;
extern crate walkdir;

use syntex_syntax::ast;
use syntex_syntax::ptr::P;
use syntax::ast;
use syntax::ptr::P;

use common::{respan, parse};

Expand Down Expand Up @@ -90,7 +92,7 @@ fn test_rustc_precedence() {
continue;
}

// Our version of `syntex_syntax` can't parse this tests
// Our version of `libsyntax` can't parse this tests
if path.to_str().unwrap().ends_with("optional_comma_in_match_arm.rs") {
continue
}
Expand Down Expand Up @@ -138,53 +140,53 @@ fn test_expressions(exprs: Vec<syn::Expr>) -> (u32, u32) {
for expr in exprs {
let raw = quote!(#expr).to_string();

let syntex_ast = if let Some(e) = syntex_parse_and_rewrite(&raw) {
let libsyntax_ast = if let Some(e) = libsyntax_parse_and_rewrite(&raw) {
e
} else {
failed += 1;
errorf!("\nFAIL - syntex failed to parse raw\n");
errorf!("\nFAIL - libsyntax failed to parse raw\n");
continue;
};

let syn_expr = syn_brackets(expr);
let syn_ast = if let Some(e) = parse::syntex_expr(&quote!(#syn_expr).to_string()) {
let syn_ast = if let Some(e) = parse::libsyntax_expr(&quote!(#syn_expr).to_string()) {
e
} else {
failed += 1;
errorf!("\nFAIL - syntex failed to parse bracketed\n");
errorf!("\nFAIL - libsyntax failed to parse bracketed\n");
continue;
};

let syn_ast = respan::respan_expr(syn_ast);
let syntex_ast = respan::respan_expr(syntex_ast);
let libsyntax_ast = respan::respan_expr(libsyntax_ast);

if syn_ast == syntex_ast {
if syn_ast == libsyntax_ast {
passed += 1;
} else {
failed += 1;
errorf!("\nFAIL\n{:?}\n!=\n{:?}\n", syn_ast, syntex_ast);
errorf!("\nFAIL\n{:?}\n!=\n{:?}\n", syn_ast, libsyntax_ast);
}
}

(passed, failed)
}

fn syntex_parse_and_rewrite(input: &str) -> Option<P<ast::Expr>> {
parse::syntex_expr(input).and_then(|e| syntex_brackets(e))
fn libsyntax_parse_and_rewrite(input: &str) -> Option<P<ast::Expr>> {
parse::libsyntax_expr(input).and_then(|e| libsyntax_brackets(e))
}

/// Wrap every expression which is not already wrapped in parens with parens, to
/// reveal the precidence of the parsed expressions, and produce a stringified form
/// of the resulting expression.
///
/// This method operates on syntex objects.
fn syntex_brackets(syntex_expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
use syntex_syntax::ast::{Expr, ExprKind, Mac, Stmt, StmtKind, Pat, Ty, Field};
use syntex_syntax::fold::{self, Folder};
use syntex_syntax::util::ThinVec;
use syntex_syntax::util::small_vector::SmallVector;
use syntex_syntax::ext::quote::rt::DUMMY_SP;
use syntex_syntax::codemap;
/// This method operates on libsyntax objects.
fn libsyntax_brackets(libsyntax_expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
use syntax::ast::{Expr, ExprKind, Mac, Stmt, StmtKind, Pat, Ty, Field};
use syntax::fold::{self, Folder};
use syntax::util::ThinVec;
use syntax::util::small_vector::SmallVector;
use syntax::ext::quote::rt::DUMMY_SP;
use syntax::codemap;

fn expr(node: ExprKind) -> P<Expr> {
P(Expr {
Expand Down Expand Up @@ -268,7 +270,7 @@ fn syntex_brackets(syntex_expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
}

fn fold_mac(&mut self, mac: Mac) -> Mac {
// By default when folding over macros, syntex panics. This is
// By default when folding over macros, libsyntax panics. This is
// because it's usually not what you want, you want to run after
// macro expansion. We do want to do that (syn doesn't do macro
// expansion), so we implement fold_mac to just return the macro
Expand All @@ -280,7 +282,7 @@ fn syntex_brackets(syntex_expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
let mut folder = BracketsFolder {
failed: false,
};
let e = folder.fold_expr(syntex_expr);
let e = folder.fold_expr(libsyntax_expr);
if folder.failed {
None
} else {
Expand Down
Loading

0 comments on commit f3ec604

Please sign in to comment.