Skip to content

Commit

Permalink
Rust: Add support for lifetime-annotation and => operator. Fix #1339
Browse files Browse the repository at this point in the history
  • Loading branch information
Golmote committed Mar 7, 2018
1 parent b98e5b9 commit 926f6f8
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 13 deletions.
12 changes: 10 additions & 2 deletions components/prism-rust.js
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand Down Expand Up @@ -56,5 +64,5 @@ Prism.languages.rust = {
}
},
'punctuation': /[{}[\];(),:]|\.+|->/,
'operator': /[-+*\/%!^=]=?|@|&[&=]?|\|[|=]?|<<?=?|>>?=?/
'operator': /[-+*\/%!^]=?|=[=>]?|@|&[&=]?|\|[|=]?|<<?=?|>>?=?/
};
2 changes: 1 addition & 1 deletion components/prism-rust.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions tests/languages/rust/char_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'a'
'स'

----------------------------------------------------

[
["char", "'a'"],
["char", "'स'"]
]

----------------------------------------------------

Checks for chars.
49 changes: 49 additions & 0 deletions tests/languages/rust/issue1339.test
Original file line number Diff line number Diff line change
@@ -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.
15 changes: 15 additions & 0 deletions tests/languages/rust/lifetime-annotation_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'foo
'a
<'a>

----------------------------------------------------

[
["lifetime-annotation", "'foo"],
["lifetime-annotation", "'a"],
["operator", "<"], ["lifetime-annotation", "'a"], ["operator", ">"]
]

----------------------------------------------------

Checks for lifetime annotations.
4 changes: 2 additions & 2 deletions tests/languages/rust/operator_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
% %=
! !=
^ ^=
= ==
= == =>
& && &=
| || |=
< << <= <<=
Expand All @@ -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", "<<="],
Expand Down
8 changes: 0 additions & 8 deletions tests/languages/rust/string_feature.test
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
""
"fo\"obar"
''
'fo\'obar'

b""
b"fo\"obar"
b''
b'fo\'obar'

r#""#
r#"fo"obar"#
Expand All @@ -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\"#"],
Expand Down

0 comments on commit 926f6f8

Please sign in to comment.