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
use crate::units::Unit::{Foot, Inch};
use crate::Operator::{Caret, Divide, LeftParen, Minus, Modulo, Multiply, Plus, RightParen};
use crate::TextOperator::{Of, To};
use crate::Token;
use crate::UnaryOperator::{Factorial, Percent};

#[derive(Debug)]
/// A struct with a [`Token`](AstNode::token) and [`AstNode`] [`children`](AstNode::children)
pub struct AstNode {
	/// The children of the [`AstNode`]
	pub children: Vec<AstNode>,
	/// The token of the [`AstNode`]
	pub token: Token,
}

impl AstNode {
	pub const fn new(token: Token) -> AstNode {
		AstNode {
			children: Vec::new(),
			token,
		}
	}
}

/// Parse [`Token`]s into an Abstract Syntax Tree ([`AstNode`])
pub fn parse(tokens: &[Token]) -> Result<AstNode, String> {
	parse_text_operators(tokens, 0).and_then(|(ast, next_pos)| {
		if next_pos == tokens.len() {
			Ok(ast)
		} else {
			Err(format!(
				"Expected end of input, found {:?} at {}",
				tokens[next_pos], next_pos
			))
		}
	})
}

// level 1 precedence (lowest): to, of
/// Parse [`To`](crate::TextOperator::To) and [`Of`](crate::TextOperator::Of)
pub fn parse_text_operators(tokens: &[Token], pos: usize) -> Result<(AstNode, usize), String> {
	// do higher precedences first, then come back down
	let (mut node, mut pos) = parse_plus(tokens, pos)?;
	// now we loop through the next tokens
	loop {
		let token = tokens.get(pos);
		match token {
			// if there's a match, we once again do higher precedences, then come
			// back down again and continue the loop
			Some(&Token::TextOperator(To)) | Some(&Token::TextOperator(Of)) => {
				let (right_node, next_pos) = parse_plus(tokens, pos + 1)?;
				let mut new_node = AstNode::new(token.unwrap().clone());
				new_node.children.push(node);
				new_node.children.push(right_node);
				node = new_node;
				pos = next_pos;
			}
			// if there's no match, we go down to a lower precedence
			_ => {
				return Ok((node, pos));
			}
		}
	}
}

/// Parse [`+`](crate::Operator::Plus), [`-`](crate::Operator::Minus)
pub fn parse_plus(tokens: &[Token], pos: usize) -> Result<(AstNode, usize), String> {
	let (mut node, mut pos) = parse_unary(tokens, pos)?;
	loop {
		let token = tokens.get(pos);
		match token {
			Some(&Token::Operator(Plus)) | Some(&Token::Operator(Minus)) => {
				let (right_node, next_pos) = parse_unary(tokens, pos + 1)?;
				let mut new_node = AstNode::new(token.unwrap().clone());
				new_node.children.push(node);
				new_node.children.push(right_node);
				node = new_node;
				pos = next_pos;
			}
			_ => {
				return Ok((node, pos));
			}
		}
	}
}

/// Parse [`unary -`](Token::Negative) (for example -5)
pub fn parse_unary(tokens: &[Token], pos: usize) -> Result<(AstNode, usize), String> {
	// Since a unary operator has no left side, we parse the the unary operator immediately
	let token = tokens.get(pos);
	match token {
		Some(&Token::Operator(Minus)) => {
			let (right_node, next_pos) = parse_mult_level(tokens, pos + 1)?;
			let mut new_node = AstNode::new(Token::Negative);
			new_node.children.push(right_node);
			Ok((new_node, next_pos))
		}
		_ => parse_mult_level(tokens, pos),
	}
}

