Skip to content

Improve statement parsing: clear diagnostics, paren validation#1804

Open
has9sayed wants to merge 4 commits into
brian-team:masterfrom
has9sayed:parse_error
Open

Improve statement parsing: clear diagnostics, paren validation#1804
has9sayed wants to merge 4 commits into
brian-team:masterfrom
has9sayed:parse_error

Conversation

@has9sayed
Copy link
Copy Markdown
Contributor

@has9sayed has9sayed commented Mar 26, 2026

Enhance parse_statement with descriptive error messages (using pyparsing labels and the - operator), automatic balanced-parenthesis checking, and full support for # inside trailing comments. This replaces generic parsing failures with clear, caret-based error diagnostics.

For the wrong input v = (5 + 3 now it gives -

ERROR MSG:
Unbalanced parentheses in expression: '(5 + 3'
v = (5 + 3
    ^^^^^^

Copy link
Copy Markdown
Member

@mstimberg mstimberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks generally good to me! While testing, I noticed a small issue that was already present in the previous code, but it would make sense to fix it here as well. Statements like x = (missing RHS) fail parsing (as they should), but x = # missing expression will parse fine, using the comment as the expression.I think we could catch this quite easily here as well.

Comment thread brian2/parsing/statements.py
@mstimberg
Copy link
Copy Markdown
Member

Oh, only just realized that the test suite is failing. This is actually a silly issue, there is a test with a missing identifier (testing whether this is detected) which happens to also have a syntax error, i.e. unbalanced parentheses… Since these are now caught first, the error type is different. Please fix the unbalanced parentheses in that test:

def test_missing_lastupdate_error_run_regularly():
G = NeuronGroup(1, "v : 1")
S = Synapses(G, G)
S.connect()
S.run_regularly("v += exp(-lastupdate/dt")
with pytest.raises(BrianObjectException) as exc:
run(0 * ms)
assert exc_isinstance(exc, KeyError)
assert "lastupdate = t" in str(exc.value.__cause__)
assert "lastupdate : second" in str(exc.value.__cause__)

@has9sayed has9sayed requested a review from mstimberg April 2, 2026 13:50
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