foch 0.0.1

EU4 mod analysis and merge toolkit
Documentation
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
//! Rewrite script numbers into the spelling EU4's own reader implies.
//!
//! EU4 reads a script number far more coarsely than byte comparison: three
//! truncated decimals for a `float` field, `atoi` for an `int` one (the
//! evidence is in [`crate::game::eu4::coercion`]). So mods can write one value
//! several ways — `0.5`, `0.50` and `0.500` are the same number to the game —
//! and comparing the text reports a difference that does not exist.
//!
//! This runs on the AST before the merge, beside
//! `canonicalize_boolean_or_definitions`, rather than inside tree
//! normalization or at conflict resolution. The AST is what every identity in
//! the merge is ultimately derived from: `assignment_anchor` and
//! `value_fingerprint` read `AstValue` directly, and the normalized tree's
//! hashes are computed from the values it carries. Canonicalizing here means
//! the matcher, the hashes, the anchors, definition provenance and the
//! semantic-equivalence check all agree by construction, instead of each
//! needing to learn the same rule.
//!
//! Which reader applies is a property of the *field*, not of the text, so this
//! acts only where the CWT schema states the field's type. Where the schema is
//! silent the text is left exactly as written — the tree then says "I make no
//! claim about this value" rather than asserting one from a token's shape.

use std::collections::HashMap;
use std::ops::Range;

use crate::game::eu4::coercion::{canonical_float_text, canonical_int_text};
use crate::game::eu4::script::parser::{
	AstFile, AstStatement, AstValue, ScalarValue, ScriptSyntax, parse_clausewitz_statements,
};
use crate::game::schema::query::{CwtQuery, RuleContext, SchemaScalarType};
use crate::model::GamePath;

/// Rewrite every schema-typed number in `file` under the installed schema.
///
/// The merge-quality harness scores generated output against a human
/// compatibility patch, and must compare the representation the merge
/// produces rather than a second rule of its own, or a compatch that wrote
/// `0.50` would count as differing from output that says `0.500`.
///
/// `relative_path` is a semantic input, not a label: the schema binds a root
/// type by matching a game-relative prefix such as `common/static_modifiers`,
/// so an absolute path on disk matches nothing and every number is left as
/// written. Callers that hold a scratch path must pass the game-relative one.
pub fn canonicalize_numeric_values_with_active_schema(
	relative_path: &GamePath,
	file: &AstFile,
) -> AstFile {
	canonicalize_numeric_values_at(
		relative_path,
		file,
		Some(crate::game::eu4::cwt::rule_engine()),
	)
}

/// Rewrite every schema-typed number in `file` into its canonical spelling.
///
/// A file whose path binds no root type, or a call with no schema, comes back
/// unchanged.
pub(crate) fn canonicalize_numeric_values(file: &AstFile, schema: Option<&CwtQuery>) -> AstFile {
	canonicalize_numeric_values_at(&file.path, file, schema)
}

fn canonicalize_numeric_values_at(
	relative_path: &GamePath,
	file: &AstFile,
	schema: Option<&CwtQuery>,
) -> AstFile {
	let Some(schema) = schema else {
		return file.clone();
	};
	let mut file = file.clone();
	let mut walker = NumericWalker {
		schema,
		file_path: relative_path,
		contexts: HashMap::new(),
		edits: Vec::new(),
	};
	walker.visit(&mut file.statements, &mut Vec::new());
	file
}

/// Rewrite the numbers in `source` and leave every other byte untouched.
///
/// Re-emitting the parsed file would work for the values but would also
/// reformat the comments and whitespace around them, and the merge-quality
/// harness measures text similarity against a human patch — reformatting would
/// swamp the signal it is trying to read. Replacing byte ranges keeps the
/// comparison about content.
pub fn canonicalize_numeric_text(relative_path: &GamePath, source: &str) -> String {
	canonicalize_numeric_text_with(
		relative_path,
		source,
		Some(crate::game::eu4::cwt::rule_engine()),
	)
}

fn canonicalize_numeric_text_with(
	relative_path: &GamePath,
	source: &str,
	schema: Option<&CwtQuery>,
) -> String {
	let Some(schema) = schema else {
		return source.to_string();
	};
	let syntax = ScriptSyntax::for_game_path(relative_path);
	let parsed = parse_clausewitz_statements(syntax, source);
	if !parsed.diagnostics.is_empty() {
		return source.to_string();
	}
	let mut statements = parsed.statements;
	let mut walker = NumericWalker {
		schema,
		file_path: relative_path,
		contexts: HashMap::new(),
		edits: Vec::new(),
	};
	walker.visit(&mut statements, &mut Vec::new());
	let mut edits = walker.edits;
	// Apply from the end so earlier offsets stay valid.
	edits.sort_by_key(|(range, _)| std::cmp::Reverse(range.start));
	let mut source = source.to_string();
	for (range, canonical) in edits {
		if source.get(range.clone()).is_some() {
			source.replace_range(range, &canonical);
		}
	}
	source
}

