2014-12-01 06:10:30 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
pygments.lexers.rust
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
Lexers for the Rust language.
|
|
|
|
|
|
2017-02-14 07:25:51 +00:00
|
|
|
|
:copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS.
|
2014-12-01 06:10:30 +00:00
|
|
|
|
:license: BSD, see LICENSE for details.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from pygments.lexer import RegexLexer, include, bygroups, words, default
|
2016-06-13 14:41:17 +00:00
|
|
|
|
from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
|
2014-12-01 06:10:30 +00:00
|
|
|
|
Number, Punctuation, Whitespace
|
|
|
|
|
|
|
|
|
|
__all__ = ['RustLexer']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RustLexer(RegexLexer):
|
|
|
|
|
"""
|
2017-02-14 07:25:51 +00:00
|
|
|
|
Lexer for the Rust programming language (version 1.10).
|
2014-12-01 06:10:30 +00:00
|
|
|
|
|
|
|
|
|
.. versionadded:: 1.6
|
|
|
|
|
"""
|
|
|
|
|
name = 'Rust'
|
2016-06-13 14:41:17 +00:00
|
|
|
|
filenames = ['*.rs', '*.rs.in']
|
2014-12-01 06:10:30 +00:00
|
|
|
|
aliases = ['rust']
|
2016-06-13 14:41:17 +00:00
|
|
|
|
mimetypes = ['text/rust']
|
2014-12-01 06:10:30 +00:00
|
|
|
|
|
2017-02-14 07:25:51 +00:00
|
|
|
|
keyword_types = (
|
|
|
|
|
words(('u8', 'u16', 'u32', 'u64', 'i8', 'i16', 'i32', 'i64',
|
|
|
|
|
'usize', 'isize', 'f32', 'f64', 'str', 'bool'),
|
|
|
|
|
suffix=r'\b'),
|
|
|
|
|
Keyword.Type)
|
|
|
|
|
|
|
|
|
|
builtin_types = (words((
|
|
|
|
|
# Reexported core operators
|
|
|
|
|
'Copy', 'Send', 'Sized', 'Sync',
|
|
|
|
|
'Drop', 'Fn', 'FnMut', 'FnOnce',
|
|
|
|
|
|
|
|
|
|
# Reexported types and traits
|
|
|
|
|
'Box',
|
|
|
|
|
'ToOwned',
|
|
|
|
|
'Clone',
|
|
|
|
|
'PartialEq', 'PartialOrd', 'Eq', 'Ord',
|
|
|
|
|
'AsRef', 'AsMut', 'Into', 'From',
|
|
|
|
|
'Default',
|
|
|
|
|
'Iterator', 'Extend', 'IntoIterator',
|
|
|
|
|
'DoubleEndedIterator', 'ExactSizeIterator',
|
|
|
|
|
'Option',
|
|
|
|
|
'Some', 'None',
|
|
|
|
|
'Result',
|
|
|
|
|
'Ok', 'Err',
|
|
|
|
|
'SliceConcatExt',
|
|
|
|
|
'String', 'ToString',
|
|
|
|
|
'Vec'), suffix=r'\b'),
|
|
|
|
|
Name.Builtin)
|
|
|
|
|
|
2014-12-01 06:10:30 +00:00
|
|
|
|
tokens = {
|
|
|
|
|
'root': [
|
2016-06-13 14:41:17 +00:00
|
|
|
|
# rust allows a file to start with a shebang, but if the first line
|
|
|
|
|
# starts with #![ then it’s not a shebang but a crate attribute.
|
|
|
|
|
(r'#![^[\r\n].*$', Comment.Preproc),
|
|
|
|
|
default('base'),
|
|
|
|
|
],
|
|
|
|
|
'base': [
|
2014-12-01 06:10:30 +00:00
|
|
|
|
# Whitespace and Comments
|
|
|
|
|
(r'\n', Whitespace),
|
|
|
|
|
(r'\s+', Whitespace),
|
2016-06-13 14:41:17 +00:00
|
|
|
|
(r'//!.*?\n', String.Doc),
|
|
|
|
|
(r'///(\n|[^/].*?\n)', String.Doc),
|
2014-12-01 06:10:30 +00:00
|
|
|
|
(r'//(.*?)\n', Comment.Single),
|
2016-06-13 14:41:17 +00:00
|
|
|
|
(r'/\*\*(\n|[^/*])', String.Doc, 'doccomment'),
|
|
|
|
|
(r'/\*!', String.Doc, 'doccomment'),
|
2014-12-01 06:10:30 +00:00
|
|
|
|
(r'/\*', Comment.Multiline, 'comment'),
|
|
|
|
|
|
|
|
|
|
# Macro parameters
|
|
|
|
|
(r"""\$([a-zA-Z_]\w*|\(,?|\),?|,?)""", Comment.Preproc),
|
|
|
|
|
# Keywords
|
|
|
|
|
(words((
|
2017-02-14 07:25:51 +00:00
|
|
|
|
'as', 'box', 'const', 'crate', 'else', 'extern',
|
|
|
|
|
'for', 'if', 'impl', 'in', 'loop', 'match', 'move',
|
|
|
|
|
'mut', 'pub', 'ref', 'return', 'static', 'super',
|
|
|
|
|
'trait', 'unsafe', 'use', 'where', 'while'), suffix=r'\b'),
|
2014-12-01 06:10:30 +00:00
|
|
|
|
Keyword),
|
2017-02-14 07:25:51 +00:00
|
|
|
|
(words(('abstract', 'alignof', 'become', 'do', 'final', 'macro',
|
|
|
|
|
'offsetof', 'override', 'priv', 'proc', 'pure', 'sizeof',
|
|
|
|
|
'typeof', 'unsized', 'virtual', 'yield'), suffix=r'\b'),
|
2014-12-01 06:10:30 +00:00
|
|
|
|
Keyword.Reserved),
|
|
|
|
|
(r'(true|false)\b', Keyword.Constant),
|
2017-02-14 07:25:51 +00:00
|
|
|
|
(r'mod\b', Keyword, 'modname'),
|
2014-12-01 06:10:30 +00:00
|
|
|
|
(r'let\b', Keyword.Declaration),
|
2017-02-14 07:25:51 +00:00
|
|
|
|
(r'fn\b', Keyword, 'funcname'),
|
|
|
|
|
(r'(struct|enum|type|union)\b', Keyword, 'typename'),
|
|
|
|
|
(r'(default)(\s+)(type|fn)\b', bygroups(Keyword, Text, Keyword)),
|
|
|
|
|
keyword_types,
|
2014-12-01 06:10:30 +00:00
|
|
|
|
(r'self\b', Name.Builtin.Pseudo),
|
2016-06-13 14:41:17 +00:00
|
|
|
|
# Prelude (taken from Rust’s src/libstd/prelude.rs)
|
2017-02-14 07:25:51 +00:00
|
|
|
|
builtin_types,
|
|
|
|
|
# Path seperators, so types don't catch them.
|
|
|
|
|
(r'::\b', Text),
|
|
|
|
|
# Types in positions.
|
|
|
|
|
(r'(?::|->)', Text, 'typename'),
|
2014-12-01 06:10:30 +00:00
|
|
|
|
# Labels
|
2017-02-14 07:25:51 +00:00
|
|
|
|
(r'(break|continue)(\s*)(\'[A-Za-z_]\w*)?',
|
|
|
|
|
bygroups(Keyword, Text.Whitespace, Name.Label)),
|
2014-12-01 06:10:30 +00:00
|
|
|
|
# Character Literal
|
2016-06-13 14:41:17 +00:00
|
|
|
|
(r"""'(\\['"\\nrt]|\\x[0-7][0-9a-fA-F]|\\0"""
|
|
|
|
|
r"""|\\u\{[0-9a-fA-F]{1,6}\}|.)'""",
|
|
|
|
|
String.Char),
|
|
|
|
|
(r"""b'(\\['"\\nrt]|\\x[0-9a-fA-F]{2}|\\0"""
|
|
|
|
|
r"""|\\u\{[0-9a-fA-F]{1,6}\}|.)'""",
|
2014-12-01 06:10:30 +00:00
|
|
|
|
String.Char),
|
|
|
|
|
# Binary Literal
|
|
|
|
|
(r'0b[01_]+', Number.Bin, 'number_lit'),
|
|
|
|
|
# Octal Literal
|
|
|
|
|
(r'0o[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][+\-]?[0-9_]+|'
|
2017-02-14 07:25:51 +00:00
|
|
|
|
r'\.[0-9_]*(?!\.)|[eE][+\-]?[0-9_]+)', Number.Float,
|
|
|
|
|
'number_lit'),
|
2014-12-01 06:10:30 +00:00
|
|
|
|
(r'[0-9][0-9_]*', Number.Integer, 'number_lit'),
|
|
|
|
|
# String Literal
|
2016-06-13 14:41:17 +00:00
|
|
|
|
(r'b"', String, 'bytestring'),
|
2014-12-01 06:10:30 +00:00
|
|
|
|
(r'"', String, 'string'),
|
2016-06-13 14:41:17 +00:00
|
|
|
|
(r'b?r(#*)".*?"\1', String),
|
|
|
|
|
|
|
|
|
|
# Lifetime
|
|
|
|
|
(r"""'static""", Name.Builtin),
|
|
|
|
|
(r"""'[a-zA-Z_]\w*""", Name.Attribute),
|
2014-12-01 06:10:30 +00:00
|
|
|
|
|
|
|
|
|
# Operators and Punctuation
|
|
|
|
|
(r'[{}()\[\],.;]', Punctuation),
|
|
|
|
|
(r'[+\-*/%&|<>^!~@=:?]', Operator),
|
|
|
|
|
|
|
|
|
|
# Identifier
|
|
|
|
|
(r'[a-zA-Z_]\w*', Name),
|
|
|
|
|
|
|
|
|
|
# Attributes
|
|
|
|
|
(r'#!?\[', Comment.Preproc, 'attribute['),
|
|
|
|
|
# Macros
|
|
|
|
|
(r'([A-Za-z_]\w*)(!)(\s*)([A-Za-z_]\w*)?(\s*)(\{)',
|
|
|
|
|
bygroups(Comment.Preproc, Punctuation, Whitespace, Name,
|
|
|
|
|
Whitespace, Punctuation), 'macro{'),
|
|
|
|
|
(r'([A-Za-z_]\w*)(!)(\s*)([A-Za-z_]\w*)?(\()',
|
|
|
|
|
bygroups(Comment.Preproc, Punctuation, Whitespace, Name,
|
|
|
|
|
Punctuation), 'macro('),
|
|
|
|
|
],
|
|
|
|
|
'comment': [
|
|
|
|
|
(r'[^*/]+', Comment.Multiline),
|
|
|
|
|
(r'/\*', Comment.Multiline, '#push'),
|
|
|
|
|
(r'\*/', Comment.Multiline, '#pop'),
|
|
|
|
|
(r'[*/]', Comment.Multiline),
|
|
|
|
|
],
|
2016-06-13 14:41:17 +00:00
|
|
|
|
'doccomment': [
|
|
|
|
|
(r'[^*/]+', String.Doc),
|
|
|
|
|
(r'/\*', String.Doc, '#push'),
|
|
|
|
|
(r'\*/', String.Doc, '#pop'),
|
|
|
|
|
(r'[*/]', String.Doc),
|
|
|
|
|
],
|
2017-02-14 07:25:51 +00:00
|
|
|
|
'modname': [
|
|
|
|
|
(r'\s+', Text),
|
|
|
|
|
(r'[a-zA-Z_]\w*', Name.Namespace, '#pop'),
|
|
|
|
|
default('#pop'),
|
|
|
|
|
],
|
|
|
|
|
'funcname': [
|
|
|
|
|
(r'\s+', Text),
|
|
|
|
|
(r'[a-zA-Z_]\w*', Name.Function, '#pop'),
|
|
|
|
|
default('#pop'),
|
|
|
|
|
],
|
|
|
|
|
'typename': [
|
|
|
|
|
(r'\s+', Text),
|
|
|
|
|
(r'&', Keyword.Pseudo),
|
|
|
|
|
builtin_types,
|
|
|
|
|
keyword_types,
|
|
|
|
|
(r'[a-zA-Z_]\w*', Name.Class, '#pop'),
|
|
|
|
|
default('#pop'),
|
|
|
|
|
],
|
2014-12-01 06:10:30 +00:00
|
|
|
|
'number_lit': [
|
2016-06-13 14:41:17 +00:00
|
|
|
|
(r'[ui](8|16|32|64|size)', Keyword, '#pop'),
|
2014-12-01 06:10:30 +00:00
|
|
|
|
(r'f(32|64)', Keyword, '#pop'),
|
|
|
|
|
default('#pop'),
|
|
|
|
|
],
|
|
|
|
|
'string': [
|
|
|
|
|
(r'"', String, '#pop'),
|
2016-06-13 14:41:17 +00:00
|
|
|
|
(r"""\\['"\\nrt]|\\x[0-7][0-9a-fA-F]|\\0"""
|
|
|
|
|
r"""|\\u\{[0-9a-fA-F]{1,6}\}""", String.Escape),
|
2014-12-01 06:10:30 +00:00
|
|
|
|
(r'[^\\"]+', String),
|
|
|
|
|
(r'\\', String),
|
|
|
|
|
],
|
2016-06-13 14:41:17 +00:00
|
|
|
|
'bytestring': [
|
|
|
|
|
(r"""\\x[89a-fA-F][0-9a-fA-F]""", String.Escape),
|
|
|
|
|
include('string'),
|
|
|
|
|
],
|
2014-12-01 06:10:30 +00:00
|
|
|
|
'macro{': [
|
|
|
|
|
(r'\{', Operator, '#push'),
|
|
|
|
|
(r'\}', Operator, '#pop'),
|
|
|
|
|
],
|
|
|
|
|
'macro(': [
|
|
|
|
|
(r'\(', Operator, '#push'),
|
|
|
|
|
(r'\)', Operator, '#pop'),
|
|
|
|
|
],
|
|
|
|
|
'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),
|
|
|
|
|
],
|
|
|
|
|
}
|