Report parser locations in incomplete diagnostics
VSG fork CI VSG tests and package installation checks passed.
MadGobblins/vhdl-style-guide/pipeline/head This commit looks good Details

This commit is contained in:
Dandaedre 2026-10-03 21:16:18 +02:00
parent 4580a83ea1
commit b7dc18c157
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
5 changed files with 54 additions and 17 deletions

View File

@ -71,7 +71,7 @@ Focused fork tests cover:
- deterministic, versioned JSON and empty diagnostic collections;
- effective severity, fixability, documentation metadata, and Unicode text;
- invalid VHDL, invalid configuration, and unexpected failures with a valid
incomplete JSON document;
incomplete JSON document, including parser locations when available;
- rule selector parsing, validation, and deduplication;
- selected rules on the same line and across correction phases;
- corrections that insert lines; and

View File

@ -154,7 +154,9 @@ Fork for ``vsg_bridge_ls``
This fork adds stream integration options so VSG can serve editor buffers.
``--diagnostics_json`` reads VHDL from ``--stdin`` and always writes one
versioned JSON diagnostic document to standard output. Failed analysis is a
valid document marked incomplete rather than missing or malformed JSON.
valid document marked incomplete rather than missing or malformed JSON. Parser
failures include their best available one-based line and column so an editor can
replace stale style findings with a useful syntax error.
``--stdout`` writes fixed VHDL to standard output; if stdin cannot be parsed or
fixing otherwise fails, it returns the original input unchanged so callers get
a safe no-op rather than an empty replacement. Repeatable ``--fix_rule

View File

@ -178,6 +178,11 @@ class testDiagnosticsJson(unittest.TestCase):
self.assert_incomplete_document(oActual)
self.assertIn("Error while processing stdin", oActual.stderr)
dError = json.loads(oActual.stdout)["errors"][0]
self.assertEqual(dError["kind"], "syntax")
self.assertEqual(dError["location"], {"line": 1, "column": 1})
self.assertIn("Unexpected token while parsing entity_declaration", dError["message"])
self.assertIn("expected `end`", dError["message"])
def test_guard_replaces_invalid_json_and_unexpected_exceptions(self):
def invalid_json():

View File

@ -4,6 +4,7 @@
import contextlib
import io
import json
import re
import sys
from vsg.report import diagnostics_json
@ -40,14 +41,8 @@ def run(fMain):
if sStdout:
print(sStdout, file=sys.stderr)
if oFailure is not None:
sKind = "internal"
elif sStdout:
sKind = "protocol"
else:
sKind = "analysis"
sMessage = _failure_message(oFailure, sStderr)
dDocument = diagnostics_json.build_incomplete_document(sKind, sMessage)
sKind, sMessage, dLocation = _failure_details(oFailure, sStdout, sStderr)
dDocument = diagnostics_json.build_incomplete_document(sKind, sMessage, dLocation)
print(diagnostics_json.render(dDocument))
raise SystemExit(iExitStatus or 1)
@ -62,17 +57,49 @@ def _is_diagnostic_document(sOutput):
return isinstance(dDocument, dict) and dDocument.get("schema") == "vsg.diagnostics"
def _failure_message(oFailure, sStderr):
def _failure_details(oFailure, sStdout, sStderr):
if oFailure is not None:
sDetail = str(oFailure).strip()
if sDetail:
return "Diagnostic analysis raised " + oFailure.__class__.__name__ + ": " + sDetail
return "Diagnostic analysis raised " + oFailure.__class__.__name__
return "internal", "Diagnostic analysis raised " + oFailure.__class__.__name__ + ": " + sDetail, None
return "internal", "Diagnostic analysis raised " + oFailure.__class__.__name__, None
if sStdout:
return "protocol", "VSG did not produce a diagnostic document", None
oUnexpected = re.search(
r"Error: Unexpected token detected while parsing (?P<context>\S+) @ Line (?P<line>\d+), Column (?P<column>\d+)",
sStderr,
)
if oUnexpected is not None:
sExpected = _label_value(sStderr, "Expecting")
sFound = _label_value(sStderr, "Found")
sMessage = "Unexpected token while parsing " + oUnexpected.group("context")
if sExpected:
sMessage += "; expected `" + sExpected + "`"
if sFound:
sMessage += "; found `" + sFound + "`"
return (
"syntax",
sMessage,
{"line": int(oUnexpected.group("line")), "column": int(oUnexpected.group("column"))},
)
oMissing = re.search(r"Error: Closing token not found while parsing Line (?P<line>\d+)", sStderr)
if oMissing is not None:
return "syntax", "Closing token not found", {"line": int(oMissing.group("line")), "column": 1}
lLines = [sLine.strip() for sLine in sStderr.splitlines() if sLine.strip()]
if lLines:
return lLines[-1]
return "VSG did not produce a complete diagnostic document"
return "analysis", lLines[-1], None
return "analysis", "VSG did not produce a complete diagnostic document", None
def _label_value(sError, sLabel):
oMatch = re.search(r"^\s*" + re.escape(sLabel) + r"\s*:\s*(.*)$", sError, re.MULTILINE)
if oMatch is None:
return ""
return oMatch.group(1).strip()
def _exit_status(oCode):

View File

@ -43,8 +43,11 @@ def build_document(oRuleList):
}
def build_incomplete_document(sKind, sMessage):
def build_incomplete_document(sKind, sMessage, dLocation=None):
"""Builds a valid envelope when diagnostic analysis cannot complete."""
dError = {"kind": str(sKind), "message": str(sMessage)}
if dLocation is not None:
dError["location"] = dLocation
return {
"schema": "vsg.diagnostics",
"schema_version": 1,
@ -53,7 +56,7 @@ def build_incomplete_document(sKind, sMessage):
"source": {"name": "stdin"},
"rules": {},
"diagnostics": [],
"errors": [{"kind": str(sKind), "message": str(sMessage)}],
"errors": [dError],
}