xdy 0.13.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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
//! # AST trait tests
//!
//! Herein are tests for the standard trait implementations of the recursive
//! abstract syntax tree (AST) types — [`Debug`], [`Display`](fmt::Display),
//! [`Clone`], [`PartialEq`], [`Hash`], and [`Drop`] — and for
//! [`untethered`](Spanned::untethered) and [`SExpressible`], all of which must
//! traverse with explicit stacks rather than recursion. The golden files
//! `tests/test_ast_debug.txt` and `tests/test_ast_debug_pretty.txt` were
//! captured from the derived implementations that these replaced, so they pin
//! the [`Debug`] output to the derived format.

use std::{
	collections::hash_map::DefaultHasher,
	fmt::{self, Write},
	hash::{Hash, Hasher}
};

use pretty_assertions::assert_eq;

use crate::{
	Parser, SourceSpan, Spanned,
	ast::*,
	s_expr::{SExpressible, SExpressibleOptions},
	support::{on_small_stack, read_compilation_test_cases}
};

////////////////////////////////////////////////////////////////////////////////
//                               Debug parity.                                //
////////////////////////////////////////////////////////////////////////////////

/// Ensure that `{:?}` matches the derived format for every case in
/// `test_ast_debug.txt`.
#[test]
fn test_debug_matches_derived()
{
	let test_cases = read_compilation_test_cases(include_str!(
		"../../tests/test_ast_debug.txt"
	));
	assert!(!test_cases.is_empty());
	for (source, expected) in test_cases
	{
		let function = Parser::parse(source).unwrap();
		assert_eq!(format!("{:?}", function), expected, "{}", source);
	}
}

/// Ensure that `{:#?}` matches the derived format for every case in
/// `test_ast_debug_pretty.txt`.
#[test]
fn test_debug_pretty_matches_derived()
{
	let test_cases = read_compilation_test_cases(include_str!(
		"../../tests/test_ast_debug_pretty.txt"
	));
	assert!(!test_cases.is_empty());
	for (source, expected) in test_cases
	{
		let function = Parser::parse(source).unwrap();
		assert_eq!(format!("{:#?}", function), expected, "{}", source);
	}
}

/// Ensure that `{:?}` passes the formatter's flags through to the leaves, as
/// the derived format does. The expectation was captured from the derived
/// implementation.
#[test]
fn test_debug_passes_flags_to_leaves()
{
	let function = Parser::parse("{x}: {x}D[-1, 10] drop highest 12").unwrap();
	assert_eq!(
		format!("{:x?}", function.body),
		"Dice(DropHighest(DropHighest { dice: Custom(CustomDice { count: \
		 Variable(Variable { name: \"x\", span: SourceSpan { start: 5, end: \
		 8 } }), faces: [ffffffff, a], span: SourceSpan { start: 5, end: 11 \
		 } }), drop: Some(Constant(Constant { value: c, span: SourceSpan { \
		 start: 1f, end: 21 } })), span: SourceSpan { start: 5, end: 21 } }))"
	);
}

/// Ensure that `{:#?}` renders a deep negation chain exactly as the derived
/// format would, as reconstructed independently by [`pretty_negations`].
#[test]
fn test_debug_pretty_deep()
{
	on_small_stack(|| {
		let depth = 500;
		let expression = nest(Nesting::Negation, depth, 1);
		let actual = format!("{:#?}", expression);
		assert!(actual == pretty_negations(depth, 1), "mismatch");
	});
}

////////////////////////////////////////////////////////////////////////////////
//                                 Semantics.                                 //
////////////////////////////////////////////////////////////////////////////////

/// Ensure that a clone equals its original, hashes identically, and renders
/// identically.
#[test]
fn test_clone_equals_original()
{
	let test_cases = read_compilation_test_cases(include_str!(
		"../../tests/test_ast_debug.txt"
	));
	for (source, _) in test_cases
	{
		let function = Parser::parse(source).unwrap();
		let body = function.body.clone();
		assert!(body == function.body, "{}", source);
		assert_eq!(hash_of(&body), hash_of(&function.body), "{}", source);
		assert_eq!(
			format!("{:#?}", body),
			format!("{:#?}", function.body),
			"{}",
			source
		);
	}
}

