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
//! # Validator
//!
//! The validator performs semantic analysis on a parsed
//! [abstract syntax tree](crate::ast) (AST) before code generation. It sits
//! between the [parser](crate::parser) and the [compiler](crate::Compiler) in
//! the compilation pipeline, rejecting ASTs that are syntactically well-formed
//! but semantically invalid.
//!
//! Keeping validation as a distinct pass keeps the code generator infallible
//! (its [`ASTVisitor::Error`] type is
//! [`Infallible`](std::convert::Infallible)) while allowing semantic errors to
//! carry typed, span-bearing payloads suitable for rich diagnostics.
//!
//! # Current checks
//!
//! - **Duplicate parameters.** A function may not declare the same formal
//!   parameter name more than once. `{x}, {x}: {x} + 1` is rejected as
//!   [`DuplicateParameter`](CompilationError::DuplicateParameter), carrying the
//!   spans of both occurrences for caret-level reporting.
//! - **Binding collides with parameter.** A [local binding](ast::Binding) may
//!   not reuse a formal-parameter name. `{x}: {x}@(3D6) + {x}` is rejected as
//!   [`BindingCollidesWithParameter`](CompilationError::BindingCollidesWithParameter),
//!   since binding, parameter, and environment-variable names share a single
//!   flat namespace per function.
//! - **Duplicate binding.** The same name may not be bound twice within a
//!   function body. `{x}@(3D6) + {x}@(1D4)` is rejected as
//!   [`DuplicateBinding`](CompilationError::DuplicateBinding).
//! - **Use before bind.** A [variable reference](ast::Variable) must lexically
//!   follow the [binding](ast::Binding) that introduces its name, including any
//!   reference inside the bound expression itself (self-reference). `{x} +
//!   {x}@(3D6)` and `{x}@({x})` are both rejected as
//!   [`UseBeforeBind`](CompilationError::UseBeforeBind).

use std::collections::{HashMap, HashSet};

use crate::{
	CompilationError, SourceSpan,
	ast::{
		self, ASTVisitor, Add, Binding, Constant, CustomDice, Div, DropHighest,
		DropLowest, Event, Exp, Expression, Group, Mod, Mul, Neg, Node, Range,
		StandardDice, Sub, Variable, Walk
	}
};

////////////////////////////////////////////////////////////////////////////////
//                              Validator pass.                               //
////////////////////////////////////////////////////////////////////////////////

/// A semantic-validation pass over an [abstract syntax tree](crate::ast)
/// (AST). Use [`Validator::validate`] as the high-level entry point; the
/// [`Validator`] type also implements [`ASTVisitor`] for
/// callers who want to drive validation directly — e.g., to interleave it
/// with other AST analyses.
#[derive(Copy, Clone, Debug, Default)]
pub struct Validator;

impl Validator
{
	/// Construct a new [`Validator`].
	///
	/// # Returns
	/// A fresh [`Validator`], ready to validate an AST.
	#[inline]
	pub const fn new() -> Self { Self }

	/// Validate the semantic well-formedness of a parsed
	/// [function](ast::Function). Runs between parsing and code generation.
	///
	/// # Type parameters
	/// - `'src`: The lifetime of the source text from which the AST was parsed.
	///   Names reported in errors are borrowed from the source wherever it
	///   spells them [canonically](crate::parser::canonical_name), so the error
	///   borrows for `'src`, but not from the AST.
	///
	/// # Parameters
	/// - `ast`: The parsed function definition.
	///
	/// # Returns
	/// `Ok(())` if the function passes all semantic checks.
	///
	/// # Errors
	/// [`DuplicateParameter`](CompilationError::DuplicateParameter) if the
	/// function declares the same formal parameter name more than once.
	pub fn validate<'src>(
		ast: &ast::Function<'src>
	) -> Result<(), CompilationError<'src>>
	{
		check_duplicate_parameters(ast)?;
		let bindings = collect_bindings_and_check_collisions(ast)?;
		check_use_before_bind(&ast.body, &bindings)
	}
}

