Skip to content

Commit 8a72fa6

Browse files
C/C++: Improved macros and expressions (#2440)
1 parent b96ed22 commit 8a72fa6

File tree

5 files changed

+164
-54
lines changed

5 files changed

+164
-54
lines changed

components/prism-c.js

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ Prism.languages.insertBefore('c', 'string', {
3636
pattern: /^(#\s*)[a-z]+/,
3737
lookbehind: true,
3838
alias: 'keyword'
39+
},
40+
'directive-hash': /^#/,
41+
'punctuation': /##|\\(?=[\r\n])/,
42+
'expression': {
43+
pattern: /\S[\s\S]*/,
44+
inside: Prism.languages.c
3945
}
4046
}
4147
},

components/prism-c.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/languages/bison/c_feature.test

+58-14
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,74 @@ exp:
2121
["bison", [
2222
["c", [
2323
["delimiter", "%{"],
24-
["macro", ["#", ["directive", "include"], ["string", "<stdio.h>"]]],
24+
["macro", [
25+
["directive-hash", "#"],
26+
["directive", "include"],
27+
["string", "<stdio.h>"]
28+
]],
2529
["delimiter", "%}"]
2630
]],
2731
["keyword", "%code"],
2832
["c", [
2933
["delimiter", "{"],
30-
["keyword", "if"], ["punctuation", "("], "foo", ["punctuation", ")"],
31-
["punctuation", "{"], ["punctuation", "}"],
34+
["keyword", "if"],
35+
["punctuation", "("],
36+
"foo",
37+
["punctuation", ")"],
38+
["punctuation", "{"],
39+
40+
["punctuation", "}"],
3241
["delimiter", "}"]
3342
]],
3443
["punctuation", "%%"],
35-
["property", "exp"], ["punctuation", ":"],
44+
["property", "exp"],
45+
["punctuation", ":"],
3646
"\r\n\tNUM ",
3747
["c", [
3848
["delimiter", "{"],
39-
["bison-variable", ["$$"]], ["operator", "="],
40-
["function", "f"], ["punctuation", "("],
41-
["bison-variable", ["$3"]], ["punctuation", ","],
42-
["bison-variable", ["$4"]], ["punctuation", ")"], ["punctuation", ";"],
43-
["bison-variable", ["@$"]], ["punctuation", "."], "first_column ", ["operator", "="],
44-
["bison-variable", ["@1"]], ["punctuation", "."], "first_column", ["punctuation", ";"],
45-
["bison-variable", ["$result"]], ["operator", "="],
46-
["bison-variable", ["$left"]], ["operator", "+"],
47-
["bison-variable", ["$", ["punctuation", "<"], "itype", ["punctuation", ">"], "1"]], ["punctuation", ";"],
49+
["bison-variable", [
50+
"$$"
51+
]],
52+
["operator", "="],
53+
["function", "f"],
54+
["punctuation", "("],
55+
["bison-variable", [
56+
"$3"
57+
]],
58+
["punctuation", ","],
59+
["bison-variable", [
60+
"$4"
61+
]],
62+
["punctuation", ")"],
63+
["punctuation", ";"],
64+
["bison-variable", [
65+
"@$"
66+
]],
67+
["punctuation", "."],
68+
"first_column ",
69+
["operator", "="],
70+
["bison-variable", [
71+
"@1"
72+
]],
73+
["punctuation", "."],
74+
"first_column",
75+
["punctuation", ";"],
76+
["bison-variable", [
77+
"$result"
78+
]],
79+
["operator", "="],
80+
["bison-variable", [
81+
"$left"
82+
]],
83+
["operator", "+"],
84+
["bison-variable", [
85+
"$",
86+
["punctuation", "<"],
87+
"itype",
88+
["punctuation", ">"],
89+
"1"
90+
]],
91+
["punctuation", ";"],
4892
["delimiter", "}"]
4993
]],
5094
["punctuation", "%%"]
@@ -53,4 +97,4 @@ exp:
5397

5498
----------------------------------------------------
5599

56-
Checks for C inside Bison, along with special Bison variables.
100+
Checks for C inside Bison, along with special Bison variables.

tests/languages/c/macro_feature.test

+86-26
Original file line numberDiff line numberDiff line change
@@ -28,101 +28,161 @@
2828
#define MAX(a, b) \
2929
((a) < (b) ? (b) : (a))
3030

31+
#define BAR(s) no_##s
32+
3133
----------------------------------------------------
3234

3335
[
3436
["macro", [
35-
"# ",
37+
["directive-hash", "#"],
3638
["directive", "include"],
3739
["string", "<stdio.h>"]
3840
]],
3941
["macro", [
40-
"# ",
42+
["directive-hash", "#"],
4143
["directive", "include"],
4244
["string", "\"stdio.h\""]
4345
]],
4446
["macro", [
45-
"#",
47+
["directive-hash", "#"],
4648
["directive", "define"],
47-
" PG_locked 0"
49+
["expression", [
50+
"PG_locked ",
51+
["number", "0"]
52+
]]
4853
]],
54+
4955
["macro", [
50-
"#",
56+
["directive-hash", "#"],
5157
["directive", "defined"]
5258
]],
5359
["macro", [
54-
"#",
60+
["directive-hash", "#"],
5561
["directive", "elif"]
5662
]],
5763
["macro", [
58-
"#",
64+
["directive-hash", "#"],
5965
["directive", "else"]
6066
]],
6167
["macro", [
62-
"#",
68+
["directive-hash", "#"],
6369
["directive", "endif"]
6470
]],
6571
["macro", [
66-
"#",
72+
["directive-hash", "#"],
6773
["directive", "error"]
6874
]],
6975
["macro", [
70-
"#",
76+
["directive-hash", "#"],
7177
["directive", "ifdef"]
7278
]],
7379
["macro", [
74-
"#",
80+
["directive-hash", "#"],
7581
["directive", "ifndef"]
7682
]],
7783
["macro", [
78-
"#",
84+
["directive-hash", "#"],
7985
["directive", "if"]
8086
]],
8187
["macro", [
82-
"#",
88+
["directive-hash", "#"],
8389
["directive", "import"]
8490
]],
8591
["macro", [
86-
"#",
92+
["directive-hash", "#"],
8793
["directive", "include"]
8894
]],
8995
["macro", [
90-
"#",
96+
["directive-hash", "#"],
9197
["directive", "line"]
9298
]],
9399
["macro", [
94-
"#",
100+
["directive-hash", "#"],
95101
["directive", "pragma"]
96102
]],
97103
["macro", [
98-
"#",
104+
["directive-hash", "#"],
99105
["directive", "undef"]
100106
]],
101107
["macro", [
102-
"#",
108+
["directive-hash", "#"],
103109
["directive", "using"]
104110
]],
111+
105112
["macro", [
106-
"#",
113+
["directive-hash", "#"],
107114
["directive", "somethingunknown"]
108115
]],
116+
109117
["macro", [
110-
"#",
118+
["directive-hash", "#"],
111119
["directive", "define"],
112-
" FOO ",
120+
["expression", [
121+
"FOO "
122+
]],
113123
["comment", "/*\r\n comment\r\n*/"],
114-
" 1"
124+
["expression", [
125+
["number", "1"]
126+
]]
115127
]],
128+
116129
["macro", [
117-
"#",
130+
["directive-hash", "#"],
118131
["directive", "define"],
119-
" FOO 1 ",
132+
["expression", [
133+
"FOO ",
134+
["number", "1"]
135+
]],
120136
["comment", "// trailing comment"]
121137
]],
138+
139+
["macro", [
140+
["directive-hash", "#"],
141+
["directive", "define"],
142+
["expression", [
143+
["function", "MAX"],
144+
["punctuation", "("],
145+
"a",
146+
["punctuation", ","],
147+
" b",
148+
["punctuation", ")"]
149+
]],
150+
["punctuation", "\\"],
151+
["expression", [
152+
["punctuation", "("],
153+
["punctuation", "("],
154+
"a",
155+
["punctuation", ")"],
156+
["operator", "<"],
157+
["punctuation", "("],
158+
"b",
159+
["punctuation", ")"],
160+
["operator", "?"],
161+
["punctuation", "("],
162+
"b",
163+
["punctuation", ")"],
164+
["operator", ":"],
165+
["punctuation", "("],
166+
"a",
167+
["punctuation", ")"],
168+
["punctuation", ")"]
169+
]]
170+
]],
171+
122172
["macro", [
123-
"#",
173+
["directive-hash", "#"],
124174
["directive", "define"],
125-
" MAX(a, b) \\\r\n\t((a) < (b) ? (b) : (a))"
175+
["expression", [
176+
["function", "BAR"],
177+
["punctuation", "("],
178+
"s",
179+
["punctuation", ")"],
180+
" no_"
181+
]],
182+
["punctuation", "##"],
183+
["expression", [
184+
"s"
185+
]]
126186
]]
127187
]
128188

tests/languages/glsl/macro_feature.test

+13-13
Original file line numberDiff line numberDiff line change
@@ -16,55 +16,55 @@
1616

1717
[
1818
["macro", [
19-
"#",
19+
["directive-hash", "#"],
2020
["directive", "define"]
2121
]],
2222
["macro", [
23-
"#",
23+
["directive-hash", "#"],
2424
["directive", "undef"]
2525
]],
2626
["macro", [
27-
"#",
27+
["directive-hash", "#"],
2828
["directive", "if"]
2929
]],
3030
["macro", [
31-
"#",
31+
["directive-hash", "#"],
3232
["directive", "ifdef"]
3333
]],
3434
["macro", [
35-
"#",
35+
["directive-hash", "#"],
3636
["directive", "ifndef"]
3737
]],
3838
["macro", [
39-
"#",
39+
["directive-hash", "#"],
4040
["directive", "else"]
4141
]],
4242
["macro", [
43-
"#",
43+
["directive-hash", "#"],
4444
["directive", "elif"]
4545
]],
4646
["macro", [
47-
"#",
47+
["directive-hash", "#"],
4848
["directive", "endif"]
4949
]],
5050
["macro", [
51-
"#",
51+
["directive-hash", "#"],
5252
["directive", "error"]
5353
]],
5454
["macro", [
55-
"#",
55+
["directive-hash", "#"],
5656
["directive", "pragma"]
5757
]],
5858
["macro", [
59-
"#",
59+
["directive-hash", "#"],
6060
["directive", "extension"]
6161
]],
6262
["macro", [
63-
"#",
63+
["directive-hash", "#"],
6464
["directive", "version"]
6565
]],
6666
["macro", [
67-
"#",
67+
["directive-hash", "#"],
6868
["directive", "line"]
6969
]]
7070
]

0 commit comments

Comments
 (0)