mcvm_parse 0.1.0

Package script lexing and parsing for mcvm
Documentation
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
use std::fmt::{Debug, Display};

use crate::unexpected_token;
use anyhow::bail;

/// Generic side for something like a bracket
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Side {
	Left,
	Right,
}

/// A token that we derive from text
#[derive(Debug, PartialEq, Clone)]
pub enum Token {
	None,
	Whitespace,
	Semicolon,
	Colon,
	Comma,
	Pipe,
	At,
	Variable(String),
	Curly(Side),
	Square(Side),
	Paren(Side),
	Angle(Side),
	Comment(String),
	Ident(String),
	Num(i64),
	Str(String),
}

impl Token {
	pub fn as_string(&self) -> String {
		match self {
			Token::None => String::from("none"),
			Token::Whitespace => String::from(" "),
			Token::Semicolon => String::from(";"),
			Token::Colon => String::from(":"),
			Token::Comma => String::from(","),
			Token::Pipe => String::from("|"),
			Token::At => String::from("@"),
			Token::Variable(name) => String::from("$") + name,
			Token::Curly(Side::Left) => String::from("{"),
			Token::Curly(Side::Right) => String::from("}"),
			Token::Square(Side::Left) => String::from("["),
			Token::Square(Side::Right) => String::from("]"),
			Token::Paren(Side::Left) => String::from("("),
			Token::Paren(Side::Right) => String::from(")"),
			Token::Angle(Side::Left) => String::from("<"),
			Token::Angle(Side::Right) => String::from(">"),
			Token::Comment(text) => String::from("# ") + text,
			Token::Ident(name) => name.clone(),
			Token::Num(num) => num.to_string(),
			Token::Str(string) => format!("\"{string}\""),
		}
	}
}

/// Text positional information with row and column
#[derive(Clone)]
pub struct TextPos(usize, usize);

impl Debug for TextPos {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		write!(f, "({}:{})", self.0, self.1)
	}
}

impl Display for TextPos {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		write!(f, "({}:{})", self.0, self.1)
	}
}

/// Token and TextPos
pub type TokenAndPos = (Token, TextPos);

/// What action to perform after lexing a string character
#[derive(Debug, PartialEq)]
enum StrLexResult {
	Append,
	Escape,
	End,
}

/// Figure out what to do with a character of a string when lexing
fn lex_string_char(c: char, escape: bool) -> StrLexResult {
	if escape {
		StrLexResult::Append
	} else {
		match c {
			'"' => StrLexResult::End,
			'\\' => StrLexResult::Escape,
			_ => StrLexResult::Append,
		}
	}
}

fn is_whitespace(c: char) -> bool {
	c.is_whitespace()
}

/// Checks if a character is part of a valid identifier
fn is_ident(c: char, first: bool) -> bool {
	if first && c.is_numeric() {
		return false;
	}
	c.is_alphanumeric() || c == '_'
}

/// Checks if a character is part of an integer
fn is_num(c: char, first: bool) -> bool {
	if first {
		c.is_numeric() || c == '-'
	} else {
		c.is_numeric()
	}
}

