ast_enum! {
#[cfg_attr(feature = "clone-impls", derive(Copy))]
pub enum BinOp {
Add(Token![+]),
Sub(Token![-]),
Mul(Token![*]),
Div(Token![/]),
Rem(Token![%]),
And(Token![&&]),
Or(Token![||]),
BitXor(Token![^]),
BitAnd(Token![&]),
BitOr(Token![|]),
Shl(Token![<<]),
Shr(Token![>>]),
Eq(Token![==]),
Lt(Token![<]),
Le(Token![<=]),
Ne(Token![!=]),
Ge(Token![>=]),
Gt(Token![>]),
AddEq(Token![+=]),
SubEq(Token![-=]),
MulEq(Token![*=]),
DivEq(Token![/=]),
RemEq(Token![%=]),
BitXorEq(Token![^=]),
BitAndEq(Token![&=]),
BitOrEq(Token![|=]),
ShlEq(Token![<<=]),
ShrEq(Token![>>=]),
}
}
ast_enum! {
#[cfg_attr(feature = "clone-impls", derive(Copy))]
pub enum UnOp {
Deref(Token![*]),
Not(Token![!]),
Neg(Token![-]),
}
}
#[cfg(feature = "parsing")]
pub mod parsing {
use super::*;
use synom::Synom;
impl BinOp {
named!(pub parse_binop -> Self, alt!(
punct!(&&) => { BinOp::And }
|
punct!(||) => { BinOp::Or }
|
punct!(<<) => { BinOp::Shl }
|
punct!(>>) => { BinOp::Shr }
|
punct!(==) => { BinOp::Eq }
|
punct!(<=) => { BinOp::Le }
|
punct!(!=) => { BinOp::Ne }
|
punct!(>=) => { BinOp::Ge }
|
punct!(+) => { BinOp::Add }
|
punct!(-) => { BinOp::Sub }
|
punct!(*) => { BinOp::Mul }
|
punct!(/) => { BinOp::Div }
|
punct!(%) => { BinOp::Rem }
|
punct!(^) => { BinOp::BitXor }
|
punct!(&) => { BinOp::BitAnd }
|
punct!(|) => { BinOp::BitOr }
|
punct!(<) => { BinOp::Lt }
|
punct!(>) => { BinOp::Gt }
));
#[cfg(feature = "full")]
named!(pub parse_assign_op -> Self, alt!(
punct!(+=) => { BinOp::AddEq }
|
punct!(-=) => { BinOp::SubEq }
|
punct!(*=) => { BinOp::MulEq }
|
punct!(/=) => { BinOp::DivEq }
|
punct!(%=) => { BinOp::RemEq }
|
punct!(^=) => { BinOp::BitXorEq }
|
punct!(&=) => { BinOp::BitAndEq }
|
punct!(|=) => { BinOp::BitOrEq }
|
punct!(<<=) => { BinOp::ShlEq }
|
punct!(>>=) => { BinOp::ShrEq }
));
}
impl Synom for UnOp {
named!(parse -> Self, alt!(
punct!(*) => { UnOp::Deref }
|
punct!(!) => { UnOp::Not }
|
punct!(-) => { UnOp::Neg }
));
fn description() -> Option<&'static str> {
Some("unary operator: `*`, `!`, or `-`")
}
}
}
#[cfg(feature = "printing")]
mod printing {
use super::*;
use quote::{ToTokens, Tokens};
impl ToTokens for BinOp {
fn to_tokens(&self, tokens: &mut Tokens) {
match *self {
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) {
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),
}
}
}
}