C/C++: Improved macros and expressions (#2440)

This commit is contained in:
Michael Schmidt 2020-06-29 13:09:56 +02:00 committed by GitHub
parent b96ed22539
commit 8a72fa6fc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 164 additions and 54 deletions

View File

@ -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
}
}
},

View File

@ -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;
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;

View File

@ -21,30 +21,74 @@ exp:
["bison", [
["c", [
["delimiter", "%{"],
["macro", ["#", ["directive", "include"], ["string", "<stdio.h>"]]],
["macro", [
["directive-hash", "#"],
["directive", "include"],
["string", "<stdio.h>"]
]],
["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.
Checks for C inside Bison, along with special Bison variables.

View File

@ -28,101 +28,161 @@
#define MAX(a, b) \
((a) < (b) ? (b) : (a))
#define BAR(s) no_##s
----------------------------------------------------
[
["macro", [
"# ",
["directive-hash", "#"],
["directive", "include"],
["string", "<stdio.h>"]
]],
["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"],
" MAX(a, b) \\\r\n\t((a) < (b) ? (b) : (a))"
["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"],
["expression", [
["function", "BAR"],
["punctuation", "("],
"s",
["punctuation", ")"],
" no_"
]],
["punctuation", "##"],
["expression", [
"s"
]]
]]
]

View File

@ -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"]
]]
]