Skip to content

Commit 2ff40fe

Browse files
authoredJul 18, 2020
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", "("],

‎tests/languages/rust/keyword_feature.test

+10-10
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ box
77
break
88
const
99
continue
10-
crate
10+
crate;
1111
do
1212
dyn
1313
else
14-
enum
14+
enum;
1515
extern
1616
final
1717
fn;
@@ -23,7 +23,7 @@ let
2323
loop
2424
macro
2525
match
26-
mod
26+
mod;
2727
move
2828
mut
2929
override
@@ -34,13 +34,13 @@ return
3434
self
3535
Self
3636
static
37-
struct
37+
struct;
3838
super
3939
trait
4040
try
4141
type
4242
typeof
43-
union
43+
union;
4444
unsafe
4545
unsized
4646
use
@@ -61,11 +61,11 @@ yield
6161
["keyword", "break"],
6262
["keyword", "const"],
6363
["keyword", "continue"],
64-
["keyword", "crate"],
64+
["keyword", "crate"], ["punctuation", ";"],
6565
["keyword", "do"],
6666
["keyword", "dyn"],
6767
["keyword", "else"],
68-
["keyword", "enum"],
68+
["keyword", "enum"], ["punctuation", ";"],
6969
["keyword", "extern"],
7070
["keyword", "final"],
7171
["keyword", "fn"], ["punctuation", ";"],
@@ -77,7 +77,7 @@ yield
7777
["keyword", "loop"],
7878
["keyword", "macro"],
7979
["keyword", "match"],
80-
["keyword", "mod"],
80+
["keyword", "mod"], ["punctuation", ";"],
8181
["keyword", "move"],
8282
["keyword", "mut"],
8383
["keyword", "override"],
@@ -88,13 +88,13 @@ yield
8888
["keyword", "self"],
8989
["keyword", "Self"],
9090
["keyword", "static"],
91-
["keyword", "struct"],
91+
["keyword", "struct"], ["punctuation", ";"],
9292
["keyword", "super"],
9393
["keyword", "trait"],
9494
["keyword", "try"],
9595
["keyword", "type"],
9696
["keyword", "typeof"],
97-
["keyword", "union"],
97+
["keyword", "union"], ["punctuation", ";"],
9898
["keyword", "unsafe"],
9999
["keyword", "unsized"],
100100
["keyword", "use"],
+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
use std::{
2+
fs::File,
3+
io::{BufRead, BufReader},
4+
path::PathBuf,
5+
};
6+
use ::serde::de::{Error, Visitor};
7+
use std::sync::atomic::{AtomicBool, Ordering};
8+
pub mod sample;
9+
extern crate test;
10+
11+
Result<Self, D::Error>
12+
13+
where D: serde::Deserializer<'de>,
14+
15+
serde_json::from_str(&line)
16+
self.read_records::<smol_str::SmolStr>()
17+
18+
pub static ALLOCATOR: alloc::Tracing = alloc::Tracing::new();
19+
20+
unsafe fn alloc(&self, layout: std::alloc::Layout) -> *mut u8 {}
21+
22+
----------------------------------------------------
23+
24+
[
25+
["keyword", "use"],
26+
["namespace", [
27+
"std",
28+
["punctuation", "::"]
29+
]],
30+
["punctuation", "{"],
31+
["namespace", [
32+
"fs",
33+
["punctuation", "::"]
34+
]],
35+
["class-name", "File"],
36+
["punctuation", ","],
37+
["namespace", [
38+
"io",
39+
["punctuation", "::"]
40+
]],
41+
["punctuation", "{"],
42+
["class-name", "BufRead"],
43+
["punctuation", ","],
44+
["class-name", "BufReader"],
45+
["punctuation", "}"],
46+
["punctuation", ","],
47+
["namespace", [
48+
"path",
49+
["punctuation", "::"]
50+
]],
51+
["class-name", "PathBuf"],
52+
["punctuation", ","],
53+
["punctuation", "}"],
54+
["punctuation", ";"],
55+
["keyword", "use"],
56+
["punctuation", "::"],
57+
["namespace", [
58+
"serde",
59+
["punctuation", "::"],
60+
"de",
61+
["punctuation", "::"]
62+
]],
63+
["punctuation", "{"],
64+
["class-name", "Error"],
65+
["punctuation", ","],
66+
["class-name", "Visitor"],
67+
["punctuation", "}"],
68+
["punctuation", ";"],
69+
["keyword", "use"],
70+
["namespace", [
71+
"std",
72+
["punctuation", "::"],
73+
"sync",
74+
["punctuation", "::"],
75+
"atomic",
76+
["punctuation", "::"]
77+
]],
78+
["punctuation", "{"],
79+
["class-name", "AtomicBool"],
80+
["punctuation", ","],
81+
["class-name", "Ordering"],
82+
["punctuation", "}"],
83+
["punctuation", ";"],
84+
["keyword", "pub"],
85+
["keyword", "mod"],
86+
["module-declaration", "sample"],
87+
["punctuation", ";"],
88+
["keyword", "extern"],
89+
["keyword", "crate"],
90+
["module-declaration", "test"],
91+
["punctuation", ";"],
92+
93+
["class-name", "Result"],
94+
["operator", "<"],
95+
["keyword", "Self"],
96+
["punctuation", ","],
97+
["class-name", "D"],
98+
["punctuation", "::"],
99+
["class-name", "Error"],
100+
["operator", ">"],
101+
102+
["keyword", "where"],
103+
["class-name", "D"],
104+
["punctuation", ":"],
105+
["namespace", [
106+
"serde",
107+
["punctuation", "::"]
108+
]],
109+
["class-name", "Deserializer"],
110+
["operator", "<"],
111+
["lifetime-annotation", "'de"],
112+
["operator", ">"],
113+
["punctuation", ","],
114+
115+
["namespace", [
116+
"serde_json",
117+
["punctuation", "::"]
118+
]],
119+
["function", "from_str"],
120+
["punctuation", "("],
121+
["operator", "&"],
122+
"line",
123+
["punctuation", ")"],
124+
["keyword", "self"],
125+
["punctuation", "."],
126+
["function", "read_records"],
127+
["punctuation", "::"],
128+
["operator", "<"],
129+
["namespace", [
130+
"smol_str",
131+
["punctuation", "::"]
132+
]],
133+
["class-name", "SmolStr"],
134+
["operator", ">"],
135+
["punctuation", "("],
136+
["punctuation", ")"],
137+
138+
["keyword", "pub"],
139+
["keyword", "static"],
140+
["constant", "ALLOCATOR"],
141+
["punctuation", ":"],
142+
["namespace", [
143+
"alloc",
144+
["punctuation", "::"]
145+
]],
146+
["class-name", "Tracing"],
147+
["operator", "="],
148+
["namespace", [
149+
"alloc",
150+
["punctuation", "::"]
151+
]],
152+
["class-name", "Tracing"],
153+
["punctuation", "::"],
154+
["function", "new"],
155+
["punctuation", "("],
156+
["punctuation", ")"],
157+
["punctuation", ";"],
158+
159+
["keyword", "unsafe"],
160+
["keyword", "fn"],
161+
["function-definition", "alloc"],
162+
["punctuation", "("],
163+
["operator", "&"],
164+
["keyword", "self"],
165+
["punctuation", ","],
166+
" layout",
167+
["punctuation", ":"],
168+
["namespace", [
169+
"std",
170+
["punctuation", "::"],
171+
"alloc",
172+
["punctuation", "::"]
173+
]],
174+
["class-name", "Layout"],
175+
["punctuation", ")"],
176+
["punctuation", "->"],
177+
["operator", "*"],
178+
["keyword", "mut"],
179+
["keyword", "u8"],
180+
["punctuation", "{"],
181+
["punctuation", "}"]
182+
]
183+
184+
----------------------------------------------------
185+
186+
Checks for namespaces.

0 commit comments

Comments
 (0)
Please sign in to comment.