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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
use types::Key;

pub mod parser;
pub mod table;
pub mod text;
pub mod toml_string;
pub mod types;

use {crate_prelude::*, std::ops::Deref};

/// BOML's TOML parser. Create a new one with `::new()` or `::parse()`, then use
/// it just like a [`Table`].
pub struct Toml<'a> {
	pub text: &'a str,
	table: Table<'a>,
}
impl<'a> Toml<'a> {
	/// A wrapper around [`Toml::parse`].
	#[inline(always)]
	pub fn new(text: &'a str) -> Result<Self, Error> {
		Self::parse(text)
	}

	/// Attempts to parse the provided string as TOML.
	pub fn parse(text: &'a str) -> Result<Self, Error> {
		let mut text = Text { text, idx: 0 };
		text.skip_whitespace_and_newlines();
		let mut root_table = Table::default();
		// (table name, table, if it's a member of an array of tables)
		let mut current_table: Option<(Key<'_>, Table<'_>, bool)> = None;

		while text.idx < text.end() {
			match text.current_byte().unwrap() {
				// Comment
				b'#' => {
					if let Some(newline_idx) = text.excerpt(text.idx..).find(b'\n') {
						text.idx = newline_idx;
					} else {
						// Comment is at end of file
						break;
					}
				}
				// Table definition
				b'[' => {
					if let Some((key, table, array)) = current_table.take() {
						insert_subtable(&mut root_table, key, table, array)?;
					}

					if text.byte(text.idx + 1) == Some(b'[') {
						text.idx += 2;
						text.skip_whitespace();
						let table_name = parser::parse_key(&mut text)?;
						text.idx += 1;
						text.skip_whitespace();

						if text.current_byte() != Some(b']')
							|| text.byte(text.idx + 1) != Some(b']')
						{
							return Err(Error {
								start: table_name.text.span().start - 1,
								end: table_name.text.span().end,
								kind: ErrorKind::UnclosedBracket,
							});
						}
						text.idx += 2;

						current_table = Some((table_name, Table::default(), true));
					} else {
						text.idx += 1;
						text.skip_whitespace();
						let table_name = parser::parse_key(&mut text)?;
						text.idx += 1;
						text.skip_whitespace();

						if text.current_byte() != Some(b']') {
							return Err(Error {
								start: table_name.text.span().start - 1,
								end: table_name.text.span().end,
								kind: ErrorKind::UnclosedBracket,
							});
						}
						text.idx += 1;

						current_table = Some((table_name, Table::default(), false));
					}
				}
				// Key definition
				_ => {
					let (key, value) = parser::parse_assignment(&mut text)?;

					let table = if let Some((_, ref mut table, _)) = current_table {
						table
					} else {
						&mut root_table
					};

					table.insert(key, value);

					text.idx += 1;
				}
			}

			text.skip_whitespace_and_newlines();
		}

		if let Some((key, table, array)) = current_table.take() {
			insert_subtable(&mut root_table, key, table, array)?;
		}

		Ok(Self {
			text: text.text,
			table: root_table,
		})
	}
}
impl<'a> Deref for Toml<'a> {
	type Target = Table<'a>;

	fn deref(&self) -> &Self::Target {
		&self.table
	}
}

fn insert_subtable<'a>(
	root_table: &mut Table<'a>,
	key: Key<'a>,
	table: Table<'a>,
	array: bool,
) -> Result<(), Error> {
	let (start, end) = (key.text.span().start, key.text.span().end);
	if array {
		let Some(TomlValue::Array(array)) =
			root_table.get_or_insert_mut(key, TomlValue::Array(Vec::new()))
		else {
			return Err(Error {
				start,
				end,
				kind: ErrorKind::ReusedKey,
			});
		};
		array.push(TomlValue::Table(table));
	} else {
		let old = root_table.insert(key, TomlValue::Table(table));
		if old {
			return Err(Error {
				start,
				end,
				kind: ErrorKind::ReusedKey,
			});
		}
	}

	Ok(())
}

/// An error while parsing TOML, and the range of text that caused
/// that error.
#[derive(Debug)]
pub struct Error {
	/// The first byte (inclusive) of the text that caused a parsing
	/// error.
	pub start: usize,
	/// The last byte (inclusive) of the text that caused a parsing
	/// error.
	pub end: usize,
	/// The type of parsing error; see the [`ErrorKind`] docs.
	pub kind: ErrorKind,
}

