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