foch-annotation 0.0.1

Foch annotations carried in EU4 script comments: extraction, schemas, validation and editor support
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
576
577
578
579
580
581
582
583
584
585
//! Find annotations in comments and attach them to the next definition.
//!
//! An annotation is `#name(arguments)`, possibly continued over several full
//! comment lines that each start with `#`. Arguments are Clausewitz script,
//! parsed at their original byte offsets so every span points into the file.
//! Extraction never stops at the first problem; malformed annotations become
//! diagnostics and are left out of the result.

use crate::diagnostic::{Code, Diagnostic, Severity};
use crate::schema::{AnnotationKind, AnnotationSchema, Registry};
use crate::source::{RelPath, SourceSpan};
use crate::value::{Value, game_path, identifier, parse_value, scalar};
use foch::game::eu4::script::parser::{
	AstStatement, AstValue, ScalarValue, Span, SpanRange, parse_clausewitz_content,
};
use serde::Serialize;

#[derive(Clone, Debug, Serialize)]
pub struct Argument {
	pub name: String,
	pub span: SourceSpan,
	pub value: Value,
}

#[derive(Clone, Debug, Serialize)]
pub struct Annotation {
	pub schema: &'static AnnotationSchema,
	pub span: SourceSpan,
	pub arguments: Vec<Argument>,
	/// Unnamed arguments, such as labels.
	pub positional: Vec<(String, SourceSpan)>,
}

impl Annotation {
	pub fn name(&self) -> &'static str {
		self.schema.name
	}

	pub fn get(&self, name: &str) -> Option<&Value> {
		self.arguments
			.iter()
			.find(|argument| argument.name == name)
			.map(|argument| &argument.value)
	}
}

/// A primary annotation with the modifiers written before it.
#[derive(Clone, Debug, Serialize)]
pub struct Applied {
	pub annotation: Annotation,
	pub modifiers: Vec<Annotation>,
}

#[derive(Clone, Debug, Serialize)]
pub struct Target {
	pub key: String,
	/// The definition's scalar `id` field, if it has one.
	pub id: Option<String>,
	pub span: SourceSpan,
}

#[derive(Clone, Debug, Serialize)]
pub struct Attachment {
	pub target: Target,
	pub annotations: Vec<Applied>,
}

#[derive(Clone, Debug, Default, Serialize)]
pub struct Extraction {
	pub attachments: Vec<Attachment>,
	pub diagnostics: Vec<Diagnostic>,
}

pub fn extract(path: &RelPath, text: &str, registry: &Registry) -> Extraction {
	let parsed = parse_clausewitz_content(game_path(path.as_str()), text);
	if !parsed.diagnostics.is_empty() {
		return Extraction {
			attachments: Vec::new(),
			diagnostics: parsed
				.diagnostics
				.into_iter()
				.map(|diagnostic| {
					Diagnostic::error(
						Code::Parse,
						SourceSpan::from_parser(path, &diagnostic.span),
						diagnostic.message,
					)
				})
				.collect(),
		};
	}
	extract_parsed(path, text, &parsed.ast.statements, registry)
}

