Skip to content

compiler: lex listings' \lstinline as a raw delimited argument - #188

Merged
GoKubar merged 1 commit into
mainfrom
agent/kabir-claude/lstinline-compiler
Sep 13, 2026
Merged

GoKubar merged 1 commit into
mainfrom
agent/kabir-claude/lstinline-compiler

Conversation

@GoKubar

@GoKubar GoKubar commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

Based on main 89e49fed, not stacked. crates/compiler only. Companion to #187 (the render-pipeline half) and #183 (the typewriter fonts).

What was wrong

\lstinline|x y| reached the parser as an ordinary TokenKind::Command, so every use raised "\lstinline is not supported by this compiler version" and the delimiters and body were typeset as prose. listings reads it exactly the way \verb reads its argument, and the compiler already has that path.

This is the \lstinline half of draft PR #145, written against main rather than merged — #145 is render-pipeline only, predates the NFSS font selection main has since gained, and its render-pipeline half is not reused (see #187).

Lexing

\lstinline joins \verb in the lexer's raw-argument branch, with the differences listings.sty has:

  • an optional [<key=value list>] first, skipped only when it closes on the same line (otherwise [ is the delimiter, as it would be for \verb);
  • the blanks after the command are skipped (\@ifnextchar), unlike \verb, whose very next character — even a space — is the delimiter;
  • \lstinline{...} closes on }.

The keys are deliberately not interpreted here: they set no text (listings.sty's \lstinline does \lstset{flexiblecolumns,#1}), and the renderer scans \lstset/\lstdefinestyle/the local list from the source. The body is raw — %, \, $, { and } are not reinterpreted.

TokenKind::Verb gains listing: bool so an unterminated one says "\lstinline has no closing delimiter on this line" under its own name.

Getting it past the expansion engine

prepare_document hides a \verb body from the TeX engine by blanking the bytes between the delimiters and recording the lexer's token, which the conversion maps back by source position. \lstinline now takes the same route: its first five bytes are spelled over as \verb in the copy the engine reads, and the rest of the construct is blanked as before.

So the engine needs no \lstinline primitive, never sees the keys or the delimiters, and the conversion's existing "verb" arm hands back this token — the one the compiler's own lexer built, carrying the listing's real text. The prepare_document fast-path guard gained \lstinline so a document that uses it and nothing else is still prepared.

Tests and gates

Three new parser tests: the six delimiter/option/brace forms, the unterminated case and its message, and the non-closing-bracket case. The three lexer tests that spell out TokenKind::Verb carry the new field.

passed failed ignored
control checkout of 89e49fed (git archive) 395 0 4
this branch 398 0 4

Verified end to end through the compiler binary: \lstinline|x y|, \lstinline[language=C]!int z! and \lstinline{p q} in one paragraph produce three verbatim text items and no diagnostic beyond the pre-existing "packages listings are recognised but not implemented".

Not in this change

  • The listings rendering model — \lstset keys, fixed/flexible columns and their lost-space kerns, frames, line numbers, keyword/comment/string styles, breaklines — is render-pipeline work and is not attempted. With this change \lstinline sets literal typewriter text, which is what listings' own default (flexiblecolumns with a typewriter basicstyle) comes to for the common case; a non-default \lstinline[...] is not yet honoured.
  • verbatim_display still expands a tab to an 8-column stop and renders \verb*'s space as U+00B7. LaTeX does neither: \@verbatim sets a tab as one ordinary space (pdfTeX puts <tab>one one typewriter width from the margin, not eight), and \verb* uses \char32 of the typewriter font. This is a real mismatch in this crate's own Core-14 layout. render-pipeline: literal text, and \verb/verbatim in the typewriter face NFSS selects #187 sidesteps it by reading those bytes from the source, so the render pipeline is unaffected; fixing verbatim_display itself would move committed compiler layout expectations and is left as its own change. Filed here so it is not lost.

Resume notes

  • After this merges, crates/render-pipeline/vendor/compiler needs a re-pin before render-pipeline: literal text, and \verb/verbatim in the typewriter face NFSS selects #187's 28-lstinline fixture can move from NOT_YET to gated.
  • lstinline is still listed in vocabulary.rs's KNOWN_UNIMPLEMENTED_COMMANDS, exactly as verb is — harmless (the lexer intercepts both before the parser's command dispatch), but that list is stale for both and worth a separate tidy.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu

