gruggers 0.9.0

rust implementation of the grug language
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
use super::SPACES_PER_INDENT;
use allocator_api2::vec::Vec;
use crate::arena::Arena;
use crate::error::{ErrorKind, Error, SourceSpan};

use std::ffi::OsStr;

#[derive(Debug, Clone, Copy)]
pub struct Token<'a> {
	pub(super) ty: TokenType,
	pub(super) value: &'a str,
	pub(super) span: SourceSpan,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum TokenType {
	OpenParenthesis,
	CloseParenthesis,
	OpenBrace,
	CloseBrace,
	OpenBracket,
	CloseBracket,
	Plus,
	Minus,
	Star,
	ForwardSlash,
	Comma,
	Colon,
	Dot,
	NewLine,
	DoubleEquals,
	NotEquals,
	Equal,
	GreaterEquals,
	Greater,
	LessEquals,
	Less,
	And,
	Or,
	Not,
	True,
	False,
	If,
	Else,
	While,
	Break,
	Return,
	Continue,
	Export,
	Local,
	Space,
	Indentation,
	String,
	Entity,
	Resource,
	Word,
	Int32,
	Float32,
	Comment,
}

impl std::fmt::Display for TokenType {
	fn fmt (&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
		return match self {
			Self::OpenParenthesis => write!(f, "'('"),
			Self::CloseParenthesis => write!(f, "')'"),
			Self::OpenBrace => write!(f, "'{{'"),
			Self::CloseBrace => write!(f, "'}}'"),
			Self::OpenBracket => write!(f, "'['"),
			Self::CloseBracket => write!(f, "']'"),
			Self::Plus => write!(f, "'+'"),
			Self::Minus => write!(f, "'-'"),
			Self::Star => write!(f, "'*'"),
			Self::ForwardSlash => write!(f, "'/'"),
			Self::Comma => write!(f, "','"),
			Self::Colon => write!(f, "':'"),
			Self::Dot => write!(f, "'.'"),
			Self::NewLine => write!(f, "line break ('\\n')"),
			Self::DoubleEquals => write!(f, "'=='"),
			Self::NotEquals => write!(f, "'!='"),
			Self::Equal => write!(f, "'='"),
			Self::GreaterEquals => write!(f, "'>='"),
			Self::Greater => write!(f, "'>'"),
			Self::LessEquals => write!(f, "'<='"),
			Self::Less => write!(f, "'<'"),
			Self::And => write!(f, "'and'"),
			Self::Or => write!(f, "'or'"),
			Self::Not => write!(f, "'not'"),
			Self::True => write!(f, "'true'"),
			Self::False => write!(f, "'false'"),
			Self::If => write!(f, "'if'"),
			Self::Else => write!(f, "'else'"),
			Self::While => write!(f, "'while'"),
			Self::Break => write!(f, "'break'"),
			Self::Return => write!(f, "'return'"),
			Self::Continue => write!(f, "'continue'"),
			Self::Export => write!(f, "'export'"),
			Self::Local => write!(f, "'local'"),
			Self::Space => write!(f, "space (' ')"),
			Self::Indentation => write!(f, "indentation"),
			Self::String => write!(f, "string"),
			Self::Word => write!(f, "word"),
			Self::Int32 => write!(f, "number"),
			Self::Float32 => write!(f, "number"),
			Self::Comment => write!(f, "comment"),
			Self::Resource => write!(f, "resource string"),
			Self::Entity => write!(f, "entity string"),
		};
	}
}

// This only exists to add coverage to the fmt function above. I'd remove it if
// i could use #[coverage(off)]
#[cfg(test)]
mod test {
	use super::*;
	#[test]
	fn test() {
		let x = &[
			TokenType::OpenParenthesis,
			TokenType::CloseParenthesis,
			TokenType::OpenBrace,
			TokenType::CloseBrace,
			TokenType::Plus,
			TokenType::Minus,
			TokenType::Star,
			TokenType::ForwardSlash,
			TokenType::Comma,
			TokenType::Colon,
			TokenType::NewLine,
			TokenType::DoubleEquals,
			TokenType::NotEquals,
			TokenType::Equal,
			TokenType::GreaterEquals,
			TokenType::Greater,
			TokenType::LessEquals,
			TokenType::Less,
			TokenType::And,
			TokenType::Or,
			TokenType::Not,
			TokenType::True,
			TokenType::False,
			TokenType::If,
			TokenType::Else,
			TokenType::While,
			TokenType::Break,
			TokenType::Return,
			TokenType::Continue,
			TokenType::Export,
			TokenType::Local,
			TokenType::Space,
			TokenType::Indentation,
			TokenType::String,
			TokenType::Entity,
			TokenType::Resource,
			TokenType::Word,
			TokenType::Int32,
			TokenType::Float32,
			TokenType::Comment,
		];
		x.into_iter().for_each(|x| println!("{}", x));
	}
}

