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: Improved macros #2320

Merged
merged 3 commits into from Apr 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
19 changes: 12 additions & 7 deletions components/prism-c.js
Expand Up @@ -17,18 +17,23 @@ Prism.languages.insertBefore('c', 'string', {
'macro': {
// allow for multiline macro definitions
// spaces after the # character compile fine with gcc
pattern: /(^\s*)#\s*[a-z]+(?:[^\r\n\\]|\\(?:\r\n|[\s\S]))*/im,
pattern: /(^\s*)#\s*[a-z]+(?:[^\r\n\\/]|\/(?!\*)|\/\*(?:[^*]|\*(?!\/))*\*\/|\\(?:\r\n|[\s\S]))*/im,
lookbehind: true,
greedy: true,
alias: 'property',
inside: {
// highlight the path of the include statement as a string
'string': {
pattern: /(#\s*include\s*)(?:<.+?>|(["'])(?:\\(?:\r\n|[\s\S])|(?!\2)[^\\\r\n])*\2)/,
lookbehind: true
},
'string': [
{
// highlight the path of the include statement as a string
pattern: /^(#\s*include\s*)<[^>]+>/,
lookbehind: true
},
Prism.languages.c['string']
],
'comment': Prism.languages.c['comment'],
// highlight macro directives as keywords
'directive': {
pattern: /(#\s*)\b(?:define|defined|elif|else|endif|error|ifdef|ifndef|if|import|include|line|pragma|undef|using)\b/,
pattern: /^(#\s*)[a-z]+/,
lookbehind: true,
alias: 'keyword'
}
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.

122 changes: 104 additions & 18 deletions tests/languages/c/macro_feature.test
@@ -1,5 +1,6 @@
# include <stdio.h>
#define PG_locked 0
# include "stdio.h"
#define PG_locked 0

#defined
#elif
Expand All @@ -16,30 +17,115 @@
#undef
#using

#somethingunknown

#define FOO /*
comment
*/ 1

#define FOO 1 // trailing comment

#define MAX(a, b) \
((a) < (b) ? (b) : (a))

----------------------------------------------------

[
["macro", [
"# ", ["directive", "include"],
"# ",
["directive", "include"],
["string", "<stdio.h>"]
]],
["macro", ["#", ["directive", "define"], " PG_locked 0"]],
["macro", ["#", ["directive", "defined"]]],
["macro", ["#", ["directive", "elif"]]],
["macro", ["#", ["directive", "else"]]],
["macro", ["#", ["directive", "endif"]]],
["macro", ["#", ["directive", "error"]]],
["macro", ["#", ["directive", "ifdef"]]],
["macro", ["#", ["directive", "ifndef"]]],
["macro", ["#", ["directive", "if"]]],
["macro", ["#", ["directive", "import"]]],
["macro", ["#", ["directive", "include"]]],
["macro", ["#", ["directive", "line"]]],
["macro", ["#", ["directive", "pragma"]]],
["macro", ["#", ["directive", "undef"]]],
["macro", ["#", ["directive", "using"]]]
["macro", [
"# ",
["directive", "include"],
["string", "\"stdio.h\""]
]],
["macro", [
"#",
["directive", "define"],
" PG_locked 0"
]],
["macro", [
"#",
["directive", "defined"]
]],
["macro", [
"#",
["directive", "elif"]
]],
["macro", [
"#",
["directive", "else"]
]],
["macro", [
"#",
["directive", "endif"]
]],
["macro", [
"#",
["directive", "error"]
]],
["macro", [
"#",
["directive", "ifdef"]
]],
["macro", [
"#",
["directive", "ifndef"]
]],
["macro", [
"#",
["directive", "if"]
]],
["macro", [
"#",
["directive", "import"]
]],
["macro", [
"#",
["directive", "include"]
]],
["macro", [
"#",
["directive", "line"]
]],
["macro", [
"#",
["directive", "pragma"]
]],
["macro", [
"#",
["directive", "undef"]
]],
["macro", [
"#",
["directive", "using"]
]],
["macro", [
"#",
["directive", "somethingunknown"]
]],
["macro", [
"#",
["directive", "define"],
" FOO ",
["comment", "/*\r\n comment\r\n*/"],
" 1"
]],
["macro", [
"#",
["directive", "define"],
" FOO 1 ",
["comment", "// trailing comment"]
]],
["macro", [
"#",
["directive", "define"],
" MAX(a, b) \\\r\n\t((a) < (b) ? (b) : (a))"
]]
]

----------------------------------------------------

Checks for macros and paths inside include statements.
Checks for macros and paths inside include statements.