UNPKG

5.41 kBJavaScriptView Raw
1/*
2Language: Rust
3Author: Andrey Vlasovskikh <andrey.vlasovskikh@gmail.com>
4Contributors: Roman Shmatov <romanshmatov@gmail.com>, Kasper Andersen <kma_untrusted@protonmail.com>
5Website: https://www.rust-lang.org
6Category: common, system
7*/
8
9/** @type LanguageFn */
10function rust(hljs) {
11 const regex = hljs.regex;
12 const FUNCTION_INVOKE = {
13 className: "title.function.invoke",
14 relevance: 0,
15 begin: regex.concat(
16 /\b/,
17 /(?!let\b)/,
18 hljs.IDENT_RE,
19 regex.lookahead(/\s*\(/))
20 };
21 const NUMBER_SUFFIX = '([ui](8|16|32|64|128|size)|f(32|64))\?';
22 const KEYWORDS = [
23 "abstract",
24 "as",
25 "async",
26 "await",
27 "become",
28 "box",
29 "break",
30 "const",
31 "continue",
32 "crate",
33 "do",
34 "dyn",
35 "else",
36 "enum",
37 "extern",
38 "false",
39 "final",
40 "fn",
41 "for",
42 "if",
43 "impl",
44 "in",
45 "let",
46 "loop",
47 "macro",
48 "match",
49 "mod",
50 "move",
51 "mut",
52 "override",
53 "priv",
54 "pub",
55 "ref",
56 "return",
57 "self",
58 "Self",
59 "static",
60 "struct",
61 "super",
62 "trait",
63 "true",
64 "try",
65 "type",
66 "typeof",
67 "unsafe",
68 "unsized",
69 "use",
70 "virtual",
71 "where",
72 "while",
73 "yield"
74 ];
75 const LITERALS = [
76 "true",
77 "false",
78 "Some",
79 "None",
80 "Ok",
81 "Err"
82 ];
83 const BUILTINS = [
84 // functions
85 'drop ',
86 // traits
87 "Copy",
88 "Send",
89 "Sized",
90 "Sync",
91 "Drop",
92 "Fn",
93 "FnMut",
94 "FnOnce",
95 "ToOwned",
96 "Clone",
97 "Debug",
98 "PartialEq",
99 "PartialOrd",
100 "Eq",
101 "Ord",
102 "AsRef",
103 "AsMut",
104 "Into",
105 "From",
106 "Default",
107 "Iterator",
108 "Extend",
109 "IntoIterator",
110 "DoubleEndedIterator",
111 "ExactSizeIterator",
112 "SliceConcatExt",
113 "ToString",
114 // macros
115 "assert!",
116 "assert_eq!",
117 "bitflags!",
118 "bytes!",
119 "cfg!",
120 "col!",
121 "concat!",
122 "concat_idents!",
123 "debug_assert!",
124 "debug_assert_eq!",
125 "env!",
126 "panic!",
127 "file!",
128 "format!",
129 "format_args!",
130 "include_bin!",
131 "include_str!",
132 "line!",
133 "local_data_key!",
134 "module_path!",
135 "option_env!",
136 "print!",
137 "println!",
138 "select!",
139 "stringify!",
140 "try!",
141 "unimplemented!",
142 "unreachable!",
143 "vec!",
144 "write!",
145 "writeln!",
146 "macro_rules!",
147 "assert_ne!",
148 "debug_assert_ne!"
149 ];
150 const TYPES = [
151 "i8",
152 "i16",
153 "i32",
154 "i64",
155 "i128",
156 "isize",
157 "u8",
158 "u16",
159 "u32",
160 "u64",
161 "u128",
162 "usize",
163 "f32",
164 "f64",
165 "str",
166 "char",
167 "bool",
168 "Box",
169 "Option",
170 "Result",
171 "String",
172 "Vec"
173 ];
174 return {
175 name: 'Rust',
176 aliases: [ 'rs' ],
177 keywords: {
178 $pattern: hljs.IDENT_RE + '!?',
179 type: TYPES,
180 keyword: KEYWORDS,
181 literal: LITERALS,
182 built_in: BUILTINS
183 },
184 illegal: '</',
185 contains: [
186 hljs.C_LINE_COMMENT_MODE,
187 hljs.COMMENT('/\\*', '\\*/', { contains: [ 'self' ] }),
188 hljs.inherit(hljs.QUOTE_STRING_MODE, {
189 begin: /b?"/,
190 illegal: null
191 }),
192 {
193 className: 'string',
194 variants: [
195 { begin: /b?r(#*)"(.|\n)*?"\1(?!#)/ },
196 { begin: /b?'\\?(x\w{2}|u\w{4}|U\w{8}|.)'/ }
197 ]
198 },
199 {
200 className: 'symbol',
201 begin: /'[a-zA-Z_][a-zA-Z0-9_]*/
202 },
203 {
204 className: 'number',
205 variants: [
206 { begin: '\\b0b([01_]+)' + NUMBER_SUFFIX },
207 { begin: '\\b0o([0-7_]+)' + NUMBER_SUFFIX },
208 { begin: '\\b0x([A-Fa-f0-9_]+)' + NUMBER_SUFFIX },
209 { begin: '\\b(\\d[\\d_]*(\\.[0-9_]+)?([eE][+-]?[0-9_]+)?)'
210 + NUMBER_SUFFIX }
211 ],
212 relevance: 0
213 },
214 {
215 begin: [
216 /fn/,
217 /\s+/,
218 hljs.UNDERSCORE_IDENT_RE
219 ],
220 className: {
221 1: "keyword",
222 3: "title.function"
223 }
224 },
225 {
226 className: 'meta',
227 begin: '#!?\\[',
228 end: '\\]',
229 contains: [
230 {
231 className: 'string',
232 begin: /"/,
233 end: /"/
234 }
235 ]
236 },
237 {
238 begin: [
239 /let/,
240 /\s+/,
241 /(?:mut\s+)?/,
242 hljs.UNDERSCORE_IDENT_RE
243 ],
244 className: {
245 1: "keyword",
246 3: "keyword",
247 4: "variable"
248 }
249 },
250 // must come before impl/for rule later
251 {
252 begin: [
253 /for/,
254 /\s+/,
255 hljs.UNDERSCORE_IDENT_RE,
256 /\s+/,
257 /in/
258 ],
259 className: {
260 1: "keyword",
261 3: "variable",
262 5: "keyword"
263 }
264 },
265 {
266 begin: [
267 /type/,
268 /\s+/,
269 hljs.UNDERSCORE_IDENT_RE
270 ],
271 className: {
272 1: "keyword",
273 3: "title.class"
274 }
275 },
276 {
277 begin: [
278 /(?:trait|enum|struct|union|impl|for)/,
279 /\s+/,
280 hljs.UNDERSCORE_IDENT_RE
281 ],
282 className: {
283 1: "keyword",
284 3: "title.class"
285 }
286 },
287 {
288 begin: hljs.IDENT_RE + '::',
289 keywords: {
290 keyword: "Self",
291 built_in: BUILTINS
292 }
293 },
294 {
295 className: "punctuation",
296 begin: '->'
297 },
298 FUNCTION_INVOKE
299 ]
300 };
301}
302
303module.exports = rust;