Skip to content

Improve errors for incomplete math operators and disallow linebreaks in operands - #7742

Merged
laurmaedje merged 3 commits into
typst:math-syntaxfrom
isuffix:math-op-incomplete
Mar 2, 2026
Merged

laurmaedje merged 3 commits into
typst:math-syntaxfrom
isuffix:math-op-incomplete

Conversation

@isuffix

@isuffix isuffix commented Jan 22, 2026

Copy link
Copy Markdown
Collaborator

Part of the Math Syntax Refactor
This PR is part of the exhaustive math syntax refactor. It should be considered exploratory, and not necessarily a final decision, even if merged to the math-syntax branch. Non-specific feedback for this change should go in one of its related issues.


This PR tightens up our handling of general operators in math (primes will be dealt with separately).

Closes #1435

First commit

In this commit, we now error more predictably when an operator has a missing side, and the error message we give is much better. This resolves the odd behavior of $(1/)$ parsing the close-paren as the fraction's denominator, now giving the error, "expected an expression to the right of the operator".

I implemented this by making better use of the stop_set argument in math_expr_prec, ensuring that we thread it through recursive operator calls when parsing an operator's right operand, and erroring if we start the call at the stop set.

This also improves the error for unclosed delimiters used for grouping. So $($ alone is still fine, but $1/($ is an error. This was done by passing in the precedence of the surrounding expression to math_delimited and checking that it was higher than zero if we didn't find a closing delimiter.

And I removed the MATH_EXPR syntax set because the unexpected error message case was really only used for these operators, and I would prefer to give a more explicit error for them.

Second commit

This is simpler and just disallows linebreaks and alignment points as direct operands, requiring parentheses or escaping to place them inside a math operator. Doing so on their own was never actually useful, so this should just improve the editing experience in math.

I did this by adding Linebreak and MathAlignPoint to a new TOP_LEVEL syntax set in math_exprs that we handle explicitly at the top-level, but treat as part of the stop set for all calls to math_expr_prec. This causes us to error whenever an operator would be adjacent to one of these kinds, for the same reason as we error for $/$, but it does not change the behavior if these would just join as part of the top-level math sequence.

@isuffix isuffix added math Related to math category, with its syntax, layout, etc. syntax About syntax, parsing, etc. labels Jan 22, 2026
@laurmaedje laurmaedje added the waiting-on-review This PR is waiting to be reviewed. label Jan 26, 2026

@laurmaedje laurmaedje left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just played around with this. It's very nice. :)

Small note: We should probably avoid the "Closes X" syntax until things have actually landed in main.

Comment thread crates/typst-syntax/src/parser.rs Resolved
Comment thread tests/suite/math/call.typ Resolved
Comment thread crates/typst-syntax/src/parser.rs Resolved
Comment thread crates/typst-syntax/src/parser.rs Resolved
@laurmaedje laurmaedje added waiting-on-author Pull request waits on author and removed waiting-on-review This PR is waiting to be reviewed. labels Jan 27, 2026
@isuffix
isuffix force-pushed the math-op-incomplete branch from 7eec090 to 707b574 Compare January 29, 2026 16:32
@isuffix
isuffix force-pushed the math-op-incomplete branch 2 times, most recently from 7df8fe1 to 72a3e44 Compare February 10, 2026 00:51
@isuffix

isuffix commented Feb 10, 2026

Copy link
Copy Markdown
Collaborator Author

Oof, sorry 😬. We'll need to rebase math-syntax in typst/typst to get these commits to go away

@laurmaedje

Copy link
Copy Markdown
Member

I updated the math-syntax branch to the latest main.

@isuffix isuffix mentioned this pull request Feb 13, 2026
@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. and removed waiting-on-author Pull request waits on author labels Feb 18, 2026
@laurmaedje laurmaedje removed the waiting-on-review This PR is waiting to be reviewed. label Feb 19, 2026
@laurmaedje
laurmaedje merged commit d92f1af into typst:math-syntax Mar 2, 2026
8 checks passed
@laurmaedje

Copy link
Copy Markdown
Member

Thank you!

@isuffix
isuffix deleted the math-op-incomplete branch March 2, 2026 20:14
@Enivex Enivex mentioned this pull request May 7, 2026
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

math Related to math category, with its syntax, layout, etc. syntax About syntax, parsing, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants