Skip to content

Commit

Permalink
Rust: Improvements (#2464)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed Jul 18, 2020
1 parent 2805ae3 commit 2ff40fe
Show file tree
Hide file tree
Showing 9 changed files with 386 additions and 35 deletions.
39 changes: 34 additions & 5 deletions components/prism-rust.js
Expand Up @@ -31,7 +31,7 @@
alias: 'string'
},
'attribute': {
pattern: /#!?\[[^[\]]*\]/,
pattern: /#!?\[(?:[^\[\]"]|"(?:\\[\s\S]|[^\\"])*")*\]/,
greedy: true,
alias: 'attr-name',
inside: {
Expand Down Expand Up @@ -66,26 +66,55 @@
'variable': /\$\w+/,

'function-definition': {
pattern: /(\bfn\s*)\w+/,
pattern: /(\bfn\s+)\w+/,
lookbehind: true,
alias: 'function'
},
'type-definition': {
pattern: /(\b(?:enum|struct|union)\s+)\w+/,
lookbehind: true,
alias: 'class-name'
},
'module-declaration': [
{
pattern: /(\b(?:crate|mod)\s+)[a-z][a-z_\d]*/,
lookbehind: true,
alias: 'namespace'
},
{
pattern: /(\b(?:crate|self|super)\s*)::\s*[a-z][a-z_\d]*\b(?:\s*::(?:\s*[a-z][a-z_\d]*\s*::)*)?/,
lookbehind: true,
alias: 'namespace',
inside: {
'punctuation': /::/
}
}
],
'keyword': [
// https://github.com/rust-lang/reference/blob/master/src/keywords.md
/\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/,
// primitives
// primitives and str
// https://doc.rust-lang.org/stable/rust-by-example/primitives.html
/\b(?:[ui](?:8|16|32|64|128|size)|f(?:32|64)|bool|char)\b/
/\b(?:[ui](?:8|16|32|64|128|size)|f(?:32|64)|bool|char|str)\b/
],

// functions can technically start with an upper-case letter, but this will introduce a lot of false positives
// and Rust's naming conventions recommend snake_case anyway.
// https://doc.rust-lang.org/1.0.0/style/style/naming/README.html
'function': /\b[a-z_]\w*(?=\s*(?:::\s*<(?:[^<>]|<(?:[^<>]|<[^<>]*>)*>)*>\s*)?\()/,
'function': /\b[a-z_]\w*(?=\s*(?:::\s*<|\())/,
'macro': {
pattern: /\w+!/,
alias: 'property'
},
'constant': /\b[A-Z_][A-Z_\d]+\b/,
'class-name': /\b[A-Z]\w*\b/,

'namespace': {
pattern: /(?:\b[a-z][a-z_\d]*\s*::\s*)*\b[a-z][a-z_\d]*\s*::(?!\s*<)/,
inside: {
'punctuation': /::/
}
},

// Hex, oct, bin, dec numbers with visual separators and type suffix
'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/,
Expand Down
2 changes: 1 addition & 1 deletion components/prism-rust.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 6 additions & 9 deletions examples/prism-rust.html
Expand Up @@ -5,7 +5,7 @@ <h2>Comments</h2>
comment */</code></pre>

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

<h2>Numbers</h2>
<pre><code>123i; // type int
123u; // type uint
123_u; // type uint
0xff_u8; // type u8
<pre><code>0xff_u8; // type u8
0o70_i16; // type i16
0b1111_1111_1001_0000_i32; // type i32

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

<h2>Functions and macros</h2>
<pre><code>println!("x is {}", x);
fn next_two(x: int) -> (int, int) { (x + 1i, x + 2i) }
next_two(5i);
vec![1i, 2, 3];
fn next_two(x: i32) -> (i32, i32) { (x + 1, x + 2) }
next_two(5);
vec![1, 2, 3];
</code></pre>

<h2>Attributes</h2>
Expand All @@ -47,6 +44,6 @@ <h2>Attributes</h2>
<h2>Closure parameters and bitwise OR</h2>
<pre><code>let x = a | b;
let y = c || d;
let add_one = |x: int| -> int { 1i + x };
let add_one = |x: i32| -> i32 { 1i + x };
let printer = || { println!("x is: {}", x); };
</code></pre>
87 changes: 87 additions & 0 deletions tests/languages/rust/class-name_feature.test
@@ -0,0 +1,87 @@
struct foo {}

let foo: CStr;
let foo: &'a CStr;
let foo: &'a Foo<dyn Bar>;
Option::Some(foo);
Option::None;

// we can differentiate between enum variants and class names
// so let's make the bug a feature!
enum Foo {
Const,
Tuple(i8,i8),
Struct {
foo: u8
}
}

----------------------------------------------------

[
["keyword", "struct"],
["type-definition", "foo"],
["punctuation", "{"],
["punctuation", "}"],

["keyword", "let"],
" foo",
["punctuation", ":"],
["class-name", "CStr"],
["punctuation", ";"],
["keyword", "let"],
" foo",
["punctuation", ":"],
["operator", "&"],
["lifetime-annotation", "'a"],
["class-name", "CStr"],
["punctuation", ";"],
["keyword", "let"],
" foo",
["punctuation", ":"],
["operator", "&"],
["lifetime-annotation", "'a"],
["class-name", "Foo"],
["operator", "<"],
["keyword", "dyn"],
["class-name", "Bar"],
["operator", ">"],
["punctuation", ";"],
["class-name", "Option"],
["punctuation", "::"],
["class-name", "Some"],
["punctuation", "("],
"foo",
["punctuation", ")"],
["punctuation", ";"],
["class-name", "Option"],
["punctuation", "::"],
["class-name", "None"],
["punctuation", ";"],

["comment", "// we can differentiate between enum variants and class names"],
["comment", "// so let's make the bug a feature!"],
["keyword", "enum"],
["type-definition", "Foo"],
["punctuation", "{"],
["class-name", "Const"],
["punctuation", ","],
["class-name", "Tuple"],
["punctuation", "("],
["keyword", "i8"],
["punctuation", ","],
["keyword", "i8"],
["punctuation", ")"],
["punctuation", ","],
["class-name", "Struct"],
["punctuation", "{"],
"\n\t\tfoo",
["punctuation", ":"],
["keyword", "u8"],
["punctuation", "}"],
["punctuation", "}"]
]

----------------------------------------------------

Checks for class names and enum variants.
19 changes: 19 additions & 0 deletions tests/languages/rust/constant_feature.test
@@ -0,0 +1,19 @@
MAX
SOME_CONSTANT

// not a constant
T

----------------------------------------------------

[
["constant", "MAX"],
["constant", "SOME_CONSTANT"],

["comment", "// not a constant"],
["class-name", "T"]
]

----------------------------------------------------

Checks for constants.
45 changes: 38 additions & 7 deletions tests/languages/rust/function_feature.test
Expand Up @@ -4,6 +4,8 @@ foo_bar_42(

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

mem::transmute::<Box<dyn FnOnce() + 'a>, Box<dyn FnOnce() + 'static>>()

fn apply<F>(f: F) where F: FnOnce() {
f();
}
Expand All @@ -21,29 +23,58 @@ fn apply<F>(f: F) where F: FnOnce() {
["function", "foo_generic"],
["punctuation", "::"],
["operator", "<"],
"T",
["class-name", "T"],
["punctuation", ","],
" Option",
["class-name", "Option"],
["operator", "<"],
["class-name", "T"],
["operator", ">>"],
["punctuation", "("],
["punctuation", ")"],

["namespace", [
"mem",
["punctuation", "::"]
]],
["function", "transmute"],
["punctuation", "::"],
["operator", "<"],
["class-name", "Box"],
["operator", "<"],
"T",
["keyword", "dyn"],
["class-name", "FnOnce"],
["punctuation", "("],
["punctuation", ")"],
["operator", "+"],
["lifetime-annotation", "'a"],
["operator", ">"],
["punctuation", ","],
["class-name", "Box"],
["operator", "<"],
["keyword", "dyn"],
["class-name", "FnOnce"],
["punctuation", "("],
["punctuation", ")"],
["operator", "+"],
["lifetime-annotation", "'static"],
["operator", ">>"],
["punctuation", "("],
["punctuation", ")"],

["keyword", "fn"],
["function-definition", "apply"],
["operator", "<"],
"F",
["class-name", "F"],
["operator", ">"],
["punctuation", "("],
"f",
["punctuation", ":"],
" F",
["class-name", "F"],
["punctuation", ")"],
["keyword", "where"],
" F",
["class-name", "F"],
["punctuation", ":"],
" FnOnce",
["class-name", "FnOnce"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],
Expand Down
8 changes: 5 additions & 3 deletions tests/languages/rust/issue1339.test
@@ -1,11 +1,12 @@
const ALL_CARDS: &'static [&'static char] = &["2"]

fn foo<'a> (first: &'a str, second: &'a str) => () { }

----------------------------------------------------

[
["keyword", "const"],
" ALL_CARDS",
["constant", "ALL_CARDS"],
["punctuation", ":"],
["operator", "&"],
["lifetime-annotation", "'static"],
Expand All @@ -19,6 +20,7 @@ fn foo<'a> (first: &'a str, second: &'a str) => () { }
["punctuation", "["],
["string", "\"2\""],
["punctuation", "]"],

["keyword", "fn"],
["function-definition", "foo"],
["operator", "<"],
Expand All @@ -29,13 +31,13 @@ fn foo<'a> (first: &'a str, second: &'a str) => () { }
["punctuation", ":"],
["operator", "&"],
["lifetime-annotation", "'a"],
" str",
["keyword", "str"],
["punctuation", ","],
" second",
["punctuation", ":"],
["operator", "&"],
["lifetime-annotation", "'a"],
" str",
["keyword", "str"],
["punctuation", ")"],
["operator", "=>"],
["punctuation", "("],
Expand Down

0 comments on commit 2ff40fe

Please sign in to comment.