From 0a9f909ca5fdf59a92b3bf636f6c366f2a60e1bc Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Sun, 5 Dec 2021 14:40:38 +0100 Subject: [PATCH] Java: Added `char` token (#3217) --- components/prism-java.js | 9 +++++++++ components/prism-java.min.js | 2 +- tests/languages/java/char_feature.test | 11 +++++++++++ tests/languages/java/generics_feature.test | 2 +- tests/languages/java/string_feature.test | 2 ++ tests/languages/scala/char_feature.test | 13 +++++++++++++ tests/languages/scala/string_feature.test | 10 +--------- 7 files changed, 38 insertions(+), 11 deletions(-) create mode 100644 tests/languages/java/char_feature.test create mode 100644 tests/languages/scala/char_feature.test diff --git a/components/prism-java.js b/components/prism-java.js index 83ddd003b8..9fe9f695f9 100644 --- a/components/prism-java.js +++ b/components/prism-java.js @@ -21,6 +21,11 @@ }; Prism.languages.java = Prism.languages.extend('clike', { + 'string': { + pattern: /(^|[^\\])"(?:\\.|[^"\\\r\n])*"/, + lookbehind: true, + greedy: true + }, 'class-name': [ className, { @@ -52,6 +57,10 @@ pattern: /"""[ \t]*[\r\n](?:(?:"|"")?(?:\\.|[^"\\]))*"""/, greedy: true, alias: 'string' + }, + 'char': { + pattern: /'(?:\\.|[^'\\\r\n]){1,6}'/, + greedy: true } }); diff --git a/components/prism-java.min.js b/components/prism-java.min.js index 75ba5da2f9..897da4e88c 100644 --- a/components/prism-java.min.js +++ b/components/prism-java.min.js @@ -1 +1 @@ -!function(e){var t=/\b(?:abstract|assert|boolean|break|byte|case|catch|char|class|const|continue|default|do|double|else|enum|exports|extends|final|finally|float|for|goto|if|implements|import|instanceof|int|interface|long|module|native|new|non-sealed|null|open|opens|package|permits|private|protected|provides|public|record|requires|return|sealed|short|static|strictfp|super|switch|synchronized|this|throw|throws|to|transient|transitive|try|uses|var|void|volatile|while|with|yield)\b/,n="(^|[^\\w.])(?:[a-z]\\w*\\s*\\.\\s*)*(?:[A-Z]\\w*\\s*\\.\\s*)*",a={pattern:RegExp(n+"[A-Z](?:[\\d_A-Z]*[a-z]\\w*)?\\b"),lookbehind:!0,inside:{namespace:{pattern:/^[a-z]\w*(?:\s*\.\s*[a-z]\w*)*(?:\s*\.)?/,inside:{punctuation:/\./}},punctuation:/\./}};e.languages.java=e.languages.extend("clike",{"class-name":[a,{pattern:RegExp(n+"[A-Z]\\w*(?=\\s+\\w+\\s*[;,=()])"),lookbehind:!0,inside:a.inside}],keyword:t,function:[e.languages.clike.function,{pattern:/(::\s*)[a-z_]\w*/,lookbehind:!0}],number:/\b0b[01][01_]*L?\b|\b0x(?:\.[\da-f_p+-]+|[\da-f_]+(?:\.[\da-f_p+-]+)?)\b|(?:\b\d[\d_]*(?:\.[\d_]*)?|\B\.\d[\d_]*)(?:e[+-]?\d[\d_]*)?[dfl]?/i,operator:{pattern:/(^|[^.])(?:<<=?|>>>?=?|->|--|\+\+|&&|\|\||::|[?:~]|[-+*/%&|^!=<>]=?)/m,lookbehind:!0}}),e.languages.insertBefore("java","string",{"triple-quoted-string":{pattern:/"""[ \t]*[\r\n](?:(?:"|"")?(?:\\.|[^"\\]))*"""/,greedy:!0,alias:"string"}}),e.languages.insertBefore("java","class-name",{annotation:{pattern:/(^|[^.])@\w+(?:\s*\.\s*\w+)*/,lookbehind:!0,alias:"punctuation"},generics:{pattern:/<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&))*>)*>)*>)*>/,inside:{"class-name":a,keyword:t,punctuation:/[<>(),.:]/,operator:/[?&|]/}},namespace:{pattern:RegExp("(\\b(?:exports|import(?:\\s+static)?|module|open|opens|package|provides|requires|to|transitive|uses|with)\\s+)(?!)[a-z]\\w*(?:\\.[a-z]\\w*)*\\.?".replace(//g,function(){return t.source})),lookbehind:!0,inside:{punctuation:/\./}}})}(Prism); \ No newline at end of file +!function(e){var t=/\b(?:abstract|assert|boolean|break|byte|case|catch|char|class|const|continue|default|do|double|else|enum|exports|extends|final|finally|float|for|goto|if|implements|import|instanceof|int|interface|long|module|native|new|non-sealed|null|open|opens|package|permits|private|protected|provides|public|record|requires|return|sealed|short|static|strictfp|super|switch|synchronized|this|throw|throws|to|transient|transitive|try|uses|var|void|volatile|while|with|yield)\b/,n="(^|[^\\w.])(?:[a-z]\\w*\\s*\\.\\s*)*(?:[A-Z]\\w*\\s*\\.\\s*)*",a={pattern:RegExp(n+"[A-Z](?:[\\d_A-Z]*[a-z]\\w*)?\\b"),lookbehind:!0,inside:{namespace:{pattern:/^[a-z]\w*(?:\s*\.\s*[a-z]\w*)*(?:\s*\.)?/,inside:{punctuation:/\./}},punctuation:/\./}};e.languages.java=e.languages.extend("clike",{string:{pattern:/(^|[^\\])"(?:\\.|[^"\\\r\n])*"/,lookbehind:!0,greedy:!0},"class-name":[a,{pattern:RegExp(n+"[A-Z]\\w*(?=\\s+\\w+\\s*[;,=()])"),lookbehind:!0,inside:a.inside}],keyword:t,function:[e.languages.clike.function,{pattern:/(::\s*)[a-z_]\w*/,lookbehind:!0}],number:/\b0b[01][01_]*L?\b|\b0x(?:\.[\da-f_p+-]+|[\da-f_]+(?:\.[\da-f_p+-]+)?)\b|(?:\b\d[\d_]*(?:\.[\d_]*)?|\B\.\d[\d_]*)(?:e[+-]?\d[\d_]*)?[dfl]?/i,operator:{pattern:/(^|[^.])(?:<<=?|>>>?=?|->|--|\+\+|&&|\|\||::|[?:~]|[-+*/%&|^!=<>]=?)/m,lookbehind:!0}}),e.languages.insertBefore("java","string",{"triple-quoted-string":{pattern:/"""[ \t]*[\r\n](?:(?:"|"")?(?:\\.|[^"\\]))*"""/,greedy:!0,alias:"string"},char:{pattern:/'(?:\\.|[^'\\\r\n]){1,6}'/,greedy:!0}}),e.languages.insertBefore("java","class-name",{annotation:{pattern:/(^|[^.])@\w+(?:\s*\.\s*\w+)*/,lookbehind:!0,alias:"punctuation"},generics:{pattern:/<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&))*>)*>)*>)*>/,inside:{"class-name":a,keyword:t,punctuation:/[<>(),.:]/,operator:/[?&|]/}},namespace:{pattern:RegExp("(\\b(?:exports|import(?:\\s+static)?|module|open|opens|package|provides|requires|to|transitive|uses|with)\\s+)(?!)[a-z]\\w*(?:\\.[a-z]\\w*)*\\.?".replace(//g,function(){return t.source})),lookbehind:!0,inside:{punctuation:/\./}}})}(Prism); \ No newline at end of file diff --git a/tests/languages/java/char_feature.test b/tests/languages/java/char_feature.test new file mode 100644 index 0000000000..f376ee9220 --- /dev/null +++ b/tests/languages/java/char_feature.test @@ -0,0 +1,11 @@ +'A' +'\n' +'\u0041' + +---------------------------------------------------- + +[ + ["char", "'A'"], + ["char", "'\\n'"], + ["char", "'\\u0041'"] +] diff --git a/tests/languages/java/generics_feature.test b/tests/languages/java/generics_feature.test index 7944a7ee8e..989187b949 100644 --- a/tests/languages/java/generics_feature.test +++ b/tests/languages/java/generics_feature.test @@ -83,7 +83,7 @@ if (a<6&&b>6){} ["punctuation", "("], ["number", "12.2585"], ["punctuation", ","], - ["string", "'C'"], + ["char", "'C'"], ["punctuation", ")"], ["punctuation", ";"], diff --git a/tests/languages/java/string_feature.test b/tests/languages/java/string_feature.test index 34971f4e7b..1734f1a190 100644 --- a/tests/languages/java/string_feature.test +++ b/tests/languages/java/string_feature.test @@ -1,3 +1,4 @@ +"" "foo" "\"foo\"" @@ -20,6 +21,7 @@ String empty = """ ---------------------------------------------------- [ + ["string", "\"\""], ["string", "\"foo\""], ["string", "\"\\\"foo\\\"\""], diff --git a/tests/languages/scala/char_feature.test b/tests/languages/scala/char_feature.test new file mode 100644 index 0000000000..b409bcfdaa --- /dev/null +++ b/tests/languages/scala/char_feature.test @@ -0,0 +1,13 @@ +'a' +'\u0041' +'\n' +'\t' + +---------------------------------------------------- + +[ + ["char", "'a'"], + ["char", "'\\u0041'"], + ["char", "'\\n'"], + ["char", "'\\t'"] +] diff --git a/tests/languages/scala/string_feature.test b/tests/languages/scala/string_feature.test index 502625d593..75878ba473 100644 --- a/tests/languages/scala/string_feature.test +++ b/tests/languages/scala/string_feature.test @@ -1,8 +1,3 @@ -'a' -'\u0041' -'\n' -'\t' - "" "fo\"obar" @@ -17,12 +12,9 @@ bar""" ---------------------------------------------------- [ - ["string", "'a'"], - ["string", "'\\u0041'"], - ["string", "'\\n'"], - ["string", "'\\t'"], ["string", "\"\""], ["string", "\"fo\\\"obar\""], + ["triple-quoted-string", "\"\"\"fo\"o\r\nbar\"\"\""], ["triple-quoted-string", "\"\"\"fo\"o\r\n// comment\r\nbar\"\"\""], ["string", "\"foo /* comment */ bar\""],