From 926f6f862226c28a9730ac2bbb33bd2f7bd894aa Mon Sep 17 00:00:00 2001 From: Golmote Date: Wed, 7 Mar 2018 21:23:21 +0100 Subject: [PATCH] Rust: Add support for lifetime-annotation and => operator. Fix #1339 --- components/prism-rust.js | 12 ++++- components/prism-rust.min.js | 2 +- tests/languages/rust/char_feature.test | 13 +++++ tests/languages/rust/issue1339.test | 49 +++++++++++++++++++ .../rust/lifetime-annotation_feature.test | 15 ++++++ tests/languages/rust/operator_feature.test | 4 +- tests/languages/rust/string_feature.test | 8 --- 7 files changed, 90 insertions(+), 13 deletions(-) create mode 100644 tests/languages/rust/char_feature.test create mode 100644 tests/languages/rust/issue1339.test create mode 100644 tests/languages/rust/lifetime-annotation_feature.test diff --git a/components/prism-rust.js b/components/prism-rust.js index 5a1e75d6c9..bfdda91e82 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 47e66dca87..e6a6a01084 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 0000000000..9a1948411a --- /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 0000000000..1dd3208f59 --- /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 0000000000..02c813debc --- /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 a6e0892c88..5295bdb292 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 09282528c6..c0667e2668 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\"#"],