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