Skip to content

Correct math unparsing - #7865

Merged
laurmaedje merged 6 commits into
typst:mainfrom
isuffix:correct-math-unparsing
Mar 31, 2026
Merged

laurmaedje merged 6 commits into
typst:mainfrom
isuffix:correct-math-unparsing

Conversation

@isuffix

@isuffix isuffix commented Feb 13, 2026 •

Copy link
Copy Markdown
Collaborator

This is now a normal PR targeting main
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.


As a quick refresher, recall these existing features of math arguments:

  • Empty arguments create empty content: $func(, , , )$
  • Semicolons create two-dimensional arguments: $mat(1, , ; ,2, ; , ,3;)$
  • Spread arguments require the expression to directly follows the two dots: $func(..spread, .. not-spread)$
  • Named arguments require the colon to directly follow the name: $func(named-param: value, not-named : X)$

This PR implements correct math unparsing by rewriting the entire math function call infrastructure and refactoring how we evaluate method function calls. It adds two new SyntaxKinds to distinguish math calling and math arguments, and now handles two-dimensional arguments in evaluation rather than parsing.

With my prior PR, #7742, this will fully resolve #7274.
#7524 is somewhat related, but is not resolved by this PR, although $sin(#1)$ does now work.

The first commit adds and updates many test cases for math function calls that will be improved in the final commit (although the test ordering in that file is still quite messy). The second commit moves some code in call.rs around to improve locality, which I prefer instead of putting all impl Trait blocks before all free functions. Finally, the third commit implements everything and updates the tests. I apologize that it is quite large, but everything in it depends on everything else, so one-commit it shall stay.

What is unparsing?

In Typst's math mode, we allow $vec(1, 2)$ to call the math.vec function, but we also allow $pi(1, 2)$ to render as the symbol pi next to (1, 2) as content. These two examples are syntactically indistinguishable, so we parse them into the same syntax tree and interpret that tree at runtime as either function arguments or content based on the type of the callee. To avoid parsing while evaluating, we currently fully parse the argument list as though we would call a function, and if we end up needing to render arguments as content, we "unparse" the argument structure and build content by manually inserting commas between arguments and wrapping in parentheses.

However, the old approach is wildly incomplete code-wise and suffers from architectural choices that make unparsing, named/spread argument syntax, and two-dimensional arguments fragile.

