xdy 0.9.0

Complex RPG dice expression evaluator with histogram support.
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
//! # Testing and benchmarking support
//!
//! This module provides support for testing and benchmarking.

use std::num::IntErrorKind;

use crate::{Function, Optimizer as _, Passes, StandardOptimizer, compile};

////////////////////////////////////////////////////////////////////////////////
//                            Compilation support.                            //
////////////////////////////////////////////////////////////////////////////////

/// A test case is a pair of a source code string and an expected output
/// string. This type is shared by compilation tests, parse tests, and any
/// other test suite that uses the `{source}\n=\n{expected}` file format.
pub type TestCase = (&'static str, &'static str);

/// A compilation test case.
pub type CompilationTestCase = TestCase;

/// Parse the compilation test cases from a test case file. The file is expected
/// to contain a series of blocks of the form:
///
/// ```text
/// {source}
/// =
/// {expected}
/// ```
///
/// Where `{source}` is the source code to compile and `{expected}` is the
/// expected output of the compiler. Blocks are separated by a blank line.
///
/// # Parameters
/// - `source`: The contents of a test case file.
///
/// # Returns
/// The test cases.
///
/// # Panics
/// If the test case file is incorrectly formatted.
pub fn read_compilation_test_cases(
	source: &'static str
) -> Vec<CompilationTestCase>
{
	let mut test_cases = Vec::new();
	let blocks = source.split("\n\n");
	for block in blocks
	{
		let parts = block.split("\n=\n").collect::<Vec<_>>();
		for part in parts[..].chunks(2)
		{
			let source = part[0].trim();
			let expected = part[1].trim();
			test_cases.push((source, expected));
		}
	}
	test_cases
}

/// Compile the specified valid dice source code into a [function](Function).
///
/// # Parameters
/// - `source`: The source code to compile.
///
/// # Returns
/// The compiled function.
pub fn compile_valid(source: &str) -> Function
{
	match compile(source)
	{
		Ok(function) => function,
		Err(e) => panic!("compilation error: {e}")
	}
}

/// Optimize the specified function using the standard optimizer and the
/// specified passes.
///
/// # Parameters
/// - `function`: The function to optimize.
///
/// # Returns
/// The optimized function.
pub fn optimize(function: Function, passes: Passes) -> Function
{
	StandardOptimizer::new(passes).optimize(function).unwrap()
}

////////////////////////////////////////////////////////////////////////////////
//                            Evaluation support.                             //
////////////////////////////////////////////////////////////////////////////////

