Rust: Add support for lifetime-annotation and => operator. Fix #1339
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
diff --git a/components/prism-rust.js b/components/prism-rust.js
index 5a1e75d..bfdda91 100644
--- a/components/prism-rust.js
+++ b/components/prism-rust.js
@@ -22,10 +22,18 @@ Prism.languages.rust = {
greedy: true
},
{
- pattern: /b?("|')(?:\\.|(?!\1)[^\\\r\n])*\1/,
+ pattern: /b?"(?:\\.|[^\\\r\n"])*"/,
greedy: true
}
],
+ 'lifetime-annotation': {
+ pattern: /'[^\s>']+(?!')/,
+ alias: 'symbol'
+ },
+ 'char': {
+ pattern: /'(?:\\.|[^\\\r\n'])*'/,
+ alias: 'string'
+ },
'keyword': /\b(?:abstract|alignof|as|be|box|break|const|continue|crate|do|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|struct|super|true|trait|type|typeof|unsafe|unsized|use|virtual|where|while|yield)\b/,
'attribute': {
@@ -56,5 +64,5 @@ Prism.languages.rust = {
}
},
'punctuation': /[{}[\];(),:]|\.+|->/,
- 'operator': /[-+*\/%!^=]=?|@|&[&=]?|\|[|=]?|<<?=?|>>?=?/
+ 'operator': /[-+*\/%!^]=?|=[=>]?|@|&[&=]?|\|[|=]?|<<?=?|>>?=?/
};
\ No newline at end of file
diff --git a/components/prism-rust.min.js b/components/prism-rust.min.js
index 47e66dc..e6a6a01 100644
--- a/components/prism-rust.min.js
+++ b/components/prism-rust.min.js
@@ -1 +1 @@
-Prism.languages.rust={comment:[{pattern:/(^|[^\\])\/\*[\s\S]*?\*\//,lookbehind:!0},{pattern:/(^|[^\\:])\/\/.*/,lookbehind:!0}],string:[{pattern:/b?r(#*)"(?:\\.|(?!"\1)[^\\\r\n])*"\1/,greedy:!0},{pattern:/b?("|')(?:\\.|(?!\1)[^\\\r\n])*\1/,greedy:!0}],keyword:/\b(?:abstract|alignof|as|be|box|break|const|continue|crate|do|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|struct|super|true|trait|type|typeof|unsafe|unsized|use|virtual|where|while|yield)\b/,attribute:{pattern:/#!?\[.+?\]/,greedy:!0,alias:"attr-name"},"function":[/\w+(?=\s*\()/,/\w+!(?=\s*\(|\[)/],"macro-rules":{pattern:/\w+!/,alias:"function"},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/,"closure-params":{pattern:/\|[^|]*\|(?=\s*[{-])/,inside:{punctuation:/[|:,]/,operator:/[&*]/}},punctuation:/[{}[\];(),:]|\.+|->/,operator:/[-+*\/%!^=]=?|@|&[&=]?|\|[|=]?|<<?=?|>>?=?/};
\ No newline at end of file
+Prism.languages.rust={comment:[{pattern:/(^|[^\\])\/\*[\s\S]*?\*\//,lookbehind:!0},{pattern:/(^|[^\\:])\/\/.*/,lookbehind:!0}],string:[{pattern:/b?r(#*)"(?:\\.|(?!"\1)[^\\\r\n])*"\1/,greedy:!0},{pattern:/b?"(?:\\.|[^\\\r\n"])*"/,greedy:!0}],"lifetime-annotation":{pattern:/'[^\s>']+(?!')/,alias:"symbol"},"char":{pattern:/'(?:\\.|[^\\\r\n'])*'/,alias:"string"},keyword:/\b(?:abstract|alignof|as|be|box|break|const|continue|crate|do|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|struct|super|true|trait|type|typeof|unsafe|unsized|use|virtual|where|while|yield)\b/,attribute:{pattern:/#!?\[.+?\]/,greedy:!0,alias:"attr-name"},"function":[/\w+(?=\s*\()/,/\w+!(?=\s*\(|\[)/],"macro-rules":{pattern:/\w+!/,alias:"function"},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/,"closure-params":{pattern:/\|[^|]*\|(?=\s*[{-])/,inside:{punctuation:/[|:,]/,operator:/[&*]/}},punctuation:/[{}[\];(),:]|\.+|->/,operator:/[-+*\/%!^]=?|=[=>]?|@|&[&=]?|\|[|=]?|<<?=?|>>?=?/};
\ No newline at end of file
diff --git a/tests/languages/rust/char_feature.test b/tests/languages/rust/char_feature.test
new file mode 100644
index 0000000..9a19484
--- /dev/null
+++ b/tests/languages/rust/char_feature.test
@@ -0,0 +1,13 @@
+'a'
+'स'
+
+----------------------------------------------------
+
+[
+ ["char", "'a'"],
+ ["char", "'स'"]
+]
+
+----------------------------------------------------
+
+Checks for chars.
\ No newline at end of file
diff --git a/tests/languages/rust/issue1339.test b/tests/languages/rust/issue1339.test
new file mode 100644
index 0000000..1dd3208
--- /dev/null
+++ b/tests/languages/rust/issue1339.test
@@ -0,0 +1,49 @@
+const ALL_CARDS: &'static [&'static char] = &["2"]
+fn foo<'a> (first: &'a str, second: &'a str) => () { }
+
+----------------------------------------------------
+
+[
+ ["keyword", "const"],
+ " ALL_CARDS",
+ ["punctuation", ":"],
+ ["operator", "&"],
+ ["lifetime-annotation", "'static"],
+ ["punctuation", "["],
+ ["operator", "&"],
+ ["lifetime-annotation", "'static"],
+ " char",
+ ["punctuation", "]"],
+ ["operator", "="],
+ ["operator", "&"],
+ ["punctuation", "["],
+ ["string", "\"2\""],
+ ["punctuation", "]"],
+ ["keyword", "fn"],
+ " foo",
+ ["operator", "<"],
+ ["lifetime-annotation", "'a"],
+ ["operator", ">"],
+ ["punctuation", "("],
+ "first",
+ ["punctuation", ":"],
+ ["operator", "&"],
+ ["lifetime-annotation", "'a"],
+ " str",
+ ["punctuation", ","],
+ " second",
+ ["punctuation", ":"],
+ ["operator", "&"],
+ ["lifetime-annotation", "'a"],
+ " str",
+ ["punctuation", ")"],
+ ["operator", "=>"],
+ ["punctuation", "("],
+ ["punctuation", ")"],
+ ["punctuation", "{"],
+ ["punctuation", "}"]
+]
+
+----------------------------------------------------
+
+Checks for lifetime annotations in real-world examples. See #1339.
\ No newline at end of file
diff --git a/tests/languages/rust/lifetime-annotation_feature.test b/tests/languages/rust/lifetime-annotation_feature.test
new file mode 100644
index 0000000..02c813d
--- /dev/null
+++ b/tests/languages/rust/lifetime-annotation_feature.test
@@ -0,0 +1,15 @@
+'foo
+'a
+<'a>
+
+----------------------------------------------------
+
+[
+ ["lifetime-annotation", "'foo"],
+ ["lifetime-annotation", "'a"],
+ ["operator", "<"], ["lifetime-annotation", "'a"], ["operator", ">"]
+]
+
+----------------------------------------------------
+
+Checks for lifetime annotations.
\ No newline at end of file
diff --git a/tests/languages/rust/operator_feature.test b/tests/languages/rust/operator_feature.test
index a6e0892..5295bdb 100644
--- a/tests/languages/rust/operator_feature.test
+++ b/tests/languages/rust/operator_feature.test
@@ -5,7 +5,7 @@
% %=
! !=
^ ^=
-= ==
+= == =>
& && &=
| || |=
< << <= <<=
@@ -22,7 +22,7 @@
["operator", "%"], ["operator", "%="],
["operator", "!"], ["operator", "!="],
["operator", "^"], ["operator", "^="],
- ["operator", "="], ["operator", "=="],
+ ["operator", "="], ["operator", "=="], ["operator", "=>"],
["operator", "&"], ["operator", "&&"], ["operator", "&="],
["operator", "|"], ["operator", "||"], ["operator", "|="],
["operator", "<"], ["operator", "<<"], ["operator", "<="], ["operator", "<<="],
diff --git a/tests/languages/rust/string_feature.test b/tests/languages/rust/string_feature.test
index 0928252..c0667e2 100644
--- a/tests/languages/rust/string_feature.test
+++ b/tests/languages/rust/string_feature.test
@@ -1,12 +1,8 @@
""
"fo\"obar"
-''
-'fo\'obar'
b""
b"fo\"obar"
-b''
-b'fo\'obar'
r#""#
r#"fo"obar"#
@@ -21,13 +17,9 @@ br###"foo#bar"###
[
["string", "\"\""],
["string", "\"fo\\\"obar\""],
- ["string", "''"],
- ["string", "'fo\\'obar'"],
["string", "b\"\""],
["string", "b\"fo\\\"obar\""],
- ["string", "b''"],
- ["string", "b'fo\\'obar'"],
["string", "r#\"\"#"],
["string", "r#\"fo\"obar\"#"],