1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
#[macro_export]
macro_rules! parse_unit_impl {
($enum_name:ident {
$($string:literal -> $var:ident,)*
}) => {
#[cfg(feature = "parse")]
impl terl::ParseUnit<$crate::Token> for $enum_name {
type Target = $enum_name;
fn parse(p: &mut terl::Parser<$crate::Token>) -> terl::ParseResult<Self, $crate::Token> {
use std::collections::HashMap;
use terl::WithSpanExt;
thread_local! {
static MAP: HashMap<&'static str, $enum_name> = {
let mut map = HashMap::new();
$(
if let Some(previous) = map.get($string) {
panic!("conflicting: both `{}` and `{}` are `{}`",
$enum_name::$var, previous, $string
);
}
map.insert($string, $enum_name::$var);
)*
map
};
}
// use peek here to avoid mutable borrow
let Some(next) = p.peek() else {
let msg = format!("expect a `{}`, but there are no token left", stringify!($enum_name));
return p.unmatch(msg)
};
match MAP.with(|map| map.get(&**next).copied()) {
Some(item) => {
// and use next here to actually use a token
p.next();
Ok(item)
},
None => {
p.unmatch(
format!("{} matched non of {}", &**next , stringify!($enum_name)),
)
}
}
}
}
};
}
macro_rules! operators {
(
$(#[$metas:meta])*
$(
symbols $sub_class:ident {
$($string:literal -> $var:ident : $ass:ident $priority:expr),*
}
)*
) => {
$(#[$metas])*
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OperatorAssociativity {
Binary,
Unary,
None,
}
impl OperatorAssociativity {
pub fn cost(&self) -> usize {
match self {
Self::Binary => 2,
Self::Unary => 1,
Self::None => 0
}
}
}
$(#[$metas])*
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OperatorTypes {
$($sub_class,)*
}
$crate::reverse_parse_keywords! {
$(#[$metas])*
keywords Operators {
$(
$($string -> $var,)*
)*
}
}
parse_unit_impl!{
Operators {
$(
$($string -> $var,)*
)*
}
}
impl Operators {
pub fn op_ty(&self) -> OperatorTypes {
match *self {
$(
$(Self::$var => OperatorTypes::$sub_class,)*
)*
}
}
pub fn associativity(&self) -> OperatorAssociativity {
match *self {
$(
$(Self::$var => OperatorAssociativity::$ass,)*
)*
}
}
pub fn cost(&self) -> usize {
self.associativity().cost()
}
/// return the priority of the symbol
///
/// samller number means higher priority
pub fn priority(&self) -> usize {
match *self {
$(
$(Self::$var => $priority,)*
)*
}
}
}
pub mod sub_classes {
use super::*;
$crate::reverse_parse_keywords! {
$(
keywords $sub_class {
$($string -> $var,)*
}
)*
}
$(
impl From<$sub_class> for Operators {
fn from(value: $sub_class) -> Operators {
match value {
$($sub_class::$var => Operators::$var,)*
}
}
}
impl TryFrom<Operators> for $sub_class {
type Error = ();
fn try_from(value: Operators) -> Result<Self, Self::Error> {
match value {
$(Operators::$var => Ok(Self::$var),)*
_ => Err(())
}
}
}
impl $sub_class {
pub fn associativity(&self) -> OperatorAssociativity {
match self {
$(Self::$var => OperatorAssociativity::$ass,)*
}
}
pub fn priority(&self) -> usize {
match self {
$(Self::$var => $priority,)*
}
}
}
)*
}
};
}
operators! {
#[derive(serde::Serialize,serde::Deserialize)]
symbols AlgebraOperator {
"jia1" -> Add : Binary 6,
"jian3" -> Sub : Binary 6,
"cheng2" -> Mul : Binary 5,
"chu2" -> Div : Binary 5,
"mo2" -> Mod : Binary 5,
"mi4" -> Pow : Binary 4,
"dui4" -> Log : Binary 4
}
symbols CompareOperator {
"tong2" -> Eq : Binary 10,
"fei1tong2" -> Neq : Binary 10,
"da4" -> Gt : Binary 8,
"xiao3" -> Lt : Binary 8,
"da4deng3" -> Ge : Binary 8,
"xiao3deng3" -> Le : Binary 8
}
symbols LogicalOperator {
"yu3" -> And : Binary 14,
"huo4" -> Or : Binary 15,
"fei1" -> Not : Unary 3
}
symbols ArithmeticOperator {
"wei4yu3" -> Band : Binary 11,
"wei4huo4" -> Bor : Binary 13,
"wei4fei1" -> Bnot : Unary 3,
"wei4yi4huo4" -> Xor : Binary 12,
"zuo3yi2" -> Shl : Binary 7,
"you4yi2" -> Shr : Binary 7
}
symbols SpecialOperator {
"qu3zhi3" -> AddrOf : Unary 3,
"fang3zhi3" -> Deref : Unary 3,
"fang3su4" -> GetElement : Binary 2,
"zhuan3" -> Cast : Unary 2,
"chang2du4" -> SizeOf : Unary 3
}
symbols StructOperator {
"jie2" -> BracketL : None 0,
"he2" -> BracketR : None 0
}
}