Skip to content

API reference

The importable Python surface: the pure diagnostic engine, which any tool can call in-process, and the server glue that maps its results to lsprotocol and publishes them. Generated from the docstrings by mkdocstrings.

bankstatementparser_lsp.diagnostics

Pure diagnostic engine for MT940 bank statement documents.

No LSP or editor dependencies, so it is unit-testable in isolation. Given the raw text of an MT940 (.mt940/.sta) file it returns a list of :class:Diagnostic objects locating missing mandatory tags and malformed balance / statement lines, using the same tag patterns the bankstatementparser MT940 parser relies on.

Severity

Bases: IntEnum

Diagnostic severity, matching the LSP numeric scale.

Source code in bankstatementparser_lsp/diagnostics.py
34
35
36
37
38
39
40
class Severity(IntEnum):
    """Diagnostic severity, matching the LSP numeric scale."""

    ERROR = 1
    WARNING = 2
    INFORMATION = 3
    HINT = 4

Diagnostic dataclass

A single editor diagnostic over a 0-based line/character range.

Attributes:

Name Type Description
line int

Zero-based line index of the affected text.

col_start int

Zero-based start character on the line.

col_end int

Zero-based end character (exclusive) on the line.

severity Severity

Diagnostic severity.

message str

Human-readable description of the problem.

code str

Stable machine-readable rule identifier.

Source code in bankstatementparser_lsp/diagnostics.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
@dataclass(frozen=True)
class Diagnostic:
    """A single editor diagnostic over a 0-based line/character range.

    Attributes:
        line: Zero-based line index of the affected text.
        col_start: Zero-based start character on the line.
        col_end: Zero-based end character (exclusive) on the line.
        severity: Diagnostic severity.
        message: Human-readable description of the problem.
        code: Stable machine-readable rule identifier.
    """

    line: int
    col_start: int
    col_end: int
    severity: Severity
    message: str
    code: str

diagnostics_for_mt940(text)

Lint an MT940 statement document and return diagnostics.

Parameters:

Name Type Description Default
text str

The full text of the MT940 document.

required

Returns:

Type Description
list[Diagnostic]

A list of :class:Diagnostic objects (empty when the document

list[Diagnostic]

is clean or has no content).

Source code in bankstatementparser_lsp/diagnostics.py
 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
def diagnostics_for_mt940(text: str) -> list[Diagnostic]:
    """Lint an MT940 statement document and return diagnostics.

    Args:
        text: The full text of the MT940 document.

    Returns:
        A list of :class:`Diagnostic` objects (empty when the document
        is clean or has no content).
    """
    # A leading UTF-8 BOM (U+FEFF) is not whitespace, so without this it
    # would cling to the first tag (e.g. ``:20:``) and be misread as
    # a missing :20:. Strip a single leading BOM before scanning.
    if text.startswith(""):
        text = text[1:]
    lines = text.splitlines()
    if not any(line.strip() for line in lines):
        return []

    diagnostics: list[Diagnostic] = []
    seen_tags: set[str] = set()
    seen_statement = False

    for index, raw_line in enumerate(lines):
        stripped = raw_line.strip()
        if not stripped:
            continue
        tag = _tag_of(stripped)
        if tag is not None:
            seen_tags.add(tag)

        if tag in (":60F:", ":62F:") and not _BALANCE_RE.match(stripped):
            diagnostics.append(
                Diagnostic(
                    line=index,
                    col_start=0,
                    col_end=len(raw_line),
                    severity=Severity.ERROR,
                    message=(
                        f"Malformed balance line {tag} — expected "
                        "C/D + YYMMDD + 3-letter currency + amount"
                    ),
                    code="malformed-balance",
                )
            )
        elif tag == ":61:":
            if _STATEMENT_RE.match(stripped):
                seen_statement = True
            else:
                diagnostics.append(
                    Diagnostic(
                        line=index,
                        col_start=0,
                        col_end=len(raw_line),
                        severity=Severity.ERROR,
                        message=(
                            "Malformed :61: statement line — expected "
                            "YYMMDD + C/D + amount"
                        ),
                        code="malformed-statement-line",
                    )
                )
        elif tag == ":86:" and not seen_statement:
            diagnostics.append(
                Diagnostic(
                    line=index,
                    col_start=0,
                    col_end=len(raw_line),
                    severity=Severity.WARNING,
                    message=(
                        ":86: information line has no preceding :61: "
                        "statement line; it will be ignored"
                    ),
                    code="orphan-information-line",
                )
            )

    for tag in _REQUIRED_TAGS:
        if tag not in seen_tags:
            diagnostics.append(
                Diagnostic(
                    line=0,
                    col_start=0,
                    col_end=len(lines[0]),
                    severity=Severity.ERROR,
                    message=f"Missing mandatory MT940 tag: {tag}",
                    code="missing-tag",
                )
            )
    return diagnostics

