Skip to content

Interpolate in double-quoted strings (Dang 2.5) - #300

Merged
vito merged 17 commits into
mainfrom
string-interpolation
Nov 12, 2026
Merged

vito merged 17 commits into
mainfrom
string-interpolation

Conversation

@vito

@vito vito commented Nov 11, 2026 •

Copy link
Copy Markdown
Owner

Fixes #171, following the plan in #171 (comment).

The delimiter character now decides how a string's contents are processed. Tripling it means multi-line and dedented, with an optional language tag:

single-line multi-line, dedented, optional lang tag
" escapes + ${} """: escapes + ${}
' verbatim ''': verbatim
` raw + ${} (unchanged) ```: raw + ${} (unchanged)

Language (Dang 2.5)

  • "..." and """...""" interpolate ${...} and process escapes. \$ is a new escape (so \${ gives a literal ${). A """ string is dedented before its escapes are decoded, so \t at the start of a line is text, not indentation.
  • '...' and '''...''' are new verbatim strings, with no escapes or interpolation. This is a pure addition, so they work in every version.
  • """ and ''' take language tags, like backtick fences.
  • The opening line of a multi-line string may hold only a language tag (trailing whitespace allowed). Text there is an error unless the string closes on the same line ("""inline"""). Without this rule, a word on that line could silently become a tag.
  • Docstrings stay verbatim (no escapes, no interpolation, no tag) in every version.
  • Undefined names get a hint. An undefined ${NAME} in a double-quoted string now reports "NAME" not found; to write ${NAME} as text, single-quote the string or escape it as \${NAME}.
  • Repr (toString, REPL) and the formatter escape ${ in the strings they quote.

Before 2.5, "..." keeps ${ literal and """ stays raw, behind the new literalQuotes gate (Version2_5, now CurrentVersion). dang migrate keeps every string's value:

  • A "..." holding ${ becomes '...' where it can, and gets \${ otherwise.
  • A raw """ holding \ or ${ becomes ''' (or gets escapes).
  • Text on an opening line moves to the next line.

The fixture is tests/testdata/migrations/2.4-to-2.5. The tracked test, demo and LSP projects move to 2.5.

Tree-sitter and editors

  • Grammar and scanner. String content is lexed by the external scanner, with one token per delimiter kind; all of them are exposed as string_content nodes, and a # inside them is never a comment. lang_tag_name is also an external token now, valid only right after an opening fence, which replaces the just_opened flag. New nodes: interpolation, single_quote_string, triple_single_quote_string.
  • Docstrings share the """ string's shape in tree-sitter, so parameter docstrings still parse. A valid docstring holding a ${ that isn't an expression (${HOME:-/root}) is an error to tree-sitter alone. Parse errors now report the tree-sitter error nearest where the PEG failed, so such a docstring doesn't steal them.
  • Error messages. Four syntax-error goldens changed slightly, because recovery no longer reads a whole line as one string token (see tests: update syntax error goldens).
  • Editor support. VSCode's grammar is updated here. Completion is suppressed inside string content but still works inside ${...}.
  • Zed and Neovim queries live in their submodules and need their own PRs: Highlight Dang 2.5 strings zed-dang#1 and Highlight Dang 2.5 strings dang.nvim#1. The editors/zed and editors/nvim submodules here point at those PRs' heads for now; move them to the editor repos' main once those merge.

Docs

  • The syntax, strings and grammar pages are rewritten around the table above, and the agent skills are updated to match.
  • New release notes in docs/lit/releases/v2.5.md.
  • I left the ~80 incidental `...${}` text templates in the docs alone; they're still valid. Sweeping them to "..." could be a follow-up.

Known gaps

  • .dagger/main.dang keeps "${GOBIN}:${PATH}": Dagger pins that module's Dang version, and the engine's Dang doesn't understand '...' or \$ yet. It needs '${GOBIN}:${PATH}' once the engine embeds 2.5.
  • A bare top-level """ string with text on its opening line, followed by something that looks like a declaration, gets a generic parse error rather than the pointed one. Such text is taken for a docstring whose declaration failed.

vito added 15 commits November 11, 2026 21:17
From Dang 2.5, `"..."` and `"""..."""` strings process escapes and
interpolate `${...}`, `\$` escapes a dollar sign, and `"""` takes a
language tag like backtick fences do. `'...'` and `'''...'''` are new
verbatim strings with no escapes or interpolation, in every version. The
opening line of a multi-line string now holds at most a language tag, so a
word there can't silently turn into one.

Before 2.5, `"..."` keeps `${` literal and `"""` stays raw, behind the new
literalQuotes gate (Version2_5, now CurrentVersion). Migrating keeps every
string's value: a `"..."` holding `${` becomes `'...'` where it can, a raw
`"""` holding `\` or `${` becomes `'''`, and text on an opening line moves
to the next. Docstrings stay verbatim in every version.

A multi-line `"""` string is dedented before its escapes are decoded, so
`\t` is text rather than indentation. Repr and the formatter escape `${` in
the strings they quote.

Signed-off-by: Alex Suraci <[email protected]>
Teach the dang-language skill the delimiter rules: "..." and """
interpolate and take escapes, single quotes are verbatim, backticks are
for code. Note the pre-2.5 behaviour and what dang migrate does, and
recommend "${...}" interpolation over + chains.

Signed-off-by: Alex Suraci <[email protected]>
Dagger's SDK, not Dang, picks the version of a module that declares none,
so such a module shouldn't be migrated to 2.5 before its engine embeds it.

Signed-off-by: Alex Suraci <[email protected]>
Migrating a raw `"""` string whose value ends in a quote right before the
closing quotes to `'''` ran the quote into the fence, and the result didn't
parse. Such a string now falls back to escapes, as an inline one did.

Signed-off-by: Alex Suraci <[email protected]>
Rewrite the string literal sections for 2.5: "..." and """...""" process
escapes and interpolate ${...}, '...' and '''...''' are verbatim, and
backticks stay raw text with ${...} for code. Cover the opening-line rule
for multi-line strings, docstrings staying verbatim, and replaceMatches'
${name} backrefs needing single quotes. The index and strings pages now
lead with "..." interpolation.

Add the v2.5 release notes, with dang migrate as the upgrade path, and
bump the dang.toml example to 2.5.

Signed-off-by: Alex Suraci <[email protected]>
Resolve the LiteralQuotes gate statically: `"..."` and `"""..."""` now
interpolate `${...}` and take escapes (`\$` too), `"""` takes a language
tag, and `'...'`/`'''...'''` are verbatim strings.

String content is lexed by the external scanner, one token per kind of
delimiter, so the scanner knows from the valid symbols what ends the content
and refuses comments inside it (`"${x}#1"`, `'# x'`). The tokens are all
string_content nodes, leaving interpolated expressions out of them. A
language tag is an external lang_tag_name token, taken only for a lone word
right after an opening fence, for backticks, `"""` and `'''` alike; it
replaces the scanner's "just opened" flag.

At a newline in a `"..."` or `'...'` string the scanner offers a statement
separator, so error recovery closes an unterminated string with a missing
quote instead of reading on into the next lines. During error recovery the
scanner no longer emits content tokens (template ones included), which no
state can take and which kept the parser from resynchronizing; comments are
recognized there instead.

Docstrings take the shape of a `"""` string, so GLR can still explore both
readings of a `"""` after `(`. Tree-sitter therefore lexes them as cooked:
the `"""` content token takes a backslash that starts no escape as text, but
a docstring whose `${` starts no expression is an error to tree-sitter.

Completion recognizes the new content nodes and language tags, and still
completes inside `${...}`. TestHighlightUnicodeRuneOffsets now highlights
valid code: it relied on how `"café" let x = 1` recovered.

Signed-off-by: Alex Suraci <[email protected]>
Refresh the embedded REPL query from the Zed one, which now captures strings
piecewise (quotes, string_content, escapes, `${`/`}`) so interpolated
expressions keep their own colors, labels `"""`/`'''` language tags, and
covers single-quoted strings. Add highlight corpus cases for each form.

Signed-off-by: Alex Suraci <[email protected]>
Add `'...'` and `'''...'''` as verbatim strings, interpolation and the full
escape set (including `\$`) in `"..."` and `"""..."""`, and language tags on
`"""`/`'''`. Try `"""` before `"`, which used to win and break triple-quoted
strings.

Signed-off-by: Alex Suraci <[email protected]>
The tracked test, demo and LSP fixture projects move to 2.5. Two strings in
them changed meaning and are respelled the way dang migrate would. New tests
cover the quoted string forms, a 2.4-to-2.5 migration fixture and the
opening-line error.

An undefined `${NAME}` in a double-quoted string, likely shell text written
before double quotes interpolated, now says how to keep it as text.

Signed-off-by: Alex Suraci <[email protected]>
Inside a multi-line template, `${name` followed by the closing fence read
the fence as a stray single backtick: the lexer state shared there skips the
newline and knows the single-backtick token. Error recovery then couldn't
insert the missing `}`, and the whole declaration became one ERROR.

Where neither fence nor any newline or inline-space token is valid inside a
template, the scanner now offers the template's closing fence anyway.
Recovery inserts the `}` right after the expression and closes the template,
restoring "missing closing '}'" at its old position.

Signed-off-by: Alex Suraci <[email protected]>
The old messages for these inputs came from tree-sitter error recovery
matching the rest of a line as one token of the old internal string-content
pattern (`[^\\"\n]+`). String content is now lexed by the external scanner,
so recovery works with real tokens: `%raw{hello}` points at the `%`, the
incomplete binop reports `let y =` on the next line instead of taking `let`
as its operand, the long template close points at the extra backtick, and
`let x 42` underlines one character less.

Signed-off-by: Alex Suraci <[email protected]>
extractComments re-lexed the source to find comments, and lost track of
strings in interpolations and of escaped quotes: `dang fmt` duplicated the
rest of a line after `"${c ?? "#fff"}"` and dropped comments after `\"""`.
The parser already knows which `#` starts a comment, so use its comments,
leaving out those inside a multi-line literal's interpolations, which the
literal echoes.

Signed-off-by: Alex Suraci <[email protected]>
A docstring whose declaration failed to parse was parsed again as a string,
which may not hold what a docstring may (text on its opening line, a
backslash starting no escape), so the error blamed the docstring. Such text
now fails the string quietly, leaving the error where the declaration went
wrong.

Whitespace may now also trail a language tag, where it was an error that
pointed at nothing visible.

Signed-off-by: Alex Suraci <[email protected]>
The error now says how to keep `${NAME}` as text.

Signed-off-by: Alex Suraci <[email protected]>
A `"""` string given as a call argument, or opening a statement, was first
tried as a docstring, and so lost its own errors (text on the opening line,
an invalid escape). Only a docstring that a declaration appears to follow is
now taken for a failed declaration.

Tree-sitter lexes docstrings like `"""` strings, so it may see an error in a
valid one (`${HOME:-/root}`). Parse errors now report the tree-sitter error
nearest where the PEG failed, rather than the first one in the file.

Signed-off-by: Alex Suraci <[email protected]>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Deploying dang with  Cloudflare Pages  Cloudflare Pages

Latest commit: ed2e280
Status: ✅  Deploy successful!
Preview URL: https://afe43a32.dang-3kk.pages.dev
Branch Preview URL: https://string-interpolation.dang-3kk.pages.dev

View logs

vito added 2 commits November 12, 2026 00:58
The highlight queries for Dang 2.5 strings land in vito/zed-dang#1 and
vito/dang.nvim#1. Point the submodules at those PRs' heads, so the
embedded copy of the Zed query and the Neovim highlight tests match the
grammar. Move them to the editor repos' main once those PRs merge.

Signed-off-by: Alex Suraci <[email protected]>
Instead of "use another form", say which form holds a `'`: `'''it's'''`
does, even inline. The regex section now shows that text interpolated into
a backtick pattern is regex syntax, and to wrap it in Regexp.escape, and
how to write a `${name}` backref in a replacement that also interpolates.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito merged commit a075f90 into main Nov 12, 2026
9 checks passed
vito added a commit to vito/zed-dang that referenced this pull request Nov 12, 2026
Points the tree-sitter grammar at vito/dang@a075f90, the merge commit of
vito/dang#300, which adds the string_content, interpolation and
single-quoted string nodes these queries capture.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit that referenced this pull request Nov 12, 2026
The 2.5 page covered only the string change. It now covers everything
merged since v2.4.1: declaration snapshots (#301), the editor and
syntax-error changes from #300, and a Go embedders section with
DiagnosticContext (#298) and the AST's new string spelling. Sources and
verification name the full range.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit that referenced this pull request Nov 13, 2026
The 2.5 page covered only the string change. It now covers everything
merged since v2.4.1: declaration snapshots (#301), the editor and
syntax-error changes from #300, and a Go embedders section with
DiagnosticContext (#298) and the AST's new string spelling. Sources and
verification name the full range.

Signed-off-by: Alex Suraci <[email protected]>
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.

Interpolation should work for double-quoted strings, too

1 participant