struct NumericWalker<'a> {
	schema: &'a CwtQuery,
	file_path: &'a GamePath,
	/// Resolved rule contexts by ancestor key chain.
	///
	/// One entry per block the walk enters, not one per number: a definition
	/// with fifty modifier fields costs one binding, then fifty cheap field
	/// lookups inside it.
	contexts: HashMap<Vec<String>, Vec<RuleContext<'a>>>,
	/// Byte ranges rewritten, for callers that edit the source text rather
	/// than the tree.
	edits: Vec<(Range<usize>, String)>,
}

impl<'a> NumericWalker<'a> {
	fn visit(&mut self, statements: &mut [AstStatement], chain: &mut Vec<String>) {
		for statement in statements {
			let AstStatement::Assignment { key, value, .. } = statement else {
				// A list item is matched by position and its text is not this
				// field's value, so the field's declared type says nothing
				// about it.
				continue;
			};
			match value {
				AstValue::Block { items, .. } => {
					chain.push(key.clone());
					self.visit(items, chain);
					chain.pop();
				}
				AstValue::Scalar {
					value: ScalarValue::Number(text),
					span,
				} => {
					if let Some(canonical) = self.canonical_text(chain, key, text) {
						self.edits
							.push((span.start.offset..span.end.offset, canonical.clone()));
						*text = canonical;
					}
				}
				AstValue::Scalar { .. } => {}
			}
		}
	}

	fn canonical_text(&mut self, chain: &[String], key: &str, text: &str) -> Option<String> {
		let scalar_type = self.scalar_type(chain, key)?;
		match scalar_type {
			SchemaScalarType::Float { .. } => canonical_float_text(text),
			SchemaScalarType::Int { .. } => canonical_int_text(text),
			// `CToken::GetBool` compares against the exact lowercase `yes`, so
			// every other spelling reads as false and they would all collapse
			// together — including the mis-spellings the editor reports as
			// errors. A boolean is never rewritten here.
			SchemaScalarType::Bool => None,
		}
		.filter(|canonical| canonical != text)
	}

	/// The primitive the schema declares for `key` inside `chain`, or `None`
	/// wherever the schema is not explicit.
	fn scalar_type(&mut self, chain: &[String], key: &str) -> Option<SchemaScalarType> {
		let contexts = self.contexts_for(chain);
		let mut resolved: Option<SchemaScalarType> = None;
		for context in contexts {
			for field_match in self.schema.bind_field_matches(context, key) {
				// The type has to come from the match, not from its field: an
				// alias-bound field's wildcard carries the
				// `alias_match_left[...]` marker rather than a type.
				let Some(scalar_type) = SchemaScalarType::from_rule_value(field_match.value())
				else {
					// A matching rule that is not a primitive scalar means the
					// key is overloaded here; the schema is not explicit.
					return None;
				};
				match resolved {
					None => resolved = Some(scalar_type),
					Some(previous) if previous.is_same_primitive(scalar_type) => {}
					Some(_) => return None,
				}
			}
		}
		resolved
	}

	/// Every reading of `chain` the schema accepts.
	///
	/// A definition's own instance key leads the chain under most root types
	/// and is absorbed by `skip_root_key` under others, and which applies is
	/// not recoverable from the chain alone, so both readings are kept and
	/// `scalar_type` requires them to agree.
	fn contexts_for(&mut self, chain: &[String]) -> Vec<RuleContext<'a>> {
		if !self.contexts.contains_key(chain) {
			let mut readings: Vec<&[String]> = vec![chain];
			if let Some((_, rest)) = chain.split_first() {
				readings.push(rest);
			}
			let contexts = readings
				.into_iter()
				.filter_map(|reading| {
					let reading = reading.iter().map(String::as_str).collect::<Vec<_>>();
					self.schema.bind_context(self.file_path, &reading)
				})
				.collect::<Vec<_>>();
			self.contexts.insert(chain.to_vec(), contexts);
		}
		self.contexts[chain].clone()
	}
}

#[cfg(test)]
mod tests {
	use std::fs;

	use tempfile::TempDir;

	use super::*;
	use crate::game::eu4::script::emit::emit_clausewitz_statements;
	use crate::game::eu4::script::parser::parse_clausewitz_content;
	use crate::game::schema::CwtSchema;

	const SCHEMA: &str = r#"
		types = {
			type[thing] = { path = "game/common/things" }
		}

		thing = {
			alias_name[modifier] = alias_match_left[modifier]
			untyped = scalar
			nested = { depth = float }
		}