/// An evaluation test case is a source code string, a set of arguments, a set
/// of externs, and an expected output string.
pub type EvaluationTestCase = (
	&'static str,
	Vec<i32>,
	Vec<(&'static str, i32)>,
	&'static str
);

/// Parse the evaluation test cases from a test case file. The file is expected
/// to conform to the following grammar:
///
/// ```text
/// file ::= cases? ;
/// cases ::= case ("\n\n" case)* ;
/// case ::= source "\n=\n" args? externs? expected ;
/// source ::= [^\n] "\n" ;
/// args ::= "args:" I32 ("," I32)* "\n" ;
/// externs ::= "externs:" I32 ("," I32)* "\n" ;
/// extern ::= name "="
/// expected ::= min "," max "\n" ;
/// min ::= I32 ;
/// max ::= I32 ;
/// I32 ::= /[0-9]+/ ;
/// WS ::= /[ \t]*/ ;
/// ```
///
/// `WS` is permitted to occur between any two tokens.
///
/// # Parameters
/// - `source`: The contents of a test case file.
///
/// # Returns
/// The test cases.
///
/// # Panics
/// If the test case file is incorrectly formatted.
pub fn read_evaluation_test_cases(
	source: &'static str
) -> Vec<EvaluationTestCase>
{
	let mut test_cases = Vec::new();
	let blocks = source.split("\n\n");
	for block in blocks
	{
		let parts: Vec<&str> = block.splitn(2, "\n=\n").collect();
		let source = parts[0].trim();
		let cases: Vec<&str> = parts[1].trim().split("\n=\n").collect();
		for case in cases
		{
			let mut lines: Vec<&str> = case.lines().collect();
			let expected = lines.pop().expect("missing expected result");
			let expected = expected.trim();
			let mut args = Vec::new();
			let mut externs = Vec::new();
			for line in lines
			{
				let line = line.trim();
				if let Some(line) = line.strip_prefix("args:")
				{
					args = line
						.split(',')
						.map(|s| match s.trim().parse::<i32>()
						{
							Ok(i) => i,
							Err(e)
								if e.kind() == &IntErrorKind::PosOverflow =>
							{
								i32::MAX
							},
							Err(e)
								if e.kind() == &IntErrorKind::NegOverflow =>
							{
								i32::MIN
							},
							_ => unreachable!()
						})
						.collect();
				}
				else if let Some(line) = line.strip_prefix("externs:")
				{
					externs = line
						.split(',')
						.map(|s| {
							let parts: Vec<&str> =
								s.trim().splitn(2, '=').collect();
							(parts[0].trim(), parts[1].trim().parse().unwrap())
						})
						.collect();
				}
			}
			test_cases.push((source, args, externs, expected));
		}
	}
	test_cases
}

////////////////////////////////////////////////////////////////////////////////
//                             Histogram support.                             //
////////////////////////////////////////////////////////////////////////////////

/// A histogram test case is a source code string, a set of arguments, a set of
/// externs, and a histogram of expected outcomes as a vector of pairs of
/// outcome and count.
pub type HistogramTestCase = (
	&'static str,
	Vec<i32>,
	Vec<(&'static str, i32)>,
	Vec<(i32, usize)>
);

/// Parse the histogram test cases from a test case file. The file is expected
/// to conform to the following grammar:
///
/// ```text
/// file ::= cases? ;
/// cases ::= case ("\n\n" case)* ;
/// case ::= source "\n=\n" args? externs? expected ;
/// source ::= [^\n] "\n" ;
/// args ::= "args:" I32 ("," I32)* "\n" ;
/// externs ::= "externs:" I32 ("," I32)* "\n" ;
/// extern ::= name "="
/// expected ::= outcome_pair ("\n" outcome_pair)* "\n";
/// outcome_pair ::= outcome ":" count ;
/// outcome ::= I32 ;
/// count ::= USIZE ;
/// min ::= I32 ;
/// max ::= I32 ;
/// I32 ::= /[0-9]+/ ;
/// WS ::= /[ \t]*/ ;
/// ```
///
/// `WS` is permitted to occur between any two tokens.
///
/// # Parameters
/// - `source`: The contents of a test case file.
///
/// # Returns
/// The test cases.
///
/// # Panics
/// If the test case file is incorrectly formatted.
pub fn read_histogram_test_cases(source: &'static str)
-> Vec<HistogramTestCase>
{
	let mut test_cases = Vec::new();
	let blocks = source.split("\n\n");
	for block in blocks
	{
		let parts: Vec<&str> = block.splitn(2, "\n=\n").collect();
		let source = parts[0].trim();
		let cases: Vec<&str> = parts[1].trim().split("\n=\n").collect();
		for case in cases
		{
			let mut expected = Vec::new();
			let mut args = Vec::new();
			let mut externs = Vec::new();
			for line in case.lines()
			{
				let line = line.trim();
				if let Some(line) = line.strip_prefix("args:")
				{
					args = line
						.split(',')
						.map(|s| match s.trim().parse::<i32>()
						{
							Ok(i) => i,
							Err(e)
								if e.kind() == &IntErrorKind::PosOverflow =>
							{
								i32::MAX
							},
							Err(e)
								if e.kind() == &IntErrorKind::NegOverflow =>
							{
								i32::MIN
							},
							_ => unreachable!()
						})
						.collect();
				}
				else if let Some(line) = line.strip_prefix("externs:")
				{
					externs = line
						.split(',')
						.map(|s| {
							let parts: Vec<&str> =
								s.trim().splitn(2, '=').collect();
							(parts[0].trim(), parts[1].trim().parse().unwrap())
						})
						.collect();
				}
				else
				{
					let parts: Vec<&str> = line.splitn(2, ':').collect();
					let outcome = parts[0].trim().parse::<i32>().unwrap();
					let count = parts[1].trim().parse::<usize>().unwrap();
					expected.push((outcome, count));
				}
			}
			test_cases.push((source, args, externs, expected));
		}
	}
	test_cases
}

////////////////////////////////////////////////////////////////////////////////
//                         Error diagnostic support.                          //
////////////////////////////////////////////////////////////////////////////////

#[cfg(test)]
/// An expected placeholder in an error test case.
#[derive(Debug)]
pub struct ExpectedPlaceholder
{
	/// The byte range in the corrected source.
	pub span: (usize, usize),

	/// The description of the placeholder.
	pub description: &'static str,

	/// The valid kinds for this placeholder.
	pub valid_kinds: Vec<&'static str>
}

#[cfg(test)]
/// An expected suggestion in an error test case.
#[derive(Debug)]
pub struct ExpectedSuggestion
{
	/// The corrected source string.
	pub corrected_source: &'static str,

	/// The expected placeholders.
	pub placeholders: Vec<ExpectedPlaceholder>
}

#[cfg(test)]
/// An expected diagnostic in an error test case.
#[derive(Debug)]
pub struct ExpectedDiagnostic
{
	/// The diagnostic kind name (e.g., "MissingRightOperand").
	pub kind: &'static str,

	/// The byte range in the original source.
	pub span: (usize, usize),

	/// The expected message.
	pub message: &'static str,

	/// The expected suggestions.
	pub suggestions: Vec<ExpectedSuggestion>
}

#[cfg(test)]
/// An error test case.
#[derive(Debug)]
pub struct ErrorTestCase
{
	/// The broken source input.
	pub source: &'static str,

	/// The expected diagnostics.
	pub expected_diagnostics: Vec<ExpectedDiagnostic>
}

#[cfg(test)]
/// Parse error test cases from a test case file. The file is expected to
/// conform to the following grammar:
///
/// ```text
/// file ::= cases? ;
/// cases ::= case ("\n\n" case)* ;
/// case ::= source "\n=\n" diagnostics ;
/// source ::= [^\n]* ;
/// diagnostics ::= diagnostic ("---\n" diagnostic)* ;
/// diagnostic ::= kind span message suggestion? ;
/// kind ::= "kind:" IDENTIFIER "\n" ;
/// span ::= "span:" USIZE ".." USIZE "\n" ;
/// message ::= "message:" [^\n]* "\n" ;
/// suggestion ::= "suggestion:" [^\n]* "\n" placeholder* ;
/// placeholder ::= "placeholder:" USIZE ".." USIZE
///     '"' [^"]* '"' "[" kinds "]" "\n" ;
/// kinds ::= IDENTIFIER ("," IDENTIFIER)* ;
/// ```
///
/// # Parameters
/// - `source`: The contents of a test case file.
///
/// # Returns
/// The test cases.
///
/// # Panics
/// If the test case file is incorrectly formatted.
pub fn read_error_test_cases(source: &'static str) -> Vec<ErrorTestCase>
{
	let mut test_cases = Vec::new();
	for block in source.split("\n\n")
	{
		let block = block.trim();
		if block.is_empty()
		{
			continue;
		}
		let parts: Vec<&str> = block.splitn(2, "\n=\n").collect();
		assert!(parts.len() == 2, "malformed test case block: {:?}", block);
		let test_source = match parts[0]
		{
			"<empty>" => "",
			other => other
		};
		let diagnostics_text = parts[1];

		let mut expected_diagnostics = Vec::new();
		for diag_block in diagnostics_text.split("\n---\n")
		{
			expected_diagnostics
				.push(parse_expected_diagnostic(diag_block.trim()));
		}

		test_cases.push(ErrorTestCase {
			source: test_source,
			expected_diagnostics
		});
	}
	test_cases
}

#[cfg(test)]
/// Parse a single expected diagnostic from its text representation.
///
/// # Parameters
/// - `text`: The text of one diagnostic section.
///
/// # Returns
/// The parsed expected diagnostic.
///
/// # Panics
/// If the text is malformed.
fn parse_expected_diagnostic(text: &'static str) -> ExpectedDiagnostic
{
	let mut kind = None;
	let mut span = None;
	let mut message = None;
	let mut suggestions: Vec<ExpectedSuggestion> = Vec::new();
	let mut current_suggestion: Option<&'static str> = None;
	let mut current_placeholders: Vec<ExpectedPlaceholder> = Vec::new();

	for line in text.lines()
	{
		let line = line.trim();
		if let Some(rest) = line.strip_prefix("kind:")
		{
			kind = Some(rest.trim());
		}
		else if let Some(rest) = line.strip_prefix("span:")
		{
			let parts: Vec<&str> = rest.trim().split("..").collect();
			span = Some((
				parts[0].parse::<usize>().unwrap(),
				parts[1].parse::<usize>().unwrap()
			));
		}
		else if let Some(rest) = line.strip_prefix("message:")
		{
			message = Some(rest.trim());
		}
		else if let Some(rest) = line.strip_prefix("suggestion:")
		{
			// Flush the previous suggestion, if any.
			if let Some(src) = current_suggestion
			{
				suggestions.push(ExpectedSuggestion {
					corrected_source: src,
					placeholders: std::mem::take(&mut current_placeholders)
				});
			}
			current_suggestion = Some(rest.trim());
		}
		else if let Some(rest) = line.strip_prefix("placeholder:")
		{
			current_placeholders.push(parse_expected_placeholder(rest.trim()));
		}
	}
	// Flush the last suggestion.
	if let Some(src) = current_suggestion
	{
		suggestions.push(ExpectedSuggestion {
			corrected_source: src,
			placeholders: std::mem::take(&mut current_placeholders)
		});
	}

	ExpectedDiagnostic {
		kind: kind.expect("missing kind"),
		span: span.expect("missing span"),
		message: message.expect("missing message"),
		suggestions
	}
}

#[cfg(test)]
/// Parse a placeholder specification from its text representation.
///
/// Expected format: `start..end "description" [kind1, kind2, ...]`
///
/// # Parameters
/// - `text`: The placeholder text.
///
/// # Returns
/// The parsed expected placeholder.
///
/// # Panics
/// If the text is malformed.
fn parse_expected_placeholder(text: &'static str) -> ExpectedPlaceholder
{
	// Parse span: "start..end"
	let span_end = text.find(' ').unwrap();
	let span_parts: Vec<&str> = text[..span_end].split("..").collect();
	let span = (
		span_parts[0].parse::<usize>().unwrap(),
		span_parts[1].parse::<usize>().unwrap()
	);

	// Parse description: "..."
	let rest = text[span_end..].trim();
	let desc_start = rest.find('"').unwrap() + 1;
	let desc_end = rest[desc_start..].find('"').unwrap() + desc_start;
	let description = &rest[desc_start..desc_end];

	// Parse valid kinds: [kind1, kind2, ...]
	let kinds_start = rest.find('[').unwrap() + 1;
	let kinds_end = rest.find(']').unwrap();
	let valid_kinds: Vec<&str> = rest[kinds_start..kinds_end]
		.split(',')
		.map(|s| s.trim())
		.collect();

	ExpectedPlaceholder {
		span,
		description,
		valid_kinds
	}
}