Skip to content

Commit

Permalink
C/C++: Improved macros and expressions (#2440)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Jun 29, 2020
1 parent b96ed22 commit 8a72fa6
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 54 deletions.
6 changes: 6 additions & 0 deletions components/prism-c.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 8a72fa6

Please sign in to comment.