Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions components/prism-c.js
Expand Up @@ -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
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion components/prism-c.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 58 additions & 14 deletions tests/languages/bison/c_feature.test
Expand Up @@ -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", "%%"]
Expand All @@ -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.
112 changes: 86 additions & 26 deletions tests/languages/c/macro_feature.test
Expand Up @@ -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"],
["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"
]]
]]
]

Expand Down
26 changes: 13 additions & 13 deletions tests/languages/glsl/macro_feature.test
Expand Up @@ -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"]
]]
]
Expand Down