Skip to content

Commit

Permalink
C: Improved macros (PrismJS#2320)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored and quentinvernot committed Sep 11, 2020
1 parent a7e1af1 commit 4a070ab
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 26 deletions.
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.

0 comments on commit 4a070ab

Please sign in to comment.