Skip to content

Improve parser diagnostics for missing ':' in equation units#1798

Open
bluemoon-o2 wants to merge 2 commits intobrian-team:masterfrom
bluemoon-o2:fix/parser-error-diagnostics-1461
Open

Improve parser diagnostics for missing ':' in equation units#1798
bluemoon-o2 wants to merge 2 commits intobrian-team:masterfrom
bluemoon-o2:fix/parser-error-diagnostics-1461

Conversation

@bluemoon-o2
Copy link
Copy Markdown

@bluemoon-o2 bluemoon-o2 commented Mar 21, 2026

Summary

Improve equation parse diagnostics for model descriptions that miss : before unit declarations.

What Changed

  • Improved error formatting in parse_string_equations to provide clearer line-level diagnostics.
  • When parsing with ZeroOrMore(EQUATION) yields a degraded top-level error, re-parse the relevant line with EQUATION to recover specific expectations.
  • Added a targeted hint for likely missing unit separators:
    • Equation syntax error: expected ':' before unit declaration.
    • Includes a concrete example (dv/dt = -v / tau : 1).
  • Reduced brittle error detection by inspecting ParseException.parser_element metadata instead of relying on raw exception strings.
  • Kept grammar behavior unchanged; this PR only improves diagnostics.

Edge-Case Handling

  • Preserves specific non-unit errors (e.g. dv/d = ... still reports Expected 'dt').
  • Avoids missing-: hints for malformed RHS expression cases (e.g. x = ().
  • Covers both equation form and parameter form missing separators (e.g. tau second).

Tests

Added test_parse_error_messages coverage for:

  • Missing : in equation definitions.
  • Missing : in multiline/comment inputs.
  • dt typo should not be masked by missing-unit hints.
  • Malformed RHS should not trigger missing-unit hints.
  • Missing : in parameter declarations.
  • Unknown-unit-like token after expression (... foobaz) should still surface missing-unit hint.
  • Non-equation junk input should not trigger missing-unit hints.

Validation run:

  • pytest -q brian2/tests/test_equations.py
  • Result: 15 passed, 1 skipped

Refs #1461

Improve parse errors for model equation strings by generating more specific diagnostics when unit separators are missing.\n\n- recover non-degraded line-level parse errors from ZeroOrMore fallthroughs\n- add explicit hint for missing ':' before unit declarations\n- reduce fragile string matching by using ParseException parser metadata\n- avoid false positives for dt typos and malformed RHS expressions\n- add regression tests for missing-colon hints and non-hint error paths\n\nRefs brian-team#1461
@bluemoon-o2 bluemoon-o2 changed the title Improve equation parser diagnostics for missing ':' units Improve parser diagnostics for missing ':' in equation units Mar 21, 2026
@bluemoon-o2
Copy link
Copy Markdown
Author

For context, this PR overlaps with #1787 on improving missing-unit diagnostics, but intentionally keeps a narrower scope.

Compared to #1787, this PR focuses on a minimal fix path for #1461:

  • clearer diagnostics for missing : before units,
  • preservation of specific non-unit errors,
  • targeted regression tests.

It does not include broader parser refactors or new equation metadata/features. If preferred, I can follow up with separate PRs for additional diagnostics after this lands.

@mstimberg
Copy link
Copy Markdown
Member

Hi @bluemoon-o2. Could you please use the pre-commit tool to fix the formatting of the file (as described here: https://brian2.readthedocs.io/en/stable/developer/guidelines/style.html#code-style) so that our linter is happy and the test suite can run? Thanks!

@bluemoon-o2
Copy link
Copy Markdown
Author

bluemoon-o2 commented Mar 31, 2026

Thanks for the reminder! I ran pre-commit on all files and pushed the formatting fix in commit a2d6a41.

I also re-ran the relevant tests (...............s [100%]
=============================== warnings summary ===============================
../../miniconda3/lib/python3.13/site-packages/_pytest/config/init.py:1428
/home/bluemoon/miniconda3/lib/python3.13/site-packages/_pytest/config/init.py:1428: PytestConfigWarning: Unknown config option: timeout

self._warn_or_fail_if_strict(f"Unknown config option: {key}\n")

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
15 passed, 1 skipped, 1 warning in 0.69s), which pass (15 passed, 1 skipped).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants