Skip to content

feat(lexers): add TypeSpec lexer - #1524

Merged
alecthomas merged 2 commits into
alecthomas:masterfrom
martynovs:feat/typespec-lexer
Nov 21, 2026
Merged

alecthomas merged 2 commits into
alecthomas:masterfrom
martynovs:feat/typespec-lexer

Conversation

@martynovs

Copy link
Copy Markdown
Contributor

Adds a lexer for TypeSpec, the API description language: *.tsp files, aliases typespec and tsp. Pygments has no TypeSpec lexer, so this one is hand written. Chroma currently renders typespec and tsp code blocks without any highlighting.

It covers declarations (model, scalar, interface, union, enum, alias, op, dec, fn, const, namespace, using, import), decorators and augment decorators (@@), directives (#suppress), doc comments with their @tags, single and triple quoted strings with escapes and ${} interpolation, number literals, the built-in scalar types, object and tuple literals (#{}, #[]) and backtick identifiers.

How it was checked

  • lexers/testdata/typespec.actual exercises each construct, with the recorded typespec.expected.
  • The tokens were compared with the grammar of the official TypeSpec VS Code extension (typespec.tmLanguage, from @typespec/compiler 1.18.0) on 22 production .tsp files, about 156 KB and 18,000 tokens. No Error tokens from this lexer, and no disagreement on comments, strings, numbers, keywords, decorators, directives or function names. The grammar scopes every type reference as entity.name.type, so the comparison treats those as equivalent.
  • Without a parser the lexer cannot tell a type reference from a value. PascalCase identifiers are NameClass, and a name followed by : or ?: is a plain Name. Enum members written without a value (Red,) are therefore coloured as types.
  • go test ./lexers/... . passes, and lexers_gen.go is regenerated.

🤖 Generated with Claude Code

martynovs and others added 2 commits November 21, 2026 10:33
Adds a lexer for TypeSpec (https://typespec.io), the API description
language, for *.tsp files (aliases: typespec, tsp).

It covers declarations (model, scalar, interface, union, enum, alias,
op, dec, fn, const, namespace, using, import), decorators and augment
decorators, directives, doc comments with tags, single and triple
quoted strings with escapes and ${} interpolation, number literals,
built-in scalar types, object and tuple literals, and backtick
identifiers.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A TypeSpec string cannot span lines, so an unterminated one no longer
colours the rest of the file as a string.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@alecthomas
alecthomas merged commit 5e1337d into alecthomas:master Nov 21, 2026
4 checks passed
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.

2 participants