		alias[modifier:upkeep] = float
		alias[modifier:slots] = int
		alias[modifier:enabled] = bool
	"#;

	fn schema() -> CwtSchema {
		let root = TempDir::new().expect("create schema directory");
		fs::write(root.path().join("things.cwt"), SCHEMA).expect("write schema");
		CwtSchema::load_with_cache(root.path(), None).expect("load schema")
	}

	fn canonicalize(path: &str, source: &str, schema: Option<&CwtQuery>) -> String {
		let parsed = parse_clausewitz_content(
			&crate::model::GamePathBuf::parse(path).expect("valid game path"),
			source,
		);
		assert!(parsed.diagnostics.is_empty(), "{:?}", parsed.diagnostics);
		let canonical = canonicalize_numeric_values(&parsed.ast, schema);
		emit_clausewitz_statements(&canonical.statements).expect("emit")
	}

	#[test]
	fn spellings_of_one_value_become_one_string() {
		let schema = schema();
		for written in ["0.5", "0.50", "0.500"] {
			assert!(
				canonicalize(
					"common/things/example.txt",
					&format!("a_thing = {{ upkeep = {written} }}\n"),
					Some(schema.facts()),
				)
				.contains("upkeep = 0.500"),
				"`{written}` should canonicalize"
			);
		}
	}

	#[test]
	fn a_fourth_decimal_is_truncated_the_way_the_reader_truncates_it() {
		let schema = schema();
		for written in ["0.1234", "0.1239"] {
			assert!(
				canonicalize(
					"common/things/example.txt",
					&format!("a_thing = {{ upkeep = {written} }}\n"),
					Some(schema.facts()),
				)
				.contains("upkeep = 0.123"),
				"`{written}` should truncate"
			);
		}
	}

	#[test]
	fn an_int_field_uses_the_integer_reader() {
		let schema = schema();
		let output = canonicalize(
			"common/things/example.txt",
			"a_thing = { slots = 3.4 }\n",
			Some(schema.facts()),
		);

		assert!(output.contains("slots = 3"), "{output}");
		assert!(!output.contains("3.4"), "{output}");
	}

	#[test]
	fn a_distinct_value_keeps_its_own_spelling() {
		let schema = schema();
		let output = canonicalize(
			"common/things/example.txt",
			"a_thing = { upkeep = 0.6 }\n",
			Some(schema.facts()),
		);

		assert!(output.contains("upkeep = 0.600"), "{output}");
	}

	#[test]
	fn nested_blocks_are_reached() {
		let schema = schema();
		let output = canonicalize(
			"common/things/example.txt",
			"a_thing = { nested = { depth = 2.5 } }\n",
			Some(schema.facts()),
		);

		assert!(output.contains("depth = 2.500"), "{output}");
	}

	#[test]
	fn a_field_the_schema_does_not_type_is_left_exactly_as_written() {
		let schema = schema();
		for source in [
			"a_thing = { untyped = 0.50 }\n",
			"a_thing = { not_in_the_schema = 0.50 }\n",
			// A boolean is never rewritten: the game reads only the exact
			// lowercase `yes` as true.
			"a_thing = { enabled = yes }\n",
		] {
			assert_eq!(
				canonicalize("common/things/example.txt", source, Some(schema.facts())),
				canonicalize("common/things/example.txt", source, None),
				"{source}"
			);
		}
	}

	#[test]
	fn a_path_the_schema_does_not_cover_is_left_alone() {
		let schema = schema();
		let source = "a_thing = { upkeep = 0.50 }\n";

		assert!(
			canonicalize("common/elsewhere/example.txt", source, Some(schema.facts()))
				.contains("upkeep = 0.50"),
		);
	}

	#[test]
	fn a_list_item_is_not_a_field_value() {
		// Items are matched by position, so the enclosing field's type says
		// nothing about their text.
		let schema = schema();
		let output = canonicalize(
			"common/things/example.txt",
			"a_thing = { upkeep = { 0.50 0.1239 } }\n",
			Some(schema.facts()),
		);

		assert!(output.contains("0.50"), "{output}");
		assert!(output.contains("0.1239"), "{output}");
	}

	#[test]
	fn no_schema_leaves_every_number_as_written() {
		let source = "a_thing = { upkeep = 0.50 slots = 3.4 }\n";

		assert!(canonicalize("common/things/example.txt", source, None).contains("upkeep = 0.50"));
	}

