|
1 |
| -/* TODO |
2 |
| - Add support for Markdown notation inside doc comments |
3 |
| - Add support for nested block comments... |
4 |
| - Match closure params even when not followed by dash or brace |
5 |
| - Add better support for macro definition |
6 |
| -*/ |
| 1 | +(function (Prism) { |
7 | 2 |
|
8 |
| -Prism.languages.rust = { |
9 |
| - 'comment': [ |
10 |
| - { |
11 |
| - pattern: /(^|[^\\])\/\*[\s\S]*?\*\//, |
12 |
| - lookbehind: true |
13 |
| - }, |
14 |
| - { |
15 |
| - pattern: /(^|[^\\:])\/\/.*/, |
16 |
| - lookbehind: true |
17 |
| - } |
18 |
| - ], |
19 |
| - 'string': [ |
20 |
| - { |
21 |
| - pattern: /b?r(#*)"(?:\\.|(?!"\1)[^\\\r\n])*"\1/, |
| 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/, |
22 | 26 | greedy: true
|
23 | 27 | },
|
24 |
| - { |
25 |
| - pattern: /b?"(?:\\.|[^\\\r\n"])*"/, |
26 |
| - greedy: true |
27 |
| - } |
28 |
| - ], |
29 |
| - 'char': { |
30 |
| - pattern: /b?'(?:\\(?:x[0-7][\da-fA-F]|u{(?:[\da-fA-F]_*){1,6}|.)|[^\\\r\n\t'])'/, |
31 |
| - alias: 'string' |
32 |
| - }, |
33 |
| - 'lifetime-annotation': { |
34 |
| - pattern: /'[^\s>']+/, |
35 |
| - alias: 'symbol' |
36 |
| - }, |
37 |
| - 'keyword': /\b(?:abstract|alignof|as|async|await|be|box|break|const|continue|crate|do|dyn|else|enum|extern|false|final|fn|for|if|impl|in|let|loop|match|mod|move|mut|offsetof|once|override|priv|pub|pure|ref|return|sizeof|static|self|Self|struct|super|true|trait|type|typeof|union|unsafe|unsized|use|virtual|where|while|yield)\b/, |
| 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: /#!?\[[^[\]]*\]/, |
| 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 | + }, |
38 | 55 |
|
39 |
| - 'attribute': { |
40 |
| - pattern: /#!?\[.+?\]/, |
41 |
| - greedy: true, |
42 |
| - alias: 'attr-name' |
43 |
| - }, |
| 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 | + 'keyword': [ |
| 74 | + // https://github.com/rust-lang/reference/blob/master/src/keywords.md |
| 75 | + /\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 |
| 77 | + // 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/ |
| 79 | + ], |
| 80 | + |
| 81 | + // functions can technically start with an upper-case letter, but this will introduce a lot of false positives |
| 82 | + // and Rust's naming conventions recommend snake_case anyway. |
| 83 | + // https://doc.rust-lang.org/1.0.0/style/style/naming/README.html |
| 84 | + 'function': /\b[a-z_]\w*(?=\s*(?:::\s*<(?:[^<>]|<(?:[^<>]|<[^<>]*>)*>)*>\s*)?\()/, |
| 85 | + 'macro': { |
| 86 | + pattern: /\w+!/, |
| 87 | + alias: 'property' |
| 88 | + }, |
44 | 89 |
|
45 |
| - 'function': [ |
46 |
| - /\w+(?=\s*\()/, |
47 |
| - // Macros can use parens or brackets |
48 |
| - /\w+!(?=\s*\(|\[)/ |
49 |
| - ], |
50 |
| - 'macro-rules': { |
51 |
| - pattern: /\w+!/, |
52 |
| - alias: 'function' |
53 |
| - }, |
| 90 | + // Hex, oct, bin, dec numbers with visual separators and type suffix |
| 91 | + '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/, |
| 92 | + 'boolean': /\b(?:false|true)\b/, |
| 93 | + 'punctuation': /->|\.\.=|\.{1,3}|::|[{}[\];(),:]/, |
| 94 | + 'operator': /[-+*\/%!^]=?|=[=>]?|&[&=]?|\|[|=]?|<<?=?|>>?=?|[@?]/ |
| 95 | + }; |
54 | 96 |
|
55 |
| - // Hex, oct, bin, dec numbers with visual separators and type suffix |
56 |
| - '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)?|f32|f64))?\b/, |
| 97 | + Prism.languages.rust['closure-params'].inside.rest = Prism.languages.rust; |
| 98 | + Prism.languages.rust['attribute'].inside['string'] = Prism.languages.rust['string']; |
57 | 99 |
|
58 |
| - // Closure params should not be confused with bitwise OR | |
59 |
| - 'closure-params': { |
60 |
| - pattern: /\|[^|]*\|(?=\s*[{-])/, |
61 |
| - inside: { |
62 |
| - 'punctuation': /[|:,]/, |
63 |
| - 'operator': /[&*]/ |
64 |
| - } |
65 |
| - }, |
66 |
| - 'punctuation': /->|\.\.=|\.{1,3}|::|[{}[\];(),:]/, |
67 |
| - 'operator': /[-+*\/%!^]=?|=[=>]?|&[&=]?|\|[|=]?|<<?=?|>>?=?|[@?]/ |
68 |
| -}; |
| 100 | +}(Prism)); |
0 commit comments