/// Parse [`*`](crate::Operator::Multiply), [`/`](crate::Operator::Divide), [`Modulo`](crate::Operator::Modulo), implicative multiplication (for example`2pi`), foot-inch syntax (for example `6'4"`)
pub fn parse_mult_level(tokens: &[Token], pos: usize) -> Result<(AstNode, usize), String> {
	// parse foot-inch syntax 6'4"
	let token0 = tokens.get(pos);
	if let Some(Token::Number(_number)) = token0 {
		let token1 = tokens.get(pos + 1);
		if let Some(Token::Unit(Foot)) = token1 {
			let token2 = tokens.get(pos + 2);
			if let Some(Token::Number(_number)) = token2 {
				let token3 = tokens.get(pos + 3);
				if let Some(Token::Unit(Inch)) = token3 {
					let new_node = AstNode {
						children: vec![
							AstNode {
								children: vec![AstNode::new(token0.unwrap().clone())],
								token: Token::Unit(Foot),
							},
							AstNode {
								children: vec![AstNode::new(token2.unwrap().clone())],
								token: Token::Unit(Inch),
							},
						],
						token: Token::Operator(Plus),
					};
					return Ok((new_node, pos + 4));
				}
			}
		}
	}

	let (mut node, mut pos) = parse_caret(tokens, pos)?;

	loop {
		let token = tokens.get(pos);
		match token {
			Some(&Token::Operator(Multiply))
			| Some(&Token::Operator(Divide))
			| Some(&Token::Operator(Modulo)) => {
				let (right_node, next_pos) = parse_caret(tokens, pos + 1)?;
				let mut new_node = AstNode::new(token.unwrap().clone());
				new_node.children.push(node);
				new_node.children.push(right_node);
				node = new_node;
				pos = next_pos;
			}

			// Below is implicative multiplication, for example '2pi'. Constants and
			// such will only end up here if they were unable to be parsed as part of
			// other operators.
			// Note that this match statement matches an AstNode token, but the
			// matches nested inside check the [`Token`]s. That's why we for example
			// match a FunctionIdentifier, and inside that, a RightParen.

			// pi2, )2
			Some(&Token::Number(_)) => {
				let last_token = tokens.get(pos - 1);
				match last_token {
					Some(&Token::Constant(_)) | Some(&Token::Operator(RightParen)) => {
						let (right_node, next_pos) = parse_caret(tokens, pos)?;
						let mut new_node = AstNode::new(Token::Operator(Multiply));
						new_node.children.push(node);
						new_node.children.push(right_node);
						node = new_node;
						pos = next_pos;
					}
					_ => {
						return Ok((node, pos));
					}
				}
			}
			// 2pi, )pi
			Some(&Token::Constant(_)) => {
				let last_token = tokens.get(pos - 1);
				match last_token {
					Some(&Token::Number(_)) | Some(&Token::Operator(RightParen)) => {
						let (right_node, next_pos) = parse_caret(tokens, pos)?;
						let mut new_node = AstNode::new(Token::Operator(Multiply));
						new_node.children.push(node);
						new_node.children.push(right_node);
						node = new_node;
						pos = next_pos;
					}
					_ => {
						return Ok((node, pos));
					}
				}
			}
			// 2log(1), )log(1)
			Some(&Token::FunctionIdentifier(_)) => {
				let last_token = tokens.get(pos - 1);
				match last_token {
					Some(&Token::Number(_)) | Some(&Token::Operator(RightParen)) => {
						let (right_node, next_pos) = parse_caret(tokens, pos)?;
						let mut new_node = AstNode::new(Token::Operator(Multiply));
						new_node.children.push(node);
						new_node.children.push(right_node);
						node = new_node;
						pos = next_pos;
					}
					_ => {
						return Ok((node, pos));
					}
				}
			}
			// 2(3), pi(3), )(3)
			Some(&Token::Operator(LeftParen)) => {
				let last_token = tokens.get(pos - 1);
				match last_token {
					Some(&Token::Number(_))
					| Some(&Token::Constant(_))
					| Some(&Token::Operator(RightParen)) => {
						let (right_node, next_pos) = parse_caret(tokens, pos)?;
						let mut new_node = AstNode::new(Token::Operator(Multiply));
						new_node.children.push(node);
						new_node.children.push(right_node);
						node = new_node;
						pos = next_pos;
					}
					_ => {
						return Ok((node, pos));
					}
				}
			}
			_ => {
				return Ok((node, pos));
			}
		}
	}
}

/// Parse [`^`](crate::Operator::Caret)
pub fn parse_caret(tokens: &[Token], pos: usize) -> Result<(AstNode, usize), String> {
	let (mut node, mut pos) = parse_unary_high(tokens, pos)?;
	loop {
		let token = tokens.get(pos);
		match token {
			Some(&Token::Operator(Caret)) => {
				let (right_node, next_pos) = parse_unary_high(tokens, pos + 1)?;
				let mut new_node = AstNode::new(token.unwrap().clone());
				new_node.children.push(node);
				new_node.children.push(right_node);
				node = new_node;
				pos = next_pos;
			}
			_ => {
				return Ok((node, pos));
			}
		}
	}
}