/// Create a list of tokens from package text contents that we will
/// then use for parsing
pub fn lex(text: &str) -> anyhow::Result<Vec<(Token, TextPos)>> {
	let mut tokens: Vec<(Token, TextPos)> = Vec::new();

	// Positional
	let mut line_n: usize = 1;
	let mut last_line_i: usize = 0;

	// Current token
	let mut tok: Token = Token::None;
	let mut tok_finished = false;

	// Specific token-related vars
	let mut escape = false;
	let mut num_str = String::new();

	for (i, c) in text.chars().enumerate() {
		let pos = TextPos(line_n, i - last_line_i);
		if c == '\n' {
			line_n += 1;
			last_line_i = i;
		}

		// Using this loop as a goto
		loop {
			let mut repeat = false;
			match &mut tok {
				Token::None => match c {
					';' => {
						tok = Token::Semicolon;
						tok_finished = true;
					}
					':' => {
						tok = Token::Colon;
						tok_finished = true;
					}
					',' => {
						tok = Token::Comma;
						tok_finished = true;
					}
					'|' => {
						tok = Token::Pipe;
						tok_finished = true;
					}
					'{' => {
						tok = Token::Curly(Side::Left);
						tok_finished = true;
					}
					'}' => {
						tok = Token::Curly(Side::Right);
						tok_finished = true;
					}
					'[' => {
						tok = Token::Square(Side::Left);
						tok_finished = true;
					}
					']' => {
						tok = Token::Square(Side::Right);
						tok_finished = true;
					}
					'(' => {
						tok = Token::Paren(Side::Left);
						tok_finished = true;
					}
					')' => {
						tok = Token::Paren(Side::Right);
						tok_finished = true;
					}
					'<' => {
						tok = Token::Angle(Side::Left);
						tok_finished = true;
					}
					'>' => {
						tok = Token::Angle(Side::Right);
						tok_finished = true;
					}
					'@' => {
						tok = Token::At;
						tok_finished = true;
					}
					'"' => tok = Token::Str(String::new()),
					'#' => tok = Token::Comment(String::new()),
					'$' => tok = Token::Variable(String::new()),
					c if is_whitespace(c) => tok = Token::Whitespace,
					c if is_num(c, true) => {
						tok = Token::Num(0);
						num_str = String::from(c);
					}
					c if is_ident(c, true) => tok = Token::Ident(String::from(c)),
					_ => unexpected_token!(tok, pos),
				},
				Token::Str(string) => match lex_string_char(c, escape) {
					StrLexResult::Append => {
						string.push(c);
						escape = false;
					}
					StrLexResult::Escape => escape = true,
					StrLexResult::End => {
						tok_finished = true;
						escape = false;
					}
				},
				Token::Comment(string) => {
					if c == '\n' {
						tok_finished = true;
					} else {
						string.push(c);
					}
				}
				Token::Variable(name) => {
					let allowed = if name.is_empty() {
						is_ident(c, true)
					} else {
						is_ident(c, false)
					};

					if allowed {
						name.push(c);
					} else {
						repeat = true;
						tokens.push((tok, pos.clone()));
						tok = Token::None;
					}
				}
				Token::Whitespace => {
					if !is_whitespace(c) {
						repeat = true;
						tokens.push((tok, pos.clone()));
						tok = Token::None;
					}
				}
				Token::Ident(name) => {
					if is_ident(c, false) {
						name.push(c);
					} else {
						repeat = true;
						tokens.push((tok, pos.clone()));
						tok = Token::None;
					}
				}
				Token::Num(num) => {
					if is_num(c, false) {
						num_str.push(c);
					} else {
						repeat = true;
						if num_str == "-" {
							bail!("Invalid number '{num_str}', {pos}");
						}
						*num = num_str.parse().expect("Number contains invalid characters");
						tokens.push((tok, pos.clone()));
						tok = Token::None;
					}
				}
				_ => {}
			}
			if !repeat {
				break;
			}
		}
		if tok_finished {
			tok_finished = false;
			tokens.push((tok, pos));
			tok = Token::None;
		}
	}
	let final_pos = TextPos(line_n, text.len() - last_line_i);
	match &mut tok {
		Token::Num(num) => {
			*num = num_str.parse().expect("Number contains invalid characters");
			tokens.push((tok, final_pos));
		}
		Token::None => {}
		_ => tokens.push((tok, final_pos)),
	}
	Ok(tokens)
}

