Files
llvm/mlir/utils/pygments
Twice 7ac6a95a11 [MLIR][Pygments] Refine the pygments MLIR lexer (#166406)
Recently, the MLIR website added API documentation for the Python
bindings generated via Sphinx
([https://mlir.llvm.org/python-bindings/](https://mlir.llvm.org/python-bindings/)).
In
[https://github.com/llvm/mlir-www/pull/245](https://github.com/llvm/mlir-www/pull/245),
I introduced the Pygments lexer from the MLIR repository to enable
syntax highlighting for MLIR code blocks in these API docs.

However, since the existing Pygments lexer was fairly minimal, it didn’t
fully handle all aspects of the MLIR syntax, leading to imperfect
highlighting in some cases. In this PR, I used ChatGPT to rewrite the
lexer by combining it with the TextMate grammar for MLIR
([https://github.com/llvm/llvm-project/blob/main/mlir/utils/textmate/mlir.json](https://github.com/llvm/llvm-project/blob/main/mlir/utils/textmate/mlir.json)).
After some manual adjustments, the results look good—so I’m submitting
this to improve the syntax highlighting experience in the Python
bindings API documentation.
2025-11-07 21:12:28 +08:00
..

Pygments Lexer for MLIR

This file contains a simple Pygments lexer configuration for MLIR, derived from the version used in the original CGO paper. Pygments allows for advanced configurable syntax highlighting of any code. This lexer is known to be incomplete and support mostly core IR with a subset of built-in types. Additions and customizations are welcome.

Standalone Usage

Install Pygments, e.g., by running pip install Pygments or a Python package manager of your choosing. Use the standalone pygmentize command by instructing it to load the custom lexer:

pygmentize -l /path/to/mlir_lexer.py:MlirLexer -x myfile.mlir

This will produce highlighted output in the terminal. Other output formats are available, see Pygments documentation for more information.

LaTeX Usage

First, make sure your distribution includes the minted package and list in the preamble.

\usepackage{minted}

Place the mlir_lexer.py in a place where the latex binary can find it, typically in the working directory next to the main .tex file. Note that you will have to invoke latex with the -shell-escape flag. See the minted package documentation for more information.

Leverage the custom lexer facility of minted to use this lexer in your document as:

\begin{minted}{mlir_lexer.py:MlirLexer -x}
   ... your code here ...
\end{minted}