/// Ensure that equality notices every kind of difference: variants, leaf
/// values, spans, names, faces, and the presence of a drop expression.
#[test]
fn test_equality_distinguishes()
{
	let pairs = [
		("1 + 2", "1 + 3"),
		("1 + 2", "1 - 2"),
		("1 + 2", "1  + 2"),
		("(1)", "{a}@(1)"),
		("{a}@(1)", "{b}@(1)"),
		("[1:2]", "[1:3]"),
		("3D[1, 2]", "3D[1, 3]"),
		("4D6 drop lowest", "4D6 drop lowest 1"),
		("4D6 drop lowest", "4D6 drop highest"),
		(
			"4D6 drop lowest drop lowest",
			"4D6 drop lowest drop highest"
		),
		("{x}: {x}", "{y}: {y}"),
		("-(1)", "-(2)")
	];
	for (left, right) in pairs
	{
		let left_body = Parser::parse(left).unwrap().body;
		let right_body = Parser::parse(right).unwrap().body;
		assert!(left_body != right_body, "{} == {}", left, right);
		assert!(left_body == left_body.clone(), "{}", left);
	}
}

/// Ensure that separately parsed but identical sources hash identically.
#[test]
fn test_hash_agrees_with_equality()
{
	let source =
		"{x}: 8D8 drop lowest (2) drop highest {a}@({x}) + [1:6] ^ -(3)";
	let first = Parser::parse(source).unwrap();
	let second = Parser::parse(source).unwrap();
	assert!(first == second);
	assert_eq!(hash_of(&first), hash_of(&second));
	assert_eq!(hash_of(&first.body), hash_of(&second.body));
}

////////////////////////////////////////////////////////////////////////////////
//                               Deep nesting.                                //
////////////////////////////////////////////////////////////////////////////////

/// The depth of the deep-nesting tests. Some of them need gigabytes of memory
/// at this depth, so every test that builds a source this deep is ignored by
/// default; `just stress` runs them.
pub(super) const DEPTH: usize = 1_000_000;

/// The value of the innermost constant of a deep source. Its rendering occurs
/// nowhere else in the source, so that a test can find it and replace it.
pub(super) const LEAF: i32 = 7_777_777;

/// Ensure that a deep chain of groups survives every trait on a small stack.
#[test]
#[ignore = "stress: run with just stress"]
fn test_deep_group() { exercise_on_small_stack(Nesting::Group) }

/// Ensure that a deep chain of bindings survives every trait on a small
/// stack.
#[test]
#[ignore = "stress: run with just stress"]
fn test_deep_binding() { exercise_on_small_stack(Nesting::Binding) }

/// Ensure that a deep chain of range starts survives every trait on a small
/// stack.
#[test]
#[ignore = "stress: run with just stress"]
fn test_deep_range_start() { exercise_on_small_stack(Nesting::RangeStart) }

/// Ensure that a deep chain of range ends survives every trait on a small
/// stack.
#[test]
#[ignore = "stress: run with just stress"]
fn test_deep_range_end() { exercise_on_small_stack(Nesting::RangeEnd) }

/// Ensure that a deep chain of negations survives every trait on a small
/// stack.
#[test]
#[ignore = "stress: run with just stress"]
fn test_deep_negation() { exercise_on_small_stack(Nesting::Negation) }

/// Ensure that a deep right-nested chain of exponents survives every trait on
/// a small stack.
#[test]
#[ignore = "stress: run with just stress"]
fn test_deep_exponent() { exercise_on_small_stack(Nesting::Exponent) }

/// Ensure that a deep left-nested chain of additions survives every trait on a
/// small stack.
#[test]
#[ignore = "stress: run with just stress"]
fn test_deep_addition() { exercise_on_small_stack(Nesting::Addition) }

/// Ensure that a deep left-nested chain of subtractions survives every trait
/// on a small stack.
#[test]
#[ignore = "stress: run with just stress"]
fn test_deep_subtraction() { exercise_on_small_stack(Nesting::Subtraction) }

/// Ensure that a deep left-nested chain of multiplications survives every
/// trait on a small stack.
#[test]
#[ignore = "stress: run with just stress"]
fn test_deep_multiplication()
{
	exercise_on_small_stack(Nesting::Multiplication)
}