/// The [duplicate-parameter](CompilationError::DuplicateParameter) check,
/// factored out so both [`Validator::validate`] and the [`ASTVisitor`]
/// implementation share a single source of truth.
///
/// # Type parameters
/// - `'src`: The lifetime of the source text.
///
/// # Parameters
/// - `ast`: The parsed function definition.
///
/// # Returns
/// `Ok(())` if no parameter name is declared more than once.
///
/// # Errors
/// [`DuplicateParameter`](CompilationError::DuplicateParameter) with the spans
/// of the first and duplicate occurrences of the repeated name.
fn check_duplicate_parameters<'src>(
	ast: &ast::Function<'src>
) -> Result<(), CompilationError<'src>>
{
	if let Some(ref parameters) = ast.parameters
	{
		let mut seen: HashMap<&str, SourceSpan> =
			HashMap::with_capacity(parameters.len());
		for param in parameters
		{
			if let Some(&first) = seen.get(&*param.name)
			{
				return Err(CompilationError::DuplicateParameter {
					name: param.name.clone(),
					first,
					duplicate: param.span
				});
			}
			seen.insert(&param.name, param.span);
		}
	}
	Ok(())
}

/// Walk the AST collecting every [local binding](Binding) by name, and in the
/// same pass reject duplicate bindings and bindings whose names collide with
/// formal parameters. The returned map records the binding-site
/// [`name_span`](Binding::name_span) for each binding, so the companion
/// [use-before-bind check](check_use_before_bind) can cite the binding site
/// when reporting an offending reference.
///
/// # Type parameters
/// - `'a`: The lifetime of the borrow of the AST. Binding names are borrowed
///   from the AST.
/// - `'src`: The lifetime of the source text.
///
/// # Parameters
/// - `ast`: The parsed function definition.
///
/// # Returns
/// A map from binding name to the [span](SourceSpan) of its binding site.
///
/// # Errors
/// * [`BindingCollidesWithParameter`](CompilationError::BindingCollidesWithParameter)
///   if a binding name matches a formal parameter name.
/// * [`DuplicateBinding`](CompilationError::DuplicateBinding) if the same name
///   appears as a binding more than once in the function body.
fn collect_bindings_and_check_collisions<'a, 'src>(
	ast: &'a ast::Function<'src>
) -> Result<HashMap<&'a str, SourceSpan>, CompilationError<'src>>
{
	let parameter_spans = match ast.parameters
	{
		Some(ref parameters) => parameters
			.iter()
			.map(|p| (&*p.name, p.span))
			.collect::<HashMap<_, _>>(),
		None => HashMap::new()
	};
	let mut bindings: HashMap<&'a str, SourceSpan> = HashMap::new();
	for event in Walk::new(Node::Expression(&ast.body))
	{
		if let Event::Enter(Node::Expression(Expression::Binding(b))) = event
		{
			if let Some(&parameter) = parameter_spans.get(&*b.name)
			{
				return Err(CompilationError::BindingCollidesWithParameter {
					name: b.name.clone(),
					parameter,
					binding: b.name_span
				});
			}
			if let Some(&first) = bindings.get(&*b.name)
			{
				return Err(CompilationError::DuplicateBinding {
					name: b.name.clone(),
					first,
					duplicate: b.name_span
				});
			}
			// Record the binding on entering it, before its bound
			// expression, so that a binding of the same name nested
			// within the bound expression is a duplicate of this one. A
			// self-reference within the bound expression is a variable,
			// not a binding, and surfaces as a use-before-bind instead.
			bindings.insert(&b.name, b.name_span);
		}
	}
	Ok(bindings)
}

/// Walk the body of a function in lexical order, rejecting any
/// [variable reference](Variable) whose name is introduced by a
/// [local binding](Binding) that has not yet been reached. Because every
/// binding is visited _after_ its own bound expression, a self-reference inside
/// the RHS surfaces here as [`UseBeforeBind`](CompilationError::UseBeforeBind)
/// — the sole mechanism by which self-reference is rejected.
///
/// # Type parameters
/// - `'a`: The lifetime of the borrow of the function body.
/// - `'src`: The lifetime of the source text.
///
/// # Parameters
/// - `body`: The function body to check.
/// - `bindings`: The complete set of bindings in the body, keyed by name, with
///   the binding-site [span](SourceSpan) used to cite the binding in errors.
///
/// # Errors
/// [`UseBeforeBind`](CompilationError::UseBeforeBind) at the first offending
/// reference encountered in a left-to-right, depth-first walk.
fn check_use_before_bind<'a, 'src>(
	body: &'a Expression<'src>,
	bindings: &HashMap<&'a str, SourceSpan>
) -> Result<(), CompilationError<'src>>
{
	// The names of the bindings reached so far. A reference to a name that
	// appears in `bindings` but not yet here is a use-before-bind error. Names
	// not in `bindings` at all are not local bindings, and flow through to the
	// compiler as external variables or parameters.
	let mut seen: HashSet<&'a str> = HashSet::new();
	for event in Walk::new(Node::Expression(body))
	{
		match event
		{
			Event::Enter(Node::Expression(Expression::Variable(v))) =>
			{
				if let Some(&binding_span) = bindings.get(&*v.name)
					&& !seen.contains(&*v.name)
				{
					return Err(CompilationError::UseBeforeBind {
						name: v.name.clone(),
						reference: v.span,
						binding: binding_span
					});
				}
			},
			Event::Leave(Node::Expression(Expression::Binding(b))) =>
			{
				seen.insert(&b.name);
			},
			_ =>
			{}
		}
	}
	Ok(())
}