In the old approach:

  • Unparsing errors with any two-dimensional arguments: $pi(1; 2)$
  • Unparsing errors with any named arguments: $pi(x: y)$
  • Unparsing includes spread arguments after spreading them, so $pi(1, ..#none, 3)$ only renders one comma between 1 and 3
  • Unparsing does not respect spaces around arguments: #repr($pi(, | ,)$) vs. #repr($pi (, | ,)$)
  • Punctuation added in unparsing (commas and parens) is not given span information
  • Two-dimensional arguments error if named arguments are not directly at the beginning or directly behind a semicolon: $mat(1, delim: "[", 2;)$ and $mat(1, 2; 3, 4, delim: "[")$
  • Spread arguments with named components never work with two-dimensional arguments: $mat(..#(delim: "["), 1, 2; 3, 4)$
  • EDIT: I forgot to mention that unparsed integer arguments like in $sin(#1)$ would always fail because we try to cast them to content instead of displaying them like we do for $#1$

This PR makes almost all of these work correctly and produce the output you would expect.

The two exceptions are that we error (with useful hints) if we would unparse a call that uses either spread argument sytnax or named argument syntax ($ pi(..items) pi(param: y) $). This is because these features resemble code mode too closely and it would be confusing if this sytnax was allowed to be used with non-functions. However, this still follows the rules from above, where adding a single space before the colon or after the two dots will allow unparsing.

One thing that is not changing in this PR is that semicolons that directly follow embedded code expressions in arguments still attach to the code instead of creating two-dimensional arguments, e.g. $mat(#"abc"; #"def"; ;)$ only has one row. This may change, but not in this PR.

@isuffix
isuffix force-pushed the correct-math-unparsing branch from 804b39a to 7cdb8b0 Compare February 13, 2026 22:40
@isuffix

isuffix commented Feb 13, 2026 •

Copy link
Copy Markdown
Collaborator Author

One interesting note is that the new math-call-symbol-{named,spread}-argument tests are the first instances where a hint uses a different span than the error (besides convergence-position and convergence-page-supplement where the main error is Detached).

--- math-call-symbol-named-argument eval ---
// Error: 10-18 named-argument syntax can only be used with functions
// Hint: 3-6 `phi` is not a function
// Hint: 10-18 to render the colon as text, escape it: `alpha\: y`
$ phi(x, alpha: y) $

I tested, and this renders great on the command line, but it may be worth opening an issue in the WebApp repo if it doesn't render well there.

error: named-argument syntax can only be used with functions
  ┌─ <stdin>:1:9
  │
1 │ $ phi(x, alpha: y) $
  │   ---    ^^^^^^^^
  │   │       
  │   `phi` is not a function
  │
  = hint: to render the colon as text, escape it: `alpha\: y`

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

laurmaedje commented Feb 19, 2026 •

Copy link
Copy Markdown
Member

I tested, and this renders great on the command line, but it may be worth opening an issue in the WebApp repo if it doesn't render well there.

Aware of that! It's not yet well-handled in the dev version on the web app, but we want to deal with that before the next release.

besides convergence-position and convergence-page-supplement where the main error is Detached

The hint in convergence-position belongs to the "heading position did not stabilize" warning, which is not detached, so I think it's basically the same as yours (which looks lovely, btw).

@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.

Thanks for your work on this! It's apparent that you paid a lot of attention to the details. The approach seems very solid to me, I just have a few comments on a few design decisions and on a few edge cases in behavior.

Comment thread crates/typst-syntax/src/ast.rs Resolved
Comment thread crates/typst-syntax/src/parser.rs Resolved
Comment thread tests/suite/foundations/dict.typ Resolved
Comment thread tests/suite/math/call.typ Resolved
Comment thread tests/suite/math/call.typ Resolved
Comment thread crates/typst-eval/src/call.rs Resolved
Comment thread crates/typst-eval/src/call.rs Resolved
Comment thread crates/typst-eval/src/call.rs Resolved
drain_into_array(&mut pos, start, self.span());
}

named.extend(pos);

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.

since this changes the order, I wonder whether the order of named vs pos arguments is currently observable other than via repr. maybe actually not. but either way, we'd probably not want to give any guarantees on that anyway.

Comment thread crates/typst-syntax/src/ast.rs Resolved
Comment thread crates/typst-eval/src/call.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 Feb 19, 2026
@isuffix
isuffix force-pushed the correct-math-unparsing branch 7 times, most recently from cea12d3 to cf4cf19 Compare March 4, 2026 04:22
@isuffix

isuffix commented Mar 4, 2026 •

Copy link
Copy Markdown
Collaborator Author

Updates!

  • The rebases are a bit complex, but this difference link is the one to use if you want to see the overall diff from the previous version.

  • This is now dependent on Replace Default impls for AstNode with new placeholder method #7897 and includes it as the first commit.

  • I've finally bit the bullet and fully reorganized math/call.typ. It now separates sections more nicely, doesn't include -repr at the end of test names, and moves some separate tests out to interactions.typ and accent.typ.

    The only really notable difference other than the reorganization and naming is the removal of math-call-named-args. This test is fully covered by math-call-named-basic, and doesn't add anything so I'm dropping it.

  • A new commit follows the updates to math/call.typ with updates in scripting/methods.typ. I chose to keep this file and expand it massively. It now contains every test I've written for mutable methods everywhere, and duplicates most of them as math-method-* tests.

  • I split out and reworked the general changes to missing method errors into a new commit. You can now see how this impacts methods.typ separately from the main commit. I believe this address most of the review comments, and I'm ready for more 💪

  • I added another commit for a small change to errors for push/pop on dictionaries that I thought was nice.

  • In the main commit, I moved the implementation of Eval for MathCall into a free function, eval_math_call. This avoids future rebase conflicts by preempting the same change in my planned adjustments to Error on function literals in math #7866.

@isuffix
isuffix force-pushed the correct-math-unparsing branch from cf4cf19 to 632e146 Compare March 4, 2026 04:57
@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. and removed waiting-on-author Pull request waits on author labels Mar 4, 2026
@laurmaedje

Copy link
Copy Markdown
Member

Great! I'll take a look at the updates soon.

I merged #7897, so you might want to rebase math-syntax and this off of main and I can then reset math-syntax to the relevant hash.

@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.

As discussed, I still had some pending comments, but we basically discussed everything syncly already, so this is just for completeness.

On the core logic in call.rs, the changes look fine to me. I'd take one last look once we have finalized the error messages and hints, but otherwise it should be fine.

Let me know if further input from my side is needed!

Comment thread crates/typst-eval/src/call.rs Resolved
Comment thread tests/suite/scripting/methods.typ Resolved
Comment thread tests/suite/scripting/methods.typ Resolved
Comment thread tests/suite/scripting/ops.typ Resolved
Comment thread tests/suite/scripting/methods.typ 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 Mar 10, 2026
@isuffix
isuffix force-pushed the correct-math-unparsing branch 2 times, most recently from e1f7194 to c78e183 Compare March 25, 2026 02:11
@isuffix
isuffix force-pushed the correct-math-unparsing branch from 4c10172 to ab061d0 Compare March 30, 2026 21:40
@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. and removed waiting-on-author Pull request waits on author labels Mar 31, 2026
@laurmaedje
laurmaedje merged commit ab061d0 into typst:main Mar 31, 2026
8 checks passed
@laurmaedje

Copy link
Copy Markdown
Member

Thank you!

@laurmaedje laurmaedje removed the waiting-on-review This PR is waiting to be reviewed. label Mar 31, 2026
@isuffix
isuffix deleted the correct-math-unparsing branch March 31, 2026 16:46
ParaN3xus added a commit to ParaN3xus/typstyle that referenced this pull request May 14, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request May 15, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request May 15, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request May 24, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request May 24, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request Jun 1, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request Jun 1, 2026
ParaN3xus added a commit to ParaN3xus/typstyle that referenced this pull request Jun 4, 2026
QuadnucYard added a commit to typstyle-rs/typstyle that referenced this pull request Jun 6, 2026
commit 65301b2
Author: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Date:   Fri Jun 5 00:57:12 2026 +0000

    build: update tinymist to 0.14.23

commit 3ac7c69
Author: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Date:   Thu Jun 4 23:30:34 2026 +0000

    build: update tinymist to 0.14.23-rc1

commit 4f5efbf
Author: paran3xus <[email protected]>
Date:   Thu Jun 4 12:41:19 2026 +0000

    feat: add compatibility for typst/typst#7865 and typst/typst#8116

commit cb25b2e
Author: paran3xus <[email protected]>
Date:   Sat May 16 10:06:29 2026 +0000

    fix: adapt typstyle-consistency to typst/typst#7964

commit ae25cb3
Author: paran3xus <[email protected]>
Date:   Sat May 16 10:06:22 2026 +0000

    fix: adapt today to typst/typst#7691

commit c5702de
Author: paran3xus <[email protected]>
Date:   Sat May 16 05:14:33 2026 +0000

    build: patch typst-bundle

commit aeffa37
Author: paran3xus <[email protected]>
Date:   Fri May 15 05:09:16 2026 +0000

    build: bump tinymist-world to nightly (remote branch)

commit 8ea80b1
Author: paran3xus <[email protected]>
Date:   Thu May 14 05:08:26 2026 +0000

    build: bump typst to nightly (local patch)

    a
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin added a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 15, 2026
This PR is part of the Typst 0.15 nightly cleanup stack. It sits on top
of the path-model group and keeps this slice focused on analyzer, query,
debug, lint, eval, and typlite API adaptation.

## Upstream Typst Changes

- [`typst/typst#7542`](typst/typst#7542) changes
function internals. Tinymist updates signature, definition, hover, and
rename paths that inspect function internals.
- [`typst/typst#7543`](typst/typst#7543) moves
call sites toward `to_element`-style element handling. Tinymist updates
field access, completion kind inference, and syntax type checking
accordingly.
- [`typst/typst#7808`](typst/typst#7808) changes
parameter metadata. Tinymist updates signature help, built-in type
metadata, scope completion, and type completion to the new parameter
representation.
- [`typst/typst#8215`](typst/typst#8215)
requests the standard library from `World` once upfront. Tinymist
updates tracked values, upstream helpers, global analysis, DAP request
evaluation, and eval engine construction to use the preloaded library
path.
- [`typst/typst#7865`](typst/typst#7865),
[`typst/typst#8116`](typst/typst#8116), and
`typst/typst@5326cd297` adjust math call, math field, and prime syntax.
Tinymist updates syntax matchers, coverage instrumentation, debugger
instrumentation, lint behavior, expression parsing, and query fixtures.
- [`typst/typst#7490`](typst/typst#7490) and
`typst/typst@cfdd4238b` change font metadata APIs. Tinymist updates
document metrics and symbol glyph handling.
- `typst/typst@0f04fd176` changes diagnostic hints into spanned values.
Tinymist updates diagnostic hint rendering.
- [`typst/typst#7897`](typst/typst#7897)
introduces import item placeholders and related attach/text API changes.
Tinymist updates import completions and math text/attach matching.
- Typst 0.15 makes `typst::ROUTINES` private, adds the library argument
to `typst_eval::eval`, and protects engine introspection. Tinymist
updates `typst-shim` eval for both nightly and default feature builds.

## Tinymist Changes

- Ports analysis and query code to the updated function, element,
parameter, font, and math syntax APIs.
- Updates debug coverage and debugger instrumentation for the new math
call/field syntax shapes.
- Updates lint behavior so function-call and math-call handling stay
aligned.
- Updates DAP and `crityp` to the preloaded engine library path.
- Updates `typst-shim` eval and syntax compatibility modules so both
`nightly` and default feature builds compile against the current Typst
0.15 dependency set.
- Refreshes targeted query and typlite fixtures affected by these API
and syntax behavior changes.

---------

Co-authored-by: paran3xus <[email protected]>
Myriad-Dreamin added a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 15, 2026
This PR is part of the Typst 0.15 nightly cleanup stack. It sits on top
of the path-model group and keeps this slice focused on analyzer, query,
debug, lint, eval, and typlite API adaptation.

- [`typst/typst#7542`](typst/typst#7542) changes
function internals. Tinymist updates signature, definition, hover, and
rename paths that inspect function internals.
- [`typst/typst#7543`](typst/typst#7543) moves
call sites toward `to_element`-style element handling. Tinymist updates
field access, completion kind inference, and syntax type checking
accordingly.
- [`typst/typst#7808`](typst/typst#7808) changes
parameter metadata. Tinymist updates signature help, built-in type
metadata, scope completion, and type completion to the new parameter
representation.
- [`typst/typst#8215`](typst/typst#8215)
requests the standard library from `World` once upfront. Tinymist
updates tracked values, upstream helpers, global analysis, DAP request
evaluation, and eval engine construction to use the preloaded library
path.
- [`typst/typst#7865`](typst/typst#7865),
[`typst/typst#8116`](typst/typst#8116), and
`typst/typst@5326cd297` adjust math call, math field, and prime syntax.
Tinymist updates syntax matchers, coverage instrumentation, debugger
instrumentation, lint behavior, expression parsing, and query fixtures.
- [`typst/typst#7490`](typst/typst#7490) and
`typst/typst@cfdd4238b` change font metadata APIs. Tinymist updates
document metrics and symbol glyph handling.
- `typst/typst@0f04fd176` changes diagnostic hints into spanned values.
Tinymist updates diagnostic hint rendering.
- [`typst/typst#7897`](typst/typst#7897)
introduces import item placeholders and related attach/text API changes.
Tinymist updates import completions and math text/attach matching.
- Typst 0.15 makes `typst::ROUTINES` private, adds the library argument
to `typst_eval::eval`, and protects engine introspection. Tinymist
updates `typst-shim` eval for both nightly and default feature builds.

- Ports analysis and query code to the updated function, element,
parameter, font, and math syntax APIs.
- Updates debug coverage and debugger instrumentation for the new math
call/field syntax shapes.
- Updates lint behavior so function-call and math-call handling stay
aligned.
- Updates DAP and `crityp` to the preloaded engine library path.
- Updates `typst-shim` eval and syntax compatibility modules so both
`nightly` and default feature builds compile against the current Typst
0.15 dependency set.
- Refreshes targeted query and typlite fixtures affected by these API
and syntax behavior changes.

---------

Co-authored-by: paran3xus <[email protected]>
Myriad-Dreamin added a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 15, 2026
This PR is part of the Typst 0.15 nightly cleanup stack. It sits on top
of the path-model group and keeps this slice focused on analyzer, query,
debug, lint, eval, and typlite API adaptation.

- [`typst/typst#7542`](typst/typst#7542) changes
function internals. Tinymist updates signature, definition, hover, and
rename paths that inspect function internals.
- [`typst/typst#7543`](typst/typst#7543) moves
call sites toward `to_element`-style element handling. Tinymist updates
field access, completion kind inference, and syntax type checking
accordingly.
- [`typst/typst#7808`](typst/typst#7808) changes
parameter metadata. Tinymist updates signature help, built-in type
metadata, scope completion, and type completion to the new parameter
representation.
- [`typst/typst#8215`](typst/typst#8215)
requests the standard library from `World` once upfront. Tinymist
updates tracked values, upstream helpers, global analysis, DAP request
evaluation, and eval engine construction to use the preloaded library
path.
- [`typst/typst#7865`](typst/typst#7865),
[`typst/typst#8116`](typst/typst#8116), and
`typst/typst@5326cd297` adjust math call, math field, and prime syntax.
Tinymist updates syntax matchers, coverage instrumentation, debugger
instrumentation, lint behavior, expression parsing, and query fixtures.
- [`typst/typst#7490`](typst/typst#7490) and
`typst/typst@cfdd4238b` change font metadata APIs. Tinymist updates
document metrics and symbol glyph handling.
- `typst/typst@0f04fd176` changes diagnostic hints into spanned values.
Tinymist updates diagnostic hint rendering.
- [`typst/typst#7897`](typst/typst#7897)
introduces import item placeholders and related attach/text API changes.
Tinymist updates import completions and math text/attach matching.
- Typst 0.15 makes `typst::ROUTINES` private, adds the library argument
to `typst_eval::eval`, and protects engine introspection. Tinymist
updates `typst-shim` eval for both nightly and default feature builds.

- Ports analysis and query code to the updated function, element,
parameter, font, and math syntax APIs.
- Updates debug coverage and debugger instrumentation for the new math
call/field syntax shapes.
- Updates lint behavior so function-call and math-call handling stay
aligned.
- Updates DAP and `crityp` to the preloaded engine library path.
- Updates `typst-shim` eval and syntax compatibility modules so both
`nightly` and default feature builds compile against the current Typst
0.15 dependency set.
- Refreshes targeted query and typlite fixtures affected by these API
and syntax behavior changes.

---------

Co-authored-by: paran3xus <[email protected]>
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.

Problems with non-functions sharing the function call syntax in math mode

2 participants