diff --git a/DISCLOSURE.md b/DISCLOSURE.md index 79bd6a2c..9309f24d 100644 --- a/DISCLOSURE.md +++ b/DISCLOSURE.md @@ -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 diff --git a/README.rst b/README.rst index a32f9da4..537d756c 100644 --- a/README.rst +++ b/README.rst @@ -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 diff --git a/tests/diagnostics_json/test_diagnostics_json.py b/tests/diagnostics_json/test_diagnostics_json.py index 063635e1..d51a6e25 100644 --- a/tests/diagnostics_json/test_diagnostics_json.py +++ b/tests/diagnostics_json/test_diagnostics_json.py @@ -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(): diff --git a/vsg/diagnostic_runtime.py b/vsg/diagnostic_runtime.py index 270a8b54..ff9dce61 100644 --- a/vsg/diagnostic_runtime.py +++ b/vsg/diagnostic_runtime.py @@ -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\S+) @ Line (?P\d+), Column (?P\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\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): diff --git a/vsg/report/diagnostics_json.py b/vsg/report/diagnostics_json.py index 5e3573a0..7c729b61 100644 --- a/vsg/report/diagnostics_json.py +++ b/vsg/report/diagnostics_json.py @@ -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], }