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.10). |
21 Lexer for the Rust programming language (version 1.40). |
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', 'rs'] |
27 aliases = ['rust', 'rs'] |
28 mimetypes = ['text/rust'] |
28 mimetypes = ['text/rust', 'text/x-rust'] |
29 |
29 |
30 keyword_types = ( |
30 keyword_types = (words(( |
31 words(('u8', 'u16', 'u32', 'u64', 'i8', 'i16', 'i32', 'i64', |
31 'u8', 'u16', 'u32', 'u64', 'u128', 'i8', 'i16', 'i32', 'i64', 'i128', |
32 'usize', 'isize', 'f32', 'f64', 'str', 'bool'), |
32 'usize', 'isize', 'f32', 'f64', 'char', 'str', 'bool', |
33 suffix=r'\b'), |
33 ), suffix=r'\b'), Keyword.Type) |
34 Keyword.Type) |
|
35 |
34 |
36 builtin_types = (words(( |
35 builtin_types = (words(( |
37 # Reexported core operators |
36 'Send', 'Sized', 'Sync', 'Unpin', |
38 'Copy', 'Send', 'Sized', 'Sync', |
|
39 'Drop', 'Fn', 'FnMut', 'FnOnce', |
37 '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', |
38 'AsRef', 'AsMut', 'Into', 'From', |
47 'Default', |
39 'Iterator', 'Extend', 'IntoIterator', 'DoubleEndedIterator', |
48 'Iterator', 'Extend', 'IntoIterator', |
40 'ExactSizeIterator', 'Option', 'Result', |
49 'DoubleEndedIterator', 'ExactSizeIterator', |
41 'Box', 'ToOwned', 'String', 'ToString', 'Vec', |
50 'Option', |
42 'Clone', 'Copy', 'Default', 'Eq', 'Hash', 'Ord', 'PartialEq', |
51 'Some', 'None', |
43 'PartialOrd', 'Eq', 'Ord', |
52 'Result', |
44 ), suffix=r'\b'), Name.Builtin) |
53 'Ok', 'Err', |
45 |
54 'SliceConcatExt', |
46 builtin_funcs_macros = (words(( |
55 'String', 'ToString', |
47 'drop', 'Some', 'None', 'Ok', 'Err', |
56 'Vec'), suffix=r'\b'), |
48 'asm!', 'assert!', 'assert_eq!', 'assert_ne!', 'cfg!', 'column!', |
57 Name.Builtin) |
49 'compile_error!', 'concat!', 'concat_idents!', 'dbg!', 'debug_assert!', |
|
50 'debug_assert_eq!', 'debug_assert_ne!', 'env!', 'eprint!', 'eprintln!', |
|
51 'file!', 'format_args!', 'format_args_nl!', 'global_asm!', 'include!', |
|
52 'include_bytes!', 'include_str!', 'line!', 'log_syntax!', |
|
53 'module_path!', 'option_env!', 'panic!', 'print!', 'println!', |
|
54 'stringify!', 'thread_local!', 'todo!', 'trace_macros!', |
|
55 'unimplemented!', 'unreachable!', 'vec!', 'write!', 'writeln!', |
|
56 ), suffix=r'\b'), Name.Builtin) |
58 |
57 |
59 tokens = { |
58 tokens = { |
60 'root': [ |
59 'root': [ |
61 # rust allows a file to start with a shebang, but if the first line |
60 # rust allows a file to start with a shebang, but if the first line |
62 # starts with #![ then it’s not a shebang but a crate attribute. |
61 # starts with #![ then it's not a shebang but a crate attribute. |
63 (r'#![^[\r\n].*$', Comment.Preproc), |
62 (r'#![^[\r\n].*$', Comment.Preproc), |
64 default('base'), |
63 default('base'), |
65 ], |
64 ], |
66 'base': [ |
65 'base': [ |
67 # Whitespace and Comments |
66 # Whitespace and Comments |
76 |
75 |
77 # Macro parameters |
76 # Macro parameters |
78 (r"""\$([a-zA-Z_]\w*|\(,?|\),?|,?)""", Comment.Preproc), |
77 (r"""\$([a-zA-Z_]\w*|\(,?|\),?|,?)""", Comment.Preproc), |
79 # Keywords |
78 # Keywords |
80 (words(( |
79 (words(( |
81 'as', 'box', 'const', 'crate', 'else', 'extern', |
80 'as', 'async', 'await', 'box', 'const', 'crate', 'dyn', 'else', |
82 'for', 'if', 'impl', 'in', 'loop', 'match', 'move', |
81 'extern', 'for', 'if', 'impl', 'in', 'loop', 'match', 'move', |
83 'mut', 'pub', 'ref', 'return', 'static', 'super', |
82 'mut', 'pub', 'ref', 'return', 'static', 'super', 'trait', |
84 'trait', 'unsafe', 'use', 'where', 'while'), suffix=r'\b'), |
83 'try', 'unsafe', 'use', 'where', 'while', 'macro_rules!', |
85 Keyword), |
84 ), suffix=r'\b'), Keyword), |
86 (words(('abstract', 'alignof', 'become', 'do', 'final', 'macro', |
85 (words(('abstract', 'alignof', 'become', 'do', 'final', 'macro', |
87 'offsetof', 'override', 'priv', 'proc', 'pure', 'sizeof', |
86 'offsetof', 'override', 'priv', 'proc', 'pure', 'sizeof', |
88 'typeof', 'unsized', 'virtual', 'yield'), suffix=r'\b'), |
87 'typeof', 'unsized', 'virtual', 'yield'), suffix=r'\b'), |
89 Keyword.Reserved), |
88 Keyword.Reserved), |
90 (r'(true|false)\b', Keyword.Constant), |
89 (r'(true|false)\b', Keyword.Constant), |
92 (r'let\b', Keyword.Declaration), |
91 (r'let\b', Keyword.Declaration), |
93 (r'fn\b', Keyword, 'funcname'), |
92 (r'fn\b', Keyword, 'funcname'), |
94 (r'(struct|enum|type|union)\b', Keyword, 'typename'), |
93 (r'(struct|enum|type|union)\b', Keyword, 'typename'), |
95 (r'(default)(\s+)(type|fn)\b', bygroups(Keyword, Text, Keyword)), |
94 (r'(default)(\s+)(type|fn)\b', bygroups(Keyword, Text, Keyword)), |
96 keyword_types, |
95 keyword_types, |
97 (r'self\b', Name.Builtin.Pseudo), |
96 (r'[sS]elf\b', Name.Builtin.Pseudo), |
98 # Prelude (taken from Rust’s src/libstd/prelude.rs) |
97 # Prelude (taken from Rust's src/libstd/prelude.rs) |
99 builtin_types, |
98 builtin_types, |
|
99 builtin_funcs_macros, |
100 # Path seperators, so types don't catch them. |
100 # Path seperators, so types don't catch them. |
101 (r'::\b', Text), |
101 (r'::\b', Text), |
102 # Types in positions. |
102 # Types in positions. |
103 (r'(?::|->)', Text, 'typename'), |
103 (r'(?::|->)', Text, 'typename'), |
104 # Labels |
104 # Labels |
105 (r'(break|continue)(\s*)(\'[A-Za-z_]\w*)?', |
105 (r'(break|continue)(\s*)(\'[A-Za-z_]\w*)?', |
106 bygroups(Keyword, Text.Whitespace, Name.Label)), |
106 bygroups(Keyword, Text.Whitespace, Name.Label)), |
107 # Character Literal |
107 |
|
108 # Character literals |
108 (r"""'(\\['"\\nrt]|\\x[0-7][0-9a-fA-F]|\\0""" |
109 (r"""'(\\['"\\nrt]|\\x[0-7][0-9a-fA-F]|\\0""" |
109 r"""|\\u\{[0-9a-fA-F]{1,6}\}|.)'""", |
110 r"""|\\u\{[0-9a-fA-F]{1,6}\}|.)'""", |
110 String.Char), |
111 String.Char), |
111 (r"""b'(\\['"\\nrt]|\\x[0-9a-fA-F]{2}|\\0""" |
112 (r"""b'(\\['"\\nrt]|\\x[0-9a-fA-F]{2}|\\0""" |
112 r"""|\\u\{[0-9a-fA-F]{1,6}\}|.)'""", |
113 r"""|\\u\{[0-9a-fA-F]{1,6}\}|.)'""", |
113 String.Char), |
114 String.Char), |
114 # Binary Literal |
115 |
|
116 # Binary literals |
115 (r'0b[01_]+', Number.Bin, 'number_lit'), |
117 (r'0b[01_]+', Number.Bin, 'number_lit'), |
116 # Octal Literal |
118 # Octal literals |
117 (r'0o[0-7_]+', Number.Oct, 'number_lit'), |
119 (r'0o[0-7_]+', Number.Oct, 'number_lit'), |
118 # Hexadecimal Literal |
120 # Hexadecimal literals |
119 (r'0[xX][0-9a-fA-F_]+', Number.Hex, 'number_lit'), |
121 (r'0[xX][0-9a-fA-F_]+', Number.Hex, 'number_lit'), |
120 # Decimal Literal |
122 # Decimal literals |
121 (r'[0-9][0-9_]*(\.[0-9_]+[eE][+\-]?[0-9_]+|' |
123 (r'[0-9][0-9_]*(\.[0-9_]+[eE][+\-]?[0-9_]+|' |
122 r'\.[0-9_]*(?!\.)|[eE][+\-]?[0-9_]+)', Number.Float, |
124 r'\.[0-9_]*(?!\.)|[eE][+\-]?[0-9_]+)', Number.Float, |
123 'number_lit'), |
125 'number_lit'), |
124 (r'[0-9][0-9_]*', Number.Integer, 'number_lit'), |
126 (r'[0-9][0-9_]*', Number.Integer, 'number_lit'), |
125 # String Literal |
127 |
|
128 # String literals |
126 (r'b"', String, 'bytestring'), |
129 (r'b"', String, 'bytestring'), |
127 (r'"', String, 'string'), |
130 (r'"', String, 'string'), |
128 (r'b?r(#*)".*?"\1', String), |
131 (r'b?r(#*)".*?"\1', String), |
129 |
132 |
130 # Lifetime |
133 # Lifetime names |
131 (r"""'static""", Name.Builtin), |
134 (r"'(static|_)", Name.Builtin), |
132 (r"""'[a-zA-Z_]\w*""", Name.Attribute), |
135 (r"'[a-zA-Z_]\w*", Name.Attribute), |
133 |
136 |
134 # Operators and Punctuation |
137 # Operators and Punctuation |
|
138 (r'\.\.=?', Operator), |
135 (r'[{}()\[\],.;]', Punctuation), |
139 (r'[{}()\[\],.;]', Punctuation), |
136 (r'[+\-*/%&|<>^!~@=:?]', Operator), |
140 (r'[+\-*/%&|<>^!~@=:?]', Operator), |
137 |
141 |
138 # Identifier |
142 # Identifiers |
139 (r'[a-zA-Z_]\w*', Name), |
143 (r'[a-zA-Z_]\w*', Name), |
|
144 # Raw identifiers |
|
145 (r'r#[a-zA-Z_]\w*', Name), |
140 |
146 |
141 # Attributes |
147 # Attributes |
142 (r'#!?\[', Comment.Preproc, 'attribute['), |
148 (r'#!?\[', Comment.Preproc, 'attribute['), |
143 # Macros |
|
144 (r'([A-Za-z_]\w*)(!)(\s*)([A-Za-z_]\w*)?(\s*)(\{)', |
|
145 bygroups(Comment.Preproc, Punctuation, Whitespace, Name, |
|
146 Whitespace, Punctuation), 'macro{'), |
|
147 (r'([A-Za-z_]\w*)(!)(\s*)([A-Za-z_]\w*)?(\()', |
|
148 bygroups(Comment.Preproc, Punctuation, Whitespace, Name, |
|
149 Punctuation), 'macro('), |
|
150 ], |
149 ], |
151 'comment': [ |
150 'comment': [ |
152 (r'[^*/]+', Comment.Multiline), |
151 (r'[^*/]+', Comment.Multiline), |
153 (r'/\*', Comment.Multiline, '#push'), |
152 (r'/\*', Comment.Multiline, '#push'), |
154 (r'\*/', Comment.Multiline, '#pop'), |
153 (r'\*/', Comment.Multiline, '#pop'), |