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

changeset 6942
2602857055c5
parent 6651
e8f3b5568b21
child 7547
21b0534faebc
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2 """
3 pygments.lexers.rust
4 ~~~~~~~~~~~~~~~~~~~~
5
6 Lexers for the Rust language.
7
8 :copyright: Copyright 2006-2017 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.10).
22
23 .. versionadded:: 1.6
24 """
25 name = 'Rust'
26 filenames = ['*.rs', '*.rs.in']
27 aliases = ['rust', 'rs']
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)
58
59 tokens = {
60 'root': [
61 # 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.
63 (r'#![^[\r\n].*$', Comment.Preproc),
64 default('base'),
65 ],
66 'base': [
67 # Whitespace and Comments
68 (r'\n', Whitespace),
69 (r'\s+', Whitespace),
70 (r'//!.*?\n', String.Doc),
71 (r'///(\n|[^/].*?\n)', String.Doc),
72 (r'//(.*?)\n', Comment.Single),
73 (r'/\*\*(\n|[^/*])', String.Doc, 'doccomment'),
74 (r'/\*!', String.Doc, 'doccomment'),
75 (r'/\*', Comment.Multiline, 'comment'),
76
77 # Macro parameters
78 (r"""\$([a-zA-Z_]\w*|\(,?|\),?|,?)""", Comment.Preproc),
79 # Keywords
80 (words((
81 'as', 'box', 'const', 'crate', 'else', 'extern',
82 'for', 'if', 'impl', 'in', 'loop', 'match', 'move',
83 'mut', 'pub', 'ref', 'return', 'static', 'super',
84 'trait', 'unsafe', 'use', 'where', 'while'), suffix=r'\b'),
85 Keyword),
86 (words(('abstract', 'alignof', 'become', 'do', 'final', 'macro',
87 'offsetof', 'override', 'priv', 'proc', 'pure', 'sizeof',
88 'typeof', 'unsized', 'virtual', 'yield'), suffix=r'\b'),
89 Keyword.Reserved),
90 (r'(true|false)\b', Keyword.Constant),
91 (r'mod\b', Keyword, 'modname'),
92 (r'let\b', Keyword.Declaration),
93 (r'fn\b', Keyword, 'funcname'),
94 (r'(struct|enum|type|union)\b', Keyword, 'typename'),
95 (r'(default)(\s+)(type|fn)\b', bygroups(Keyword, Text, Keyword)),
96 keyword_types,
97 (r'self\b', Name.Builtin.Pseudo),
98 # Prelude (taken from Rust’s src/libstd/prelude.rs)
99 builtin_types,
100 # Path seperators, so types don't catch them.
101 (r'::\b', Text),
102 # Types in positions.
103 (r'(?::|->)', Text, 'typename'),
104 # Labels
105 (r'(break|continue)(\s*)(\'[A-Za-z_]\w*)?',
106 bygroups(Keyword, Text.Whitespace, Name.Label)),
107 # Character Literal
108 (r"""'(\\['"\\nrt]|\\x[0-7][0-9a-fA-F]|\\0"""
109 r"""|\\u\{[0-9a-fA-F]{1,6}\}|.)'""",
110 String.Char),
111 (r"""b'(\\['"\\nrt]|\\x[0-9a-fA-F]{2}|\\0"""
112 r"""|\\u\{[0-9a-fA-F]{1,6}\}|.)'""",
113 String.Char),
114 # Binary Literal
115 (r'0b[01_]+', Number.Bin, 'number_lit'),
116 # Octal Literal
117 (r'0o[0-7_]+', Number.Oct, 'number_lit'),
118 # Hexadecimal Literal
119 (r'0[xX][0-9a-fA-F_]+', Number.Hex, 'number_lit'),
120 # Decimal Literal
121 (r'[0-9][0-9_]*(\.[0-9_]+[eE][+\-]?[0-9_]+|'
122 r'\.[0-9_]*(?!\.)|[eE][+\-]?[0-9_]+)', Number.Float,
123 'number_lit'),
124 (r'[0-9][0-9_]*', Number.Integer, 'number_lit'),
125 # String Literal
126 (r'b"', String, 'bytestring'),
127 (r'"', String, 'string'),
128 (r'b?r(#*)".*?"\1', String),
129
130 # Lifetime
131 (r"""'static""", Name.Builtin),
132 (r"""'[a-zA-Z_]\w*""", Name.Attribute),
133
134 # Operators and Punctuation
135 (r'[{}()\[\],.;]', Punctuation),
136 (r'[+\-*/%&|<>^!~@=:?]', Operator),
137
138 # Identifier
139 (r'[a-zA-Z_]\w*', Name),
140
141 # Attributes
142 (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 ],
151 'comment': [
152 (r'[^*/]+', Comment.Multiline),
153 (r'/\*', Comment.Multiline, '#push'),
154 (r'\*/', Comment.Multiline, '#pop'),
155 (r'[*/]', Comment.Multiline),
156 ],
157 'doccomment': [
158 (r'[^*/]+', String.Doc),
159 (r'/\*', String.Doc, '#push'),
160 (r'\*/', String.Doc, '#pop'),
161 (r'[*/]', String.Doc),
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 ],
181 'number_lit': [
182 (r'[ui](8|16|32|64|size)', Keyword, '#pop'),
183 (r'f(32|64)', Keyword, '#pop'),
184 default('#pop'),
185 ],
186 'string': [
187 (r'"', String, '#pop'),
188 (r"""\\['"\\nrt]|\\x[0-7][0-9a-fA-F]|\\0"""
189 r"""|\\u\{[0-9a-fA-F]{1,6}\}""", String.Escape),
190 (r'[^\\"]+', String),
191 (r'\\', String),
192 ],
193 'bytestring': [
194 (r"""\\x[89a-fA-F][0-9a-fA-F]""", String.Escape),
195 include('string'),
196 ],
197 'macro{': [
198 (r'\{', Operator, '#push'),
199 (r'\}', Operator, '#pop'),
200 ],
201 'macro(': [
202 (r'\(', Operator, '#push'),
203 (r'\)', Operator, '#pop'),
204 ],
205 'attribute_common': [
206 (r'"', String, 'string'),
207 (r'\[', Comment.Preproc, 'attribute['),
208 (r'\(', Comment.Preproc, 'attribute('),
209 ],
210 'attribute[': [
211 include('attribute_common'),
212 (r'\];?', Comment.Preproc, '#pop'),
213 (r'[^"\]]+', Comment.Preproc),
214 ],
215 'attribute(': [
216 include('attribute_common'),
217 (r'\);?', Comment.Preproc, '#pop'),
218 (r'[^")]+', Comment.Preproc),
219 ],
220 }

eric ide

mercurial