`\lstinline|x y|` reached the parser as an ordinary `TokenKind::Command`,
so every use raised "\lstinline is not supported by this compiler version"
and the delimiters and body were typeset as prose. listings reads it exactly
the way `\verb` reads its argument, and the compiler already has that path.

This is the `\lstinline` half of draft PR #145, written against main rather
than merged (#145 is render-pipeline only and predates the NFSS font
selection main has since gained; its render-pipeline half is not reused).

## Lexing

`\lstinline` joins `\verb` in the lexer's raw-argument branch, with the two
differences listings.sty has:

* an optional `[<key=value list>]` comes first, and is skipped only when it
  closes on the same line (otherwise `[` is the delimiter, as it would be
  for `\verb`);
* the blanks after the command *are* skipped (`\@ifnextchar`), unlike
  `\verb`, whose very next character -- even a space -- is the delimiter;
* `\lstinline{...}` closes on `}`.

The keys are deliberately not interpreted here: they set no text
(listings.sty's `\lstinline` does `\lstset{flexiblecolumns,#1}`), and the
renderer scans `\lstset`/`\lstdefinestyle`/the local list from the source.
The body is raw: `%`, `\`, `$`, `{` and `}` are not reinterpreted.

`TokenKind::Verb` gains `listing: bool` so an unterminated one says
"\lstinline has no closing delimiter on this line" under its own name.

## Getting it past the expansion engine

`prepare_document` hides a `\verb` body from the TeX engine by blanking the
bytes between the delimiters and recording the lexer's token, which the
conversion then maps back by source position. `\lstinline` now takes the
same route: its first five bytes are spelled over as `\verb` in the copy the
engine reads, and the rest of the construct is blanked as before. The engine
therefore needs no `\lstinline` primitive, never sees the keys or the
delimiters, and the conversion's existing `"verb"` arm hands back *this*
token -- the one the compiler's own lexer built, carrying the listing's real
text. The `prepare_document` fast-path guard gained `\lstinline` so a
document that uses it and nothing else is still prepared.

## Tests

Three new parser tests: the six delimiter/option/brace forms above, the
unterminated case and its message, and the non-closing-bracket case. The
three lexer tests that spell out `TokenKind::Verb` carry the new field.

`crates/compiler` `cargo test --release`: **398 passed, 0 failed, 4
ignored**. Control checkout of the same base (89e49fe, `git archive`):
395 passed, 0 failed, 4 ignored -- the three new ones are this change.

Verified end to end through the compiler binary: `\lstinline|x y|`,
`\lstinline[language=C]!int z!` and `\lstinline{p q}` in one paragraph
produce three verbatim text items and no diagnostic beyond the pre-existing
"packages listings are recognised but not implemented".

## Not in this change

The listings *rendering* model -- `\lstset` keys, fixed/flexible columns and
their lost-space kerns, frames, line numbers, keyword/comment/string styles,
`breaklines` -- is render-pipeline work and is not attempted here. With this
change `\lstinline` sets literal typewriter text, which is what listings'
own default (`flexiblecolumns`, a typewriter `basicstyle`) comes to for the
common case; a non-default `\lstinline[...]` is not yet honoured.

`verbatim_display` still expands a tab to an 8-column tab stop and renders
`\verb*`'s space as U+00B7. LaTeX does neither -- `\@verbatim` sets a tab as
one ordinary space, and `\verb*` uses `\char32` of the typewriter font --
which is a real mismatch in this crate's own Core-14 layout. The render
pipeline now reads those bytes from the source instead of from this string
(companion PR), so it is unaffected; fixing `verbatim_display` itself would
change committed compiler layout expectations and is left as its own change.

Implementation-Agent: engine-engineer (linux-primary, NixOS PC)
Commit-Executor: engine-engineer via git (Cursor usage limit fallback)
Co-authored-by: GoKubar <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu
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.

1 participant