/// Ensure that a deep left-nested chain of divisions survives every trait on a
/// small stack.
#[test]
#[ignore = "stress: run with just stress"]
fn test_deep_division() { exercise_on_small_stack(Nesting::Division) }

/// Ensure that a deep left-nested chain of modulos survives every trait on a
/// small stack.
#[test]
#[ignore = "stress: run with just stress"]
fn test_deep_modulo() { exercise_on_small_stack(Nesting::Modulo) }

/// Ensure that a deep chain of dice counts survives every trait on a small
/// stack.
#[test]
#[ignore = "stress: run with just stress"]
fn test_deep_dice_count() { exercise_on_small_stack(Nesting::DiceCount) }

/// Ensure that a deep chain of standard dice faces survives every trait on a
/// small stack.
#[test]
#[ignore = "stress: run with just stress"]
fn test_deep_dice_faces() { exercise_on_small_stack(Nesting::DiceFaces) }

/// Ensure that a deep chain of custom dice counts survives every trait on a
/// small stack.
#[test]
#[ignore = "stress: run with just stress"]
fn test_deep_custom_count() { exercise_on_small_stack(Nesting::CustomCount) }

/// Ensure that a deep chain of drop expressions survives every trait on a
/// small stack.
#[test]
#[ignore = "stress: run with just stress"]
fn test_deep_drop_expression()
{
	exercise_on_small_stack(Nesting::DropExpression)
}

/// Ensure that a deep chain of stacked drop clauses survives every trait on a
/// small stack.
#[test]
#[ignore = "stress: run with just stress"]
fn test_deep_drop_clauses() { exercise_on_small_stack(Nesting::DropClauses) }

/// Ensure that a deep interleaving of every nesting construct survives every
/// trait on a small stack.
#[test]
#[ignore = "stress: run with just stress"]
fn test_deep_mixed() { exercise_on_small_stack(Nesting::Mixed) }

////////////////////////////////////////////////////////////////////////////////
//                                   Miri.                                    //
////////////////////////////////////////////////////////////////////////////////

// These tests are small enough to run under Miri, which checks the `unsafe`
// code in `Drop` for undefined behavior and leaks:
//
// ```sh
// cargo +nightly miri test -p xdy --lib tests::ast::test_miri
// MIRIFLAGS=-Zmiri-tree-borrows \
//     cargo +nightly miri test -p xdy --lib tests::ast::test_miri
// ```

/// Ensure that every trait works on a shallow interleaving of every nesting
/// construct.
#[test]
fn test_miri_mixed() { exercise(|leaf| nest(Nesting::Mixed, 60, leaf)) }

/// Ensure that every trait works on a chain of stacked drop clauses.
#[test]
fn test_miri_drop_clauses()
{
	exercise(|leaf| nest(Nesting::DropClauses, 40, leaf))
}

/// Ensure that a standalone chain of stacked drop clauses drops, both as a
/// [`DiceExpression`] and as a bare [`DropLowest`].
#[test]
fn test_miri_standalone_drop_clauses()
{
	let chain = drop_clauses(standard_dice(constant(1)), 40);
	drop(chain.clone());
	match &chain
	{
		DiceExpression::DropLowest(clause) => drop(clause.clone()),
		DiceExpression::DropHighest(clause) => drop(clause.clone()),
		_ => unreachable!()
	}
	drop(chain);
}

/// Ensure that a drop clause directly atop a leaf dice expression drops.
#[test]
fn test_miri_single_drop_clause()
{
	let expression = Parser::parse("{x}: {x}D[1, 2] drop lowest (2D6)")
		.unwrap()
		.body;
	drop(expression.clone());
	drop(expression);
}

////////////////////////////////////////////////////////////////////////////////
//                                  Helpers.                                  //
////////////////////////////////////////////////////////////////////////////////

/// A way to nest one expression inside another.
#[derive(Copy, Clone, Debug)]
pub(super) enum Nesting
{
	/// `(e)`.
	Group,

	/// `{a}@(e)`.
	Binding,

	/// `[e:1]`.
	RangeStart,

