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