1 | (function (Prism) {
|
---|
2 |
|
---|
3 | var multilineComment = /\/\*(?:[^*/]|\*(?!\/)|\/(?!\*)|<self>)*\*\//.source;
|
---|
4 | for (var i = 0; i < 2; i++) {
|
---|
5 | // support 4 levels of nested comments
|
---|
6 | multilineComment = multilineComment.replace(/<self>/g, function () { return multilineComment; });
|
---|
7 | }
|
---|
8 | multilineComment = multilineComment.replace(/<self>/g, function () { return /[^\s\S]/.source; });
|
---|
9 |
|
---|
10 |
|
---|
11 | Prism.languages.rust = {
|
---|
12 | 'comment': [
|
---|
13 | {
|
---|
14 | pattern: RegExp(/(^|[^\\])/.source + multilineComment),
|
---|
15 | lookbehind: true,
|
---|
16 | greedy: true
|
---|
17 | },
|
---|
18 | {
|
---|
19 | pattern: /(^|[^\\:])\/\/.*/,
|
---|
20 | lookbehind: true,
|
---|
21 | greedy: true
|
---|
22 | }
|
---|
23 | ],
|
---|
24 | 'string': {
|
---|
25 | pattern: /b?"(?:\\[\s\S]|[^\\"])*"|b?r(#*)"(?:[^"]|"(?!\1))*"\1/,
|
---|
26 | greedy: true
|
---|
27 | },
|
---|
28 | 'char': {
|
---|
29 | pattern: /b?'(?:\\(?:x[0-7][\da-fA-F]|u\{(?:[\da-fA-F]_*){1,6}\}|.)|[^\\\r\n\t'])'/,
|
---|
30 | greedy: true
|
---|
31 | },
|
---|
32 | 'attribute': {
|
---|
33 | pattern: /#!?\[(?:[^\[\]"]|"(?:\\[\s\S]|[^\\"])*")*\]/,
|
---|
34 | greedy: true,
|
---|
35 | alias: 'attr-name',
|
---|
36 | inside: {
|
---|
37 | 'string': null // see below
|
---|
38 | }
|
---|
39 | },
|
---|
40 |
|
---|
41 | // Closure params should not be confused with bitwise OR |
|
---|
42 | 'closure-params': {
|
---|
43 | pattern: /([=(,:]\s*|\bmove\s*)\|[^|]*\||\|[^|]*\|(?=\s*(?:\{|->))/,
|
---|
44 | lookbehind: true,
|
---|
45 | greedy: true,
|
---|
46 | inside: {
|
---|
47 | 'closure-punctuation': {
|
---|
48 | pattern: /^\||\|$/,
|
---|
49 | alias: 'punctuation'
|
---|
50 | },
|
---|
51 | rest: null // see below
|
---|
52 | }
|
---|
53 | },
|
---|
54 |
|
---|
55 | 'lifetime-annotation': {
|
---|
56 | pattern: /'\w+/,
|
---|
57 | alias: 'symbol'
|
---|
58 | },
|
---|
59 |
|
---|
60 | 'fragment-specifier': {
|
---|
61 | pattern: /(\$\w+:)[a-z]+/,
|
---|
62 | lookbehind: true,
|
---|
63 | alias: 'punctuation'
|
---|
64 | },
|
---|
65 | 'variable': /\$\w+/,
|
---|
66 |
|
---|
67 | 'function-definition': {
|
---|
68 | pattern: /(\bfn\s+)\w+/,
|
---|
69 | lookbehind: true,
|
---|
70 | alias: 'function'
|
---|
71 | },
|
---|
72 | 'type-definition': {
|
---|
73 | pattern: /(\b(?:enum|struct|trait|type|union)\s+)\w+/,
|
---|
74 | lookbehind: true,
|
---|
75 | alias: 'class-name'
|
---|
76 | },
|
---|
77 | 'module-declaration': [
|
---|
78 | {
|
---|
79 | pattern: /(\b(?:crate|mod)\s+)[a-z][a-z_\d]*/,
|
---|
80 | lookbehind: true,
|
---|
81 | alias: 'namespace'
|
---|
82 | },
|
---|
83 | {
|
---|
84 | pattern: /(\b(?:crate|self|super)\s*)::\s*[a-z][a-z_\d]*\b(?:\s*::(?:\s*[a-z][a-z_\d]*\s*::)*)?/,
|
---|
85 | lookbehind: true,
|
---|
86 | alias: 'namespace',
|
---|
87 | inside: {
|
---|
88 | 'punctuation': /::/
|
---|
89 | }
|
---|
90 | }
|
---|
91 | ],
|
---|
92 | 'keyword': [
|
---|
93 | // https://github.com/rust-lang/reference/blob/master/src/keywords.md
|
---|
94 | /\b(?:Self|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|static|struct|super|trait|try|type|typeof|union|unsafe|unsized|use|virtual|where|while|yield)\b/,
|
---|
95 | // primitives and str
|
---|
96 | // https://doc.rust-lang.org/stable/rust-by-example/primitives.html
|
---|
97 | /\b(?:bool|char|f(?:32|64)|[ui](?:8|16|32|64|128|size)|str)\b/
|
---|
98 | ],
|
---|
99 |
|
---|
100 | // functions can technically start with an upper-case letter, but this will introduce a lot of false positives
|
---|
101 | // and Rust's naming conventions recommend snake_case anyway.
|
---|
102 | // https://doc.rust-lang.org/1.0.0/style/style/naming/README.html
|
---|
103 | 'function': /\b[a-z_]\w*(?=\s*(?:::\s*<|\())/,
|
---|
104 | 'macro': {
|
---|
105 | pattern: /\b\w+!/,
|
---|
106 | alias: 'property'
|
---|
107 | },
|
---|
108 | 'constant': /\b[A-Z_][A-Z_\d]+\b/,
|
---|
109 | 'class-name': /\b[A-Z]\w*\b/,
|
---|
110 |
|
---|
111 | 'namespace': {
|
---|
112 | pattern: /(?:\b[a-z][a-z_\d]*\s*::\s*)*\b[a-z][a-z_\d]*\s*::(?!\s*<)/,
|
---|
113 | inside: {
|
---|
114 | 'punctuation': /::/
|
---|
115 | }
|
---|
116 | },
|
---|
117 |
|
---|
118 | // Hex, oct, bin, dec numbers with visual separators and type suffix
|
---|
119 | 'number': /\b(?:0x[\dA-Fa-f](?:_?[\dA-Fa-f])*|0o[0-7](?:_?[0-7])*|0b[01](?:_?[01])*|(?:(?:\d(?:_?\d)*)?\.)?\d(?:_?\d)*(?:[Ee][+-]?\d+)?)(?:_?(?:f32|f64|[iu](?:8|16|32|64|size)?))?\b/,
|
---|
120 | 'boolean': /\b(?:false|true)\b/,
|
---|
121 | 'punctuation': /->|\.\.=|\.{1,3}|::|[{}[\];(),:]/,
|
---|
122 | 'operator': /[-+*\/%!^]=?|=[=>]?|&[&=]?|\|[|=]?|<<?=?|>>?=?|[@?]/
|
---|
123 | };
|
---|
124 |
|
---|
125 | Prism.languages.rust['closure-params'].inside.rest = Prism.languages.rust;
|
---|
126 | Prism.languages.rust['attribute'].inside['string'] = Prism.languages.rust['string'];
|
---|
127 |
|
---|
128 | }(Prism));
|
---|