Pare down the Synom trait
I would like to make it clearer that parsing a string is second-class
functionality compared to parsing tokens.
diff --git a/tests/common/parse.rs b/tests/common/parse.rs
index 15c2faf..a0c54e5 100644
--- a/tests/common/parse.rs
+++ b/tests/common/parse.rs
@@ -1,4 +1,6 @@
+extern crate proc_macro2;
extern crate syn;
+extern crate synom;
extern crate syntex_syntax;
use self::syntex_syntax::ast;
@@ -8,6 +10,8 @@
use std::panic;
+use self::synom::{Synom, SynomBuffer};
+
pub fn syntex_expr(input: &str) -> Option<P<ast::Expr>> {
match panic::catch_unwind(|| {
let sess = ParseSess::new(FilePathMapping::empty());
@@ -37,7 +41,7 @@
}
pub fn syn_expr(input: &str) -> Option<syn::Expr> {
- match input.parse::<syn::Expr>() {
+ match syn::parse_str(input) {
Ok(e) => Some(e),
Err(msg) => {
errorf!("syn failed to parse\n{:?}\n", msg);
@@ -45,3 +49,20 @@
}
}
}
+
+pub fn syn<T: Synom>(tokens: proc_macro2::TokenStream) -> T {
+ let buf = SynomBuffer::new(tokens);
+ let result = T::parse(buf.begin());
+ match result {
+ Ok((rest, t)) => {
+ if rest.eof() {
+ t
+ } else if rest == buf.begin() {
+ panic!("failed to parse anything")
+ } else {
+ panic!("failed to parse all tokens")
+ }
+ }
+ Err(err) => panic!("failed to parse: {}", err),
+ }
+}