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
use super::document::{
	CsvRow, DocumentRecord, JsonProperty, LocalisationDefinition, LocalisationDuplicate,
	ParseIssue, ResourceReference, UiDefinition,
};
use super::{GamePathBuf, MaybeScope, ScopeSet};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(
	Clone,
	Copy,
	Debug,
	Eq,
	PartialEq,
	Hash,
	Serialize,
	Deserialize,
	rkyv::Archive,
	rkyv::Serialize,
	rkyv::Deserialize,
)]
pub enum SymbolKind {
	ScriptedEffect,
	ScriptedTrigger,
	Event,
	Decision,
	DiplomaticAction,
	TriggeredModifier,
}

impl SymbolKind {
	pub fn as_str(self) -> &'static str {
		match self {
			SymbolKind::ScriptedEffect => "scripted_effect",
			SymbolKind::ScriptedTrigger => "scripted_trigger",
			SymbolKind::Event => "event",
			SymbolKind::Decision => "decision",
			SymbolKind::DiplomaticAction => "diplomatic_action",
			SymbolKind::TriggeredModifier => "triggered_modifier",
		}
	}
}

#[derive(
	Clone,
	Copy,
	Debug,
	Eq,
	PartialEq,
	Serialize,
	Deserialize,
	rkyv::Archive,
	rkyv::Serialize,
	rkyv::Deserialize,
)]
pub enum ScopeKind {
	File,
	Event,
	Decision,
	ScriptedEffect,
	Trigger,
	Effect,
	Loop,
	AliasBlock,
	Block,
}

#[derive(
	Clone,
	Debug,
	Eq,
	PartialEq,
	Serialize,
	Deserialize,
	rkyv::Archive,
	rkyv::Serialize,
	rkyv::Deserialize,
)]
pub struct SourceSpan {
	pub line: usize,
	pub column: usize,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ScopeNode {
	pub id: usize,
	pub kind: ScopeKind,
	pub parent: Option<usize>,
	pub this_type: MaybeScope,
	pub aliases: HashMap<String, MaybeScope>,
	pub mod_id: String,
	pub path: GamePathBuf,
	pub span: SourceSpan,
	/// The block key that created this scope (e.g. "multiply_variable", "OR").
	/// Empty for file-level scopes.
	#[serde(default)]
	pub key: String,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SymbolDefinition {
	pub kind: SymbolKind,
	pub name: String,
	pub module: String,
	pub local_name: String,
	pub mod_id: String,
	pub path: GamePathBuf,
	pub line: usize,
	pub column: usize,
	pub scope_id: usize,
	pub declared_this_type: MaybeScope,
	pub inferred_this_type: MaybeScope,
	#[serde(default)]
	pub inferred_this_mask: ScopeSet,
	/// Static caller-FROM scope mask inferred from invocation sites. For
	/// callables (scripted_effects, scripted_triggers, ...), this is the
	/// union of FROM types observed at every callsite. Non-zero means FROM
	/// is statically resolvable inside the body and invisible-scope-alias should not flag
	/// FROM aliases there.
	#[serde(default)]
	pub inferred_from_mask: ScopeSet,
	/// Static caller-ROOT scope mask inferred from invocation sites. ROOT
	/// propagates into scripted_effect / scripted_trigger bodies (the body
	/// keeps the caller's outermost evaluation context). For events ROOT is
	/// already pinned to the event scope, but we still record the mask for
	/// downstream resolution and consistency.
	#[serde(default)]
	pub inferred_root_mask: ScopeSet,
	pub required_params: Vec<String>,
	#[serde(default)]
	pub optional_params: Vec<String>,
	#[serde(default)]
	pub param_contract: Option<ParamContract>,
	#[serde(default)]
	pub scope_param_names: Vec<String>,
}

#[derive(
	Clone,
	Debug,
	Eq,
	PartialEq,
	Serialize,
	Deserialize,
	rkyv::Archive,
	rkyv::Serialize,
	rkyv::Deserialize,
)]
pub struct ParamBinding {
	pub name: String,
	pub value: String,
}

#[derive(
	Clone,
	Debug,
	Eq,
	PartialEq,
	Serialize,
	Deserialize,
	rkyv::Archive,
	rkyv::Serialize,
	rkyv::Deserialize,
)]
pub struct ConditionalParamRule {
	pub when_present: String,
	pub requires_any_of: Vec<String>,
}

