Pygments syntax highlighting for MLIR
Pygments lexer for the Multi-Level Intermediate Representation (MLIR) used in compiler infrastructures. Provides syntax highlighting in terminals, HTML, Sphinx, and other Pygments-powered tools.
Features
- Highlights MLIR core syntax: operations (incl. dialect ops), types, attributes, SSA values, blocks/regions, and comments.
- Works with pygmentize (CLI), Python APIs, and Sphinx code blocks.
- Lightweight dependency on Pygments.
Installation
Install the latest release from PyPI:
uv add pygments-mlir
This also installs the pygments package if not already installed.
Quick check that the lexer is registered:
pygmentize -L lexers | grep -i mlir
Usage
- CLI (terminal colors):
pygmentize -l mlir -f terminal16m example.mlir
- CLI (HTML output):
pygmentize -l mlir -f html -O full,style=default -o example.html example.mlir
- Python API:
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
code = """
module {
func.func @add(%a: i32, %b: i32) -> i32 {
%0 = arith.addi %a, %b : i32
return %0 : i32
}
}
""".strip()
lexer = get_lexer_by_name("mlir")
formatter = HtmlFormatter(full=True, style="default")
html = highlight(code, lexer, formatter)
# write html to a file or display it
- Sphinx:
- Ensure
pygments-mliris in your build environment (requirements). - In docs, use the
mlirlanguage in code blocks:
- Ensure
```mlir
module {
func.func @add(%a: i32, %b: i32) -> i32 {
%0 = arith.addi %a, %b : i32
return %0 : i32
}
}
```
Example
module {
func.func @add(%a: i32, %b: i32) -> i32 {
%0 = arith.addi %a, %b : i32
return %0 : i32
}
}
Development
- Install from source for local testing:
uv sync
pygmentize -L lexers | grep -i mlir
- Style/coverage/tests: contributions welcome via PR.
Acknowledgments
This package is adapted from https://gist.github.com/oowekyala/110dacc9343dbc1b86b452902d7dc553 (MIT License). It was also inspired by the openqasm-pygments project (Apache 2.0).
License
This project is licensed under the MIT License - see the LICENSE file for details.
Metadata
Release files for mlir-pygments 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mlir_pygments-1.0.0.tar.gz | 4.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mlir_pygments-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.5 kB
Release files / mlir_pygments-1.0.0.tar.gz
| Download URL | mlir_pygments-1.0.0.tar.gz |
|---|---|
| Size | 4.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bd518ddcdb1437a42a9beba68bd1b6a6cee48dffdc873e25185904c43ba5eedb
|
|
BLAKE2b-256 checksum How to use checksums |
927519719ce387b081efaf47437dfb3cb8cb4b55886e2e49d06622ea6a11e373
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.8.19
|
Release files / mlir_pygments-1.0.0-py3-none-any.whl
| Download URL | mlir_pygments-1.0.0-py3-none-any.whl |
|---|---|
| Size | 5.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c326fcbd08950a33b4abc49386d5cffc3127985371eb012e948b406c7ce0f9a0
|
|
BLAKE2b-256 checksum How to use checksums |
94f201c3bb853a8d4d7eb2236bc7ba39e56fba51f6513ad10079bc96c8425fb2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.8.19
|