|
| 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 | + } |
0 commit comments