/// Parse [`unary -`](Token::Negative) at high precedence (for example in 3^-2)
pub fn parse_unary_high(tokens: &[Token], pos: usize) -> Result<(AstNode, usize), String> {
	let token = tokens.get(pos);
	match token {
		Some(&Token::Operator(Minus)) => {
			let (right_node, next_pos) = parse_suffix(tokens, pos + 1)?;
			let mut new_node = AstNode::new(Token::Negative);
			new_node.children.push(right_node);
			Ok((new_node, next_pos))
		}
		_ => parse_suffix(tokens, pos),
	}
}

/// Parse [`!!`](crate::UnaryOperator::Factorial), [`Percent`](crate::UnaryOperator::Percent), units attached to values
pub fn parse_suffix(tokens: &[Token], pos: usize) -> Result<(AstNode, usize), String> {
	let (mut node, mut pos) = parse_highest(tokens, pos)?;
	loop {
		let token = tokens.get(pos);
		match token {
			Some(&Token::UnaryOperator(Factorial))
			| Some(&Token::UnaryOperator(Percent))
			| Some(&Token::NamedNumber(_)) => {
				// Here we are handling unary operators, aka stuff written as
				// "Number Operator" (3!) instead of "Number Operator Number" (3+3).
				// Therefore, if we find a match, we don't parse what comes after it.
				let mut new_node = AstNode::new(token.unwrap().clone());
				new_node.children.push(node);
				node = new_node;
				pos += 1;
			}
			Some(&Token::Unit(_unit)) => {
				// We won't allow units to repeat, like "1min min", so we end the loop if it's found.
				let mut new_node = AstNode::new(token.unwrap().clone());
				new_node.children.push(node);
				return Ok((new_node, pos + 1));
			}
			_ => {
				// let's say we parse 1+2. parse_level_7 then returns 1, and token
				// is set to plus. Plus has lower precedence than level 4, so we
				// don't do anything, and pass the number down to a lower precedence.
				return Ok((node, pos));
			}
		}
	}
}

/// Parse [`Number`](Token::Number), standalone [`Unit`](Token::Unit), [`Constant`](Token::Constant), [`FunctionIdentifier`](Token::FunctionIdentifier), [`Paren`](Token::Paren)
pub fn parse_highest(tokens: &[Token], pos: usize) -> Result<(AstNode, usize), String> {
	let token: &Token = tokens
		.get(pos)
		.ok_or(format!("Unexpected end of input at {}", pos))?;
	match token {
		&Token::Number(_number) => {
			let node = AstNode::new(token.clone());
			Ok((node, pos + 1))
		}
		&Token::Unit(_unit) => {
			let node = AstNode::new(token.clone());
			Ok((node, pos + 1))
		}
		Token::Constant(_constant) => {
			let node = AstNode::new(token.clone());
			Ok((node, pos + 1))
		}
		Token::FunctionIdentifier(_function_identifier) => {
			let left_paren_pos = pos + 1;
			let left_paren_token = tokens.get(left_paren_pos);
			// check if '(' comes after function identifier, like 'log('
			match left_paren_token {
				Some(&Token::Operator(LeftParen)) => {
					// parse everything inside as you would with normal parentheses,
					// then put it inside an ast node.
					parse_text_operators(tokens, left_paren_pos + 1).and_then(|(node, next_pos)| {
						if let Some(&Token::Operator(RightParen)) = tokens.get(next_pos) {
							let mut function_node = AstNode::new(token.clone());
							function_node.children.push(node);
							Ok((function_node, next_pos + 1))
						} else {
							Err(format!(
								"Expected closing paren at {} but found {:?}",
								next_pos,
								tokens.get(next_pos)
							))
						}
					})
				}
				_ => Err(format!(
					"Expected ( after {} at {:?} but found {:?}",
					left_paren_pos, token, left_paren_token
				)),
			}
		}
		Token::Operator(LeftParen) => {
			parse_text_operators(tokens, pos + 1).and_then(|(node, next_pos)| {
				if let Some(&Token::Operator(RightParen)) = tokens.get(next_pos) {
					let mut paren_node = AstNode::new(Token::Paren);
					paren_node.children.push(node);
					Ok((paren_node, next_pos + 1))
				} else {
					Err(format!(
						"Expected closing paren at {} but found {:?}",
						next_pos,
						tokens.get(next_pos)
					))
				}
			})
		}
		_ => Err(format!(
			"Unexpected token {:?}, expected paren or number",
			token
		)),
	}
}