	/// `[1:e]`.
	RangeEnd,

	/// `-e`.
	Negation,

	/// `2 ^ e`.
	Exponent,

	/// `e + 1`.
	Addition,

	/// `e - 1`.
	Subtraction,

	/// `e * 1`.
	Multiplication,

	/// `e / 1`.
	Division,

	/// `e % 1`.
	Modulo,

	/// `(e)D6`.
	DiceCount,

	/// `1D(e)`.
	DiceFaces,

	/// `(e)D[1, 2]`.
	CustomCount,

	/// `1D6 drop lowest (e)`.
	DropExpression,

	/// `1D6 drop lowest (e) drop highest`: a stack of drop clauses. Nested
	/// alone, this produces a single chain of drop clauses whose leaf count is
	/// `e`.
	DropClauses,

	/// Every other construct, in rotation.
	Mixed
}

impl Nesting
{
	/// The constructs through which [`Mixed`](Self::Mixed) rotates.
	pub(super) const ROTATION: [Nesting; 16] = [
		Nesting::Group,
		Nesting::Binding,
		Nesting::RangeStart,
		Nesting::RangeEnd,
		Nesting::Negation,
		Nesting::Exponent,
		Nesting::Addition,
		Nesting::Subtraction,
		Nesting::Multiplication,
		Nesting::Division,
		Nesting::Modulo,
		Nesting::DiceCount,
		Nesting::DiceFaces,
		Nesting::CustomCount,
		Nesting::DropExpression,
		Nesting::DropClauses
	];

	/// Answer whether nesting this way introduces [bindings](Binding). Every
	/// such binding is named `a`, so a deep nesting binds `a` inside itself.
	///
	/// # Returns
	/// `true` for [`Binding`](Self::Binding) and [`Mixed`](Self::Mixed).
	pub(super) const fn binds(self) -> bool
	{
		matches!(self, Nesting::Binding | Nesting::Mixed)
	}
}

/// Build an expression by nesting a constant `depth` times.
///
/// # Parameters
/// - `nesting`: The way to nest.
/// - `depth`: The number of levels of nesting.
/// - `leaf`: The value of the innermost constant.
///
/// # Returns
/// The expression.
pub(super) fn nest(
	nesting: Nesting,
	depth: usize,
	leaf: i32
) -> Expression<'static>
{
	if let Nesting::DropClauses = nesting
	{
		return Expression::Dice(drop_clauses(
			standard_dice(constant(leaf)),
			depth
		))
	}
	let mut expression = constant(leaf);
	for level in 0..depth
	{
		let nesting = match nesting
		{
			Nesting::Mixed =>
			{
				Nesting::ROTATION[level % Nesting::ROTATION.len()]
			},
			nesting => nesting
		};
		expression = wrap(nesting, expression);
	}
	expression
}

/// Build a function without parameters whose body adds a reference to the
/// external variable `x` to a constant nested `depth` times. A left-to-right
/// walk must therefore traverse the whole nesting before it reaches the
/// reference.
///
/// # Parameters
/// - `nesting`: The way to nest.
/// - `depth`: The number of levels of nesting.
///
/// # Returns
/// The function.
pub(super) fn nest_function(nesting: Nesting, depth: usize)
-> Function<'static>
{
	let span = SourceSpan::SYNTHETIC;
	Function {
		parameters: None,
		body: Expression::Arithmetic(ArithmeticExpression::Add(Add {
			left: Box::new(nest(nesting, depth, 1)),
			right: Box::new(Expression::Variable(Variable {
				name: "x".into(),
				span
			})),
			span
		})),
		span
	}
}

