Skip to content

Add structured template strings - #5512

Open
CutieDeng wants to merge 4 commits into
racket:masterfrom
CutieDeng:feat-tstring
Open

CutieDeng wants to merge 4 commits into
racket:masterfrom
CutieDeng:feat-tstring

Conversation

@CutieDeng

Copy link
Copy Markdown
Contributor

Summary

  • Add a new racket-tstring package that implements structured template strings, including parsing, expansion, rendering, and reader support.
  • Add the tstring package as the public wrapper/alias and register both packages in the build configuration.
  • Add racket/interactive/tstring support for REPL use.

Tests

  • raco make pkgs/racket-tstring/tests/*.rkt pkgs/tstring/tests/*.rkt
  • raco test pkgs/racket-tstring/tests pkgs/tstring/tests (95 tests passed)

CutieDeng added 4 commits May 14, 2026 03:00
"
New racket-tstring package providing template strings with
interpolation, parsing, rendering to HTML/SQL, and compile-time
source transformation. Includes comprehensive tests for all modules.

Modified:
- pkgs/racket-tstring/info.rkt
- pkgs/racket-tstring/lang/reader.rkt
- pkgs/racket-tstring/main.rkt
- pkgs/racket-tstring/private/expand.rkt
- pkgs/racket-tstring/private/parse.rkt
- pkgs/racket-tstring/private/render.rkt
- pkgs/racket-tstring/private/source-transform.rkt
- pkgs/racket-tstring/private/template.rkt
- pkgs/racket-tstring/tests/expand-test.rkt
- pkgs/racket-tstring/tests/parse-test.rkt
- pkgs/racket-tstring/tests/reader-basic.rkt
- pkgs/racket-tstring/tests/reader-error-test.rkt
- pkgs/racket-tstring/tests/reader-nesting.rkt
- pkgs/racket-tstring/tests/reader-test.rkt
- pkgs/racket-tstring/tests/render-test.rkt
")
"
Add the tstring collection as a short `#lang reader` wrapper around
racket-tstring, providing f\"...\" and t\"...\" template string syntaxes
with tests covering rendering, template values, and nesting.

Modified:
- Makefile
- pkgs/tstring/info.rkt
- pkgs/tstring/lang/reader.rkt
- pkgs/tstring/main.rkt
- pkgs/tstring/tests/require-test.rkt
- pkgs/tstring/tests/wrapper-racket-base.rkt
- pkgs/tstring/tests/wrapper-test.rkt
")
"
Replace interpolation-kind/interpolation-source with
interpolation-format-spec/interpolation-conversion, supporting format
specifiers and conversion flags in template strings. Add template-parts
to interleave strings and interpolations. Add
racket/interactive/tstring.rkt for REPL template string support.

Modified:
- pkgs/racket-tstring/main.rkt
- pkgs/racket-tstring/private/expand.rkt
- pkgs/racket-tstring/private/render.rkt
- pkgs/racket-tstring/private/template.rkt
- pkgs/racket-tstring/tests/expand-test.rkt
- pkgs/racket-tstring/tests/reader-test.rkt
- pkgs/racket-tstring/tests/render-test.rkt
- pkgs/tstring/tests/require-test.rkt
- pkgs/tstring/tests/wrapper-test.rkt
- racket/collects/racket/interactive/tstring.rkt
- racket/src/pkgs-config.rkt
")
"
Implement format-fstring-value to support Python-style format
specifiers including alignment (<, >, ^, =), sign handling (+,
-, space), zero padding, width, precision, and type specifiers
(d, b, o, x, X, f, F, e, E, g, G, %). Wire format spec and
conversion through expand-time to runtime interception.

Modified:
- pkgs/racket-tstring/private/expand.rkt
- pkgs/racket-tstring/private/render.rkt
- pkgs/racket-tstring/tests/expand-test.rkt
")

@CutieDeng CutieDeng left a comment

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.

This feature branch introduces a non-trivial feature and still needs more testing and validation. Please treat this PR as ready for review and discussion, but do not merge it immediately.

@usaoc

usaoc commented May 14, 2026

Copy link
Copy Markdown
Contributor

I think there’s no reason these packages need to be in this repo. (We already have @-notation for string interpolation, anyway.) Maybe consider submitting them to the Racket Package Index?

@CutieDeng

Copy link
Copy Markdown
Contributor Author

I think there’s no reason these packages need to be in this repo. (We already have @-notation for string interpolation, anyway.) Maybe consider submitting them to the Racket Package Index?

From the implementation side, bare f"..." / t"..." syntax and REPL support require reader-level integration, so this cannot be meaningfully delivered as an ordinary library after the reader has already parsed the source.

From the feature side, this follows Python’s established f-string model and its newer t-string/template-string direction (PEP 498, PEP 750), and explores whether first-tier Racket support can improve everyday REPL ergonomics and structured-string workflows.

@samth

samth commented Jun 10, 2026

Copy link
Copy Markdown
Member

It's true that it needs reader-level integration, but Racket's reader is extensible and #lang notation with meta-languages like at-exp makes it easy to add this to any Racket program.

@racket-discourse-github-bot

Copy link
Copy Markdown

This pull request has been mentioned on Racket Discourse. There might be relevant details there:

https://racket.discourse.group/t/when-racket-meets-python-style-strings/4295/1

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.

4 participants