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 1.0). |
21 Lexer for the Rust programming language (version 1.10). |
22 |
22 |
23 .. versionadded:: 1.6 |
23 .. versionadded:: 1.6 |
24 """ |
24 """ |
25 name = 'Rust' |
25 name = 'Rust' |
26 filenames = ['*.rs', '*.rs.in'] |
26 filenames = ['*.rs', '*.rs.in'] |
27 aliases = ['rust'] |
27 aliases = ['rust'] |
28 mimetypes = ['text/rust'] |
28 mimetypes = ['text/rust'] |
|
29 |
|
30 keyword_types = ( |
|
31 words(('u8', 'u16', 'u32', 'u64', 'i8', 'i16', 'i32', 'i64', |
|
32 'usize', 'isize', 'f32', 'f64', 'str', 'bool'), |
|
33 suffix=r'\b'), |
|
34 Keyword.Type) |
|
35 |
|
36 builtin_types = (words(( |
|
37 # Reexported core operators |
|
38 'Copy', 'Send', 'Sized', 'Sync', |
|
39 'Drop', 'Fn', 'FnMut', 'FnOnce', |
|
40 |
|
41 # Reexported types and traits |
|
42 'Box', |
|
43 'ToOwned', |
|
44 'Clone', |
|
45 'PartialEq', 'PartialOrd', 'Eq', 'Ord', |
|
46 'AsRef', 'AsMut', 'Into', 'From', |
|
47 'Default', |
|
48 'Iterator', 'Extend', 'IntoIterator', |
|
49 'DoubleEndedIterator', 'ExactSizeIterator', |
|
50 'Option', |
|
51 'Some', 'None', |
|
52 'Result', |
|
53 'Ok', 'Err', |
|
54 'SliceConcatExt', |
|
55 'String', 'ToString', |
|
56 'Vec'), suffix=r'\b'), |
|
57 Name.Builtin) |
29 |
58 |
30 tokens = { |
59 tokens = { |
31 'root': [ |
60 'root': [ |
32 # rust allows a file to start with a shebang, but if the first line |
61 # 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. |
62 # starts with #![ then it’s not a shebang but a crate attribute. |
47 |
76 |
48 # Macro parameters |
77 # Macro parameters |
49 (r"""\$([a-zA-Z_]\w*|\(,?|\),?|,?)""", Comment.Preproc), |
78 (r"""\$([a-zA-Z_]\w*|\(,?|\),?|,?)""", Comment.Preproc), |
50 # Keywords |
79 # Keywords |
51 (words(( |
80 (words(( |
52 'as', 'box', 'crate', 'do', 'else', 'enum', 'extern', # break and continue are in labels |
81 'as', 'box', 'const', 'crate', 'else', 'extern', |
53 'fn', 'for', 'if', 'impl', 'in', 'loop', 'match', 'mut', 'priv', |
82 'for', 'if', 'impl', 'in', 'loop', 'match', 'move', |
54 'proc', 'pub', 'ref', 'return', 'static', 'struct', |
83 'mut', 'pub', 'ref', 'return', 'static', 'super', |
55 'trait', 'true', 'type', 'unsafe', 'while'), suffix=r'\b'), |
84 'trait', 'unsafe', 'use', 'where', 'while'), suffix=r'\b'), |
56 Keyword), |
85 Keyword), |
57 (words(('alignof', 'be', 'const', 'offsetof', 'pure', 'sizeof', |
86 (words(('abstract', 'alignof', 'become', 'do', 'final', 'macro', |
58 'typeof', 'once', 'unsized', 'yield'), suffix=r'\b'), |
87 'offsetof', 'override', 'priv', 'proc', 'pure', 'sizeof', |
|
88 'typeof', 'unsized', 'virtual', 'yield'), suffix=r'\b'), |
59 Keyword.Reserved), |
89 Keyword.Reserved), |
60 (r'(mod|use)\b', Keyword.Namespace), |
|
61 (r'(true|false)\b', Keyword.Constant), |
90 (r'(true|false)\b', Keyword.Constant), |
|
91 (r'mod\b', Keyword, 'modname'), |
62 (r'let\b', Keyword.Declaration), |
92 (r'let\b', Keyword.Declaration), |
63 (words(('u8', 'u16', 'u32', 'u64', 'i8', 'i16', 'i32', 'i64', 'usize', |
93 (r'fn\b', Keyword, 'funcname'), |
64 'isize', 'f32', 'f64', 'str', 'bool'), suffix=r'\b'), |
94 (r'(struct|enum|type|union)\b', Keyword, 'typename'), |
65 Keyword.Type), |
95 (r'(default)(\s+)(type|fn)\b', bygroups(Keyword, Text, Keyword)), |
|
96 keyword_types, |
66 (r'self\b', Name.Builtin.Pseudo), |
97 (r'self\b', Name.Builtin.Pseudo), |
67 # Prelude (taken from Rust’s src/libstd/prelude.rs) |
98 # Prelude (taken from Rust’s src/libstd/prelude.rs) |
68 (words(( |
99 builtin_types, |
69 # Reexported core operators |
100 # Path seperators, so types don't catch them. |
70 'Copy', 'Send', 'Sized', 'Sync', |
101 (r'::\b', Text), |
71 'Drop', 'Fn', 'FnMut', 'FnOnce', |
102 # Types in positions. |
72 |
103 (r'(?::|->)', Text, 'typename'), |
73 # Reexported functions |
|
74 'drop', |
|
75 |
|
76 # Reexported types and traits |
|
77 'Box', |
|
78 'ToOwned', |
|
79 'Clone', |
|
80 'PartialEq', 'PartialOrd', 'Eq', 'Ord', |
|
81 'AsRef', 'AsMut', 'Into', 'From', |
|
82 'Default', |
|
83 'Iterator', 'Extend', 'IntoIterator', |
|
84 'DoubleEndedIterator', 'ExactSizeIterator', |
|
85 'Option', |
|
86 'Some', 'None', |
|
87 'Result', |
|
88 'Ok', 'Err', |
|
89 'SliceConcatExt', |
|
90 'String', 'ToString', |
|
91 'Vec', |
|
92 ), suffix=r'\b'), |
|
93 Name.Builtin), |
|
94 # Labels |
104 # Labels |
95 (r'(break|continue)(\s*)(\'[A-Za-z_]\w*)?', bygroups(Keyword, Text.Whitespace, Name.Label)), |
105 (r'(break|continue)(\s*)(\'[A-Za-z_]\w*)?', |
|
106 bygroups(Keyword, Text.Whitespace, Name.Label)), |
96 # Character Literal |
107 # Character Literal |
97 (r"""'(\\['"\\nrt]|\\x[0-7][0-9a-fA-F]|\\0""" |
108 (r"""'(\\['"\\nrt]|\\x[0-7][0-9a-fA-F]|\\0""" |
98 r"""|\\u\{[0-9a-fA-F]{1,6}\}|.)'""", |
109 r"""|\\u\{[0-9a-fA-F]{1,6}\}|.)'""", |
99 String.Char), |
110 String.Char), |
100 (r"""b'(\\['"\\nrt]|\\x[0-9a-fA-F]{2}|\\0""" |
111 (r"""b'(\\['"\\nrt]|\\x[0-9a-fA-F]{2}|\\0""" |
106 (r'0o[0-7_]+', Number.Oct, 'number_lit'), |
117 (r'0o[0-7_]+', Number.Oct, 'number_lit'), |
107 # Hexadecimal Literal |
118 # Hexadecimal Literal |
108 (r'0[xX][0-9a-fA-F_]+', Number.Hex, 'number_lit'), |
119 (r'0[xX][0-9a-fA-F_]+', Number.Hex, 'number_lit'), |
109 # Decimal Literal |
120 # Decimal Literal |
110 (r'[0-9][0-9_]*(\.[0-9_]+[eE][+\-]?[0-9_]+|' |
121 (r'[0-9][0-9_]*(\.[0-9_]+[eE][+\-]?[0-9_]+|' |
111 r'\.[0-9_]*(?!\.)|[eE][+\-]?[0-9_]+)', Number.Float, 'number_lit'), |
122 r'\.[0-9_]*(?!\.)|[eE][+\-]?[0-9_]+)', Number.Float, |
|
123 'number_lit'), |
112 (r'[0-9][0-9_]*', Number.Integer, 'number_lit'), |
124 (r'[0-9][0-9_]*', Number.Integer, 'number_lit'), |
113 # String Literal |
125 # String Literal |
114 (r'b"', String, 'bytestring'), |
126 (r'b"', String, 'bytestring'), |
115 (r'"', String, 'string'), |
127 (r'"', String, 'string'), |
116 (r'b?r(#*)".*?"\1', String), |
128 (r'b?r(#*)".*?"\1', String), |
146 (r'[^*/]+', String.Doc), |
158 (r'[^*/]+', String.Doc), |
147 (r'/\*', String.Doc, '#push'), |
159 (r'/\*', String.Doc, '#push'), |
148 (r'\*/', String.Doc, '#pop'), |
160 (r'\*/', String.Doc, '#pop'), |
149 (r'[*/]', String.Doc), |
161 (r'[*/]', String.Doc), |
150 ], |
162 ], |
|
163 'modname': [ |
|
164 (r'\s+', Text), |
|
165 (r'[a-zA-Z_]\w*', Name.Namespace, '#pop'), |
|
166 default('#pop'), |
|
167 ], |
|
168 'funcname': [ |
|
169 (r'\s+', Text), |
|
170 (r'[a-zA-Z_]\w*', Name.Function, '#pop'), |
|
171 default('#pop'), |
|
172 ], |
|
173 'typename': [ |
|
174 (r'\s+', Text), |
|
175 (r'&', Keyword.Pseudo), |
|
176 builtin_types, |
|
177 keyword_types, |
|
178 (r'[a-zA-Z_]\w*', Name.Class, '#pop'), |
|
179 default('#pop'), |
|
180 ], |
151 'number_lit': [ |
181 'number_lit': [ |
152 (r'[ui](8|16|32|64|size)', Keyword, '#pop'), |
182 (r'[ui](8|16|32|64|size)', Keyword, '#pop'), |
153 (r'f(32|64)', Keyword, '#pop'), |
183 (r'f(32|64)', Keyword, '#pop'), |
154 default('#pop'), |
184 default('#pop'), |
155 ], |
185 ], |