Skip to content

Clearer error message for invalid UCUM units#76

Merged
dalito merged 1 commit into
mainfrom
clearer-parse-errors
Jun 4, 2026
Merged

Clearer error message for invalid UCUM units#76
dalito merged 1 commit into
mainfrom
clearer-parse-errors

Conversation

@dalito

@dalito dalito commented Jun 4, 2026

Copy link
Copy Markdown
Owner

Invalid UCUM codes previously surfaced lark's raw parser error, which leaks grammar-internal terminal names (ANNOTATION, EXPONENT, ...) and never states that the unit is invalid:

No terminal matches '[' in the current parser context, at line 1 col 3

dB[mW]
  ^
Expected one of:
	* ANNOTATION
	* EXPONENT
	* OPERATOR

This adds parse_ucum() and an InvalidUcumError, used by from_ucum() and ucum_preprocessor, producing a domain-focused message that names the unit and keeps the caret:

'dB[mW]' is not a valid UCUM unit: unexpected character '[' at column 3.
dB[mW]
  ^

Truncated input is reported too:

'm/' is not a valid UCUM unit: unexpected end of input.
m/
 ^

InvalidUcumError subclasses lark.exceptions.LarkError, so existing except LarkError handlers keep working. The interactive CLI is left as-is (it already catches UnexpectedInput).

Discovered while fixing #62 (the invalid dB[mW] case).

@dalito

dalito commented Jun 4, 2026

Copy link
Copy Markdown
Owner Author

Related: #75 (Fix decibel (dB) parsing of logarithmic units). The need for a clearer parse error surfaced while investigating the invalid dB[mW] case.

@dalito dalito force-pushed the clearer-parse-errors branch 2 times, most recently from 5a3c6b0 to 9f26974 Compare June 4, 2026 18:44
Invalid UCUM codes surfaced lark's raw UnexpectedInput, leaking grammar
terminal names (ANNOTATION, EXPONENT, ...) and never stating that the
unit is invalid. Add parse_ucum(), used by from_ucum and the
preprocessor, which raises InvalidUcumError naming the unit and pointing
at the failure. It subclasses LarkError so existing handlers keep working.
@dalito dalito force-pushed the clearer-parse-errors branch from 9f26974 to 10016c2 Compare June 4, 2026 19:03
@dalito dalito merged commit 6ad55c4 into main Jun 4, 2026
5 checks passed
@dalito dalito deleted the clearer-parse-errors branch June 4, 2026 19:17
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.

1 participant