lean-runtime 0.6.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,52 @@
1
+ """Best-effort normalization of Lean's version-dependent diagnostics."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import re
6
+
7
+ from .models import Diagnostic, Severity
8
+
9
+ _HEADER = re.compile(
10
+ r"^(?P<file>.*?):(?P<line>\d+):(?P<column>\d+): "
11
+ r"(?P<severity>error|warning|information):\s*(?P<message>.*)$"
12
+ )
13
+
14
+
15
+ def parse_diagnostics(output: str) -> tuple[Diagnostic, ...]:
16
+ """Parse ordinary Lean diagnostic headers and their continuation lines.
17
+
18
+ Lean does not expose one stable machine-readable diagnostic format across
19
+ all supported releases. Unknown output remains available verbatim on the
20
+ execution result rather than being discarded.
21
+ """
22
+ diagnostics: list[Diagnostic] = []
23
+ current: dict[str, object] | None = None
24
+
25
+ def finish() -> None:
26
+ nonlocal current
27
+ if current is not None:
28
+ diagnostics.append(Diagnostic(**current)) # type: ignore[arg-type]
29
+ current = None
30
+
31
+ for line in output.splitlines():
32
+ match = _HEADER.match(line)
33
+ if match:
34
+ finish()
35
+ current = {
36
+ "severity": match.group("severity"),
37
+ "message": match.group("message"),
38
+ "file": match.group("file"),
39
+ "line": int(match.group("line")),
40
+ "column": int(match.group("column")),
41
+ }
42
+ elif current is not None:
43
+ message = str(current["message"])
44
+ current["message"] = f"{message}\n{line}" if line else message
45
+ finish()
46
+ return tuple(diagnostics)
47
+
48
+
49
+ def error_diagnostic(message: str) -> Diagnostic:
50
+ """Create an infrastructure diagnostic."""
51
+ severity: Severity = "error"
52
+ return Diagnostic(severity=severity, message=message)