C/C++: Improvemed `comment` pattern (#2229) This improves the `comment` pattern by allowing backslash-escaped line breaks in single-line comments.
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
diff --git a/components/prism-c.js b/components/prism-c.js
index 0c058ff..9c9971f 100644
--- a/components/prism-c.js
+++ b/components/prism-c.js
@@ -1,4 +1,8 @@
Prism.languages.c = Prism.languages.extend('clike', {
+ 'comment': {
+ pattern: /\/\/(?:[^\r\n\\]|\\(?:\r\n?|\n|(?![\r\n])))*|\/\*[\s\S]*?(?:\*\/|$)/,
+ greedy: true
+ },
'class-name': {
pattern: /(\b(?:enum|struct)\s+(?:__attribute__\s*\(\([\s\S]*?\)\)\s*)?)\w+/,
lookbehind: true
diff --git a/components/prism-c.min.js b/components/prism-c.min.js
index aafd9fc..9acdccc 100644
--- a/components/prism-c.min.js
+++ b/components/prism-c.min.js
@@ -1 +1 @@
-Prism.languages.c=Prism.languages.extend("clike",{"class-name":{pattern:/(\b(?:enum|struct)\s+(?:__attribute__\s*\(\([\s\S]*?\)\)\s*)?)\w+/,lookbehind:!0},keyword:/\b(?:__attribute__|_Alignas|_Alignof|_Atomic|_Bool|_Complex|_Generic|_Imaginary|_Noreturn|_Static_assert|_Thread_local|asm|typeof|inline|auto|break|case|char|const|continue|default|do|double|else|enum|extern|float|for|goto|if|int|long|register|return|short|signed|sizeof|static|struct|switch|typedef|union|unsigned|void|volatile|while)\b/,function:/[a-z_]\w*(?=\s*\()/i,operator:/>>=?|<<=?|->|([-+&|:])\1|[?:~]|[-+*/%&|^!=<>]=?/,number:/(?:\b0x(?:[\da-f]+\.?[\da-f]*|\.[\da-f]+)(?:p[+-]?\d+)?|(?:\b\d+\.?\d*|\B\.\d+)(?:e[+-]?\d+)?)[ful]*/i}),Prism.languages.insertBefore("c","string",{macro:{pattern:/(^\s*)#\s*[a-z]+(?:[^\r\n\\]|\\(?:\r\n|[\s\S]))*/im,lookbehind:!0,alias:"property",inside:{string:{pattern:/(#\s*include\s*)(?:<.+?>|("|')(?:\\?.)+?\2)/,lookbehind:!0},directive:{pattern:/(#\s*)\b(?:define|defined|elif|else|endif|error|ifdef|ifndef|if|import|include|line|pragma|undef|using)\b/,lookbehind:!0,alias:"keyword"}}},constant:/\b(?:__FILE__|__LINE__|__DATE__|__TIME__|__TIMESTAMP__|__func__|EOF|NULL|SEEK_CUR|SEEK_END|SEEK_SET|stdin|stdout|stderr)\b/}),delete Prism.languages.c.boolean;
\ No newline at end of file
+Prism.languages.c=Prism.languages.extend("clike",{comment:{pattern:/\/\/(?:[^\r\n\\]|\\(?:\r\n?|\n|(?![\r\n])))*|\/\*[\s\S]*?(?:\*\/|$)/,greedy:!0},"class-name":{pattern:/(\b(?:enum|struct)\s+(?:__attribute__\s*\(\([\s\S]*?\)\)\s*)?)\w+/,lookbehind:!0},keyword:/\b(?:__attribute__|_Alignas|_Alignof|_Atomic|_Bool|_Complex|_Generic|_Imaginary|_Noreturn|_Static_assert|_Thread_local|asm|typeof|inline|auto|break|case|char|const|continue|default|do|double|else|enum|extern|float|for|goto|if|int|long|register|return|short|signed|sizeof|static|struct|switch|typedef|union|unsigned|void|volatile|while)\b/,function:/[a-z_]\w*(?=\s*\()/i,operator:/>>=?|<<=?|->|([-+&|:])\1|[?:~]|[-+*/%&|^!=<>]=?/,number:/(?:\b0x(?:[\da-f]+\.?[\da-f]*|\.[\da-f]+)(?:p[+-]?\d+)?|(?:\b\d+\.?\d*|\B\.\d+)(?:e[+-]?\d+)?)[ful]*/i}),Prism.languages.insertBefore("c","string",{macro:{pattern:/(^\s*)#\s*[a-z]+(?:[^\r\n\\]|\\(?:\r\n|[\s\S]))*/im,lookbehind:!0,alias:"property",inside:{string:{pattern:/(#\s*include\s*)(?:<.+?>|("|')(?:\\?.)+?\2)/,lookbehind:!0},directive:{pattern:/(#\s*)\b(?:define|defined|elif|else|endif|error|ifdef|ifndef|if|import|include|line|pragma|undef|using)\b/,lookbehind:!0,alias:"keyword"}}},constant:/\b(?:__FILE__|__LINE__|__DATE__|__TIME__|__TIMESTAMP__|__func__|EOF|NULL|SEEK_CUR|SEEK_END|SEEK_SET|stdin|stdout|stderr)\b/}),delete Prism.languages.c.boolean;
\ No newline at end of file
diff --git a/tests/languages/c/comment_feature.test b/tests/languages/c/comment_feature.test
new file mode 100644
index 0000000..6370e5d
--- /dev/null
+++ b/tests/languages/c/comment_feature.test
@@ -0,0 +1,26 @@
+//
+// comment
+// the comment \
+ continues!
+
+/**/
+/*
+ * comment
+ */
+
+/* open-ended comment
+
+----------------------------------------------------
+
+[
+ ["comment", "//"],
+ ["comment", "// comment"],
+ ["comment", "// the comment \\\r\n continues!"],
+ ["comment", "/**/"],
+ ["comment", "/*\r\n * comment\r\n */"],
+ ["comment", "/* open-ended comment"]
+]
+
+----------------------------------------------------
+
+Checks for comments.
diff --git a/tests/languages/cpp/comment_feature.test b/tests/languages/cpp/comment_feature.test
new file mode 100644
index 0000000..6370e5d
--- /dev/null
+++ b/tests/languages/cpp/comment_feature.test
@@ -0,0 +1,26 @@
+//
+// comment
+// the comment \
+ continues!
+
+/**/
+/*
+ * comment
+ */
+
+/* open-ended comment
+
+----------------------------------------------------
+
+[
+ ["comment", "//"],
+ ["comment", "// comment"],
+ ["comment", "// the comment \\\r\n continues!"],
+ ["comment", "/**/"],
+ ["comment", "/*\r\n * comment\r\n */"],
+ ["comment", "/* open-ended comment"]
+]
+
+----------------------------------------------------
+
+Checks for comments.
diff --git a/tests/languages/objectivec/comment_feature.test b/tests/languages/objectivec/comment_feature.test
new file mode 100644
index 0000000..6370e5d
--- /dev/null
+++ b/tests/languages/objectivec/comment_feature.test
@@ -0,0 +1,26 @@
+//
+// comment
+// the comment \
+ continues!
+
+/**/
+/*
+ * comment
+ */
+
+/* open-ended comment
+
+----------------------------------------------------
+
+[
+ ["comment", "//"],
+ ["comment", "// comment"],
+ ["comment", "// the comment \\\r\n continues!"],
+ ["comment", "/**/"],
+ ["comment", "/*\r\n * comment\r\n */"],
+ ["comment", "/* open-ended comment"]
+]
+
+----------------------------------------------------
+
+Checks for comments.