Skip to content
This repository was archived by the owner on Jun 27, 2018. It is now read-only.

Commit 917eb9f

Browse files
committed
Merge pull request #200 from birkenfeld/mir-highlighting
Add a Pygments lexer for MIR output, and fix HTML escaping.
2 parents a08cf6b + 118a58b commit 917eb9f

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

mirlexer.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from pygments.lexer import RegexLexer
2+
from pygments.token import Whitespace, Comment, Name, Keyword, Number, \
3+
String, Punctuation, Operator, Text
4+
5+
6+
class RustMirLexer(RegexLexer):
7+
name = 'Rust MIR'
8+
filenames = []
9+
aliases = ['rustmir']
10+
mimetypes = ['text/x-rust-mir']
11+
12+
tokens = {
13+
'root': [
14+
(r'\s+', Whitespace),
15+
(r'//(.*?)\n', Comment.Single),
16+
(r'/\*', Comment.Multiline, 'comment'),
17+
(r"bb\d+:", Name.Function),
18+
(r'let\b', Keyword.Declaration),
19+
(r'(fn|as|const|goto|mut|resume|return|unwind|asm!)\b', Keyword),
20+
(r'(Box|Len|Add|Sub|Mul|Div|Rem|BitXor|BitAnd|BitOr|Shl|Shr|Eq|'
21+
r'Lt|Le|Ne|Ge|Gt|Not|Neg|drop|switch|switchInt)\b', Name.Builtin),
22+
(r'(true|false)\b', Keyword.Constant),
23+
(r'(u8|u16|u32|u64|i8|i16|i32|i64|usize|isize|f32|f64|str|bool)\b',
24+
Keyword.Type),
25+
(r"""'(\\['"\\nrt]|\\x[0-7][0-9a-fA-F]|\\0"""
26+
r"""|\\u\{[0-9a-fA-F]{1,6}\}|.)'""", String.Char),
27+
(r"""b'(\\['"\\nrt]|\\x[0-9a-fA-F]{2}|\\0"""
28+
r"""|\\u\{[0-9a-fA-F]{1,6}\}|.)'""", String.Char),
29+
(r'[0-9][0-9_]*(\.[0-9_]+[eE][+\-]?[0-9_]+|'
30+
r'\.[0-9_]*(?!\.)|[eE][+\-]?[0-9_]+)', Number.Float, 'number_lit'),
31+
(r'[0-9][0-9_]*', Number.Integer, 'number_lit'),
32+
(r'b"', String, 'bytestring'),
33+
(r'"', String, 'string'),
34+
(r"'(static|<.*?>|[a-zA-Z_]\w*)", Name.Attribute),
35+
(r'[{}()\[\],.;]', Punctuation),
36+
(r'[+\-*/%&|<>^!~@=:?]', Operator),
37+
(r'[a-zA-Z_]\w*', Name),
38+
],
39+
'comment': [
40+
(r'[^*/]+', Comment.Multiline),
41+
(r'/\*', Comment.Multiline, '#push'),
42+
(r'\*/', Comment.Multiline, '#pop'),
43+
(r'[*/]', Comment.Multiline),
44+
],
45+
'number_lit': [
46+
(r'[ui](8|16|32|64|size)', Number, '#pop'),
47+
(r'f(32|64)', Number, '#pop'),
48+
(r'', Text, '#pop'),
49+
],
50+
'string': [
51+
(r'"', String, '#pop'),
52+
(r"\\['\"\\nrt]|\\x[0-7][0-9a-fA-F]|\\0|\\u\{[0-9a-fA-F]{1,6}\}",
53+
String.Escape),
54+
(r'[^\\"]+', String),
55+
(r'\\', String),
56+
],
57+
'bytestring': [
58+
(r'"', String, '#pop'),
59+
(r"\\['\"\\nrt]|\\x[0-7][0-9a-fA-F]|\\0", String.Escape),
60+
(r'[^\\"]+', String),
61+
(r'\\', String),
62+
],
63+
}

web.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pygments import highlight
1010
from pygments.formatters import HtmlFormatter
1111
from pygments.lexers import GasLexer, LlvmLexer
12+
from mirlexer import RustMirLexer
1213

1314
import playpen
1415

@@ -153,7 +154,7 @@ def compile(emit, optimize, version, color, syntax, backtrace_str):
153154
elif emit == "llvm-ir":
154155
return {"result": highlight(split[1].decode(), LlvmLexer(), HtmlFormatter(nowrap=True))}
155156
else:
156-
return {"result": split[1].decode()}
157+
return {"result": highlight(split[1].decode(), RustMirLexer(), HtmlFormatter(nowrap=True))}
157158

158159
os.chdir(sys.path[0])
159160
run(host='0.0.0.0', port=80, server='cherrypy')

0 commit comments

Comments
 (0)