Rewrite tokenization with `proc-macro2` tokens
This ended up being a bit larger of a commit than I intended! I imagine that
this'll be one of the larger of the commits working towards #142. The purpose of
this commit is to use an updated version of the `quote` crate which doesn't work
with strings but rather works with tokens form the `proc-macro2` crate. The
`proc-macro2` crate itself is based on the proposed API for `proc_macro` itself,
and will continue to mirror it. The hope is that we'll flip an easy switch
eventually to use compiler tokens, whereas for now we'll stick to string parsing
at the lowest layer.
The largest change here is the addition of span information to the AST. Building
on the previous PRs to refactor the AST this makes it relatively easy from a
user perspective to digest and use the AST still, it's just a few extra fields
on the side. The fallout from this was then quite large throughout the
`printing` feature of the crate. The `parsing`, `fold`, and `visit` features
then followed suit to get updated as well.
This commit also changes the the semantics of the AST somewhat as well.
Previously it was inferred what tokens should be printed, for example if you
have a closure argument `syn` would automatically not print the colon in `a: b`
if the type listed was "infer this type". Now the colon is a separate field and
must be in sync with the type listed as the colon/type will be printed
unconditionally (emitting no output if both are `None`).
diff --git a/src/op.rs b/src/op.rs
index 6393e00..636b974 100644
--- a/src/op.rs
+++ b/src/op.rs
@@ -1,42 +1,64 @@
+use tokens;
+
ast_enum! {
#[cfg_attr(feature = "clone-impls", derive(Copy))]
pub enum BinOp {
/// The `+` operator (addition)
- Add,
+ Add(tokens::Add),
/// The `-` operator (subtraction)
- Sub,
+ Sub(tokens::Sub),
/// The `*` operator (multiplication)
- Mul,
+ Mul(tokens::Star),
/// The `/` operator (division)
- Div,
+ Div(tokens::Div),
/// The `%` operator (modulus)
- Rem,
+ Rem(tokens::Rem),
/// The `&&` operator (logical and)
- And,
+ And(tokens::AndAnd),
/// The `||` operator (logical or)
- Or,
+ Or(tokens::OrOr),
/// The `^` operator (bitwise xor)
- BitXor,
+ BitXor(tokens::Caret),
/// The `&` operator (bitwise and)
- BitAnd,
+ BitAnd(tokens::And),
/// The `|` operator (bitwise or)
- BitOr,
+ BitOr(tokens::Or),
/// The `<<` operator (shift left)
- Shl,
+ Shl(tokens::Shl),
/// The `>>` operator (shift right)
- Shr,
+ Shr(tokens::Shr),
/// The `==` operator (equality)
- Eq,
+ Eq(tokens::EqEq),
/// The `<` operator (less than)
- Lt,
+ Lt(tokens::Lt),
/// The `<=` operator (less than or equal to)
- Le,
+ Le(tokens::Le),
/// The `!=` operator (not equal to)
- Ne,
+ Ne(tokens::Ne),
/// The `>=` operator (greater than or equal to)
- Ge,
+ Ge(tokens::Ge),
/// The `>` operator (greater than)
- Gt,
+ Gt(tokens::Gt),
+ /// The `+=` operator
+ AddEq(tokens::AddEq),
+ /// The `-=` operator
+ SubEq(tokens::SubEq),
+ /// The `*=` operator
+ MulEq(tokens::MulEq),
+ /// The `/=` operator
+ DivEq(tokens::DivEq),
+ /// The `%=` operator
+ RemEq(tokens::RemEq),
+ /// The `^=` operator
+ BitXorEq(tokens::CaretEq),
+ /// The `&=` operator
+ BitAndEq(tokens::AndEq),
+ /// The `|=` operator
+ BitOrEq(tokens::OrEq),
+ /// The `<<=` operator
+ ShlEq(tokens::ShlEq),
+ /// The `>>=` operator
+ ShrEq(tokens::ShrEq),
}
}
@@ -44,11 +66,11 @@
#[cfg_attr(feature = "clone-impls", derive(Copy))]
pub enum UnOp {
/// The `*` operator for dereferencing
- Deref,
+ Deref(tokens::Star),
/// The `!` operator for logical inversion
- Not,
+ Not(tokens::Bang),
/// The `-` operator for negation
- Neg,
+ Neg(tokens::Sub),
}
}
@@ -57,72 +79,72 @@
use super::*;
named!(pub binop -> BinOp, alt!(
- punct!("&&") => { |_| BinOp::And }
+ punct!("&&") => { |_| BinOp::And(tokens::AndAnd::default()) }
|
- punct!("||") => { |_| BinOp::Or }
+ punct!("||") => { |_| BinOp::Or(tokens::OrOr::default()) }
|
- punct!("<<") => { |_| BinOp::Shl }
+ punct!("<<") => { |_| BinOp::Shl(tokens::Shl::default()) }
|
- punct!(">>") => { |_| BinOp::Shr }
+ punct!(">>") => { |_| BinOp::Shr(tokens::Shr::default()) }
|
- punct!("==") => { |_| BinOp::Eq }
+ punct!("==") => { |_| BinOp::Eq(tokens::EqEq::default()) }
|
- punct!("<=") => { |_| BinOp::Le }
+ punct!("<=") => { |_| BinOp::Le(tokens::Le::default()) }
|
- punct!("!=") => { |_| BinOp::Ne }
+ punct!("!=") => { |_| BinOp::Ne(tokens::Ne::default()) }
|
- punct!(">=") => { |_| BinOp::Ge }
+ punct!(">=") => { |_| BinOp::Ge(tokens::Ge::default()) }
|
- punct!("+") => { |_| BinOp::Add }
+ punct!("+") => { |_| BinOp::Add(tokens::Add::default()) }
|
- punct!("-") => { |_| BinOp::Sub }
+ punct!("-") => { |_| BinOp::Sub(tokens::Sub::default()) }
|
- punct!("*") => { |_| BinOp::Mul }
+ punct!("*") => { |_| BinOp::Mul(tokens::Star::default()) }
|
- punct!("/") => { |_| BinOp::Div }
+ punct!("/") => { |_| BinOp::Div(tokens::Div::default()) }
|
- punct!("%") => { |_| BinOp::Rem }
+ punct!("%") => { |_| BinOp::Rem(tokens::Rem::default()) }
|
- punct!("^") => { |_| BinOp::BitXor }
+ punct!("^") => { |_| BinOp::BitXor(tokens::Caret::default()) }
|
- punct!("&") => { |_| BinOp::BitAnd }
+ punct!("&") => { |_| BinOp::BitAnd(tokens::And::default()) }
|
- punct!("|") => { |_| BinOp::BitOr }
+ punct!("|") => { |_| BinOp::BitOr(tokens::Or::default()) }
|
- punct!("<") => { |_| BinOp::Lt }
+ punct!("<") => { |_| BinOp::Lt(tokens::Lt::default()) }
|
- punct!(">") => { |_| BinOp::Gt }
+ punct!(">") => { |_| BinOp::Gt(tokens::Gt::default()) }
));
#[cfg(feature = "full")]
named!(pub assign_op -> BinOp, alt!(
- punct!("+=") => { |_| BinOp::Add }
+ punct!("+=") => { |_| BinOp::AddEq(tokens::AddEq::default()) }
|
- punct!("-=") => { |_| BinOp::Sub }
+ punct!("-=") => { |_| BinOp::SubEq(tokens::SubEq::default()) }
|
- punct!("*=") => { |_| BinOp::Mul }
+ punct!("*=") => { |_| BinOp::MulEq(tokens::MulEq::default()) }
|
- punct!("/=") => { |_| BinOp::Div }
+ punct!("/=") => { |_| BinOp::DivEq(tokens::DivEq::default()) }
|
- punct!("%=") => { |_| BinOp::Rem }
+ punct!("%=") => { |_| BinOp::RemEq(tokens::RemEq::default()) }
|
- punct!("^=") => { |_| BinOp::BitXor }
+ punct!("^=") => { |_| BinOp::BitXorEq(tokens::CaretEq::default()) }
|
- punct!("&=") => { |_| BinOp::BitAnd }
+ punct!("&=") => { |_| BinOp::BitAndEq(tokens::AndEq::default()) }
|
- punct!("|=") => { |_| BinOp::BitOr }
+ punct!("|=") => { |_| BinOp::BitOrEq(tokens::OrEq::default()) }
|
- punct!("<<=") => { |_| BinOp::Shl }
+ punct!("<<=") => { |_| BinOp::ShlEq(tokens::ShlEq::default()) }
|
- punct!(">>=") => { |_| BinOp::Shr }
+ punct!(">>=") => { |_| BinOp::ShrEq(tokens::ShrEq::default()) }
));
named!(pub unop -> UnOp, alt!(
- punct!("*") => { |_| UnOp::Deref }
+ punct!("*") => { |_| UnOp::Deref(tokens::Star::default()) }
|
- punct!("!") => { |_| UnOp::Not }
+ punct!("!") => { |_| UnOp::Not(tokens::Bang::default()) }
|
- punct!("-") => { |_| UnOp::Neg }
+ punct!("-") => { |_| UnOp::Neg(tokens::Sub::default()) }
));
}
@@ -131,66 +153,48 @@
use super::*;
use quote::{Tokens, ToTokens};
- impl BinOp {
- pub fn op(&self) -> &'static str {
- match *self {
- BinOp::Add => "+",
- BinOp::Sub => "-",
- BinOp::Mul => "*",
- BinOp::Div => "/",
- BinOp::Rem => "%",
- BinOp::And => "&&",
- BinOp::Or => "||",
- BinOp::BitXor => "^",
- BinOp::BitAnd => "&",
- BinOp::BitOr => "|",
- BinOp::Shl => "<<",
- BinOp::Shr => ">>",
- BinOp::Eq => "==",
- BinOp::Lt => "<",
- BinOp::Le => "<=",
- BinOp::Ne => "!=",
- BinOp::Ge => ">=",
- BinOp::Gt => ">",
- }
- }
-
- pub fn assign_op(&self) -> Option<&'static str> {
- match *self {
- BinOp::Add => Some("+="),
- BinOp::Sub => Some("-="),
- BinOp::Mul => Some("*="),
- BinOp::Div => Some("/="),
- BinOp::Rem => Some("%="),
- BinOp::BitXor => Some("^="),
- BinOp::BitAnd => Some("&="),
- BinOp::BitOr => Some("|="),
- BinOp::Shl => Some("<<="),
- BinOp::Shr => Some(">>="),
- _ => None,
- }
- }
- }
-
impl ToTokens for BinOp {
fn to_tokens(&self, tokens: &mut Tokens) {
- tokens.append(self.op());
- }
- }
-
- impl UnOp {
- pub fn op(&self) -> &'static str {
match *self {
- UnOp::Deref => "*",
- UnOp::Not => "!",
- UnOp::Neg => "-",
+ BinOp::Add(ref t) => t.to_tokens(tokens),
+ BinOp::Sub(ref t) => t.to_tokens(tokens),
+ BinOp::Mul(ref t) => t.to_tokens(tokens),
+ BinOp::Div(ref t) => t.to_tokens(tokens),
+ BinOp::Rem(ref t) => t.to_tokens(tokens),
+ BinOp::And(ref t) => t.to_tokens(tokens),
+ BinOp::Or(ref t) => t.to_tokens(tokens),
+ BinOp::BitXor(ref t) => t.to_tokens(tokens),
+ BinOp::BitAnd(ref t) => t.to_tokens(tokens),
+ BinOp::BitOr(ref t) => t.to_tokens(tokens),
+ BinOp::Shl(ref t) => t.to_tokens(tokens),
+ BinOp::Shr(ref t) => t.to_tokens(tokens),
+ BinOp::Eq(ref t) => t.to_tokens(tokens),
+ BinOp::Lt(ref t) => t.to_tokens(tokens),
+ BinOp::Le(ref t) => t.to_tokens(tokens),
+ BinOp::Ne(ref t) => t.to_tokens(tokens),
+ BinOp::Ge(ref t) => t.to_tokens(tokens),
+ BinOp::Gt(ref t) => t.to_tokens(tokens),
+ BinOp::AddEq(ref t) => t.to_tokens(tokens),
+ BinOp::SubEq(ref t) => t.to_tokens(tokens),
+ BinOp::MulEq(ref t) => t.to_tokens(tokens),
+ BinOp::DivEq(ref t) => t.to_tokens(tokens),
+ BinOp::RemEq(ref t) => t.to_tokens(tokens),
+ BinOp::BitXorEq(ref t) => t.to_tokens(tokens),
+ BinOp::BitAndEq(ref t) => t.to_tokens(tokens),
+ BinOp::BitOrEq(ref t) => t.to_tokens(tokens),
+ BinOp::ShlEq(ref t) => t.to_tokens(tokens),
+ BinOp::ShrEq(ref t) => t.to_tokens(tokens),
}
}
}
impl ToTokens for UnOp {
fn to_tokens(&self, tokens: &mut Tokens) {
- tokens.append(self.op());
+ match *self {
+ UnOp::Deref(ref t) => t.to_tokens(tokens),
+ UnOp::Not(ref t) => t.to_tokens(tokens),
+ UnOp::Neg(ref t) => t.to_tokens(tokens),
+ }
}
}
}