/// A type of error while parsing TOML.
#[derive(Debug, PartialEq, Eq)]
pub enum ErrorKind {
	/// A bare key (key without quotes) contains an invalid character.
	InvalidBareKey,
	/// There was a space in the middle of a bare key.
	BareKeyHasSpace,
	/// There was no `=` sign in a key/value assignment.
	NoEqualsInAssignment,
	/// There was no key in a key/value assignment.
	NoKeyInAssignment,
	/// There was no value in a key/value assignment.
	NoValueInAssignment,
	/// A string literal or quoted key didn't have a closing quote.
	UnclosedString,
	/// The value in a key/value assignment wasn't recognised.
	UnrecognisedValue,
	/// The same key was used twice.
	ReusedKey,
	/// A number was too big to fit in an i64. This will also be thrown
	/// for numbers that are "too little", ie, are too negative to fit.
	NumberTooLarge,
	/// A number has an invalid base or a leading zero.
	NumberHasInvalidBaseOrLeadingZero,
	/// A number is malformed/not parseable.
	InvalidNumber,
	/// A basic string has an unknown escape sequence.
	UnknownEscapeSequence,
	/// A unicode escape in a basic string has an unknown unicode scalar value.
	UnknownUnicodeScalar,
	/// A table, inline table, or array didn't have a closing bracket.
	UnclosedBracket,
	/// There was no `,` in between values in an inline table or array.
	NoCommaDelimeter,
}

mod crate_prelude {
	pub use super::{
		table::Table,
		text::{Span, Text},
		toml_string::TomlString,
		types::{Key, TomlValue, TomlValueType},
		Error, ErrorKind,
	};
}

