Skip to main content

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-mlir is in your build environment (requirements).
    • In docs, use the mlir language in code blocks:
```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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mlir_pygments-1.0.0.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mlir_pygments-1.0.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file mlir_pygments-1.0.0.tar.gz.

File metadata

  • Download URL: mlir_pygments-1.0.0.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.19

File hashes

Hashes for mlir_pygments-1.0.0.tar.gz
Algorithm Hash digest
SHA256 bd518ddcdb1437a42a9beba68bd1b6a6cee48dffdc873e25185904c43ba5eedb
MD5 173b7ae2a8879b4c1035a2f17914c575
BLAKE2b-256 927519719ce387b081efaf47437dfb3cb8cb4b55886e2e49d06622ea6a11e373

See more details on using hashes here.

File details

Details for the file mlir_pygments-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mlir_pygments-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c326fcbd08950a33b4abc49386d5cffc3127985371eb012e948b406c7ce0f9a0
MD5 9bbd3c22745e5c58244683d8fad082cf
BLAKE2b-256 94f201c3bb853a8d4d7eb2236bc7ba39e56fba51f6513ad10079bc96c8425fb2

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page