ThirdParty/Pygments/pygments/lexers/rust.py

changeset 4697
c2e9bf425554
parent 4172
4f20dba37ab6
child 5713
6762afd9f963
equal deleted inserted replaced
4696:bf4d19a7cade 4697:c2e9bf425554
3 pygments.lexers.rust 3 pygments.lexers.rust
4 ~~~~~~~~~~~~~~~~~~~~ 4 ~~~~~~~~~~~~~~~~~~~~
5 5
6 Lexers for the Rust language. 6 Lexers for the Rust language.
7 7
8 :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. 8 :copyright: Copyright 2006-2015 by the Pygments team, see AUTHORS.
9 :license: BSD, see LICENSE for details. 9 :license: BSD, see LICENSE for details.
10 """ 10 """
11 11
12 from pygments.lexer import RegexLexer, include, bygroups, words, default 12 from pygments.lexer import RegexLexer, include, bygroups, words, default
13 from pygments.token import Comment, Operator, Keyword, Name, String, \ 13 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
14 Number, Punctuation, Whitespace 14 Number, Punctuation, Whitespace
15 15
16 __all__ = ['RustLexer'] 16 __all__ = ['RustLexer']
17 17
18 18
19 class RustLexer(RegexLexer): 19 class RustLexer(RegexLexer):
20 """ 20 """
21 Lexer for the Rust programming language (version 0.9). 21 Lexer for the Rust programming language (version 1.0).
22 22
23 .. versionadded:: 1.6 23 .. versionadded:: 1.6
24 """ 24 """
25 name = 'Rust' 25 name = 'Rust'
26 filenames = ['*.rs'] 26 filenames = ['*.rs', '*.rs.in']
27 aliases = ['rust'] 27 aliases = ['rust']
28 mimetypes = ['text/x-rustsrc'] 28 mimetypes = ['text/rust']
29 29
30 tokens = { 30 tokens = {
31 'root': [ 31 'root': [
32 # rust allows a file to start with a shebang, but if the first line
33 # starts with #![ then it’s not a shebang but a crate attribute.
34 (r'#![^[\r\n].*$', Comment.Preproc),
35 default('base'),
36 ],
37 'base': [
32 # Whitespace and Comments 38 # Whitespace and Comments
33 (r'\n', Whitespace), 39 (r'\n', Whitespace),
34 (r'\s+', Whitespace), 40 (r'\s+', Whitespace),
35 (r'//[/!](.*?)\n', Comment.Doc), 41 (r'//!.*?\n', String.Doc),
42 (r'///(\n|[^/].*?\n)', String.Doc),
36 (r'//(.*?)\n', Comment.Single), 43 (r'//(.*?)\n', Comment.Single),
44 (r'/\*\*(\n|[^/*])', String.Doc, 'doccomment'),
45 (r'/\*!', String.Doc, 'doccomment'),
37 (r'/\*', Comment.Multiline, 'comment'), 46 (r'/\*', Comment.Multiline, 'comment'),
38 47
39 # Lifetime
40 (r"""'[a-zA-Z_]\w*""", Name.Label),
41 # Macro parameters 48 # Macro parameters
42 (r"""\$([a-zA-Z_]\w*|\(,?|\),?|,?)""", Comment.Preproc), 49 (r"""\$([a-zA-Z_]\w*|\(,?|\),?|,?)""", Comment.Preproc),
43 # Keywords 50 # Keywords
44 (words(( 51 (words((
45 'as', 'box', 'break', 'continue', 'do', 'else', 'enum', 'extern', 52 'as', 'box', 'crate', 'do', 'else', 'enum', 'extern', # break and continue are in labels
46 'fn', 'for', 'if', 'impl', 'in', 'loop', 'match', 'mut', 'priv', 53 'fn', 'for', 'if', 'impl', 'in', 'loop', 'match', 'mut', 'priv',
47 'proc', 'pub', 'ref', 'return', 'static', '\'static', 'struct', 54 'proc', 'pub', 'ref', 'return', 'static', 'struct',
48 'trait', 'true', 'type', 'unsafe', 'while'), suffix=r'\b'), 55 'trait', 'true', 'type', 'unsafe', 'while'), suffix=r'\b'),
49 Keyword), 56 Keyword),
50 (words(('alignof', 'be', 'const', 'offsetof', 'pure', 'sizeof', 57 (words(('alignof', 'be', 'const', 'offsetof', 'pure', 'sizeof',
51 'typeof', 'once', 'unsized', 'yield'), suffix=r'\b'), 58 'typeof', 'once', 'unsized', 'yield'), suffix=r'\b'),
52 Keyword.Reserved), 59 Keyword.Reserved),
53 (r'(mod|use)\b', Keyword.Namespace), 60 (r'(mod|use)\b', Keyword.Namespace),
54 (r'(true|false)\b', Keyword.Constant), 61 (r'(true|false)\b', Keyword.Constant),
55 (r'let\b', Keyword.Declaration), 62 (r'let\b', Keyword.Declaration),
56 (words(('u8', 'u16', 'u32', 'u64', 'i8', 'i16', 'i32', 'i64', 'uint', 63 (words(('u8', 'u16', 'u32', 'u64', 'i8', 'i16', 'i32', 'i64', 'usize',
57 'int', 'f32', 'f64', 'str', 'bool'), suffix=r'\b'), 64 'isize', 'f32', 'f64', 'str', 'bool'), suffix=r'\b'),
58 Keyword.Type), 65 Keyword.Type),
59 (r'self\b', Name.Builtin.Pseudo), 66 (r'self\b', Name.Builtin.Pseudo),
60 # Prelude 67 # Prelude (taken from Rust’s src/libstd/prelude.rs)
61 (words(( 68 (words((
62 'Freeze', 'Pod', 'Send', 'Sized', 'Add', 'Sub', 'Mul', 'Div', 'Rem', 'Neg', 'Not', 'BitAnd', 69 # Reexported core operators
63 'BitOr', 'BitXor', 'Drop', 'Shl', 'Shr', 'Index', 'Option', 'Some', 'None', 'Result', 70 'Copy', 'Send', 'Sized', 'Sync',
64 'Ok', 'Err', 'from_str', 'range', 'print', 'println', 'Any', 'AnyOwnExt', 'AnyRefExt', 71 'Drop', 'Fn', 'FnMut', 'FnOnce',
65 'AnyMutRefExt', 'Ascii', 'AsciiCast', 'OnwedAsciiCast', 'AsciiStr', 72
66 'IntoBytes', 'Bool', 'ToCStr', 'Char', 'Clone', 'DeepClone', 'Eq', 'ApproxEq', 73 # Reexported functions
67 'Ord', 'TotalEq', 'Ordering', 'Less', 'Equal', 'Greater', 'Equiv', 'Container', 74 'drop',
68 'Mutable', 'Map', 'MutableMap', 'Set', 'MutableSet', 'Default', 'FromStr', 75
69 'Hash', 'FromIterator', 'Extendable', 'Iterator', 'DoubleEndedIterator', 76 # Reexported types and traits
70 'RandomAccessIterator', 'CloneableIterator', 'OrdIterator', 77 'Box',
71 'MutableDoubleEndedIterator', 'ExactSize', 'Times', 'Algebraic', 78 'ToOwned',
72 'Trigonometric', 'Exponential', 'Hyperbolic', 'Bitwise', 'BitCount', 79 'Clone',
73 'Bounded', 'Integer', 'Fractional', 'Real', 'RealExt', 'Num', 'NumCast', 80 'PartialEq', 'PartialOrd', 'Eq', 'Ord',
74 'CheckedAdd', 'CheckedSub', 'CheckedMul', 'Orderable', 'Signed', 81 'AsRef', 'AsMut', 'Into', 'From',
75 'Unsigned', 'Round', 'Primitive', 'Int', 'Float', 'ToStrRadix', 82 'Default',
76 'ToPrimitive', 'FromPrimitive', 'GenericPath', 'Path', 'PosixPath', 83 'Iterator', 'Extend', 'IntoIterator',
77 'WindowsPath', 'RawPtr', 'Buffer', 'Writer', 'Reader', 'Seek', 84 'DoubleEndedIterator', 'ExactSizeIterator',
78 'SendStr', 'SendStrOwned', 'SendStrStatic', 'IntoSendStr', 'Str', 85 'Option',
79 'StrVector', 'StrSlice', 'OwnedStr', 'IterBytes', 'ToStr', 'IntoStr', 86 'Some', 'None',
80 'CopyableTuple', 'ImmutableTuple', 'ImmutableEqVector', 'ImmutableTotalOrdVector', 87 'Result',
81 'ImmutableCopyableVector', 'OwnedVector', 'OwnedCopyableVector', 88 'Ok', 'Err',
82 'OwnedEqVector', 'MutableVector', 'MutableTotalOrdVector', 89 'SliceConcatExt',
83 'Vector', 'VectorVector', 'CopyableVector', 'ImmutableVector', 90 'String', 'ToString',
84 'Port', 'Chan', 'SharedChan', 'spawn', 'drop'), suffix=r'\b'), 91 'Vec',
92 ), suffix=r'\b'),
85 Name.Builtin), 93 Name.Builtin),
86 (r'(ImmutableTuple\d+|Tuple\d+)\b', Name.Builtin),
87 # Borrowed pointer
88 (r'(&)(\'[A-Za-z_]\w*)?', bygroups(Operator, Name)),
89 # Labels 94 # Labels
90 (r'\'[A-Za-z_]\w*:', Name.Label), 95 (r'(break|continue)(\s*)(\'[A-Za-z_]\w*)?', bygroups(Keyword, Text.Whitespace, Name.Label)),
91 # Character Literal 96 # Character Literal
92 (r"""'(\\['"\\nrt]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}""" 97 (r"""'(\\['"\\nrt]|\\x[0-7][0-9a-fA-F]|\\0"""
93 r"""|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|.)'""", 98 r"""|\\u\{[0-9a-fA-F]{1,6}\}|.)'""",
99 String.Char),
100 (r"""b'(\\['"\\nrt]|\\x[0-9a-fA-F]{2}|\\0"""
101 r"""|\\u\{[0-9a-fA-F]{1,6}\}|.)'""",
94 String.Char), 102 String.Char),
95 # Binary Literal 103 # Binary Literal
96 (r'0b[01_]+', Number.Bin, 'number_lit'), 104 (r'0b[01_]+', Number.Bin, 'number_lit'),
97 # Octal Literal 105 # Octal Literal
98 (r'0o[0-7_]+', Number.Oct, 'number_lit'), 106 (r'0o[0-7_]+', Number.Oct, 'number_lit'),
99 # Hexadecimal Literal 107 # Hexadecimal Literal
100 (r'0[xX][0-9a-fA-F_]+', Number.Hex, 'number_lit'), 108 (r'0[xX][0-9a-fA-F_]+', Number.Hex, 'number_lit'),
101 # Decimal Literal 109 # Decimal Literal
102 (r'[0-9][0-9_]*(\.[0-9_]+[eE][+\-]?[0-9_]+|' 110 (r'[0-9][0-9_]*(\.[0-9_]+[eE][+\-]?[0-9_]+|'
103 r'\.[0-9_]*|[eE][+\-]?[0-9_]+)', Number.Float, 'number_lit'), 111 r'\.[0-9_]*(?!\.)|[eE][+\-]?[0-9_]+)', Number.Float, 'number_lit'),
104 (r'[0-9][0-9_]*', Number.Integer, 'number_lit'), 112 (r'[0-9][0-9_]*', Number.Integer, 'number_lit'),
105 # String Literal 113 # String Literal
114 (r'b"', String, 'bytestring'),
106 (r'"', String, 'string'), 115 (r'"', String, 'string'),
107 (r'r(#*)".*?"\1', String.Raw), 116 (r'b?r(#*)".*?"\1', String),
117
118 # Lifetime
119 (r"""'static""", Name.Builtin),
120 (r"""'[a-zA-Z_]\w*""", Name.Attribute),
108 121
109 # Operators and Punctuation 122 # Operators and Punctuation
110 (r'[{}()\[\],.;]', Punctuation), 123 (r'[{}()\[\],.;]', Punctuation),
111 (r'[+\-*/%&|<>^!~@=:?]', Operator), 124 (r'[+\-*/%&|<>^!~@=:?]', Operator),
112 125
127 (r'[^*/]+', Comment.Multiline), 140 (r'[^*/]+', Comment.Multiline),
128 (r'/\*', Comment.Multiline, '#push'), 141 (r'/\*', Comment.Multiline, '#push'),
129 (r'\*/', Comment.Multiline, '#pop'), 142 (r'\*/', Comment.Multiline, '#pop'),
130 (r'[*/]', Comment.Multiline), 143 (r'[*/]', Comment.Multiline),
131 ], 144 ],
145 'doccomment': [
146 (r'[^*/]+', String.Doc),
147 (r'/\*', String.Doc, '#push'),
148 (r'\*/', String.Doc, '#pop'),
149 (r'[*/]', String.Doc),
150 ],
132 'number_lit': [ 151 'number_lit': [
133 (r'[ui](8|16|32|64)', Keyword, '#pop'), 152 (r'[ui](8|16|32|64|size)', Keyword, '#pop'),
134 (r'f(32|64)', Keyword, '#pop'), 153 (r'f(32|64)', Keyword, '#pop'),
135 default('#pop'), 154 default('#pop'),
136 ], 155 ],
137 'string': [ 156 'string': [
138 (r'"', String, '#pop'), 157 (r'"', String, '#pop'),
139 (r"""\\['"\\nrt]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}""" 158 (r"""\\['"\\nrt]|\\x[0-7][0-9a-fA-F]|\\0"""
140 r"""|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}""", String.Escape), 159 r"""|\\u\{[0-9a-fA-F]{1,6}\}""", String.Escape),
141 (r'[^\\"]+', String), 160 (r'[^\\"]+', String),
142 (r'\\', String), 161 (r'\\', String),
162 ],
163 'bytestring': [
164 (r"""\\x[89a-fA-F][0-9a-fA-F]""", String.Escape),
165 include('string'),
143 ], 166 ],
144 'macro{': [ 167 'macro{': [
145 (r'\{', Operator, '#push'), 168 (r'\{', Operator, '#push'),
146 (r'\}', Operator, '#pop'), 169 (r'\}', Operator, '#pop'),
147 ], 170 ],

eric ide

mercurial