/// Build an expression by nesting a constant `depth` times, as [`nest`] does,
/// but in a form that the parser can produce, so that its
/// [rendering](fmt::Display) parses back to the same expression. [`nest`]
/// places dice and arithmetic directly where the grammar admits only a
/// constant, a variable, a binding, or a group, e.g., as a dice count, and
/// negates a positive constant, which renders as a negative constant. This
/// function wraps the inner expression in a [`Group`] wherever the grammar
/// requires one.
///
/// # Parameters
/// - `nesting`: The way to nest.
/// - `depth`: The number of levels of nesting.
/// - `leaf`: The value of the innermost constant.
///
/// # Returns
/// The expression.
pub(super) fn nest_parsable(
	nesting: Nesting,
	depth: usize,
	leaf: i32
) -> Expression<'static>
{
	if let Nesting::DropClauses = nesting
	{
		return nest(nesting, depth, leaf)
	}
	let mut expression = constant(leaf);
	for level in 0..depth
	{
		let nesting = match nesting
		{
			Nesting::Mixed =>
			{
				Nesting::ROTATION[level % Nesting::ROTATION.len()]
			},
			nesting => nesting
		};
		let atomic = matches!(
			expression,
			Expression::Constant(_)
				| Expression::Variable(_)
				| Expression::Binding(_)
				| Expression::Group(_)
		);
		let needs_group = match nesting
		{
			Nesting::DiceCount
			| Nesting::DiceFaces
			| Nesting::CustomCount
			| Nesting::DropExpression
			| Nesting::DropClauses => !atomic,
			Nesting::Negation => matches!(expression, Expression::Constant(_)),
			Nesting::Multiplication | Nesting::Division | Nesting::Modulo =>
			{
				matches!(
					expression,
					Expression::Arithmetic(
						ArithmeticExpression::Add(_)
							| ArithmeticExpression::Sub(_)
					)
				)
			},
			Nesting::Exponent => matches!(
				expression,
				Expression::Arithmetic(
					ArithmeticExpression::Add(_)
						| ArithmeticExpression::Sub(_)
						| ArithmeticExpression::Mul(_)
						| ArithmeticExpression::Div(_)
						| ArithmeticExpression::Mod(_)
				)
			),
			_ => false
		};
		if needs_group
		{
			expression = Expression::Group(Group {
				expression: Box::new(expression),
				span: SourceSpan::SYNTHETIC
			});
		}
		expression = wrap(nesting, expression);
	}
	expression
}

