Skip to content

Commit 9d6befc

Browse files
committed
chore(rust) simplify and clean up
1 parent 56f6186 commit 9d6befc

File tree

1 file changed

+212
-54
lines changed

1 file changed

+212
-54
lines changed

src/languages/rust.js

+212-54
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,170 @@ Website: https://www.rust-lang.org
66
Category: common, system
77
*/
88

9+
/** @type LanguageFn */
910
export default function(hljs) {
10-
const NUM_SUFFIX = '([ui](8|16|32|64|128|size)|f(32|64))\?';
11-
const KEYWORDS =
12-
'abstract as async await become box break const continue crate do dyn ' +
13-
'else enum extern false final fn for if impl in let loop macro match mod ' +
14-
'move mut override priv pub ref return self Self static struct super ' +
15-
'trait true try type typeof unsafe unsized use virtual where while yield';
16-
const BUILTINS =
11+
const NUMBER_SUFFIX = '([ui](8|16|32|64|128|size)|f(32|64))\?';
12+
const KEYWORDS = [
13+
"abstract",
14+
"as",
15+
"async",
16+
"await",
17+
"become",
18+
"box",
19+
"break",
20+
"const",
21+
"continue",
22+
"crate",
23+
"do",
24+
"dyn",
25+
"else",
26+
"enum",
27+
"extern",
28+
"false",
29+
"final",
30+
"fn",
31+
"for",
32+
"if",
33+
"impl",
34+
"in",
35+
"let",
36+
"loop",
37+
"macro",
38+
"match",
39+
"mod",
40+
"move",
41+
"mut",
42+
"override",
43+
"priv",
44+
"pub",
45+
"ref",
46+
"return",
47+
"self",
48+
"Self",
49+
"static",
50+
"struct",
51+
"super",
52+
"trait",
53+
"true",
54+
"try",
55+
"type",
56+
"typeof",
57+
"unsafe",
58+
"unsized",
59+
"use",
60+
"virtual",
61+
"where",
62+
"while",
63+
"yield"
64+
];
65+
const LITERALS = [
66+
"true",
67+
"false",
68+
"Some",
69+
"None",
70+
"Ok",
71+
"Err"
72+
];
73+
const BUILTINS = [
1774
// functions
18-
'drop ' +
19-
// types
20-
'i8 i16 i32 i64 i128 isize ' +
21-
'u8 u16 u32 u64 u128 usize ' +
22-
'f32 f64 ' +
23-
'str char bool ' +
24-
'Box Option Result String Vec ' +
75+
'drop ',
2576
// traits
26-
'Copy Send Sized Sync Drop Fn FnMut FnOnce ToOwned Clone Debug ' +
27-
'PartialEq PartialOrd Eq Ord AsRef AsMut Into From Default Iterator ' +
28-
'Extend IntoIterator DoubleEndedIterator ExactSizeIterator ' +
29-
'SliceConcatExt ToString ' +
77+
"Copy",
78+
"Send",
79+
"Sized",
80+
"Sync",
81+
"Drop",
82+
"Fn",
83+
"FnMut",
84+
"FnOnce",
85+
"ToOwned",
86+
"Clone",
87+
"Debug",
88+
"PartialEq",
89+
"PartialOrd",
90+
"Eq",
91+
"Ord",
92+
"AsRef",
93+
"AsMut",
94+
"Into",
95+
"From",
96+
"Default",
97+
"Iterator",
98+
"Extend",
99+
"IntoIterator",
100+
"DoubleEndedIterator",
101+
"ExactSizeIterator",
102+
"SliceConcatExt",
103+
"ToString",
30104
// macros
31-
'assert! assert_eq! bitflags! bytes! cfg! col! concat! concat_idents! ' +
32-
'debug_assert! debug_assert_eq! env! panic! file! format! format_args! ' +
33-
'include_bin! include_str! line! local_data_key! module_path! ' +
34-
'option_env! print! println! select! stringify! try! unimplemented! ' +
35-
'unreachable! vec! write! writeln! macro_rules! assert_ne! debug_assert_ne!';
105+
"assert!",
106+
"assert_eq!",
107+
"bitflags!",
108+
"bytes!",
109+
"cfg!",
110+
"col!",
111+
"concat!",
112+
"concat_idents!",
113+
"debug_assert!",
114+
"debug_assert_eq!",
115+
"env!",
116+
"panic!",
117+
"file!",
118+
"format!",
119+
"format_args!",
120+
"include_bin!",
121+
"include_str!",
122+
"line!",
123+
"local_data_key!",
124+
"module_path!",
125+
"option_env!",
126+
"print!",
127+
"println!",
128+
"select!",
129+
"stringify!",
130+
"try!",
131+
"unimplemented!",
132+
"unreachable!",
133+
"vec!",
134+
"write!",
135+
"writeln!",
136+
"macro_rules!",
137+
"assert_ne!",
138+
"debug_assert_ne!"
139+
];
140+
const TYPES = [
141+
"i8",
142+
"i16",
143+
"i32",
144+
"i64",
145+
"i128",
146+
"isize",
147+
"u8",
148+
"u16",
149+
"u32",
150+
"u64",
151+
"u128",
152+
"usize",
153+
"f32",
154+
"f64",
155+
"str",
156+
"char",
157+
"bool",
158+
"Box",
159+
"Option",
160+
"Result",
161+
"String",
162+
"Vec"
163+
];
36164
return {
37165
name: 'Rust',
38166
aliases: [ 'rs' ],
39167
keywords: {
40168
$pattern: hljs.IDENT_RE + '!?',
41-
keyword:
42-
KEYWORDS,
43-
literal:
44-
'true false Some None Ok Err',
45-
built_in:
46-
BUILTINS
169+
type: TYPES,
170+
keyword: KEYWORDS,
171+
literal: LITERALS,
172+
built_in: BUILTINS
47173
},
48174
illegal: '</',
49175
contains: [
@@ -74,27 +200,31 @@ export default function(hljs) {
74200
className: 'number',
75201
variants: [
76202
{
77-
begin: '\\b0b([01_]+)' + NUM_SUFFIX
203+
begin: '\\b0b([01_]+)' + NUMBER_SUFFIX
78204
},
79205
{
80-
begin: '\\b0o([0-7_]+)' + NUM_SUFFIX
206+
begin: '\\b0o([0-7_]+)' + NUMBER_SUFFIX
81207
},
82208
{
83-
begin: '\\b0x([A-Fa-f0-9_]+)' + NUM_SUFFIX
209+
begin: '\\b0x([A-Fa-f0-9_]+)' + NUMBER_SUFFIX
84210
},
85211
{
86212
begin: '\\b(\\d[\\d_]*(\\.[0-9_]+)?([eE][+-]?[0-9_]+)?)' +
87-
NUM_SUFFIX
213+
NUMBER_SUFFIX
88214
}
89215
],
90216
relevance: 0
91217
},
92218
{
93-
className: 'function',
94-
beginKeywords: 'fn',
95-
end: '(\\(|<)',
96-
excludeEnd: true,
97-
contains: [ hljs.UNDERSCORE_TITLE_MODE ]
219+
begin: [
220+
/fn/,
221+
/\s+/,
222+
hljs.UNDERSCORE_IDENT_RE
223+
],
224+
className: {
225+
1: "keyword",
226+
3: "title.function"
227+
}
98228
},
99229
{
100230
className: 'meta',
@@ -109,34 +239,62 @@ export default function(hljs) {
109239
]
110240
},
111241
{
112-
className: 'class',
113-
beginKeywords: 'type',
114-
end: ';',
115-
contains: [
116-
hljs.inherit(hljs.UNDERSCORE_TITLE_MODE, {
117-
endsParent: true
118-
})
242+
begin: [
243+
/let/,
244+
/\s+/,
245+
hljs.UNDERSCORE_IDENT_RE
119246
],
120-
illegal: '\\S'
247+
className: {
248+
1: "keyword",
249+
3: "variable"
250+
}
121251
},
252+
// must come before impl/for rule later
122253
{
123-
className: 'class',
124-
beginKeywords: 'trait enum struct union',
125-
end: /\{/,
126-
contains: [
127-
hljs.inherit(hljs.UNDERSCORE_TITLE_MODE, {
128-
endsParent: true
129-
})
254+
begin: [
255+
/for/,
256+
/\s+/,
257+
hljs.UNDERSCORE_IDENT_RE,
258+
/\s+/,
259+
/in/
260+
],
261+
className: {
262+
1: "keyword",
263+
3: "variable",
264+
5: "keyword"
265+
}
266+
},
267+
{
268+
begin: [
269+
/type/,
270+
/\s+/,
271+
hljs.UNDERSCORE_IDENT_RE
130272
],
131-
illegal: '[\\w\\d]'
273+
className: {
274+
1: "keyword",
275+
3: "title.class"
276+
}
277+
},
278+
{
279+
begin: [
280+
/(?:trait|enum|struct|union|impl|for)/,
281+
/\s+/,
282+
hljs.UNDERSCORE_IDENT_RE
283+
],
284+
className: {
285+
1: "keyword",
286+
3: "title.class"
287+
}
132288
},
133289
{
134290
begin: hljs.IDENT_RE + '::',
135291
keywords: {
292+
keyword: "Self",
136293
built_in: BUILTINS
137294
}
138295
},
139296
{
297+
className: "punctuation",
140298
begin: '->'
141299
}
142300
]

0 commit comments

Comments
 (0)