/// Removes whitespace characters and comments from an iterator of tokens
pub fn reduce_tokens<'a, T: Iterator<Item = &'a TokenAndPos>>(
	tokens: T,
) -> impl Iterator<Item = &'a TokenAndPos> {
	tokens.filter(|(tok, ..)| !matches!(tok, Token::Comment(..) | Token::Whitespace | Token::None))
}

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

	macro_rules! assert_tokens {
		($text:literal, $toks:expr) => {
			assert_tokens!(lex($text), $toks)
		};

		($lexed:expr, $toks:expr) => {
			match $lexed {
				Ok(lexed) => {
					assert_eq!(lexed.len(), $toks.len());
					for ((left, _), right) in lexed.iter().zip($toks) {
						assert_eq!(left, &right);
					}
				}
				Err(e) => {
					cprintln!("<r>{}", e);
					panic!();
				}
			};
		};
	}

	#[test]
	fn test_chars() {
		assert!(is_ident('a', false));
		assert!(is_ident('a', true));
		assert!(is_ident('B', false));
		assert!(is_ident('B', true));
		assert!(is_ident('_', false));
		assert!(is_ident('_', true));

		assert!(is_ident('5', false));
		assert!(!is_ident('2', true));

		assert!(is_num('8', false));
		assert!(is_num('8', true));
		assert!(!is_num('t', false));
		assert!(!is_num('t', true));
		assert!(!is_num('.', false));
		assert!(!is_num('.', true));
		assert!(is_num('-', true));
		assert!(!is_num('-', false));

		assert!(is_whitespace(' '));
		assert!(is_whitespace('\n'));
		assert!(!is_whitespace('a'));
		assert!(!is_whitespace('%'));
	}

	#[test]
	fn test_semicolon() {
		assert_tokens!(";;", vec![Token::Semicolon, Token::Semicolon]);
	}

	#[test]
	fn test_string_chars() {
		assert_eq!(lex_string_char('d', false), StrLexResult::Append);
		assert_eq!(lex_string_char('\'', false), StrLexResult::Append);
		assert_eq!(lex_string_char('"', false), StrLexResult::End);
		assert_eq!(lex_string_char('"', true), StrLexResult::Append);
		assert_eq!(lex_string_char('\\', false), StrLexResult::Escape);
		assert_eq!(lex_string_char('\\', true), StrLexResult::Append);
	}

	#[test]
	fn test_string() {
		assert_tokens!("\"Hello\"", vec![Token::Str(String::from("Hello"))]);
	}

	#[test]
	fn test_combo() {
		assert_tokens!(
			"\"Uno\"; \"Dos\"; \"Tres\"; Identifier",
			vec![
				Token::Str(String::from("Uno")),
				Token::Semicolon,
				Token::Whitespace,
				Token::Str(String::from("Dos")),
				Token::Semicolon,
				Token::Whitespace,
				Token::Str(String::from("Tres")),
				Token::Semicolon,
				Token::Whitespace,
				Token::Ident(String::from("Identifier"))
			]
		);
	}

	#[test]
	fn test_all() {
		assert_tokens!(
			"\"Hello\"; ident{}@routine[]$var():-1000,|# comment",
			vec![
				Token::Str(String::from("Hello")),
				Token::Semicolon,
				Token::Whitespace,
				Token::Ident(String::from("ident")),
				Token::Curly(Side::Left),
				Token::Curly(Side::Right),
				Token::At,
				Token::Ident(String::from("routine")),
				Token::Square(Side::Left),
				Token::Square(Side::Right),
				Token::Variable(String::from("var")),
				Token::Paren(Side::Left),
				Token::Paren(Side::Right),
				Token::Colon,
				Token::Num(-1000),
				Token::Comma,
				Token::Pipe,
				Token::Comment(String::from(" comment"))
			]
		);
	}

	#[test]
	fn test_comment() {
		assert_tokens!(
			"\"Foo\" # Comment\n \"Bar\"",
			vec![
				Token::Str(String::from("Foo")),
				Token::Whitespace,
				Token::Comment(String::from(" Comment")),
				Token::Whitespace,
				Token::Str(String::from("Bar"))
			]
		);
	}

	#[test]
	fn test_num() {
		assert_tokens!(
			"12345;888;0;-10",
			vec![
				Token::Num(12345),
				Token::Semicolon,
				Token::Num(888),
				Token::Semicolon,
				Token::Num(0),
				Token::Semicolon,
				Token::Num(-10)
			]
		);
	}
}