bankstatementparser_lsp.server

Language Server (stdio) for MT940 bank statement files.

Wraps the pure :mod:bankstatementparser_lsp.diagnostics engine in a pygls server so editors get live missing-tag and malformed-line diagnostics as they type. Run it with bankstatementparser-lsp.

main()

Start the language server over stdio.

Source code in bankstatementparser_lsp/server.py
102
103
104
def main() -> None:
    """Start the language server over stdio."""
    server.start_io()

did_open(ls, params)

Lint a document when it is opened.

Parameters:

Name Type Description Default
ls LanguageServer

The active language server.

required
params DidOpenTextDocumentParams

The open-document notification parameters.

required
Source code in bankstatementparser_lsp/server.py
76
77
78
79
80
81
82
83
84
85
86
@server.feature(lsp.TEXT_DOCUMENT_DID_OPEN)
def did_open(
    ls: LanguageServer, params: lsp.DidOpenTextDocumentParams
) -> None:
    """Lint a document when it is opened.

    Args:
        ls: The active language server.
        params: The open-document notification parameters.
    """
    _publish(ls, params.text_document.uri)

did_change(ls, params)

Re-lint a document when it changes.

Parameters:

Name Type Description Default
ls LanguageServer

The active language server.

required
params DidChangeTextDocumentParams

The change-document notification parameters.

required
Source code in bankstatementparser_lsp/server.py
89
90
91
92
93
94
95
96
97
98
99
@server.feature(lsp.TEXT_DOCUMENT_DID_CHANGE)
def did_change(
    ls: LanguageServer, params: lsp.DidChangeTextDocumentParams
) -> None:
    """Re-lint a document when it changes.

    Args:
        ls: The active language server.
        params: The change-document notification parameters.
    """
    _publish(ls, params.text_document.uri)

_to_lsp_diagnostic(diagnostic)

Convert an internal diagnostic to an LSP diagnostic.

Parameters:

Name Type Description Default
diagnostic Diagnostic

The engine-produced diagnostic.

required

Returns:

Type Description
Diagnostic

The equivalent lsprotocol diagnostic.

Source code in bankstatementparser_lsp/server.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def _to_lsp_diagnostic(diagnostic: Diagnostic) -> lsp.Diagnostic:
    """Convert an internal diagnostic to an LSP diagnostic.

    Args:
        diagnostic: The engine-produced diagnostic.

    Returns:
        The equivalent ``lsprotocol`` diagnostic.
    """
    return lsp.Diagnostic(
        range=lsp.Range(
            start=lsp.Position(
                line=diagnostic.line, character=diagnostic.col_start
            ),
            end=lsp.Position(
                line=diagnostic.line, character=diagnostic.col_end
            ),
        ),
        message=diagnostic.message,
        severity=lsp.DiagnosticSeverity(diagnostic.severity.value),
        code=diagnostic.code,
        source="bankstatementparser",
    )

_publish(ls, uri)

Lint a document and publish its diagnostics to the client.

Parameters:

Name Type Description Default
ls LanguageServer

The active language server.

required
uri str

URI of the document to lint.

required
Source code in bankstatementparser_lsp/server.py
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def _publish(ls: LanguageServer, uri: str) -> None:
    """Lint a document and publish its diagnostics to the client.

    Args:
        ls: The active language server.
        uri: URI of the document to lint.
    """
    document = ls.workspace.get_text_document(uri)
    diagnostics = [
        _to_lsp_diagnostic(d) for d in diagnostics_for_mt940(document.source)
    ]
    ls.text_document_publish_diagnostics(
        lsp.PublishDiagnosticsParams(uri=uri, diagnostics=diagnostics)
    )