/// Wrap an expression in one level of nesting.
///
/// # Parameters
/// - `nesting`: The way to nest.
/// - `inner`: The expression to wrap.
///
/// # Returns
/// The wrapped expression.
fn wrap(nesting: Nesting, inner: Expression<'static>) -> Expression<'static>
{
	let span = SourceSpan::SYNTHETIC;
	let inner = Box::new(inner);
	match nesting
	{
		Nesting::Group => Expression::Group(Group {
			expression: inner,
			span
		}),
		Nesting::Binding => Expression::Binding(Binding {
			name: "a".into(),
			name_span: span,
			expression: inner,
			span
		}),
		Nesting::RangeStart => Expression::Range(Range {
			start: inner,
			end: Box::new(constant(1)),
			span
		}),
		Nesting::RangeEnd => Expression::Range(Range {
			start: Box::new(constant(1)),
			end: inner,
			span
		}),
		Nesting::Negation =>
		{
			Expression::Arithmetic(ArithmeticExpression::Neg(Neg {
				operand: inner,
				span
			}))
		},
		Nesting::Exponent =>
		{
			Expression::Arithmetic(ArithmeticExpression::Exp(Exp {
				left: Box::new(constant(2)),
				right: inner,
				span
			}))
		},
		Nesting::Addition =>
		{
			Expression::Arithmetic(ArithmeticExpression::Add(Add {
				left: inner,
				right: Box::new(constant(1)),
				span
			}))
		},
		Nesting::Subtraction =>
		{
			Expression::Arithmetic(ArithmeticExpression::Sub(Sub {
				left: inner,
				right: Box::new(constant(1)),
				span
			}))
		},
		Nesting::Multiplication =>
		{
			Expression::Arithmetic(ArithmeticExpression::Mul(Mul {
				left: inner,
				right: Box::new(constant(1)),
				span
			}))
		},
		Nesting::Division =>
		{
			Expression::Arithmetic(ArithmeticExpression::Div(Div {
				left: inner,
				right: Box::new(constant(1)),
				span
			}))
		},
		Nesting::Modulo =>
		{
			Expression::Arithmetic(ArithmeticExpression::Mod(Mod {
				left: inner,
				right: Box::new(constant(1)),
				span
			}))
		},
		Nesting::DiceCount =>
		{
			Expression::Dice(DiceExpression::Standard(StandardDice {
				count: inner,
				faces: Box::new(constant(6)),
				span
			}))
		},
		Nesting::DiceFaces =>
		{
			Expression::Dice(DiceExpression::Standard(StandardDice {
				count: Box::new(constant(1)),
				faces: inner,
				span
			}))
		},
		Nesting::CustomCount =>
		{
			Expression::Dice(DiceExpression::Custom(CustomDice {
				count: inner,
				faces: vec![1, 2],
				span
			}))
		},
		Nesting::DropExpression =>
		{
			Expression::Dice(DiceExpression::DropLowest(DropLowest {
				dice: Box::new(standard_dice(constant(1))),
				drop: Some(inner),
				span
			}))
		},
		Nesting::DropClauses =>
		{
			Expression::Dice(DiceExpression::DropHighest(DropHighest {
				dice: Box::new(DiceExpression::DropLowest(DropLowest {
					dice: Box::new(standard_dice(constant(1))),
					drop: Some(inner),
					span
				})),
				drop: None,
				span
			}))
		},
		Nesting::Mixed => unreachable!()
	}
}

/// Stack `depth` drop clauses atop a dice expression, alternating between
/// drop-lowest and drop-highest, with a drop expression on every third clause.
///
/// # Parameters
/// - `dice`: The dice expression at the bottom of the stack.
/// - `depth`: The number of drop clauses.
///
/// # Returns
/// The stack of drop clauses.
fn drop_clauses(
	dice: DiceExpression<'static>,
	depth: usize
) -> DiceExpression<'static>
{
	let span = SourceSpan::SYNTHETIC;
	let mut dice = dice;
	for level in 0..depth
	{
		let drop = (level % 3 == 0).then(|| Box::new(constant(1)));
		let inner = Box::new(dice);
		dice = if level % 2 == 0
		{
			DiceExpression::DropLowest(DropLowest {
				dice: inner,
				drop,
				span
			})
		}
		else
		{
			DiceExpression::DropHighest(DropHighest {
				dice: inner,
				drop,
				span
			})
		};
	}
	dice
}

/// Answer a constant with a synthetic span.
///
/// # Parameters
/// - `value`: The value of the constant.
///
/// # Returns
/// The constant.
fn constant(value: i32) -> Expression<'static>
{
	Expression::Constant(Constant {
		value,
		span: SourceSpan::SYNTHETIC
	})
}

/// Answer a standard dice expression with six faces.
///
/// # Parameters
/// - `count`: The number of dice.
///
/// # Returns
/// The dice expression.
fn standard_dice(count: Expression<'static>) -> DiceExpression<'static>
{
	DiceExpression::Standard(StandardDice {
		count: Box::new(count),
		faces: Box::new(constant(6)),
		span: SourceSpan::SYNTHETIC
	})
}

/// Build a pair of `DEPTH`-deep expressions on a small stack, and
/// [exercise](exercise) every trait on them.
///
/// # Parameters
/// - `nesting`: The way to nest.
fn exercise_on_small_stack(nesting: Nesting)
{
	on_small_stack(|| exercise(|leaf| nest(nesting, DEPTH, leaf)));
}

/// Exercise every trait on an expression: equality against a copy that
/// differs only in its innermost leaf, [`Clone`], equality and hashing against
/// the clone, compact [`Debug`] and [`Display`](fmt::Display) rendering,
/// [`untethered`](Spanned::untethered), S-expression sizing and writing, and
/// [`Drop`]. Uses `assert!` rather than `assert_eq!` on the expressions
/// themselves, since a failure would otherwise try to render them in full.
///
/// The S-expressions are written with an unbounded soft limit, so that they
/// fit on one line, and their length must then equal their size. Wrapped
/// output would be quadratic in the depth, since each level indents by a tab,
/// so [`test_read_deep_wrapped`](super::parser::s_expr_deep) checks wrapping at
/// a more modest depth instead.
///
/// # Parameters
/// - `build`: Builds the expression, given the value of its innermost leaf.
fn exercise(build: impl Fn(i32) -> Expression<'static>)
{
	let original = build(1);
	let different = build(2);
	assert!(original != different, "differing leaves compared equal");
	drop(different);
	let copy = original.clone();
	assert!(original == copy, "clone compared unequal");
	assert_eq!(hash_of(&original), hash_of(&copy));
	let length = debug_length(&original);
	assert!(length > 0);
	assert_eq!(length, debug_length(&copy));
	let length = display_length(&original);
	assert!(length > 0);
	assert_eq!(length, display_length(&copy));
	// Every span of the tree is synthetic, i.e., the default, so untethering
	// must reproduce it exactly.
	assert!(
		original.untethered() == original,
		"untethering changed spans"
	);
	for with_groups in [false, true]
	{
		let options =
			SExpressibleOptions::new(0, 4, usize::MAX).with_groups(with_groups);
		let size = original.size_s_expr(options);
		assert!(size > 0);
		assert_eq!(s_expr_length(&original, options), size);
	}
}

/// Hash a value with the default hasher.
///
/// # Parameters
/// - `value`: The value to hash.
///
/// # Returns
/// The hash.
fn hash_of(value: &impl Hash) -> u64
{
	let mut hasher = DefaultHasher::new();
	value.hash(&mut hasher);
	hasher.finish()
}

/// A sink that counts the bytes written to it.
struct Counter(usize);

impl Write for Counter
{
	fn write_str(&mut self, s: &str) -> fmt::Result
	{
		self.0 += s.len();
		Ok(())
	}
}

/// Measure the compact [`Debug`] rendering of a value without storing it.
///
/// # Parameters
/// - `value`: The value to render.
///
/// # Returns
/// The length of the rendering, in bytes.
fn debug_length(value: &impl fmt::Debug) -> usize
{
	let mut counter = Counter(0);
	write!(counter, "{:?}", value).unwrap();
	counter.0
}

/// Measure the [`Display`](fmt::Display) rendering of a value without storing
/// it.
///
/// # Parameters
/// - `value`: The value to render.
///
/// # Returns
/// The length of the rendering, in bytes.
fn display_length(value: &impl fmt::Display) -> usize
{
	let mut counter = Counter(0);
	write!(counter, "{}", value).unwrap();
	counter.0
}

/// Measure the S-expression representation of a value without storing it.
///
/// # Parameters
/// - `value`: The value to write.
/// - `options`: The formatting options.
///
/// # Returns
/// The length of the representation, in bytes.
fn s_expr_length(
	value: &impl SExpressible,
	options: SExpressibleOptions
) -> usize
{
	let mut counter = Counter(0);
	value
		.write_s_expr(&mut counter, options.soft_limit, options)
		.unwrap();
	counter.0
}

/// Reconstruct the derived `{:#?}` rendering of `depth` negations of a
/// constant, all with synthetic spans, without recursion.
///
/// # Parameters
/// - `depth`: The number of negations.
/// - `leaf`: The value of the constant.
///
/// # Returns
/// The rendering.
fn pretty_negations(depth: usize, leaf: i32) -> String
{
	let indent = |level: usize| "    ".repeat(level);
	let span = |level: usize| {
		format!(
			"SourceSpan {{\n{}start: 0,\n{}end: 0,\n{}}}",
			indent(level + 1),
			indent(level + 1),
			indent(level)
		)
	};
	let mut out = String::new();
	for level in 0..depth
	{
		let base = 3 * level;
		out.push_str("Arithmetic(\n");
		out.push_str(&format!("{}Neg(\n", indent(base + 1)));
		out.push_str(&format!("{}Neg {{\n", indent(base + 2)));
		out.push_str(&format!("{}operand: ", indent(base + 3)));
	}
	let base = 3 * depth;
	out.push_str(&format!(
		"Constant(\n{}Constant {{\n{}value: {},\n{}span: {},\n{}}},\n{})",
		indent(base + 1),
		indent(base + 2),
		leaf,
		indent(base + 2),
		span(base + 2),
		indent(base + 1),
		indent(base)
	));
	for level in (0..depth).rev()
	{
		let base = 3 * level;
		out.push_str(&format!(
			",\n{}span: {},\n{}}},\n{}),\n{})",
			indent(base + 3),
			span(base + 3),
			indent(base + 2),
			indent(base + 1),
			indent(base)
		));
	}
	out
}