Skip to content

Commit 2ff40fe

Browse files
Rust: Improvements (#2464)
1 parent 2805ae3 commit 2ff40fe

9 files changed

+386
-35
lines changed

components/prism-rust.js

+34-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
alias: 'string'
3232
},
3333
'attribute': {
34-
pattern: /#!?\[[^[\]]*\]/,
34+
pattern: /#!?\[(?:[^\[\]"]|"(?:\\[\s\S]|[^\\"])*")*\]/,
3535
greedy: true,
3636
alias: 'attr-name',
3737
inside: {
@@ -66,26 +66,55 @@
6666
'variable': /\$\w+/,
6767

6868
'function-definition': {
69-
pattern: /(\bfn\s*)\w+/,
69+
pattern: /(\bfn\s+)\w+/,
7070
lookbehind: true,
7171
alias: 'function'
7272
},
73+
'type-definition': {
74+
pattern: /(\b(?:enum|struct|union)\s+)\w+/,
75+
lookbehind: true,
76+
alias: 'class-name'
77+
},
78+
'module-declaration': [
79+
{
80+
pattern: /(\b(?:crate|mod)\s+)[a-z][a-z_\d]*/,
81+
lookbehind: true,
82+
alias: 'namespace'
83+
},
84+
{
85+
pattern: /(\b(?:crate|self|super)\s*)::\s*[a-z][a-z_\d]*\b(?:\s*::(?:\s*[a-z][a-z_\d]*\s*::)*)?/,
86+
lookbehind: true,
87+
alias: 'namespace',
88+
inside: {
89+
'punctuation': /::/
90+
}
91+
}
92+
],
7393
'keyword': [
7494
// https://github.com/rust-lang/reference/blob/master/src/keywords.md
7595
/\b(?:abstract|as|async|await|become|box|break|const|continue|crate|do|dyn|else|enum|extern|final|fn|for|if|impl|in|let|loop|macro|match|mod|move|mut|override|priv|pub|ref|return|self|Self|static|struct|super|trait|try|type|typeof|union|unsafe|unsized|use|virtual|where|while|yield)\b/,
76-
// primitives
96+
// primitives and str
7797
// https://doc.rust-lang.org/stable/rust-by-example/primitives.html
78-
/\b(?:[ui](?:8|16|32|64|128|size)|f(?:32|64)|bool|char)\b/
98+
/\b(?:[ui](?:8|16|32|64|128|size)|f(?:32|64)|bool|char|str)\b/
7999
],
80100

81101
// functions can technically start with an upper-case letter, but this will introduce a lot of false positives
82102
// and Rust's naming conventions recommend snake_case anyway.
83103
// https://doc.rust-lang.org/1.0.0/style/style/naming/README.html
84-
'function': /\b[a-z_]\w*(?=\s*(?:::\s*<(?:[^<>]|<(?:[^<>]|<[^<>]*>)*>)*>\s*)?\()/,
104+
'function': /\b[a-z_]\w*(?=\s*(?:::\s*<|\())/,
85105
'macro': {
86106
pattern: /\w+!/,
87107
alias: 'property'
88108
},
109+
'constant': /\b[A-Z_][A-Z_\d]+\b/,
110+
'class-name': /\b[A-Z]\w*\b/,
111+
112+
'namespace': {
113+
pattern: /(?:\b[a-z][a-z_\d]*\s*::\s*)*\b[a-z][a-z_\d]*\s*::(?!\s*<)/,
114+
inside: {
115+
'punctuation': /::/
116+
}
117+
},
89118

90119
// Hex, oct, bin, dec numbers with visual separators and type suffix
91120
'number': /\b(?:0x[\dA-Fa-f](?:_?[\dA-Fa-f])*|0o[0-7](?:_?[0-7])*|0b[01](?:_?[01])*|(?:\d(?:_?\d)*)?\.?\d(?:_?\d)*(?:[Ee][+-]?\d+)?)(?:_?(?:[iu](?:8|16|32|64|size)?|f32|f64))?\b/,

components/prism-rust.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/prism-rust.html

+6-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ <h2>Comments</h2>
55
comment */</code></pre>
66

77
<h2>Strings</h2>
8-
<pre><code>'C'; '\''; '\n'; '\u7FFF'; // Characters
8+
<pre><code>'C'; '\''; '\n'; '\u{7FFF}'; // Characters
99
"foo \"bar\" baz"; // String
1010
r##"foo #"bar"# baz"##; // Raw string with # pairs
1111
b'C'; b'\''; b'\n'; // Bytes
@@ -14,10 +14,7 @@ <h2>Strings</h2>
1414
</code></pre>
1515

1616
<h2>Numbers</h2>
17-
<pre><code>123i; // type int
18-
123u; // type uint
19-
123_u; // type uint
20-
0xff_u8; // type u8
17+
<pre><code>0xff_u8; // type u8
2118
0o70_i16; // type i16
2219
0b1111_1111_1001_0000_i32; // type i32
2320

@@ -32,9 +29,9 @@ <h2>Booleans</h2>
3229

3330
<h2>Functions and macros</h2>
3431
<pre><code>println!("x is {}", x);
35-
fn next_two(x: int) -> (int, int) { (x + 1i, x + 2i) }
36-
next_two(5i);
37-
vec![1i, 2, 3];
32+
fn next_two(x: i32) -> (i32, i32) { (x + 1, x + 2) }
33+
next_two(5);
34+
vec![1, 2, 3];
3835
</code></pre>
3936

4037
<h2>Attributes</h2>
@@ -47,6 +44,6 @@ <h2>Attributes</h2>
4744
<h2>Closure parameters and bitwise OR</h2>
4845
<pre><code>let x = a | b;
4946
let y = c || d;
50-
let add_one = |x: int| -> int { 1i + x };
47+
let add_one = |x: i32| -> i32 { 1i + x };
5148
let printer = || { println!("x is: {}", x); };
5249
</code></pre>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
struct foo {}
2+
3+
let foo: CStr;
4+
let foo: &'a CStr;
5+
let foo: &'a Foo<dyn Bar>;
6+
Option::Some(foo);
7+
Option::None;
8+
9+
// we can differentiate between enum variants and class names
10+
// so let's make the bug a feature!
11+
enum Foo {
12+
Const,
13+
Tuple(i8,i8),
14+
Struct {
15+
foo: u8
16+
}
17+
}
18+
19+
----------------------------------------------------
20+
21+
[
22+
["keyword", "struct"],
23+
["type-definition", "foo"],
24+
["punctuation", "{"],
25+
["punctuation", "}"],
26+
27+
["keyword", "let"],
28+
" foo",
29+
["punctuation", ":"],
30+
["class-name", "CStr"],
31+
["punctuation", ";"],
32+
["keyword", "let"],
33+
" foo",
34+
["punctuation", ":"],
35+
["operator", "&"],
36+
["lifetime-annotation", "'a"],
37+
["class-name", "CStr"],
38+
["punctuation", ";"],
39+
["keyword", "let"],
40+
" foo",
41+
["punctuation", ":"],
42+
["operator", "&"],
43+
["lifetime-annotation", "'a"],
44+
["class-name", "Foo"],
45+
["operator", "<"],
46+
["keyword", "dyn"],
47+
["class-name", "Bar"],
48+
["operator", ">"],
49+
["punctuation", ";"],
50+
["class-name", "Option"],
51+
["punctuation", "::"],
52+
["class-name", "Some"],
53+
["punctuation", "("],
54+
"foo",
55+
["punctuation", ")"],
56+
["punctuation", ";"],
57+
["class-name", "Option"],
58+
["punctuation", "::"],
59+
["class-name", "None"],
60+
["punctuation", ";"],
61+
62+
["comment", "// we can differentiate between enum variants and class names"],
63+
["comment", "// so let's make the bug a feature!"],
64+
["keyword", "enum"],
65+
["type-definition", "Foo"],
66+
["punctuation", "{"],
67+
["class-name", "Const"],
68+
["punctuation", ","],
69+
["class-name", "Tuple"],
70+
["punctuation", "("],
71+
["keyword", "i8"],
72+
["punctuation", ","],
73+
["keyword", "i8"],
74+
["punctuation", ")"],
75+
["punctuation", ","],
76+
["class-name", "Struct"],
77+
["punctuation", "{"],
78+
"\n\t\tfoo",
79+
["punctuation", ":"],
80+
["keyword", "u8"],
81+
["punctuation", "}"],
82+
["punctuation", "}"]
83+
]
84+
85+
----------------------------------------------------
86+
87+
Checks for class names and enum variants.
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
MAX
2+
SOME_CONSTANT
3+
4+
// not a constant
5+
T
6+
7+
----------------------------------------------------
8+
9+
[
10+
["constant", "MAX"],
11+
["constant", "SOME_CONSTANT"],
12+
13+
["comment", "// not a constant"],
14+
["class-name", "T"]
15+
]
16+
17+
----------------------------------------------------
18+
19+
Checks for constants.

tests/languages/rust/function_feature.test

+38-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ foo_bar_42(
44

55
foo_generic::<T, Option<T>>()
66

7+
mem::transmute::<Box<dyn FnOnce() + 'a>, Box<dyn FnOnce() + 'static>>()
8+
79
fn apply<F>(f: F) where F: FnOnce() {
810
f();
911
}
@@ -21,29 +23,58 @@ fn apply<F>(f: F) where F: FnOnce() {
2123
["function", "foo_generic"],
2224
["punctuation", "::"],
2325
["operator", "<"],
24-
"T",
26+
["class-name", "T"],
2527
["punctuation", ","],
26-
" Option",
28+
["class-name", "Option"],
29+
["operator", "<"],
30+
["class-name", "T"],
31+
["operator", ">>"],
32+
["punctuation", "("],
33+
["punctuation", ")"],
34+
35+
["namespace", [
36+
"mem",
37+
["punctuation", "::"]
38+
]],
39+
["function", "transmute"],
40+
["punctuation", "::"],
41+
["operator", "<"],
42+
["class-name", "Box"],
2743
["operator", "<"],
28-
"T",
44+
["keyword", "dyn"],
45+
["class-name", "FnOnce"],
46+
["punctuation", "("],
47+
["punctuation", ")"],
48+
["operator", "+"],
49+
["lifetime-annotation", "'a"],
50+
["operator", ">"],
51+
["punctuation", ","],
52+
["class-name", "Box"],
53+
["operator", "<"],
54+
["keyword", "dyn"],
55+
["class-name", "FnOnce"],
56+
["punctuation", "("],
57+
["punctuation", ")"],
58+
["operator", "+"],
59+
["lifetime-annotation", "'static"],
2960
["operator", ">>"],
3061
["punctuation", "("],
3162
["punctuation", ")"],
3263

3364
["keyword", "fn"],
3465
["function-definition", "apply"],
3566
["operator", "<"],
36-
"F",
67+
["class-name", "F"],
3768
["operator", ">"],
3869
["punctuation", "("],
3970
"f",
4071
["punctuation", ":"],
41-
" F",
72+
["class-name", "F"],
4273
["punctuation", ")"],
4374
["keyword", "where"],
44-
" F",
75+
["class-name", "F"],
4576
["punctuation", ":"],
46-
" FnOnce",
77+
["class-name", "FnOnce"],
4778
["punctuation", "("],
4879
["punctuation", ")"],
4980
["punctuation", "{"],

tests/languages/rust/issue1339.test

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
const ALL_CARDS: &'static [&'static char] = &["2"]
2+
23
fn foo<'a> (first: &'a str, second: &'a str) => () { }
34

45
----------------------------------------------------
56

67
[
78
["keyword", "const"],
8-
" ALL_CARDS",
9+
["constant", "ALL_CARDS"],
910
["punctuation", ":"],
1011
["operator", "&"],
1112
["lifetime-annotation", "'static"],
@@ -19,6 +20,7 @@ fn foo<'a> (first: &'a str, second: &'a str) => () { }
1920
["punctuation", "["],
2021
["string", "\"2\""],
2122
["punctuation", "]"],
23+
2224
["keyword", "fn"],
2325
["function-definition", "foo"],
2426
["operator", "<"],
@@ -29,13 +31,13 @@ fn foo<'a> (first: &'a str, second: &'a str) => () { }
2931
["punctuation", ":"],
3032
["operator", "&"],
3133
["lifetime-annotation", "'a"],
32-
" str",
34+
["keyword", "str"],
3335
["punctuation", ","],
3436
" second",
3537
["punctuation", ":"],
3638
["operator", "&"],
3739
["lifetime-annotation", "'a"],
38-
" str",
40+
["keyword", "str"],
3941
["punctuation", ")"],
4042
["operator", "=>"],
4143
["punctuation", "("],

0 commit comments

Comments
 (0)