From 8a72fa6fc7a96d74332d463dfb303567f5262482 Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Mon, 29 Jun 2020 13:09:56 +0200 Subject: [PATCH] C/C++: Improved macros and expressions (#2440) --- components/prism-c.js | 6 ++ components/prism-c.min.js | 2 +- tests/languages/bison/c_feature.test | 72 ++++++++++++--- tests/languages/c/macro_feature.test | 112 ++++++++++++++++++------ tests/languages/glsl/macro_feature.test | 26 +++--- 5 files changed, 164 insertions(+), 54 deletions(-) diff --git a/components/prism-c.js b/components/prism-c.js index c755712680..53245c6d27 100644 --- a/components/prism-c.js +++ b/components/prism-c.js @@ -36,6 +36,12 @@ Prism.languages.insertBefore('c', 'string', { pattern: /^(#\s*)[a-z]+/, lookbehind: true, alias: 'keyword' + }, + 'directive-hash': /^#/, + 'punctuation': /##|\\(?=[\r\n])/, + 'expression': { + pattern: /\S[\s\S]*/, + inside: Prism.languages.c } } }, diff --git a/components/prism-c.min.js b/components/prism-c.min.js index ccc6c806b0..6a4fff5e78 100644 --- a/components/prism-c.min.js +++ b/components/prism-c.min.js @@ -1 +1 @@ -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,greedy:!0,alias:"property",inside:{string:[{pattern:/^(#\s*include\s*)<[^>]+>/,lookbehind:!0},Prism.languages.c.string],comment:Prism.languages.c.comment,directive:{pattern:/^(#\s*)[a-z]+/,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,greedy:!0,alias:"property",inside:{string:[{pattern:/^(#\s*include\s*)<[^>]+>/,lookbehind:!0},Prism.languages.c.string],comment:Prism.languages.c.comment,directive:{pattern:/^(#\s*)[a-z]+/,lookbehind:!0,alias:"keyword"},"directive-hash":/^#/,punctuation:/##|\\(?=[\r\n])/,expression:{pattern:/\S[\s\S]*/,inside:Prism.languages.c}}},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/bison/c_feature.test b/tests/languages/bison/c_feature.test index 4d8be839d5..6732d96992 100644 --- a/tests/languages/bison/c_feature.test +++ b/tests/languages/bison/c_feature.test @@ -21,30 +21,74 @@ exp: ["bison", [ ["c", [ ["delimiter", "%{"], - ["macro", ["#", ["directive", "include"], ["string", ""]]], + ["macro", [ + ["directive-hash", "#"], + ["directive", "include"], + ["string", ""] + ]], ["delimiter", "%}"] ]], ["keyword", "%code"], ["c", [ ["delimiter", "{"], - ["keyword", "if"], ["punctuation", "("], "foo", ["punctuation", ")"], - ["punctuation", "{"], ["punctuation", "}"], + ["keyword", "if"], + ["punctuation", "("], + "foo", + ["punctuation", ")"], + ["punctuation", "{"], + + ["punctuation", "}"], ["delimiter", "}"] ]], ["punctuation", "%%"], - ["property", "exp"], ["punctuation", ":"], + ["property", "exp"], + ["punctuation", ":"], "\r\n\tNUM ", ["c", [ ["delimiter", "{"], - ["bison-variable", ["$$"]], ["operator", "="], - ["function", "f"], ["punctuation", "("], - ["bison-variable", ["$3"]], ["punctuation", ","], - ["bison-variable", ["$4"]], ["punctuation", ")"], ["punctuation", ";"], - ["bison-variable", ["@$"]], ["punctuation", "."], "first_column ", ["operator", "="], - ["bison-variable", ["@1"]], ["punctuation", "."], "first_column", ["punctuation", ";"], - ["bison-variable", ["$result"]], ["operator", "="], - ["bison-variable", ["$left"]], ["operator", "+"], - ["bison-variable", ["$", ["punctuation", "<"], "itype", ["punctuation", ">"], "1"]], ["punctuation", ";"], + ["bison-variable", [ + "$$" + ]], + ["operator", "="], + ["function", "f"], + ["punctuation", "("], + ["bison-variable", [ + "$3" + ]], + ["punctuation", ","], + ["bison-variable", [ + "$4" + ]], + ["punctuation", ")"], + ["punctuation", ";"], + ["bison-variable", [ + "@$" + ]], + ["punctuation", "."], + "first_column ", + ["operator", "="], + ["bison-variable", [ + "@1" + ]], + ["punctuation", "."], + "first_column", + ["punctuation", ";"], + ["bison-variable", [ + "$result" + ]], + ["operator", "="], + ["bison-variable", [ + "$left" + ]], + ["operator", "+"], + ["bison-variable", [ + "$", + ["punctuation", "<"], + "itype", + ["punctuation", ">"], + "1" + ]], + ["punctuation", ";"], ["delimiter", "}"] ]], ["punctuation", "%%"] @@ -53,4 +97,4 @@ exp: ---------------------------------------------------- -Checks for C inside Bison, along with special Bison variables. \ No newline at end of file +Checks for C inside Bison, along with special Bison variables. diff --git a/tests/languages/c/macro_feature.test b/tests/languages/c/macro_feature.test index 8fb83b359c..8ae3d473ac 100644 --- a/tests/languages/c/macro_feature.test +++ b/tests/languages/c/macro_feature.test @@ -28,101 +28,161 @@ #define MAX(a, b) \ ((a) < (b) ? (b) : (a)) +#define BAR(s) no_##s + ---------------------------------------------------- [ ["macro", [ - "# ", + ["directive-hash", "#"], ["directive", "include"], ["string", ""] ]], ["macro", [ - "# ", + ["directive-hash", "#"], ["directive", "include"], ["string", "\"stdio.h\""] ]], ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "define"], - " PG_locked 0" + ["expression", [ + "PG_locked ", + ["number", "0"] + ]] ]], + ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "defined"] ]], ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "elif"] ]], ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "else"] ]], ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "endif"] ]], ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "error"] ]], ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "ifdef"] ]], ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "ifndef"] ]], ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "if"] ]], ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "import"] ]], ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "include"] ]], ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "line"] ]], ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "pragma"] ]], ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "undef"] ]], ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "using"] ]], + ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "somethingunknown"] ]], + ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "define"], - " FOO ", + ["expression", [ + "FOO " + ]], ["comment", "/*\r\n comment\r\n*/"], - " 1" + ["expression", [ + ["number", "1"] + ]] ]], + ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "define"], - " FOO 1 ", + ["expression", [ + "FOO ", + ["number", "1"] + ]], ["comment", "// trailing comment"] ]], + + ["macro", [ + ["directive-hash", "#"], + ["directive", "define"], + ["expression", [ + ["function", "MAX"], + ["punctuation", "("], + "a", + ["punctuation", ","], + " b", + ["punctuation", ")"] + ]], + ["punctuation", "\\"], + ["expression", [ + ["punctuation", "("], + ["punctuation", "("], + "a", + ["punctuation", ")"], + ["operator", "<"], + ["punctuation", "("], + "b", + ["punctuation", ")"], + ["operator", "?"], + ["punctuation", "("], + "b", + ["punctuation", ")"], + ["operator", ":"], + ["punctuation", "("], + "a", + ["punctuation", ")"], + ["punctuation", ")"] + ]] + ]], + ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "define"], - " MAX(a, b) \\\r\n\t((a) < (b) ? (b) : (a))" + ["expression", [ + ["function", "BAR"], + ["punctuation", "("], + "s", + ["punctuation", ")"], + " no_" + ]], + ["punctuation", "##"], + ["expression", [ + "s" + ]] ]] ] diff --git a/tests/languages/glsl/macro_feature.test b/tests/languages/glsl/macro_feature.test index e893c306cd..2ce7114d73 100644 --- a/tests/languages/glsl/macro_feature.test +++ b/tests/languages/glsl/macro_feature.test @@ -16,55 +16,55 @@ [ ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "define"] ]], ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "undef"] ]], ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "if"] ]], ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "ifdef"] ]], ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "ifndef"] ]], ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "else"] ]], ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "elif"] ]], ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "endif"] ]], ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "error"] ]], ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "pragma"] ]], ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "extension"] ]], ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "version"] ]], ["macro", [ - "#", + ["directive-hash", "#"], ["directive", "line"] ]] ]