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.
- 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.
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
- 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-mlir
is in your build environment (requirements). - In docs, use the
mlir
language in code blocks:
- Ensure
```mlir
module {
func.func @add(%a: i32, %b: i32) -> i32 {
%0 = arith.addi %a, %b : i32
return %0 : i32
}
}
```
module {
func.func @add(%a: i32, %b: i32) -> i32 {
%0 = arith.addi %a, %b : i32
return %0 : i32
}
}
- Install from source for local testing:
uv sync
pygmentize -L lexers | grep -i mlir
- Style/coverage/tests: contributions welcome via PR.
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).
This project is licensed under the MIT License - see the LICENSE file for details.