/// Like [`extract`], for callers that already parsed `text`.
pub fn extract_parsed(
	path: &RelPath,
	text: &str,
	statements: &[AstStatement],
	registry: &Registry,
) -> Extraction {
	let mut out = Extraction::default();
	let mut modifiers: Vec<Annotation> = Vec::new();
	let mut primaries: Vec<Applied> = Vec::new();
	let error = |code, span: &SpanRange, message: String| {
		Diagnostic::error(code, SourceSpan::from_parser(path, span), message)
	};
	let mut index = 0;
	while index < statements.len() {
		match &statements[index] {
			AstStatement::Comment {
				text: comment,
				span,
			} => {
				let Some(schema) = annotation_schema(comment, registry) else {
					if let Some(name) = unknown_annotation(comment) {
						out.diagnostics.push(Diagnostic {
							severity: Severity::Warning,
							..error(
								Code::UnknownAnnotation,
								span,
								format!("#{name}(...) is not a known annotation"),
							)
						});
					}
					index += 1;
					continue;
				};
				let start = span.clone();
				let annotation = read_annotation(statements, &mut index, text, schema.name)
					.map_err(|message| vec![error(Code::Malformed, &start, message)])
					.and_then(|raw| parse_arguments(path, schema, raw));
				match annotation {
					Err(diagnostics) => out.diagnostics.extend(diagnostics),
					Ok(annotation) => match schema.kind {
						AnnotationKind::Modifier { .. } => modifiers.push(annotation),
						AnnotationKind::Primary => {
							let mut applied = Applied {
								annotation,
								modifiers: Vec::new(),
							};
							for modifier in modifiers.drain(..) {
								attach_modifier(&mut applied, modifier, &mut out.diagnostics);
							}
							primaries.push(applied);
						}
					},
				}
			}
			statement => {
				for modifier in modifiers.drain(..) {
					let of = match modifier.schema.kind {
						AnnotationKind::Modifier { of } => of,
						AnnotationKind::Primary => unreachable!("only modifiers wait"),
					};
					out.diagnostics.push(Diagnostic::error(
						Code::Orphan,
						modifier.span.clone(),
						format!("#{} must precede a #{of}", modifier.name()),
					));
				}
				if !primaries.is_empty() {
					let target = target(path, statement);
					let mut annotations = Vec::new();
					for applied in primaries.drain(..) {
						match check_target(statement, applied.annotation.schema) {
							Ok(()) => annotations.push(applied),
							Err(message) => out.diagnostics.push(Diagnostic::error(
								Code::UnsupportedTarget,
								applied.annotation.span.clone(),
								message,
							)),
						}
					}
					if !annotations.is_empty() {
						out.attachments.push(Attachment {
							target,
							annotations,
						});
					}
				}
				nested(path, statement, registry, &mut out.diagnostics);
				index += 1;
			}
		}
	}
	for annotation in modifiers
		.into_iter()
		.chain(primaries.into_iter().map(|applied| applied.annotation))
	{
		out.diagnostics.push(Diagnostic::error(
			Code::Orphan,
			annotation.span.clone(),
			format!("#{} has no following definition", annotation.name()),
		));
	}
	out
}

fn attach_modifier(applied: &mut Applied, modifier: Annotation, diagnostics: &mut Vec<Diagnostic>) {
	let primary = applied.annotation.name();
	if modifier.schema.kind != (AnnotationKind::Modifier { of: primary }) {
		diagnostics.push(Diagnostic::error(
			Code::Orphan,
			modifier.span.clone(),
			format!("#{} does not apply to #{primary}", modifier.name()),
		));
	} else if applied
		.modifiers
		.iter()
		.any(|existing| existing.name() == modifier.name())
	{
		diagnostics.push(Diagnostic::error(
			Code::DuplicateModifier,
			modifier.span.clone(),
			format!(
				"#{} is given twice for the same #{primary}",
				modifier.name()
			),
		));
	} else {
		applied.modifiers.push(modifier);
	}
}

/// The registered annotation a comment starts, if any. A bare primary name,
/// such as `#test` without arguments, is still recognized so it is reported
/// instead of silently ignored.
pub fn annotation_schema(comment: &str, registry: &Registry) -> Option<&'static AnnotationSchema> {
	let text = comment.trim_start();
	registry.schemas.iter().find(|schema| {
		text.strip_prefix(schema.name).is_some_and(|rest| {
			rest.trim_start().starts_with('(')
				|| (schema.kind == AnnotationKind::Primary && rest.trim().is_empty())
		})
	})
}

/// `#tset(...)`-like comments: a lowercase identifier immediately followed by `(`.
fn unknown_annotation(comment: &str) -> Option<&str> {
	let text = comment.trim_start();
	let name = &text[..text.find('(')?];
	(!name.is_empty()
		&& name
			.bytes()
			.all(|byte| byte.is_ascii_lowercase() || byte == b'_'))
	.then_some(name)
}

fn nested(
	path: &RelPath,
	statement: &AstStatement,
	registry: &Registry,
	out: &mut Vec<Diagnostic>,
) {
	let value = match statement {
		AstStatement::Assignment { value, .. } | AstStatement::Item { value, .. } => value,
		AstStatement::Comment { .. } => return,
	};
	if let AstValue::Block { items, .. } = value {
		for item in items {
			match item {
				AstStatement::Comment { text, span } => {
					if let Some(schema) = annotation_schema(text, registry) {
						out.push(Diagnostic::error(
							Code::Nested,
							SourceSpan::from_parser(path, span),
							format!("#{} must precede a top-level definition", schema.name),
						));
					}
				}
				_ => nested(path, item, registry, out),
			}
		}
	}
}

