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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//! A crate to enable easy use of the [precedence climbing][1] algorithm.
//! You plug your own handler function, token struct, and operator enum,
//! and this crate provides the algorithm.
//!
//! Because the crate is sufficiently generic, It's possible to add
//! very complex behavior without having to roll your own algorith.
//! the [`int_math`][2] example demonstrates adding parenteses that respect ordering.
//!
//! The relationships between the required structures you provide are enforced
//! by std library traits, which will let your structures play well with
//! your existing code base.
//!
//! [1]: https://en.wikipedia.org/wiki/Operator-precedence_parser#Precedence_climbing_method
//! [2]: https://github.com/mcpar-land/prec/blob/master/examples/int_math.rs

use std::collections::HashMap;
use std::fmt;
use std::hash::Hash;
use std::marker::PhantomData;

pub trait Token<Re, Err, Ctx = ()> {
	fn convert(self, ctx: &Ctx) -> Result<Re, Err>;
}

/// A struct containing the order of operations rules and a pointer to a handler function.
///
/// # Generics
/// Has three generics:
/// ## `Op`
/// An enum or other value that is used to specify different rules.
///
/// Required implementations: [Hash], [Eq], [Copy]
///
/// ## `To`
/// A token used in expressions between operators.
///
/// Required implementations: [Into<Re>], [Clone]
///
/// ## `Re`
/// A result value returned from the `compute` function.
///
/// ## `Err`
/// The error type returned in results.
///
/// ## `Ctx`
/// A context value made available across an entire expression while evaluating.
/// Entirely optional, defaults to `()`
///
///
/// [Hash]: https://doc.rust-lang.org/std/hash/index.html
/// [Eq]: https://doc.rust-lang.org/std/cmp/trait.Eq.ht
/// [Copy]: https://doc.rust-lang.org/std/marker/trait.Copy.html
/// [Clone]: https://doc.rust-lang.org/std/clone/trait.Clone.html
pub struct Climber<
	Op: Hash + Eq + Copy,
	To: Token<Re, Err, Ctx> + Clone,
	Re,
	Err,
	Ctx = (),
> {
	/// A map of [Rule](struct.Rule.html) s.
	///
	/// [1]: https://en.wikipedia.org/wiki/Operator-precedence_parser#Precedence_climbing_method
	pub rules: HashMap<Op, (usize, Assoc)>,
	/// Function to handle the result of an operator between two tokens.
	///
	/// Arguments are:
	/// - Left-hand side token
	/// - Operator
	/// - Right-hand side token
	pub handler: fn(To, Op, To, &Ctx) -> Result<To, Err>,
	p_rule_value: PhantomData<Op>,
	p_token: PhantomData<To>,
	p_result: PhantomData<Re>,
	p_ctx: PhantomData<Ctx>,
}