#[derive(
	Clone,
	Debug,
	Eq,
	PartialEq,
	Serialize,
	Deserialize,
	rkyv::Archive,
	rkyv::Serialize,
	rkyv::Deserialize,
)]
pub struct ParamContract {
	pub required_all: Vec<String>,
	pub optional: Vec<String>,
	pub one_of_groups: Vec<Vec<String>>,
	pub conditional_required: Vec<ConditionalParamRule>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SymbolReference {
	pub kind: SymbolKind,
	pub name: String,
	pub module: String,
	pub mod_id: String,
	pub path: GamePathBuf,
	pub line: usize,
	pub column: usize,
	pub scope_id: usize,
	pub provided_params: Vec<String>,
	pub param_bindings: Vec<ParamBinding>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AliasUsage {
	pub alias: String,
	pub mod_id: String,
	pub path: GamePathBuf,
	pub line: usize,
	pub column: usize,
	pub scope_id: usize,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct KeyUsage {
	pub key: String,
	pub mod_id: String,
	pub path: GamePathBuf,
	pub line: usize,
	pub column: usize,
	pub scope_id: usize,
	pub this_type: MaybeScope,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ScalarAssignment {
	pub key: String,
	pub value: String,
	pub mod_id: String,
	pub path: GamePathBuf,
	pub line: usize,
	pub column: usize,
	pub scope_id: usize,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct SemanticIndex {
	pub documents: Vec<DocumentRecord>,
	pub scopes: Vec<ScopeNode>,
	pub definitions: Vec<SymbolDefinition>,
	pub references: Vec<SymbolReference>,
	pub alias_usages: Vec<AliasUsage>,
	pub key_usages: Vec<KeyUsage>,
	pub scalar_assignments: Vec<ScalarAssignment>,
	pub localisation_definitions: Vec<LocalisationDefinition>,
	pub localisation_duplicates: Vec<LocalisationDuplicate>,
	pub ui_definitions: Vec<UiDefinition>,
	pub resource_references: Vec<ResourceReference>,
	pub csv_rows: Vec<CsvRow>,
	pub json_properties: Vec<JsonProperty>,
	pub parse_issues: Vec<ParseIssue>,
}

/// Record kinds of a [`SemanticIndex`], in the order
/// [`index_with_one_record_of_each_kind`] places them.
#[cfg(test)]
pub const SEMANTIC_RECORD_KINDS: [&str; 14] = [
	"document",
	"scope",
	"definition",
	"reference",
	"alias usage",
	"key usage",
	"scalar assignment",
	"localisation definition",
	"localisation duplicate",
	"ui definition",
	"resource reference",
	"csv row",
	"json property",
	"parse issue",
];

/// An index holding one record of each kind in [`SEMANTIC_RECORD_KINDS`], the
/// record of kind `n` at `path(n)`. Codec tests corrupt one persisted path at
/// a time with it, to show that every record's path is validated when read.
#[cfg(test)]
pub fn index_with_one_record_of_each_kind(path: impl Fn(usize) -> GamePathBuf) -> SemanticIndex {
	let mod_id = || "mod-a".to_string();
	SemanticIndex {
		documents: vec![DocumentRecord {
			mod_id: mod_id(),
			path: path(0),
			family: super::DocumentFamily::Clausewitz,
			parse_ok: true,
		}],
		scopes: vec![ScopeNode {
			id: 0,
			kind: ScopeKind::File,
			parent: None,
			this_type: MaybeScope::Unknown,
			aliases: HashMap::new(),
			mod_id: mod_id(),
			path: path(1),
			span: SourceSpan { line: 1, column: 1 },
			key: String::new(),
		}],
		definitions: vec![SymbolDefinition {
			kind: SymbolKind::ScriptedEffect,
			name: "eu4::m::defined".to_string(),
			module: "m".to_string(),
			local_name: "defined".to_string(),
			mod_id: mod_id(),
			path: path(2),
			line: 1,
			column: 1,
			scope_id: 0,
			declared_this_type: MaybeScope::Unknown,
			inferred_this_type: MaybeScope::Unknown,
			inferred_this_mask: ScopeSet::default(),
			inferred_from_mask: ScopeSet::default(),
			inferred_root_mask: ScopeSet::default(),
			required_params: Vec::new(),
			optional_params: Vec::new(),
			param_contract: None,
			scope_param_names: Vec::new(),
		}],
		references: vec![SymbolReference {
			kind: SymbolKind::ScriptedEffect,
			name: "eu4::m::referenced".to_string(),
			module: "m".to_string(),
			mod_id: mod_id(),
			path: path(3),
			line: 1,
			column: 1,
			scope_id: 0,
			provided_params: Vec::new(),
			param_bindings: Vec::new(),
		}],
		alias_usages: vec![AliasUsage {
			alias: "ROOT".to_string(),
			mod_id: mod_id(),
			path: path(4),
			line: 1,
			column: 1,
			scope_id: 0,
		}],
		key_usages: vec![KeyUsage {
			key: "used_key".to_string(),
			mod_id: mod_id(),
			path: path(5),
			line: 1,
			column: 1,
			scope_id: 0,
			this_type: MaybeScope::Unknown,
		}],
		scalar_assignments: vec![ScalarAssignment {
			key: "assigned".to_string(),
			value: "1".to_string(),
			mod_id: mod_id(),
			path: path(6),
			line: 1,
			column: 1,
			scope_id: 0,
		}],
		localisation_definitions: vec![LocalisationDefinition {
			key: "LOC_KEY".to_string(),
			mod_id: mod_id(),
			path: path(7),
			line: 1,
			column: 1,
		}],
		localisation_duplicates: vec![LocalisationDuplicate {
			key: "LOC_DUP".to_string(),
			mod_id: mod_id(),
			path: path(8),
			first_line: 1,
			duplicate_line: 2,
		}],
		ui_definitions: vec![UiDefinition {
			name: "ui_name".to_string(),
			mod_id: mod_id(),
			path: path(9),
			line: 1,
			column: 1,
		}],
		resource_references: vec![ResourceReference {
			key: "texturefile".to_string(),
			value: "gfx/a.dds".to_string(),
			mod_id: mod_id(),
			path: path(10),
			line: 1,
			column: 1,
		}],
		csv_rows: vec![CsvRow {
			identity: "row".to_string(),
			mod_id: mod_id(),
			path: path(11),
			line: 1,
			column: 1,
		}],
		json_properties: vec![JsonProperty {
			key_path: "a.b".to_string(),
			mod_id: mod_id(),
			path: path(12),
			line: 1,
			column: 1,
		}],
		parse_issues: vec![ParseIssue {
			mod_id: mod_id(),
			path: path(13),
			line: 1,
			column: 1,
			message: "issue".to_string(),
		}],
	}
}