Skip to content

Commit fdcf7ed

Browse files
C: Improved macros (#2320)
1 parent 4b61661 commit fdcf7ed

File tree

3 files changed

+117
-26
lines changed

3 files changed

+117
-26
lines changed

components/prism-c.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,23 @@ Prism.languages.insertBefore('c', 'string', {
1717
'macro': {
1818
// allow for multiline macro definitions
1919
// spaces after the # character compile fine with gcc
20-
pattern: /(^\s*)#\s*[a-z]+(?:[^\r\n\\]|\\(?:\r\n|[\s\S]))*/im,
20+
pattern: /(^\s*)#\s*[a-z]+(?:[^\r\n\\/]|\/(?!\*)|\/\*(?:[^*]|\*(?!\/))*\*\/|\\(?:\r\n|[\s\S]))*/im,
2121
lookbehind: true,
22+
greedy: true,
2223
alias: 'property',
2324
inside: {
24-
// highlight the path of the include statement as a string
25-
'string': {
26-
pattern: /(#\s*include\s*)(?:<.+?>|(["'])(?:\\(?:\r\n|[\s\S])|(?!\2)[^\\\r\n])*\2)/,
27-
lookbehind: true
28-
},
25+
'string': [
26+
{
27+
// highlight the path of the include statement as a string
28+
pattern: /^(#\s*include\s*)<[^>]+>/,
29+
lookbehind: true
30+
},
31+
Prism.languages.c['string']
32+
],
33+
'comment': Prism.languages.c['comment'],
2934
// highlight macro directives as keywords
3035
'directive': {
31-
pattern: /(#\s*)\b(?:define|defined|elif|else|endif|error|ifdef|ifndef|if|import|include|line|pragma|undef|using)\b/,
36+
pattern: /^(#\s*)[a-z]+/,
3237
lookbehind: true,
3338
alias: 'keyword'
3439
}

components/prism-c.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/languages/c/macro_feature.test

Lines changed: 104 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# include <stdio.h>
2-
#define PG_locked 0
2+
# include "stdio.h"
3+
#define PG_locked 0
34

45
#defined
56
#elif
@@ -16,30 +17,115 @@
1617
#undef
1718
#using
1819

20+
#somethingunknown
21+
22+
#define FOO /*
23+
comment
24+
*/ 1
25+
26+
#define FOO 1 // trailing comment
27+
28+
#define MAX(a, b) \
29+
((a) < (b) ? (b) : (a))
30+
1931
----------------------------------------------------
2032

2133
[
2234
["macro", [
23-
"# ", ["directive", "include"],
35+
"# ",
36+
["directive", "include"],
2437
["string", "<stdio.h>"]
2538
]],
26-
["macro", ["#", ["directive", "define"], " PG_locked 0"]],
27-
["macro", ["#", ["directive", "defined"]]],
28-
["macro", ["#", ["directive", "elif"]]],
29-
["macro", ["#", ["directive", "else"]]],
30-
["macro", ["#", ["directive", "endif"]]],
31-
["macro", ["#", ["directive", "error"]]],
32-
["macro", ["#", ["directive", "ifdef"]]],
33-
["macro", ["#", ["directive", "ifndef"]]],
34-
["macro", ["#", ["directive", "if"]]],
35-
["macro", ["#", ["directive", "import"]]],
36-
["macro", ["#", ["directive", "include"]]],
37-
["macro", ["#", ["directive", "line"]]],
38-
["macro", ["#", ["directive", "pragma"]]],
39-
["macro", ["#", ["directive", "undef"]]],
40-
["macro", ["#", ["directive", "using"]]]
39+
["macro", [
40+
"# ",
41+
["directive", "include"],
42+
["string", "\"stdio.h\""]
43+
]],
44+
["macro", [
45+
"#",
46+
["directive", "define"],
47+
" PG_locked 0"
48+
]],
49+
["macro", [
50+
"#",
51+
["directive", "defined"]
52+
]],
53+
["macro", [
54+
"#",
55+
["directive", "elif"]
56+
]],
57+
["macro", [
58+
"#",
59+
["directive", "else"]
60+
]],
61+
["macro", [
62+
"#",
63+
["directive", "endif"]
64+
]],
65+
["macro", [
66+
"#",
67+
["directive", "error"]
68+
]],
69+
["macro", [
70+
"#",
71+
["directive", "ifdef"]
72+
]],
73+
["macro", [
74+
"#",
75+
["directive", "ifndef"]
76+
]],
77+
["macro", [
78+
"#",
79+
["directive", "if"]
80+
]],
81+
["macro", [
82+
"#",
83+
["directive", "import"]
84+
]],
85+
["macro", [
86+
"#",
87+
["directive", "include"]
88+
]],
89+
["macro", [
90+
"#",
91+
["directive", "line"]
92+
]],
93+
["macro", [
94+
"#",
95+
["directive", "pragma"]
96+
]],
97+
["macro", [
98+
"#",
99+
["directive", "undef"]
100+
]],
101+
["macro", [
102+
"#",
103+
["directive", "using"]
104+
]],
105+
["macro", [
106+
"#",
107+
["directive", "somethingunknown"]
108+
]],
109+
["macro", [
110+
"#",
111+
["directive", "define"],
112+
" FOO ",
113+
["comment", "/*\r\n comment\r\n*/"],
114+
" 1"
115+
]],
116+
["macro", [
117+
"#",
118+
["directive", "define"],
119+
" FOO 1 ",
120+
["comment", "// trailing comment"]
121+
]],
122+
["macro", [
123+
"#",
124+
["directive", "define"],
125+
" MAX(a, b) \\\r\n\t((a) < (b) ? (b) : (a))"
126+
]]
41127
]
42128

43129
----------------------------------------------------
44130

45-
Checks for macros and paths inside include statements.
131+
Checks for macros and paths inside include statements.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy