Skip to content

String interpolation - #7620

Open
tingerrr wants to merge 7 commits into
typst:mainfrom
tingerrr:tinger/otksvqxpzqwx
Open

tingerrr wants to merge 7 commits into
typst:mainfrom
tingerrr:tinger/otksvqxpzqwx

Conversation

@tingerrr

@tingerrr tingerrr commented Dec 22, 2025 •

Copy link
Copy Markdown
Contributor

This is a draft, I only opened this because it's easier to check out this way. The exact syntax and semantics for non-string values will be discussed in the syntax forge.

Resolves #2267.

@tingerrr
tingerrr force-pushed the tinger/otksvqxpzqwx branch from e8e6ef1 to 28e384d Compare December 22, 2025 16:32
@tingerrr
tingerrr force-pushed the tinger/otksvqxpzqwx branch from 28e384d to 6c1e968 Compare January 2, 2026 15:17
@laurmaedje laurmaedje added scripting About Typst's coding capabilities syntax About syntax, parsing, etc. interface PRs that add to or change Typst's user-facing interface as opposed to internals or docs changes. labels Jan 5, 2026
@laurmaedje laurmaedje added the waiting-on-author Pull request waits on author label Feb 9, 2026
@tingerrr
tingerrr force-pushed the tinger/otksvqxpzqwx branch from 6c1e968 to f9a39be Compare May 25, 2026 09:00
@tingerrr
tingerrr force-pushed the tinger/otksvqxpzqwx branch 5 times, most recently from 136cdea to 11aba8a Compare May 31, 2026 09:58
@laurmaedje

Copy link
Copy Markdown
Member

@tingerrr I didn't leave any reviews thus far since this is still marked as draft. Do you still have plans to work on this? If yes, we can keep it open, but I'm going through older PRs trying to establish which ones may actually still move forward.

@tingerrr

Copy link
Copy Markdown
Contributor Author

Yeah, I do plan to finish this, but if somebody else wants to base their work of this and finish it, I wouldn't mind either.

@tingerrr
tingerrr force-pushed the tinger/otksvqxpzqwx branch 9 times, most recently from a6eadca to 5383bc9 Compare August 9, 2026 18:06
@tingerrr

tingerrr commented Aug 9, 2026

Copy link
Copy Markdown
Contributor Author

I think this can be reviewed now. As always, I suggest going commit by commit, note that some of the commits in this branch are actually part of #7800, so you may want to look at this first.

If any of the commit messages are unclear let me know, but I hope they explain my reasoning behind the structure of this patch.

@tingerrr
tingerrr marked this pull request as ready for review August 9, 2026 18:09
@tingerrr tingerrr changed the title Draft: String interpolation String interpolation Aug 9, 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 Aug 10, 2026
@tingerrr

Copy link
Copy Markdown
Contributor Author

Looks like this needs some conflicts resolved, I'll hopefully get to this during the next weekend. Not sure if you want to set this to wating-on-author for the time being.

@laurmaedje

Copy link
Copy Markdown
Member

No, it's not really waiting on you after all since the conflicts grew out of review not happening for so long.

@tingerrr

Copy link
Copy Markdown
Contributor Author

I noticed a few issues while resolving those, so there was an upside to it.

@tingerrr
tingerrr force-pushed the tinger/otksvqxpzqwx branch 3 times, most recently from e047f18 to 665cd03 Compare October 18, 2026 10:30
@tingerrr

Copy link
Copy Markdown
Contributor Author

Ok, I've updated this and the PR it depends on. I noticed during the conflict resolution that the dependencies reported for typst watch were spitting out manifests for project roots too, so I had to move that around first.

@tingerrr
tingerrr force-pushed the tinger/otksvqxpzqwx branch 5 times, most recently from a92cf2b to 0281518 Compare October 18, 2026 12:03
@tingerrr

tingerrr commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor Author

I ran into a linker failure on Windows, which I can't really test locally. So I forced a rebuild (hopefully with a fresh cargo cache) by updating the workspace manifest and reverting it again. If that persists, then I'm not sure how to approach this. The failure seems to indicate that the Windows runner can't find the libfuzzer symbol in the fuzzer library itself.

EDIT: Turns out it seems to have been a problem before too, but just not triggered until you import something in the fuzz/lib.rs file from libfuzzer_sys. I've added that as a separate commit here.

@tingerrr
tingerrr force-pushed the tinger/otksvqxpzqwx branch from 0281518 to 299a496 Compare October 18, 2026 12:39
@tingerrr
tingerrr force-pushed the tinger/otksvqxpzqwx branch 3 times, most recently from 4cd8e28 to 7a1dde5 Compare October 29, 2026 22:02
/// Currently, you'll have to do this manually (e.g. by resetting counters,
/// adjusting selectors, etc.) We
/// #link("https://github.com/typst/typst/issues/7735#issuecomment-3908841853")[plan
/// #link("https://github.com/typst/typst/issues/7735\#issuecomment-3908841853")[plan

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I can't wait for this string interpolation feature (really nice work!) but this is rather unfortunate 🫤 did the team/community discuss other designs like enforcing #{something} and disallowing #something not to break the current links? Or maybe allowing raw wherever str is expected (which would also be a nice quality-of-life feature for asset/html.script/etc) to use #link(`url#fragment`) instead of #link("url\#fragment")?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right now I went with the same rules as # in markdown and math for consistency. There was definitely some discussion about it being a breaking change, which is why I also drafted the compatibility mechanism proposed in https://laurmaedje.github.io/posts/evolving-typst/ and based this one on it as a proof of concept.