	#[test]
	fn rewriting_text_touches_only_the_numbers() {
		let source = "# a leading note\n\
			a_thing = {\n\
			\t# why this value\n\
			\tupkeep    =    0.5   # trailing note\n\
			\tuntyped = 0.50\n\
			}\n";

		let schema = schema();
		let rewritten = canonicalize_numeric_text_with(
			GamePath::new("common/things/example.txt").expect("valid game path"),
			source,
			Some(schema.facts()),
		);

		// Only the schema-typed number moves; the comments, the odd spacing
		// and the untyped value are byte-for-byte what they were.
		assert_eq!(
			rewritten,
			source.replace("0.5   #", "0.500   #"),
			"{rewritten}"
		);
	}

	#[test]
	fn rewriting_text_is_a_no_op_where_nothing_is_typed() {
		for source in ["a_thing = { untyped = 0.50 }\n", "not_parseable = {\n"] {
			assert_eq!(
				canonicalize_numeric_text_with(
					GamePath::new("common/things/example.txt").expect("valid game path"),
					source,
					Some(schema().facts())
				),
				source
			);
		}
	}

	#[test]
	fn canonicalizing_twice_changes_nothing_the_second_time() {
		let schema = schema();
		let once = canonicalize(
			"common/things/example.txt",
			"a_thing = { upkeep = 0.5 slots = 3.4 }\n",
			Some(schema.facts()),
		);

		assert_eq!(
			canonicalize("common/things/example.txt", &once, Some(schema.facts())),
			once
		);
	}
}

#[cfg(test)]
mod coverage_probe {
	use std::path::{Path, PathBuf};

	use super::*;
	use crate::game::eu4::script::parser::parse_clausewitz_content;

	fn walk(dir: &Path, out: &mut Vec<PathBuf>) {
		let Ok(entries) = std::fs::read_dir(dir) else {
			return;
		};
		for entry in entries.flatten() {
			let path = entry.path();
			if path.is_dir() {
				walk(&path, out);
			} else if path.extension().is_some_and(|e| e == "txt") {
				out.push(path);
			}
		}
	}

	fn redundant(text: &str) -> bool {
		match text.split_once('.') {
			Some((_, fraction)) => {
				!fraction.is_empty()
					&& fraction.bytes().all(|b| b.is_ascii_digit())
					&& (fraction.ends_with('0') || fraction.len() > 3)
			}
			None => false,
		}
	}

	/// Count redundantly spelled numbers, and how many of them the transform
	/// actually rewrote. The canonical form itself ends in zeros, so the count
	/// has to come from comparing the two trees rather than re-testing shape.
	fn count(
		before: &[AstStatement],
		after: &[AstStatement],
		redundant_total: &mut usize,
		changed: &mut usize,
		rewritten: &mut usize,
	) {
		for (before, after) in before.iter().zip(after) {
			let (
				AstStatement::Assignment { value: before, .. },
				AstStatement::Assignment { value: after, .. },
			) = (before, after)
			else {
				continue;
			};
			match (before, after) {
				(AstValue::Block { items: before, .. }, AstValue::Block { items: after, .. }) => {
					count(before, after, redundant_total, changed, rewritten);
				}
				(
					AstValue::Scalar {
						value: ScalarValue::Number(before),
						..
					},
					AstValue::Scalar {
						value: ScalarValue::Number(after),
						..
					},
				) => {
					if redundant(before) {
						*redundant_total += 1;
						if before != after {
							*changed += 1;
						}
					}
					if before != after {
						*rewritten += 1;
					}
				}
				_ => {}
			}
		}
	}

	#[test]
	#[ignore = "P-695 coverage probe: needs EU4_ROOT"]
	fn measure_coverage_against_vanilla() {
		let schema = crate::game::eu4::cwt::rule_engine();
		let root = PathBuf::from(std::env::var("EU4_ROOT").expect("EU4_ROOT"));
		let mut files = Vec::new();
		for directory in ["common", "events", "decisions", "missions"] {
			walk(&root.join(directory), &mut files);
		}
		files.sort();

		let (mut parsed_files, mut redundant_total, mut changed, mut rewritten) =
			(0usize, 0usize, 0usize, 0usize);
		for file in &files {
			let Ok(source) = std::fs::read_to_string(file) else {
				continue;
			};
			let relative = crate::model::GamePathBuf::from_physical(&root, file)
				.expect("vanilla file has a game path");
			let parsed = parse_clausewitz_content(&relative, &source);
			if !parsed.diagnostics.is_empty() {
				continue;
			}
			parsed_files += 1;
			let canonical = canonicalize_numeric_values(&parsed.ast, Some(schema));
			count(
				&parsed.ast.statements,
				&canonical.statements,
				&mut redundant_total,
				&mut changed,
				&mut rewritten,
			);
		}
		eprintln!(
			"P695 COVERAGE files={parsed_files} redundantly_spelled={redundant_total} \
			 of_those_covered={changed} numbers_rewritten_total={rewritten}"
		);
	}
}