Chroma compatibility

fasthighlight follows Chroma, the syntax highlighter widely used in Go, as its reference.

Chroma 2.27.0 is the reference, and the intended changes to its results are listed. For the supported languages, the tokens are checked by fuzz tests to match Chroma everywhere except at those changes.

Fuzz tests compare the two on large numbers of generated inputs. They are a check, not a proof that every input matches.

What matches

What was changed

Chroma is the reference, not something to copy exactly. Where Chroma has a bug, or behaves in a way that gets in the way of readers, fasthighlight differs. Changes are limited to places where the colors would clearly go wrong (an error color, or a string or tag color running on past where it should end), and to places where Chroma leaves out a color in a way that is plainly inconsistent (the names of types you define, in type positions).

Where Chroma fasthighlight
{} inside ${ … } of a template literal (TypeScript, JavaScript, react (JSX), <script> in HTML) the first } ends the ${ … }, so what follows looks like a string counts the braces
TypeScript JSX (<div …>) < followed by a letter is a tag, so Map<string, number> and a<b become tags, sometimes followed by errors not treated as tags (< is an operator)
Escapes such as \n in TypeScript and react (JSX) template literals \ is an error string color, as in JavaScript
Names of types in type positions (Go, TypeScript, Java, C#, Kotlin, Rust, Swift, Python, PHP) only built-in types such as string or int get the type color; a type you define (User) gets the name color, so Map<string, User> shows one type colored and one not the type color, the same one the language already uses for its types: Go struct fields and embedded fields, var, const and type declarations, parameters, results and receivers (ctx context.Context; the package as a namespace); TypeScript type arguments and return types (Map<string, User>, f<User>(), (): User); Java and C# type arguments, declared types, parameter and return types (List<User> xs, User get(…), C# also after new and as); Kotlin types after : and in type arguments (val xs: List<User>, ): User); Rust types after : and -> and in ::<…> (Vec<User>, a path before :: as a namespace); Swift types after :, ->, as and is, in [User], User?, generics and inheritance lists; Python type hints (m: dict[str, User], -> User, xs: list[User] at the start of a line); PHP parameter, return and property types (?User $u, ): A|B). Expressions are left as they are: comparisons (a < b && c > d), conditionals (c ? a : b), calls, dictionary and struct literals, and Go's single name in a parameter list (func(T), which can be a name or a type). When a position is unclear, it is left without the type color
A Rust keyword right after : or -> (mut in x: &mut T, loop in 'a: loop) the type color the keyword color
Standard library names of current Swift (Codable, Identifiable, Sendable, Error, Collection, …) Chroma's list of built-in names is from Swift 1 and 2: Hashable and Int are built-ins, Codable and Identifiable are not, so struct Task: Identifiable, Codable, Hashable shows Hashable in another color built-in color for them too. Common words that programs also use for their own types (Task, Result) are left out
Names starting with a non-ASCII letter in TypeScript, Swift and Ruby error color name color
Ruby strings of the form % abc the delimiter is output twice output once
Ruby defined? not a keyword in defined? x or defined?(x) a keyword
The start of an R name [, \, ], ^ and ` can start a name (a typo for [a-zA-Z] in the rule), so x[[1]] and n^2 are names letters only
The start of a Go name digits other than ASCII, and numbers in templates ({{ 1 }}), can start a name digits cannot start a name
Java numbers (42, 1.5, 0x1F) name color number color
C++ raw strings (R"(…)") the rule almost never matches, so R is a name and the rest a normal string raw strings
Python docstrings ("""…""" at the start of a line) the rule almost never matches, so they get the normal string color docstring color
PHP <?php and ?> <? is an operator and php a name; after ?>, everything is an error <?php and ?> get the preprocessor color, and the text between ?> and the next <? is read as HTML
A newline right after #, -, * or > in Markdown the next line becomes part of the heading, list item or quote a newline there does not count
Emphasis at the start of a line or a list item in Markdown (**Note**: …, - **a** b) not emphasis emphasis
Code blocks inside Markdown colored with any of Chroma's languages colored with the registered languages only
Front matter in Markdown with \r alone as newlines found before newlines are normalized found after, like everything else
Very long tokens of the same kind (8,192 bytes or more) in Markdown code blocks and front matter, and in HTTP message bodies read by a registered language split at one place split at another place (looks the same)
The body of an HTTP message in a language with a trailing-newline rule (JavaScript, C, PHP and others), when the message does not end with a newline a newline is added to the body nothing is added; the tokens join back into the input
The body of an HTTP message whose content type is in a language fasthighlight does not support (text/plain is Nu in Chroma) colored with that language not colored
Rules whose regular expression takes more than 250 ms on an input treated as not matching, so the result depends on how fast the machine is always the same result

Each change has a switch, used only in tests, that turns it back to Chroma's behavior; the fuzz tests compare that version with Chroma, so the changes are known to be the only differences. (For the HTTP body rows, the tests instead remove Chroma's added newline, or skip the content types fasthighlight does not support, before comparing.)

Some of Chroma's quirks are kept as they are, because they do not show: in Docker, the newline after an instruction is an error token, and so is an empty line in a Bash session (console); an error token on a newline has no visible color.

Why it is fast

How others do it

Many highlighters, Chroma among them, use grammars made of regular expressions (such as the grammars of VS Code or Sublime Text). One shared mechanism handles many languages, existing grammars can be reused, and adding a language means adding a grammar file. This is a sound design for supporting many languages.

How fasthighlight does it

fasthighlight supports a chosen set of languages (the list), and each has its own hand-written tokenizer. From its current state and the next character, it goes straight to what needs doing: for a letter, it reads the whole name and looks it up in the keyword table. There is no step that tries many grammar rules at each position.

The trade-off

Grammars of regular expressions Hand-written tokenizers (fasthighlight)
Adding a language add a grammar file write code for the language
Maintaining a grammar edit the grammar file edit the code
Existing grammars reusable not usable as they are
Speed depends on a general regular expression engine each language can be tuned
Number of languages many (giallo: more than 220) only the supported ones

To keep the maintenance down, fasthighlight follows Chroma's rules instead of designing its own, generates long word lists from Chroma's rules, and checks the result against Chroma with fuzz tests.

What this does not mean

The measurements are on the top page.