Skip to content

Instantly share code, notes, and snippets.

@jamorton
Created April 6, 2012 22:37
Show Gist options
  • Save jamorton/2323601 to your computer and use it in GitHub Desktop.
Save jamorton/2323601 to your computer and use it in GitHub Desktop.
Rust Lexer for pygments
class RustLexer(RegexLexer):
"""
Lexer for Mozilla's Rust programming language
"""
name = 'Rust'
filenames = ['*.rs', '*.rc']
aliases = ['rust']
mimetypes = ['text/x-rustsrc']
tokens = {
'root': [
# Whitespace and Comments
(r'\n', Text),
(r'\s+', Text),
(r'//(.*?)\n', Comment.Single),
(r'/[*](.|\n)*?[*]/', Comment.Multiline),
# Keywords
(r'(alt|as|assert|be|break|check|claim|class|const'
r'|cont|copy|crust|do|else|enum|export|fail'
r'|false|fn|for|if|iface|impl|import|let|log'
r'|loop|mod|mut|native|pure|resource|ret|true'
r'|type|unsafe|use|white|note|bind|prove|unchecked'
r'|with|syntax)\b', Keyword),
# Character Literal
(r"""'(\\['"\\nrt]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}"""
r"""|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|.)'""",
String.Char),
# Binary Literal
(r'0[Bb][01_]+', Number, 'number_lit'),
# Octal Literal
(r'0[0-7_]+', Number.Oct, 'number_lit'),
# Hexadecimal Literal
(r'0[xX][0-9a-fA-F_]+', Number.Hex, 'number_lit'),
# Decimal Literal
(r'[0-9][0-9_]*(\.[0-9_]+[eE][+\-]?'
r'[0-9_]+|\.[0-9_]*|[eE][+\-]?[0-9_]+)?', Number, 'number_lit'),
# String Literal
(r'"', String, 'string'),
# Operators and Punctuation
(r'[{}()\[\],.;]', Punctuation),
(r'[+\-*/%&|<>^!~@=:?]', Operator),
# Identifier
(r'[a-zA-Z_$][a-zA-Z0-9_]*', Name),
# Attributes
(r'#\[', Comment.Preproc, 'attribute['),
(r'#\(', Comment.Preproc, 'attribute('),
# Macros
(r'#[A-Za-z_][A-Za-z0-9_]*\[', Comment.Preproc, 'attribute['),
(r'#[A-Za-z_][A-Za-z0-9_]*\(', Comment.Preproc, 'attribute(')
],
'number_lit': {
(r'(([ui](8|16|32|64)?)|(f(32|64)?))?', Keyword, '#pop')
},
'string': {
(r'"', String, '#pop'),
(r"""\\['"\\nrt]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}"""
r"""|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}""", String.Escape),
(r'[^\\"]+', String),
(r'\\', String)
},
'attribute_common': {
(r'"', String, 'string'),
(r'\[', Comment.Preproc, 'attribute['),
(r'\(', Comment.Preproc, 'attribute('),
},
'attribute[': {
include('attribute_common'),
(r'\];?', Comment.Preproc, '#pop'),
(r'[^"\]]+', Comment.Preproc)
},
'attribute(': {
include('attribute_common'),
(r'\);?', Comment.Preproc, '#pop'),
(r'[^"\)]+', Comment.Preproc)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment