Skip to content

Commit c7d2370

Browse files
authored
[mlir] add a simple pygments lexer (#120942)
This enables syntax highlighting of MLIR using the Pygments package in Python, which is in turn usable from LaTeX via the minted package.
1 parent b3a7ab6 commit c7d2370

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

mlir/utils/pygments/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## Pygments Lexer for MLIR
2+
3+
This file contains a simple Pygments lexer configuration for MLIR, derived from
4+
the version used in the original CGO paper. Pygments allows for advanced
5+
configurable syntax highlighting of any code. This lexer is known to be
6+
incomplete and support mostly core IR with a subset of built-in types.
7+
Additions and customizations are welcome.
8+
9+
### Standalone Usage
10+
11+
Install Pygments, e.g., by running `pip install Pygments` or a Python package
12+
manager of your choosing. Use the standalone `pygmentize` command by
13+
instructing it to load the custom lexer:
14+
15+
```
16+
pygmentize -l /path/to/mlir_lexer.py:MlirLexer -x myfile.mlir
17+
```
18+
19+
This will produce highlighted output in the terminal. Other output formats are
20+
available, see Pygments [documentation](https://pygments.org/docs/) for more
21+
information.
22+
23+
### LaTeX Usage
24+
25+
First, make sure your distribution includes the `minted` package and list in
26+
the preamble.
27+
28+
```latex
29+
\usepackage{minted}
30+
```
31+
32+
Place the `mlir_lexer.py` in a place where the `latex` binary can find it,
33+
typically in the working directory next to the main `.tex` file. Note that you
34+
will have to invoke `latex` with the `-shell-escape` flag. See the `minted`
35+
package [documentation](https://ctan.org/pkg/minted?lang=en) for more
36+
information.
37+
38+
Leverage the custom lexer facility of `minted` to use this lexer in your
39+
document as:
40+
41+
```latex
42+
\begin{minted}{mlir_lexer.py:MlirLexer -x}
43+
... your code here ...
44+
\end{minted}
45+
```

mlir/utils/pygments/mlir_lexer.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2+
# See https://llvm.org/LICENSE.txt for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
from pygments.lexer import RegexLexer
6+
from pygments.token import *
7+
8+
9+
class MlirLexer(RegexLexer):
10+
name = "MLIR"
11+
aliases = ["mlir"]
12+
filenames = ["*.mlir"]
13+
14+
tokens = {
15+
"root": [
16+
(r"%[a-zA-Z0-9_]+", Name.Variable),
17+
(r"@[a-zA-Z_][a-zA-Z0-9_]+", Name.Function),
18+
(r"\^[a-zA-Z0-9_]+", Name.Label),
19+
(r"#[a-zA-Z0-9_]+", Name.Constant),
20+
(r"![a-zA-Z0-9_]+", Keyword.Type),
21+
(r"[a-zA-Z_][a-zA-Z0-9_]*\.", Name.Entity),
22+
(r"memref[^.]", Keyword.Type),
23+
(r"index", Keyword.Type),
24+
(r"i[0-9]+", Keyword.Type),
25+
(r"f[0-9]+", Keyword.Type),
26+
(r"[0-9]+", Number.Integer),
27+
(r"[0-9]*\.[0-9]*", Number.Float),
28+
(r'"[^"]*"', String.Double),
29+
(r"affine_map", Keyword.Reserved),
30+
# TODO: this should be within affine maps only
31+
(r"\+-\*\/", Operator),
32+
(r"floordiv", Operator.Word),
33+
(r"ceildiv", Operator.Word),
34+
(r"mod", Operator.Word),
35+
(r"()\[\]<>,{}", Punctuation),
36+
(r"\/\/.*\n", Comment.Single),
37+
]
38+
}

0 commit comments

Comments
 (0)