Commit fa630726876ce7fad7baba5384491c11382b07cc

Michael Schmidt 2020-02-24T23:57:55

C/C++: Improvemed `comment` pattern (#2229) This improves the `comment` pattern by allowing backslash-escaped line breaks in single-line comments.

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.