I also thought about the idea of allowing raw wherever string is allowed, but I haven't thought much about what this could mean otherwise. For example, I always thought that raw syntax would be cool as an alternative to bytes for things that take either a file path or content like bibliography does. Allowing raw in place of strings would prevent that, but perhaps that's not so bad since we got bytes for this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I also thought about the idea of allowing raw wherever string is allowed, but I haven't thought much about what this could mean otherwise. For example, I always thought that raw syntax would be cool as an alternative to bytes for things that take either a file path or content like bibliography does. Allowing raw in place of strings would prevent that, but perhaps that's not so bad since we got bytes for this.

Maybe we can use raw as str only in link which expects a URL (possibly with a URL fragment) and raw as bytes wherever we can pass bytes (bibliography, asset...), as str in those cases would be a path which shouldn't contain #? I guess it would be too "smart" of a feature, but still very convenient:

  • link("url\#fragment")
  • link(`url#fragment`)
  • bibliography("bib.yaml")
  • bibliography(```yaml ...```)
  • asset("styles.css")
  • asset(```css ...```)

The preferred compiler version set within a `typst.toml` manifest
file applies to every file in the root this manifest is contained in.
This preferred version can then be used to opt into limited backwards
compatiblity guarantees.

For example, a future version of typst may introduce custom types,
which require dedicated syntax like a `type` keyword. To avoid making
every old version of a package break instantly and forcing the whole
ecosystem to migrate effectively at once, the compatibilty feature could
retain the old parsing behavior in packages. This allows projects and
packages to target new typst version without requiring updates to their
dependencies immedately.
By adding a string parsing mode before implementing string interpolation
we already gain better highlighting for string escapes and a base
line from which we can make it conditional without disturbing the AST
structure too much.

This subtly changes the structure for tests which directly depend on raw
highlighting of typst code itself, as the strings are emitted in parts
instead of a while. However, the overall positioning is left intact.

I deliberately kept this patch from introducing visible changes in the
output to keep the structural change sound before actually adding string
interpolation.
Escape sequences in string such as `\n` or `\u{1b}` now get highlighted
the same as markup escape sequences.
By making hashes no longer optional and removing them from the
representation we remove the most common usage of `#` in strings in
prepration for string interpolation.
The binaries likely already have the test and doc targets disabled to
fix this exact issue. Although it doesn't explicitly state this, there's
strong evidence at:
https://rust-fuzz.github.io/book/cargo-fuzz/windows/dll-fuzzing.html#msvc-and-libfuzzers-main-function

This seems to be triggered for the library only once you actually import
symbols from `libfuzzer_sys`.
@tingerrr
tingerrr force-pushed the tinger/otksvqxpzqwx branch from 3868633 to 729d5a6 Compare November 28, 2026 18:00
String interpolation is a syntax feature with which values can be
conveniently embedded into strings. It works similar to placing values
in document markup. It can also be though of as syntax sugar for
concatenating the string representation of values into pure string
literals, but the string representation follows slightly different
conversion rules than just `str` or `repr`.

For example, the string representation chosen for an array `a` may be
the same as `repr(a)`, but the string representation of a string `s` is
just `s`.
@tingerrr
tingerrr force-pushed the tinger/otksvqxpzqwx branch from 729d5a6 to e6c5c19 Compare November 28, 2026 18:00
@tingerrr

Copy link
Copy Markdown
Contributor Author

Here's extra context from the discord server:

  • tinger [JŒVR], — 10/29/26, 10:54 PM

    @isuffix I wonder if you have any thoughts about String interpolation #7620, especially the reparser code, right now the logic of where the version and thus the parsing behavior is set never changes for a certain root, but you must still pass it down for the parser/reparser to know how to parse things and I don't like having a parameter that is essentially "decide once and then always pass the same value down". For parsing it makes sense to pass it down, but for reparsing I'd rather store it on the root node or so, but that comment about the wasted 8 bytes from lifting SyntaxKind tells me the code is quite sensitive to putting more data in nodes. Any ideas about this?
    I'm also convinced we'll need to adjust reparsing in some way to either throw away the old file and completely reparse if the version changed or to cleverly go down into the tree and reparse the string nodes alongside the given range too.

  • isuffix — 10/30/26, 1:49 AM

    I've thought for a bit that we should differentiate a SyntaxRoot type that just limits some methods on a SyntaxNode and could contain data like the FileId. For example the synthesize_* methods make no sense in the middle of the tree. However, this role is actually already being filled somewhat by Source, so perhaps some methods and extra data could be moved over to it, or maybe it could just contain a SyntaxRoot directly. Although I'm not sure whether it would be useful to have a non-Source root or one without a FileId.
    I'll try to review 7620 properly soon. I've actually meant to do so for a while, but haven't made the time.

  • tinger [JŒVR], — 10/30/26, 7:20 AM

    No worries, even tho it was marked as ready I noticed that reparse issue only once I looked at the reparse conflicts yesterday.

This question about how to handle reparsing is still open. In general, reparsing with a different version than before shouldn't regularly happen, but it can happen. Either a user edits a package manifest or a third party tool allows configuring the preferred project version or we later allow something else that invalidates this assumption.

Right now we don't encode this assumption, we don't force rewriting the source when the version changes and the reparse function has no idea what the original version was so it can't do it either. I dont' remember adding anything about this sort of invalidation so this would still be a bug in the current implementation that has to be resolved.

Storing the version in there somehow seems sensible, but it would also be a sort of temporary field for the compatibility behavior. That feels like a stop gap, but I don't think it really is.

I'm open to suggestions on how to handle this.

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

Labels

interface PRs that add to or change Typst's user-facing interface as opposed to internals or docs changes. scripting About Typst's coding capabilities syntax About syntax, parsing, etc. waiting-on-review This PR is waiting to be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Convenient string formatting

3 participants