pub fn tokenize<'a, P: AsRef<OsStr>>(file_text: &'a str, arena: &'a Arena, file_path: P) -> Result<Vec<Token<'a>, &'a Arena>, Error> {
	let file_path = file_path.as_ref();
	let mut tokens = Vec::new_in(arena);
	let mut cur_line = 1;

	macro_rules! new_tokenizer_error{
		(($offset: expr, $line: expr) => $format_str: literal $(, $args: expr)*) => {
			Error::new(ErrorKind::TOKENIZER_ERROR, "", file_path, file_text, SourceSpan{offset: $offset, line: $line}, format_args!($format_str $(,$args)*))
		};
		($span: expr => $format_str: literal $(, $args: expr)*) => {
			Error::new(ErrorKind::TOKENIZER_ERROR, "", file_path, file_text, $span, format_args!($format_str $(,$args)*))
		}
	}

	let file_text_str = file_text;
	let file_text = file_text.as_bytes();
	let mut i = 0;

	'outer: while i < file_text.len() {
		macro_rules! token_match {
			($tag: literal => $expr: expr$(, $extra_expr: expr)?) => {
				let lit_len = $tag.len();
				if i + lit_len <= file_text.len() && &file_text[i..(i+lit_len)] == &*$tag {
					// SAFETY: string is guaranteed to be utf8 because it tests equal to tag which is utf8 despite being a byte array
					tokens.push(Token{
						ty: $expr, 
						value: unsafe{str::from_utf8_unchecked(&file_text[i..(i+lit_len)])},
						span: SourceSpan{offset: i, line: cur_line},
					});
					i += lit_len;
					$($extra_expr;)?
					continue;
				}
			}
		}
		macro_rules! token_match_word {
			($tag: literal => $expr: expr$(, $extra_expr: expr)?) => {
				let lit_len = $tag.len();
				if i + lit_len <= file_text.len() && &file_text[i..(i+lit_len)] == &*$tag && (i + lit_len == file_text.len() || !is_word_char(file_text[i+lit_len] as char)) {
					// SAFETY: string is guaranteed to be utf8 because it tests equal to tag which is utf8 despite being a byte array
					tokens.push(Token{
						ty: $expr, 
						value: unsafe{str::from_utf8_unchecked(&file_text[i..(i+lit_len)])},
						span: SourceSpan{offset: i, line: cur_line},
					});
					i += lit_len;
					$($extra_expr;)?
					continue 'outer;
				}
			}
		}
		token_match!(b"(" => TokenType::OpenParenthesis);
		token_match!(b")" => TokenType::CloseParenthesis);
		token_match!(b"{" => TokenType::OpenBrace);
		token_match!(b"}" => TokenType::CloseBrace);
		token_match!(b"[" => TokenType::OpenBracket);
		token_match!(b"]" => TokenType::CloseBracket);
		token_match!(b"+" => TokenType::Plus);
		token_match!(b"-" => TokenType::Minus);
		token_match!(b"*" => TokenType::Star);
		token_match!(b"/" => TokenType::ForwardSlash);
		token_match!(b"," => TokenType::Comma);
		token_match!(b":" => TokenType::Colon);
		token_match!(b"." => TokenType::Dot);
		token_match!(b"\n" => TokenType::NewLine, {cur_line += 1;});
		token_match!(b"\r\n" => TokenType::NewLine, {cur_line += 1;});
		token_match!(b"==" => TokenType::DoubleEquals);
		token_match!(b"!=" => TokenType::NotEquals);
		token_match!(b"=" => TokenType::Equal);
		token_match!(b">=" => TokenType::GreaterEquals);
		token_match!(b">" => TokenType::Greater);
		token_match!(b"<=" => TokenType::LessEquals);
		token_match!(b"<" => TokenType::Less);
		token_match_word!(b"and" => TokenType::And);
		token_match_word!(b"or" => TokenType::Or);
		token_match_word!(b"not" => TokenType::Not);
		token_match_word!(b"true" => TokenType::True);
		token_match_word!(b"false" => TokenType::False);
		token_match_word!(b"if" => TokenType::If);
		token_match_word!(b"else" => TokenType::Else);
		token_match_word!(b"while" => TokenType::While);
		token_match_word!(b"break" => TokenType::Break);
		token_match_word!(b"return" => TokenType::Return);
		token_match_word!(b"continue" => TokenType::Continue);
		token_match_word!(b"export" => TokenType::Export);
		token_match_word!(b"local" => TokenType::Local);

		// Spaces
		let lit_len = b" ".len();
		if file_text[i..(i+lit_len)] == *b" " {
			let old_i = i;
			while i < file_text.len() && file_text[i] == b' ' {
				i += 1;
			}
			let num_spaces = i - old_i;
			if num_spaces == 1 {
				// SAFETY: string starting at current index is guaranteed to be utf8 it matches a valid utf8 byte
				tokens.push(Token{
					ty: TokenType::Space, 
					value: unsafe{str::from_utf8_unchecked(&file_text[old_i..i])},
					span: SourceSpan{offset: old_i, line: cur_line},
				});
				continue;
			}
			if num_spaces % SPACES_PER_INDENT != 0 {
				return Err(new_tokenizer_error!(
					(i - num_spaces, cur_line) => 
					"Expected multiple of {} spaces but found {} spaces", SPACES_PER_INDENT, num_spaces
				));
			}

			// SAFETY: string starting at current index is guaranteed to be utf8 it matches a valid utf8 byte
			tokens.push(Token{
				ty: TokenType::Indentation, 
				value: unsafe{str::from_utf8_unchecked(&file_text[old_i..i])},
				span: SourceSpan{offset: old_i, line: cur_line},
			});
			continue;
		}
			
		// Entitiy strings, resource strings, and basic strings
		// Strings
		for (start, ty) in [(&b"r\""[..], TokenType::Resource), (&b"e\""[..], TokenType::Entity), (&b"\""[..], TokenType::String)] {
			if file_text[i..].starts_with(start) {
				let quote_start_index = i;
				i += start.len();
				let start_index = i;
				let start_line = cur_line;

				let mut is_escaped = false;

				let mut allocated = Vec::new_in(arena);
				let mut copied_len = 0;

				while i < file_text.len() && file_text[i] != b'"' && !is_escaped {
					if is_escaped {
						is_escaped = false;
						// TODO: Wait for response 
						// [https://github.com/grug-lang/grug-tests/issues/64]
						let next_char = match file_text[i] {
							b't'  => b'\t',
							b'n'  => b'\n',
							b'r'  => b'\r',
							x => x,
						};
						allocated.push(next_char);
						copied_len = i - start_index;
						// only normal strings can be escaped 
					} else if file_text[i] == b'\\' && ty == TokenType::String {
						is_escaped = true;
						allocated.extend_from_slice(&file_text[(start_index + copied_len)..i]);
						copied_len = i - start_index;
					}
					if file_text[i] == b'\0' {
						return Err(new_tokenizer_error!(
							(i, cur_line) => 
							"Unexpected null byte on line {}", cur_line
						));
					}
					if i + 2 < file_text.len() && is_escaped && (&file_text[i..=(i+2)] == b"\\\r\n" || &file_text[i..=(i+1)] == b"\\\n") {
						return Err(new_tokenizer_error!(
							(i, cur_line) => 
							"Unexpected line break in string on line {}", cur_line
						));
					}
					if file_text[i] == b'\n' {
						cur_line += 1;
					}
					i += 1;
				}
				if i >= file_text.len() {
					return Err(new_tokenizer_error!(
						(quote_start_index, start_line) => 
						"Unclosed \" on line {}", start_line
					));
				}
				let value = if !allocated.is_empty() {
					unsafe{str::from_utf8_unchecked(allocated.leak())}
				} else {
					unsafe{str::from_utf8_unchecked(&file_text[start_index..(i)])}
				};
				tokens.push(Token{
					ty,
					// SAFETY: string starting at current index is guaranteed to be utf8 it matches a valid utf8 byte
					value,
					span: SourceSpan{offset: quote_start_index, line: start_line},
				});
				i += 1;
				continue 'outer;
			}
		}

		// TODO: Handle unicode strings
		// Words
		if (file_text[i] as char).is_ascii_alphabetic() || file_text[i] == b'_' {
			let start = i;
			while i < file_text.len() && ((file_text[i] as char).is_ascii_alphanumeric() || file_text[i] == b'_'){
				i += 1
			}
			// SAFETY: string starting at current index is guaranteed to be utf8 it matches a valid utf8 byte
			tokens.push(Token{
				ty: TokenType::Word, 
				value: unsafe{str::from_utf8_unchecked(&file_text[start..i])},
				span: SourceSpan{offset: start, line: cur_line},
			});
			continue;
		}

		// Numbers
		if (file_text[i] as char).is_ascii_digit() {
			let start = i;
			let mut seen_period = false;
			i += 1;
			while i < file_text.len() && ((file_text[i] as char).is_ascii_digit() || file_text[i] == b'.') {
				if file_text[i] == b'.'{
					if seen_period {
						return Err(new_tokenizer_error!(
							(i, cur_line) => 
							"Encountered two '.' periods in a number on line {}", cur_line
						));
					}
					seen_period = true;
				}
				i += 1;
			}

			if seen_period {
				if file_text[i - 1] == b'.' {
					// NOTE: I think floats with trailing periods
					// should be allowed but i can understand why
					// they're not
					return Err(new_tokenizer_error!(
						(i, cur_line) => 
						"Missing digit after decimal point in '{}'", &file_text_str[start..i]
					));
				}
				// SAFETY: string starting at current index is guaranteed to be utf8 it matches a valid utf8 byte
				tokens.push(Token{
					ty: TokenType::Float32, 
					value: unsafe{str::from_utf8_unchecked(&file_text[start..i])},
					span: SourceSpan{offset: start, line: cur_line},
				});
			}
			else {
				// SAFETY: string starting at current index is guaranteed to be utf8 it matches a valid utf8 byte
				tokens.push(Token{
					ty: TokenType::Int32, 
					value: unsafe{str::from_utf8_unchecked(&file_text[start..i])},
					span: SourceSpan{offset: start, line: cur_line},
				});
			}
			continue;
		}

		// Comments
		if file_text[i] == b'#' {
			let old_i = i;
			i += 1;
			if i >= file_text.len() || file_text[i] != b' ' {
				return Err(new_tokenizer_error!(
					(i, cur_line) => 
					"Expected space (' ') after '#'"
				));
			}
			i += 1;
			let start = i;
			while i < file_text.len() && file_text[i] != b'\r' && file_text[i] != b'\n' {
				if file_text[i] == b'\0' {
					return Err(new_tokenizer_error!(
						(i, cur_line) => 
						"Unexpected null byte on line {}", cur_line
					));
				}
				i += 1;
			}
			
			if (i - start) == 0 {
				return Err(new_tokenizer_error!(
					(i - 1, cur_line) => 
					"Expected comment to contain some text"
				));
			} else if (file_text[i - 1] as char).is_ascii_whitespace() {
				return Err(new_tokenizer_error!(
					(i, cur_line) => 
					"A comment has trailing whitespace on line {}", cur_line
				));
			}

			// SAFETY: string starting at current index is guaranteed to be utf8 it matches a valid utf8 byte
			tokens.push(Token{
				ty: TokenType::Comment, 
				value: unsafe{str::from_utf8_unchecked(&file_text[start..i])},
				span: SourceSpan{offset: old_i, line: cur_line},
			});
			continue;
		}

		return Err(new_tokenizer_error!(
			(i, cur_line) => 
			"Unrecognized character '{}'", file_text_str[i..].chars().next().expect("There is atleast one more character")
		));
	}
	
	Ok(tokens)
}

fn is_word_char(ch: char) -> bool {
	ch.is_ascii_alphanumeric() || ch == '_'
}