fn statement_span(statement: &AstStatement) -> &SpanRange {
	match statement {
		AstStatement::Assignment { span, .. }
		| AstStatement::Item { span, .. }
		| AstStatement::Comment { span, .. } => span,
	}
}

fn target(path: &RelPath, statement: &AstStatement) -> Target {
	let (key, id) = match statement {
		AstStatement::Assignment {
			key,
			value: AstValue::Block { items, .. },
			..
		} => (key.clone(), single_scalar(items, "id")),
		AstStatement::Assignment { key, .. } => (key.clone(), None),
		_ => (String::new(), None),
	};
	Target {
		key,
		id,
		span: SourceSpan::from_parser(path, statement_span(statement)),
	}
}

fn single_scalar(items: &[AstStatement], name: &str) -> Option<String> {
	let mut values = items.iter().filter_map(|item| match item {
		AstStatement::Assignment { key, value, .. } if key == name => Some(value),
		_ => None,
	});
	let value = values.next()?;
	values
		.next()
		.is_none()
		.then(|| scalar(value).ok())
		.flatten()
}

fn check_target(statement: &AstStatement, schema: &AnnotationSchema) -> Result<(), String> {
	let Some(rule) = schema.target else {
		return Ok(());
	};
	let AstStatement::Assignment {
		key,
		value: AstValue::Block { items, .. },
		..
	} = statement
	else {
		return Err(format!(
			"#{} must be followed by a {} definition",
			schema.name, rule.key
		));
	};
	if key != rule.key {
		return Err(format!("#{} supports {}, not {key}", schema.name, rule.key));
	}
	for field in rule.required_yes {
		let set = items
			.iter()
			.filter(|item| matches!(item, AstStatement::Assignment { key, .. } if key == field));
		let values: Vec<_> = set.collect();
		let yes = matches!(
			values.as_slice(),
			[AstStatement::Assignment {
				value: AstValue::Scalar {
					value: ScalarValue::Bool(true),
					..
				},
				..
			}]
		);
		if !yes {
			return Err(format!(
				"#{} requires the {} to declare {field} = yes exactly once",
				schema.name, rule.key
			));
		}
	}
	Ok(())
}

struct RawAnnotation {
	span: SpanRange,
	statements: Vec<AstStatement>,
	/// Annotation text at the original byte positions, blanks elsewhere.
	source: String,
}

fn parse_arguments(
	path: &RelPath,
	schema: &'static AnnotationSchema,
	raw: RawAnnotation,
) -> Result<Annotation, Vec<Diagnostic>> {
	let span = SourceSpan::from_parser(path, &raw.span);
	let mut diagnostics = Vec::new();
	let mut annotation = Annotation {
		schema,
		span: span.clone(),
		arguments: Vec::new(),
		positional: Vec::new(),
	};
	let mut given = Vec::new();
	for statement in &raw.statements {
		match statement {
			AstStatement::Comment { .. } => {}
			AstStatement::Assignment {
				key,
				key_span,
				value,
				..
			} => {
				let key_span = SourceSpan::from_parser(path, key_span);
				given.push(key.as_str());
				let Some(param) = schema.param(key) else {
					diagnostics.push(Diagnostic::error(
						Code::UnknownParameter,
						key_span,
						format!("unknown #{} parameter {key}", schema.name),
					));
					continue;
				};
				if given[..given.len() - 1].contains(&key.as_str()) {
					diagnostics.push(Diagnostic::error(
						Code::DuplicateParameter,
						key_span,
						format!("duplicate #{} parameter {key}", schema.name),
					));
					continue;
				}
				match parse_value(path, &raw.source, value, param.value_type) {
					Ok(parsed) => annotation.arguments.push(Argument {
						name: key.clone(),
						span: key_span,
						value: parsed,
					}),
					Err(message) => diagnostics.push(Diagnostic::error(
						Code::InvalidValue,
						SourceSpan::from_parser(path, value.span()),
						format!("{key}: {message}"),
					)),
				}
			}
			AstStatement::Item { value, span: item } => {
				let item_span = SourceSpan::from_parser(path, item);
				let label = scalar(value).ok().filter(|label| identifier(label));
				match (schema.positional, label) {
					(Some(_), Some(label)) => annotation.positional.push((label, item_span)),
					(Some(_), None) => diagnostics.push(Diagnostic::error(
						Code::InvalidValue,
						item_span,
						format!("#{} expects identifiers", schema.name),
					)),
					(None, _) => diagnostics.push(Diagnostic::error(
						Code::Malformed,
						item_span,
						format!("#{} expects named key=value arguments", schema.name),
					)),
				}
			}
		}
	}
	for param in schema.params.iter().filter(|param| param.required) {
		if !given.contains(&param.name) {
			diagnostics.push(Diagnostic::error(
				Code::MissingParameter,
				span.clone(),
				format!("missing required #{} parameter {}", schema.name, param.name),
			));
		}
	}
	if diagnostics.is_empty() {
		Ok(annotation)
	} else {
		Err(diagnostics)
	}
}