impl<Op: Hash + Eq + Copy, To: Token<Re, Err, Ctx> + Clone, Re, Err, Ctx>
	Climber<Op, To, Re, Err, Ctx>
{
	/// Construtor for a new climber.
	/// Rules with the same [precedence level][1] are separated by a `|` character.
	/// ```ignore
	/// fn handler(lhs: f64, op: Op, rhs: f64, _:&()) -> Result<f64, ()> {
	/// 	Ok(match op {
	/// 		Op::Add => lhs + rhs,
	/// 		Op::Sub => lhs - rhs,
	/// 		Op::Mul => lhs * rhs,
	/// 		Op::Div => lhs / rhs,
	///			Op::Exp => lhs.powf(rhs)
	/// 	})
	/// }
	///
	/// let climber = Climber::new(
	/// 	vec![
	/// 		Rule::new(Op::Add, Assoc::Left) | Rule::new(Op::Sub, Assoc::Right),
	/// 		Rule::new(Op::Mul, Assoc::Left) | Rule::new(Op::Div, Assoc::Right),
	/// 		Rule::new(Op::Exp, Assoc::Right)
	/// 	],
	///		handler
	/// );
	/// ```
	pub fn new(
		rules: Vec<Rule<Op>>,
		handler: fn(To, Op, To, &Ctx) -> Result<To, Err>,
	) -> Self {
		let rules =
			rules
				.into_iter()
				.zip(1..)
				.fold(HashMap::new(), |mut map, (op, prec)| {
					let mut next = Some(op);
					while let Some(op) = next.take() {
						match op {
							Rule {
								op,
								assoc,
								next: val_next,
							} => {
								map.insert(op, (prec, assoc));
								next = val_next.map(|val| *val);
							}
						}
					}
					map
				});
		Self {
			rules,
			handler,
			p_rule_value: PhantomData,
			p_token: PhantomData,
			p_result: PhantomData,
			p_ctx: PhantomData,
		}
	}

	/// Process an [Expression](struct.Expression.html) and return the resulting value.
	/// ```ignore
	/// // 2 + 2 * 3
	/// // 2 + 6
	/// // 8
	/// let expression = Expression::new(
	/// 	2.0f64,
	/// 	vec![
	/// 		(Op::Add, 2.0f64),
	/// 		(Op::Mul, 3.0f64)
	/// 	]
	/// );
	/// assert_eq!(climber.process(&expression, &()).unwrap(), 8.0f64);
	/// ```
	pub fn process(
		&self,
		expr: &Expression<Op, To>,
		ctx: &Ctx,
	) -> Result<Re, Err> {
		let mut primary = expr.first_token.clone().convert(ctx)?;
		let lhs = expr.first_token.clone();
		let mut tokens = expr.pairs.iter().peekable();
		self
			.process_rec(
				lhs, //
				0,
				&mut primary,
				&mut tokens,
				ctx,
			)?
			.convert(ctx)
	}

	fn process_rec(
		&self,
		mut lhs: To,
		min_prec: usize,
		primary: &mut Re,
		tokens: &mut std::iter::Peekable<std::slice::Iter<(Op, To)>>,
		ctx: &Ctx,
	) -> Result<To, Err> {
		while let Some((rule, _)) = tokens.peek() {
			if let Some(&(prec, _)) = self.rules.get(rule) {
				if prec >= min_prec {
					let (_, rhs_ref) = tokens.next().unwrap();
					let mut rhs = rhs_ref.clone();

					while let Some((peek_rule, _)) = tokens.peek() {
						if let Some(&(peek_prec, peek_assoc)) = self.rules.get(peek_rule) {
							if peek_prec > prec
								|| peek_assoc == Assoc::Right && peek_prec == prec
							{
								rhs = self.process_rec(rhs, peek_prec, primary, tokens, ctx)?;
							} else {
								break;
							}
						} else {
							break;
						}
					}
					lhs = (self.handler)(lhs, *rule, rhs, ctx)?;
				} else {
					break;
				}
			} else {
				break;
			}
		}
		Ok(lhs)
	}
}

/// Used within a [Rule](struct.Rule.html) to indicate the left/right association of an operator.
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum Assoc {
	Left,
	Right,
}

/// A single operator and an [Assoc](enum.Assoc.html)
#[derive(Debug)]
pub struct Rule<Op> {
	op: Op,
	assoc: Assoc,
	next: Option<Box<Rule<Op>>>,
}

impl<Op> Rule<Op> {
	pub fn new(op: Op, assoc: Assoc) -> Self {
		Self {
			op,
			assoc,
			next: None,
		}
	}
}

impl<Ru> std::ops::BitOr for Rule<Ru> {
	type Output = Self;
	fn bitor(mut self, rhs: Self) -> Self {
		fn assign_next<Ru>(op: &mut Rule<Ru>, next: Rule<Ru>) {
			if let Some(ref mut child) = op.next {
				assign_next(child, next);
			} else {
				op.next = Some(Box::new(next));
			}
		}
		assign_next(&mut self, rhs);
		self
	}
}

