UNPKG

3.83 kBJavaScriptView Raw
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 alias: 'string'
32 },
33 'attribute': {
34 pattern: /#!?\[(?:[^\[\]"]|"(?:\\[\s\S]|[^\\"])*")*\]/,
35 greedy: true,
36 alias: 'attr-name',
37 inside: {
38 'string': null // see below
39 }
40 },
41
42 // Closure params should not be confused with bitwise OR |
43 'closure-params': {
44 pattern: /([=(,:]\s*|\bmove\s*)\|[^|]*\||\|[^|]*\|(?=\s*(?:\{|->))/,
45 lookbehind: true,
46 greedy: true,
47 inside: {
48 'closure-punctuation': {
49 pattern: /^\||\|$/,
50 alias: 'punctuation'
51 },
52 rest: null // see below
53 }
54 },
55
56 'lifetime-annotation': {
57 pattern: /'\w+/,
58 alias: 'symbol'
59 },
60
61 'fragment-specifier': {
62 pattern: /(\$\w+:)[a-z]+/,
63 lookbehind: true,
64 alias: 'punctuation'
65 },
66 'variable': /\$\w+/,
67
68 'function-definition': {
69 pattern: /(\bfn\s+)\w+/,
70 lookbehind: true,
71 alias: 'function'
72 },
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 ],
93 'keyword': [
94 // https://github.com/rust-lang/reference/blob/master/src/keywords.md
95 /\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/,
96 // primitives and str
97 // https://doc.rust-lang.org/stable/rust-by-example/primitives.html
98 /\b(?:[ui](?:8|16|32|64|128|size)|f(?:32|64)|bool|char|str)\b/
99 ],
100
101 // functions can technically start with an upper-case letter, but this will introduce a lot of false positives
102 // and Rust's naming conventions recommend snake_case anyway.
103 // https://doc.rust-lang.org/1.0.0/style/style/naming/README.html
104 'function': /\b[a-z_]\w*(?=\s*(?:::\s*<|\())/,
105 'macro': {
106 pattern: /\b\w+!/,
107 alias: 'property'
108 },
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 },
118
119 // Hex, oct, bin, dec numbers with visual separators and type suffix
120 '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/,
121 'boolean': /\b(?:false|true)\b/,
122 'punctuation': /->|\.\.=|\.{1,3}|::|[{}[\];(),:]/,
123 'operator': /[-+*\/%!^]=?|=[=>]?|&[&=]?|\|[|=]?|<<?=?|>>?=?|[@?]/
124 };
125
126 Prism.languages.rust['closure-params'].inside.rest = Prism.languages.rust;
127 Prism.languages.rust['attribute'].inside['string'] = Prism.languages.rust['string'];
128
129}(Prism));