/// Reads one annotation, possibly continued over several full comment lines,
/// and parses its arguments as Clausewitz script at the original offsets.
fn read_annotation(
	statements: &[AstStatement],
	index: &mut usize,
	source: &str,
	name: &str,
) -> Result<RawAnnotation, String> {
	let AstStatement::Comment { span: start, .. } = &statements[*index] else {
		unreachable!("annotations start at a comment")
	};
	let first = *index;
	let start = start.clone();
	let mut virtual_source: Vec<u8> = source
		.bytes()
		.map(|byte| if byte == b'\n' { b'\n' } else { b' ' })
		.collect();
	let mut quote = false;
	let mut escape = false;
	let mut braces = 0usize;
	let mut end_offset = None;
	while *index < statements.len() {
		let AstStatement::Comment { span, .. } = &statements[*index] else {
			break;
		};
		let line_start = source[..span.start.offset]
			.rfind('\n')
			.map_or(0, |offset| offset + 1);
		if !source[line_start..span.start.offset].trim().is_empty() {
			*index += 1;
			return Err(format!(
				"#{name} and its continuation must occupy full comment lines"
			));
		}
		let mut content_start = span.start.offset + 1;
		if *index == first {
			let content = &source[content_start..span.end.offset];
			let rest = content
				.trim_start()
				.strip_prefix(name)
				.unwrap_or_default()
				.trim_start();
			if !rest.starts_with('(') {
				*index += 1;
				return Err(format!("expected #{name}(...)"));
			}
			content_start = span.end.offset - rest.len() + 1;
		}
		*index += 1;
		if span.end.offset - start.start.offset > 65_536 {
			return Err("annotation exceeds 64 KiB".into());
		}
		for offset in content_start..span.end.offset {
			let byte = source.as_bytes()[offset];
			if quote {
				virtual_source[offset] = byte;
				if escape {
					escape = false;
				} else if byte == b'\\' {
					escape = true;
				} else if byte == b'"' {
					quote = false;
				}
				continue;
			}
			match byte {
				b'#' => break,
				b'"' => {
					quote = true;
					virtual_source[offset] = byte;
				}
				b'{' => {
					braces += 1;
					virtual_source[offset] = byte;
				}
				b'}' => {
					braces = braces.checked_sub(1).ok_or("unmatched } in annotation")?;
					virtual_source[offset] = byte;
				}
				b')' if braces == 0 => {
					if !source[offset + 1..span.end.offset].trim().is_empty() {
						return Err(format!("unexpected text after #{name}(...)"));
					}
					end_offset = Some(offset + 1);
					break;
				}
				b'(' | b')' if braces == 0 => {
					return Err("unexpected parenthesis in annotation arguments".into());
				}
				b',' if braces == 0 => {}
				_ => virtual_source[offset] = byte,
			}
		}
		if end_offset.is_some() {
			break;
		}
	}
	let end_offset = end_offset.ok_or_else(|| {
		format!("unterminated #{name}(...); every continuation must be a comment")
	})?;
	virtual_source.truncate(end_offset);
	let virtual_source =
		String::from_utf8(virtual_source).map_err(|_| "invalid annotation encoding")?;
	let parsed = parse_clausewitz_content(game_path("annotation.txt"), &virtual_source);
	if let Some(error) = parsed.diagnostics.first() {
		return Err(format!(
			"invalid #{name} arguments at line {}: {}",
			error.span.start.line, error.message
		));
	}
	let end = Span {
		line: source[..end_offset]
			.bytes()
			.filter(|&byte| byte == b'\n')
			.count() + 1,
		column: end_offset
			- source[..end_offset]
				.rfind('\n')
				.map_or(0, |offset| offset + 1)
			+ 1,
		offset: end_offset,
	};
	Ok(RawAnnotation {
		span: SpanRange {
			start: start.start,
			end,
		},
		statements: parsed.ast.statements,
		source: virtual_source,
	})
}