/// A faithful, always-valid representation of an expression.
///
/// It's impossible to throw an error due to the order of `token, operator, token` not being respected.
#[derive(Debug, Clone)]
pub struct Expression<Op: Copy, To: Clone> {
	pub first_token: To,
	pub pairs: Vec<(Op, To)>,
}

impl<Op: Copy, To: Clone> Expression<Op, To> {
	/// ```ignore
	/// // 5 * 6 + 3 / 2 ^ 4
	/// let expression = Expression::new(
	/// 	5.0f64,
	/// 	vec![
	/// 		(Op::Mul, 6.0),
	/// 		(Op::Add, 3.0),
	/// 		(Op::Div, 2.0),
	/// 		(Op::Exp, 4.0)
	/// 	]
	/// );
	/// ```
	pub fn new(first_token: To, pairs: Vec<(Op, To)>) -> Self {
		Self { first_token, pairs }
	}
}

impl<Op: Copy + fmt::Display, To: Clone + fmt::Display> fmt::Display
	for Expression<Op, To>
{
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		let mut s = format!("{}", self.first_token);
		for (o, t) in &self.pairs {
			s = format!("{} {} {}", s, o, t);
		}
		write!(f, "{}", s)
	}
}

impl<Op: Copy + PartialEq, To: Clone + PartialEq> PartialEq
	for Expression<Op, To>
{
	fn eq(&self, other: &Self) -> bool {
		self.first_token == other.first_token && self.pairs == other.pairs
	}
}

impl<Op: Copy + Eq, To: Clone + Eq> Eq for Expression<Op, To> {}

#[cfg(test)]
mod test {
	use super::*;

	fn c(
		expression: &Expression<MathOperator, MathToken>,
		ctx: &f32,
	) -> Result<f32, &'static str> {
		use MathOperator::*;
		let climber = Climber::new(
			vec![
				Rule::new(Add, Assoc::Left) | Rule::new(Sub, Assoc::Left),
				Rule::new(Mul, Assoc::Left) | Rule::new(Div, Assoc::Left),
			],
			|lhs: MathToken, op: MathOperator, rhs: MathToken, ctx: &f32| {
				let lhs: f32 = lhs.convert(ctx)?;
				let rhs: f32 = rhs.convert(ctx)?;
				Ok(match op {
					MathOperator::Add => MathToken::Num(lhs + rhs),
					MathOperator::Sub => MathToken::Num(lhs - rhs),
					MathOperator::Mul => MathToken::Num(lhs * rhs),
					MathOperator::Div => MathToken::Num(lhs / rhs),
				})
			},
		);
		climber.process(&expression, ctx)
	}

	#[derive(Hash, Eq, PartialEq, Copy, Clone)]
	pub enum MathOperator {
		Add,
		Sub,
		Mul,
		Div,
	}

	#[derive(Clone)]
	pub enum MathToken {
		Paren(Box<Expression<MathOperator, MathToken>>),
		Num(f32),
		X,
	}

	impl Token<f32, &'static str, f32> for MathToken {
		fn convert(self, ctx: &f32) -> Result<f32, &'static str> {
			Ok(match self {
				MathToken::Paren(expr) => c(expr.as_ref(), ctx)?,
				MathToken::Num(n) => n,
				MathToken::X => *ctx,
			})
		}
	}

	#[test]
	fn process() {
		let res = c(
			&Expression::new(
				MathToken::Num(7.0),
				vec![(MathOperator::Add, MathToken::X)],
			),
			&8.0,
		)
		.unwrap();

		assert_eq!(res, 15.0);
	}
	#[test]
	fn proces_complex() {
		use MathOperator::*;
		use MathToken::*;
		let res = c(
			&Expression::new(
				Num(10.0),
				vec![(Add, Num(5.0)), (Mul, Num(3.0)), (Add, Num(1.0))],
			),
			&8.0,
		)
		.unwrap();
		println!("{}", res);
	}
}