pub mod prelude {
	pub use crate::{
		table::TomlGetError,
		types::{TomlValue, TomlValueType},
		Error as TomlError, ErrorKind as TomlErrorKind, Toml,
	};
}

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

	/// Test that boml can parse booleans and bare keys.
	#[test]
	fn bools_and_bare_keys() {
		let toml_source = concat!(
			"val1 = true\n",
			"val2 = false\n",
			"5678 = true\n",
			"dash-ed = true\n",
			"under_score = true\n"
		);
		let toml = Toml::parse(toml_source).unwrap();
		toml.assert_values(
			vec![
				("val1", true),
				("val2", false),
				("5678", true),
				("dash-ed", true),
				("under_score", true),
			]
			.into_iter()
			.map(|(k, v)| (k, TomlValue::Boolean(v)))
			.collect(),
		);
	}

	/// Test that boml can parse quoted keys.
	#[test]
	fn quoted_keys() {
		let toml_source = concat!(
			"'val0.1.1' = true\n",
			"'ʎǝʞ' = true\n",
			"\"quoted 'key'\" = true\n",
			"'quoted \"key\" 2' = true\n",
		);
		let toml = Toml::parse(toml_source).unwrap();
		toml.assert_values(
			vec![
				("val0.1.1", true),
				("ʎǝʞ", true),
				("quoted 'key'", true),
				("quoted \"key\" 2", true),
			]
			.into_iter()
			.map(|(k, v)| (k, TomlValue::Boolean(v)))
			.collect(),
		);
	}

	/// Test that boml can handle dotted keys.
	#[test]
	fn dotted_keys() {
		let toml_source = concat!(
			"table.bool = true\n",
			"table.string = 'hi'\n",
			"table. spaced = 69\n",
			"table  .infinity = -inf\n",
		);
		let toml = Toml::parse(toml_source).unwrap();

		let table = toml.get_table("table").unwrap();
		assert!(table.get_boolean("bool").unwrap());
		assert_eq!(table.get_string("string").unwrap(), "hi");
		assert_eq!(table.get_integer("spaced").unwrap(), 69);
		assert_eq!(table.get_float("infinity").unwrap(), -f64::INFINITY);
	}

	/// Test that boml can parse literal strings and multiline literal strings.
	#[test]
	fn literal_strings() {
		let single = "Me when I have to write a demo sentence to test my incredible TOML parser but dunno what to say";
		let multi = "Bruhhhh I gotta write\n*another*\ndemo sentence???\n:(";
		let toml_source = format!("single = '{single}'\n") + &format!("multi = '''{multi}'''");
		let toml = Toml::parse(&toml_source).unwrap();
		toml.assert_strings(vec![("single", single), ("multi", multi)]);
	}

	/// Test that boml can parse basic strings and multiline basic strings.
	#[test]
	fn basic_strings() {
		let toml_source = concat!(
			"normal = \"normality 100\"\n",
			r#"quotes = "Bro I got \"quotes\"" "#,
			"\n",
			r#"escapes = "\t\n\r\\" "#,
			"\n",
			"multi = \"\"\"me when\\n",
			"i do multiline\\r pretty neat",
			"\"\"\"\n",
			"whitespace = \"\"\"white\\    \n\n\n\r\n    space\"\"\""
		);
		let toml = Toml::parse(toml_source).unwrap();
		toml.assert_strings(vec![
			("normal", "normality 100"),
			("quotes", "Bro I got \"quotes\""),
			("escapes", "\t\n\r\\"),
			("multi", "me when\ni do multiline\r pretty neat"),
			("whitespace", "whitespace"),
		]);
	}

	/// Test that boml can parse integers.
	#[test]
	fn integers() {
		let toml_source = concat!(
			"hex = 0x10\n",
			"decimal = 10\n",
			"octal = 0o10\n",
			"binary = 0b10\n",
			"neghex = -0x10\n",
			"posoctal = +0o10\n",
			"lmao = -0\n",
			"underscore = 10_00\n"
		);
		let toml = Toml::parse(toml_source).unwrap();
		toml.assert_values(
			vec![
				("hex", 16),
				("decimal", 10),
				("octal", 8),
				("binary", 2),
				("neghex", -16),
				("posoctal", 8),
				("lmao", 0),
				("underscore", 1000),
			]
			.into_iter()
			.map(|(k, v)| (k, TomlValue::Integer(v)))
			.collect(),
		);
	}

	/// Test that boml can parse floats.
	#[test]
	fn floats() {
		let toml_source = concat!(
			"fractional = 0.345\n",
			"exponential = 4e2\n",
			"exponential_neg = 4e-2\n",
			"exponential_pos = 4e+2\n",
			"pos_fractional = +0.567\n",
			"neg_fractional = -0.123\n",
			"capital_exponential = 2E2\n",
			"combined = 7.27e2\n",
			"nan = +nan\n",
			"infinity = -inf\n",
			"underscore = 10_00.0\n",
		);

		let toml = Toml::parse(toml_source).unwrap();
		toml.assert_values(
			vec![
				("fractional", 0.345),
				("exponential", 4e2),
				("exponential_neg", 4e-2),
				("exponential_pos", 4e2),
				("pos_fractional", 0.567),
				("neg_fractional", -0.123),
				("capital_exponential", 2e2),
				("combined", 727.0),
				("infinity", -f64::INFINITY),
				("underscore", 1000.0),
			]
			.into_iter()
			.map(|(key, val)| (key, TomlValue::Float(val)))
			.collect(),
		);

		// NaN != NaN, so we have to check with the `is_nan()` method.
		let nan = toml.get_float("nan");
		assert!(nan.is_ok());
		assert!(nan.unwrap().is_nan())
	}

	/// Test that boml can parse tables.
	#[test]
	fn tables() {
		let toml_source = concat!(
			"inline = { name = 'inline', num = inf }\n",
			"\n",
			"[table1]\n",
			"name = 'table1'\n",
			"\n",
			"[table2]\n",
			"name = 'table2'\n",
			"num = 420\n"
		);
		let toml = Toml::parse(toml_source).unwrap();

		let inline = toml.get_table("inline").unwrap();
		assert_eq!(inline.get_string("name"), Ok("inline"));
		assert_eq!(inline.get_float("num"), Ok(f64::INFINITY));

		let table1 = toml.get_table("table1").unwrap();
		assert_eq!(table1.get_string("name"), Ok("table1"));

		let table2 = toml.get_table("table2").unwrap();
		assert_eq!(table2.get_string("name"), Ok("table2"));
		assert_eq!(table2.get_integer("num"), Ok(420));
	}

	/// Test that boml can parse arrays.
	#[test]
	fn arrays() {
		let toml_source = concat!(
			"strings = ['hi', 'hello', 'how are you']\n",
			"nested = ['me', ['when i', 'nest'], 'arrays']\n",
			"tables = [{name = 'bruh'}, {name = 'bruh 2 electric boogaloo'}]\n"
		);
		let toml = Toml::parse(toml_source).unwrap();

		let strings = toml.get_array("strings").unwrap();
		let strings: Vec<&str> = strings.iter().map(|val| val.string().unwrap()).collect();
		assert_eq!(strings, vec!["hi", "hello", "how are you"]);

		let mut nested = toml.get_array("nested").unwrap().iter();
		assert_eq!(nested.next().unwrap().string().unwrap(), "me");
		let mut subtable = nested.next().unwrap().array().unwrap().iter();
		assert_eq!(subtable.next().unwrap().string().unwrap(), "when i");
		assert_eq!(subtable.next().unwrap().string().unwrap(), "nest");
		assert_eq!(nested.next().unwrap().string().unwrap(), "arrays");

		let mut tables = toml.get_array("tables").unwrap().iter();
		let table1 = tables.next().unwrap().table().unwrap();
		assert_eq!(table1.get_string("name").unwrap(), "bruh");
		let table2 = tables.next().unwrap().table().unwrap();
		assert_eq!(
			table2.get_string("name").unwrap(),
			"bruh 2 electric boogaloo"
		);
	}

	/// Test that boml can parse array tables.
	#[test]
	fn array_tables() {
		let toml_source = concat!(
			"[[entry]]\n",
			"idx = 0\n",
			"value = 'HALLO'\n",
			"\n",
			"[[entry]]\n",
			"idx = 1\n",
			"value = 727\n",
			"\n",
			"[[entry]]\n",
			"idx = 2\n",
			"value = true\n",
		);
		let toml = Toml::parse(toml_source).unwrap();

		let entries = toml.get_array("entry").unwrap();

		let first = entries[0].table().unwrap();
		assert_eq!(first.get_integer("idx").unwrap(), 0);
		assert_eq!(first.get_string("value").unwrap(), "HALLO");

		let second = entries[1].table().unwrap();
		assert_eq!(second.get_integer("idx").unwrap(), 1);
		assert_eq!(second.get_integer("value").unwrap(), 727);

		let third = entries[2].table().unwrap();
		assert_eq!(third.get_integer("idx").unwrap(), 2);
		assert!(third.get_boolean("value").unwrap());
	}

	/// Test that boml works with weird formats - CRLF, weird spacings, etc.
	#[test]
	fn weird_formats() {
		let toml_source = concat!(
			"   val1 = true\r\n",
			"val2=      false",
			"\n\r\n\r\n\n",
			"val3  =true\n",
			"val4=false\n",
			"val5 = true      \n",
			"[parent .  \"child.dotted\"]\n",
			"yippee = true"
		);
		let toml = Toml::new(toml_source).unwrap();
		toml.assert_values(
			vec![
				("val1", true),
				("val2", false),
				("val3", true),
				("val4", false),
				("val5", true),
			]
			.into_iter()
			.map(|(k, v)| (k, TomlValue::Boolean(v)))
			.collect(),
		);

		let parent = toml.get_table("parent").unwrap();
		let child = parent.get_table("child.dotted").unwrap();
		assert!(child.get_boolean("yippee").unwrap());
	}

	impl<'a> Toml<'a> {
		#[inline]
		pub fn assert_value(&self, key: &str, expected_value: TomlValue<'_>) {
			assert_eq!(*self.get(key).unwrap(), expected_value);
		}
		#[inline]
		pub fn assert_values(&self, expected_values: Vec<(&str, TomlValue<'_>)>) {
			for (key, expected_value) in expected_values {
				self.assert_value(key, expected_value);
			}
		}
		pub fn assert_strings(&self, strings: Vec<(&str, &str)>) {
			for (key, expected_string) in strings {
				let value = self.get_string(key);
				assert!(value.is_ok());
				assert_eq!(value.unwrap(), expected_string);
			}
		}
	}
}