////////////////////////////////////////////////////////////////////////////////
//                         ASTVisitor for Validator.                          //
////////////////////////////////////////////////////////////////////////////////

impl<'a, 'src: 'a> ASTVisitor<'a, 'src> for Validator
{
	type Error = CompilationError<'src>;
	type Output = ();

	fn enter_function(
		&mut self,
		node: &'a ast::Function<'src>
	) -> Result<(), Self::Error>
	{
		check_duplicate_parameters(node)
	}

	fn visit_function(
		&mut self,
		_node: &'a ast::Function<'src>,
		_body: ()
	) -> Result<(), Self::Error>
	{
		Ok(())
	}

	fn visit_group(
		&mut self,
		_node: &'a Group<'src>,
		_expression: ()
	) -> Result<(), Self::Error>
	{
		Ok(())
	}

	fn visit_constant(&mut self, _node: &'a Constant)
	-> Result<(), Self::Error>
	{
		Ok(())
	}

	fn visit_variable(
		&mut self,
		_node: &'a Variable<'src>
	) -> Result<(), Self::Error>
	{
		Ok(())
	}

	fn visit_binding(
		&mut self,
		_node: &'a Binding<'src>,
		_expression: ()
	) -> Result<(), Self::Error>
	{
		Ok(())
	}

	fn visit_range(
		&mut self,
		_node: &'a Range<'src>,
		_start: (),
		_end: ()
	) -> Result<(), Self::Error>
	{
		Ok(())
	}

	fn visit_standard_dice(
		&mut self,
		_node: &'a StandardDice<'src>,
		_count: (),
		_faces: ()
	) -> Result<(), Self::Error>
	{
		Ok(())
	}

	fn visit_custom_dice(
		&mut self,
		_node: &'a CustomDice<'src>,
		_count: ()
	) -> Result<(), Self::Error>
	{
		Ok(())
	}

	fn visit_drop_lowest(
		&mut self,
		_node: &'a DropLowest<'src>,
		_dice: (),
		_drop: Option<()>
	) -> Result<(), Self::Error>
	{
		Ok(())
	}

	fn visit_drop_highest(
		&mut self,
		_node: &'a DropHighest<'src>,
		_dice: (),
		_drop: Option<()>
	) -> Result<(), Self::Error>
	{
		Ok(())
	}

	fn visit_add(
		&mut self,
		_node: &'a Add<'src>,
		_left: (),
		_right: ()
	) -> Result<(), Self::Error>
	{
		Ok(())
	}

	fn visit_sub(
		&mut self,
		_node: &'a Sub<'src>,
		_left: (),
		_right: ()
	) -> Result<(), Self::Error>
	{
		Ok(())
	}

	fn visit_mul(
		&mut self,
		_node: &'a Mul<'src>,
		_left: (),
		_right: ()
	) -> Result<(), Self::Error>
	{
		Ok(())
	}

	fn visit_div(
		&mut self,
		_node: &'a Div<'src>,
		_left: (),
		_right: ()
	) -> Result<(), Self::Error>
	{
		Ok(())
	}

	fn visit_mod(
		&mut self,
		_node: &'a Mod<'src>,
		_left: (),
		_right: ()
	) -> Result<(), Self::Error>
	{
		Ok(())
	}

	fn visit_exp(
		&mut self,
		_node: &'a Exp<'src>,
		_left: (),
		_right: ()
	) -> Result<(), Self::Error>
	{
		Ok(())
	}

	fn visit_neg(
		&mut self,
		_node: &'a Neg<'src>,
		_operand: ()
	) -> Result<(), Self::Error>
	{
		Ok(())
	}
}