eric6/ThirdParty/Pygments/pygments/lexers/rust.py

changeset 8258
82b608e352ec
parent 8257
28146736bbfc
child 8259
2bbec88047dd
equal deleted inserted replaced
8257:28146736bbfc 8258:82b608e352ec
1 # -*- coding: utf-8 -*-
2 """
3 pygments.lexers.rust
4 ~~~~~~~~~~~~~~~~~~~~
5
6 Lexers for the Rust language.
7
8 :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
9 :license: BSD, see LICENSE for details.
10 """
11
12 from pygments.lexer import RegexLexer, include, bygroups, words, default
13 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
14 Number, Punctuation, Whitespace
15
16 __all__ = ['RustLexer']
17
18
19 class RustLexer(RegexLexer):
20 """
21 Lexer for the Rust programming language (version 1.47).
22
23 .. versionadded:: 1.6
24 """
25 name = 'Rust'
26 filenames = ['*.rs', '*.rs.in']
27 aliases = ['rust', 'rs']
28 mimetypes = ['text/rust', 'text/x-rust']
29
30 keyword_types = (words((
31 'u8', 'u16', 'u32', 'u64', 'u128', 'i8', 'i16', 'i32', 'i64', 'i128',
32 'usize', 'isize', 'f32', 'f64', 'char', 'str', 'bool',
33 ), suffix=r'\b'), Keyword.Type)
34
35 builtin_funcs_types = (words((
36 'Copy', 'Send', 'Sized', 'Sync', 'Unpin',
37 'Drop', 'Fn', 'FnMut', 'FnOnce', 'drop',
38 'Box', 'ToOwned', 'Clone',
39 'PartialEq', 'PartialOrd', 'Eq', 'Ord',
40 'AsRef', 'AsMut', 'Into', 'From', 'Default',
41 'Iterator', 'Extend', 'IntoIterator', 'DoubleEndedIterator',
42 'ExactSizeIterator',
43 'Option', 'Some', 'None',
44 'Result', 'Ok', 'Err',
45 'String', 'ToString', 'Vec',
46 ), suffix=r'\b'), Name.Builtin)
47
48 builtin_macros = (words((
49 'asm', 'assert', 'assert_eq', 'assert_ne', 'cfg', 'column',
50 'compile_error', 'concat', 'concat_idents', 'dbg', 'debug_assert',
51 'debug_assert_eq', 'debug_assert_ne', 'env', 'eprint', 'eprintln',
52 'file', 'format', 'format_args', 'format_args_nl', 'global_asm',
53 'include', 'include_bytes', 'include_str',
54 'is_aarch64_feature_detected',
55 'is_arm_feature_detected',
56 'is_mips64_feature_detected',
57 'is_mips_feature_detected',
58 'is_powerpc64_feature_detected',
59 'is_powerpc_feature_detected',
60 'is_x86_feature_detected',
61 'line', 'llvm_asm', 'log_syntax', 'macro_rules', 'matches',
62 'module_path', 'option_env', 'panic', 'print', 'println', 'stringify',
63 'thread_local', 'todo', 'trace_macros', 'unimplemented', 'unreachable',
64 'vec', 'write', 'writeln',
65 ), suffix=r'!'), Name.Function.Magic)
66
67 tokens = {
68 'root': [
69 # rust allows a file to start with a shebang, but if the first line
70 # starts with #![ then it's not a shebang but a crate attribute.
71 (r'#![^[\r\n].*$', Comment.Preproc),
72 default('base'),
73 ],
74 'base': [
75 # Whitespace and Comments
76 (r'\n', Whitespace),
77 (r'\s+', Whitespace),
78 (r'//!.*?\n', String.Doc),
79 (r'///(\n|[^/].*?\n)', String.Doc),
80 (r'//(.*?)\n', Comment.Single),
81 (r'/\*\*(\n|[^/*])', String.Doc, 'doccomment'),
82 (r'/\*!', String.Doc, 'doccomment'),
83 (r'/\*', Comment.Multiline, 'comment'),
84
85 # Macro parameters
86 (r"""\$([a-zA-Z_]\w*|\(,?|\),?|,?)""", Comment.Preproc),
87 # Keywords
88 (words(('as', 'async', 'await', 'box', 'const', 'crate', 'dyn',
89 'else', 'extern', 'for', 'if', 'impl', 'in', 'loop',
90 'match', 'move', 'mut', 'pub', 'ref', 'return', 'static',
91 'super', 'trait', 'unsafe', 'use', 'where', 'while'),
92 suffix=r'\b'), Keyword),
93 (words(('abstract', 'become', 'do', 'final', 'macro', 'override',
94 'priv', 'typeof', 'try', 'unsized', 'virtual', 'yield'),
95 suffix=r'\b'), Keyword.Reserved),
96 (r'(true|false)\b', Keyword.Constant),
97 (r'self\b', Name.Builtin.Pseudo),
98 (r'mod\b', Keyword, 'modname'),
99 (r'let\b', Keyword.Declaration),
100 (r'fn\b', Keyword, 'funcname'),
101 (r'(struct|enum|type|union)\b', Keyword, 'typename'),
102 (r'(default)(\s+)(type|fn)\b', bygroups(Keyword, Text, Keyword)),
103 keyword_types,
104 (r'[sS]elf\b', Name.Builtin.Pseudo),
105 # Prelude (taken from Rust's src/libstd/prelude.rs)
106 builtin_funcs_types,
107 builtin_macros,
108 # Path seperators, so types don't catch them.
109 (r'::\b', Text),
110 # Types in positions.
111 (r'(?::|->)', Text, 'typename'),
112 # Labels
113 (r'(break|continue)(\s*)(\'[A-Za-z_]\w*)?',
114 bygroups(Keyword, Text.Whitespace, Name.Label)),
115
116 # Character literals
117 (r"""'(\\['"\\nrt]|\\x[0-7][0-9a-fA-F]|\\0"""
118 r"""|\\u\{[0-9a-fA-F]{1,6}\}|.)'""",
119 String.Char),
120 (r"""b'(\\['"\\nrt]|\\x[0-9a-fA-F]{2}|\\0"""
121 r"""|\\u\{[0-9a-fA-F]{1,6}\}|.)'""",
122 String.Char),
123
124 # Binary literals
125 (r'0b[01_]+', Number.Bin, 'number_lit'),
126 # Octal literals
127 (r'0o[0-7_]+', Number.Oct, 'number_lit'),
128 # Hexadecimal literals
129 (r'0[xX][0-9a-fA-F_]+', Number.Hex, 'number_lit'),
130 # Decimal literals
131 (r'[0-9][0-9_]*(\.[0-9_]+[eE][+\-]?[0-9_]+|'
132 r'\.[0-9_]*(?!\.)|[eE][+\-]?[0-9_]+)', Number.Float,
133 'number_lit'),
134 (r'[0-9][0-9_]*', Number.Integer, 'number_lit'),
135
136 # String literals
137 (r'b"', String, 'bytestring'),
138 (r'"', String, 'string'),
139 (r'b?r(#*)".*?"\1', String),
140
141 # Lifetime names
142 (r"'", Operator, 'lifetime'),
143
144 # Operators and Punctuation
145 (r'\.\.=?', Operator),
146 (r'[{}()\[\],.;]', Punctuation),
147 (r'[+\-*/%&|<>^!~@=:?]', Operator),
148
149 # Identifiers
150 (r'[a-zA-Z_]\w*', Name),
151 # Raw identifiers
152 (r'r#[a-zA-Z_]\w*', Name),
153
154 # Attributes
155 (r'#!?\[', Comment.Preproc, 'attribute['),
156 ],
157 'comment': [
158 (r'[^*/]+', Comment.Multiline),
159 (r'/\*', Comment.Multiline, '#push'),
160 (r'\*/', Comment.Multiline, '#pop'),
161 (r'[*/]', Comment.Multiline),
162 ],
163 'doccomment': [
164 (r'[^*/]+', String.Doc),
165 (r'/\*', String.Doc, '#push'),
166 (r'\*/', String.Doc, '#pop'),
167 (r'[*/]', String.Doc),
168 ],
169 'modname': [
170 (r'\s+', Text),
171 (r'[a-zA-Z_]\w*', Name.Namespace, '#pop'),
172 default('#pop'),
173 ],
174 'funcname': [
175 (r'\s+', Text),
176 (r'[a-zA-Z_]\w*', Name.Function, '#pop'),
177 default('#pop'),
178 ],
179 'typename': [
180 (r'\s+', Text),
181 (r'&', Keyword.Pseudo),
182 (r"'", Operator, 'lifetime'),
183 builtin_funcs_types,
184 keyword_types,
185 (r'[a-zA-Z_]\w*', Name.Class, '#pop'),
186 default('#pop'),
187 ],
188 'lifetime': [
189 (r"(static|_)", Name.Builtin),
190 (r"[a-zA-Z_]+\w*", Name.Attribute),
191 default('#pop'),
192 ],
193 'number_lit': [
194 (r'[ui](8|16|32|64|size)', Keyword, '#pop'),
195 (r'f(32|64)', Keyword, '#pop'),
196 default('#pop'),
197 ],
198 'string': [
199 (r'"', String, '#pop'),
200 (r"""\\['"\\nrt]|\\x[0-7][0-9a-fA-F]|\\0"""
201 r"""|\\u\{[0-9a-fA-F]{1,6}\}""", String.Escape),
202 (r'[^\\"]+', String),
203 (r'\\', String),
204 ],
205 'bytestring': [
206 (r"""\\x[89a-fA-F][0-9a-fA-F]""", String.Escape),
207 include('string'),
208 ],
209 'attribute_common': [
210 (r'"', String, 'string'),
211 (r'\[', Comment.Preproc, 'attribute['),
212 (r'\(', Comment.Preproc, 'attribute('),
213 ],
214 'attribute[': [
215 include('attribute_common'),
216 (r'\];?', Comment.Preproc, '#pop'),
217 (r'[^"\]]+', Comment.Preproc),
218 ],
219 'attribute(': [
220 include('attribute_common'),
221 (r'\);?', Comment.Preproc, '#pop'),
222 (r'[^")]+', Comment.Preproc),
223 ],
224 }

eric ide

mercurial