Skip to content

Commit

Permalink
C: Better class name and macro name detection (#2585)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Nov 4, 2020
1 parent 8e1f38f commit 129faf5
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 86 deletions.
13 changes: 12 additions & 1 deletion components/prism-c.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Prism.languages.c = Prism.languages.extend('clike', {
greedy: true
},
'class-name': {
pattern: /(\b(?:enum|struct)\s+(?:__attribute__\s*\(\([\s\S]*?\)\)\s*)?)\w+/,
pattern: /(\b(?:enum|struct)\s+(?:__attribute__\s*\(\([\s\S]*?\)\)\s*)?)\w+|\b[a-z]\w*_t\b/,
lookbehind: true
},
'keyword': /\b(?:__attribute__|_Alignas|_Alignof|_Atomic|_Bool|_Complex|_Generic|_Imaginary|_Noreturn|_Static_assert|_Thread_local|asm|typeof|inline|auto|break|case|char|const|continue|default|do|double|else|enum|extern|float|for|goto|if|int|long|register|return|short|signed|sizeof|static|struct|switch|typedef|union|unsigned|void|volatile|while)\b/,
Expand All @@ -31,6 +31,17 @@ Prism.languages.insertBefore('c', 'string', {
Prism.languages.c['string']
],
'comment': Prism.languages.c['comment'],
'macro-name': [
{
pattern: /(^#\s*define\s+)\w+\b(?!\()/i,
lookbehind: true
},
{
pattern: /(^#\s*define\s+)\w+\b(?=\()/i,
lookbehind: true,
alias: 'function'
}
],
// highlight macro directives as keywords
'directive': {
pattern: /^(#\s*)[a-z]+/,
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.

13 changes: 10 additions & 3 deletions components/prism-opencl.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/prism-opencl.min.js

Large diffs are not rendered by default.

22 changes: 19 additions & 3 deletions tests/languages/c/class-name_feature.test
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
struct foo
enum bar
struct foo;
enum bar;

struct foo var;
struct __attribute__ ((aligned (8))) S { short f[3]; };

// by name
uint32_t foo;
static dtrace_helptrace_t *bar;

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

[
["keyword", "struct"],
["class-name", "foo"],
["punctuation", ";"],
["keyword", "enum"],
["class-name", "bar"],
["punctuation", ";"],

["keyword", "struct"],
["class-name", "foo"],
Expand All @@ -35,9 +41,19 @@ struct __attribute__ ((aligned (8))) S { short f[3]; };
["punctuation", "]"],
["punctuation", ";"],
["punctuation", "}"],
["punctuation", ";"],

["comment", "// by name"],
["class-name", "uint32_t"],
" foo",
["punctuation", ";"],
["keyword", "static"],
["class-name", "dtrace_helptrace_t"],
["operator", "*"],
"bar",
["punctuation", ";"]
]

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

Checks for structs and enums.
Checks for structs and enums.
27 changes: 19 additions & 8 deletions tests/languages/c/macro_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/ 1

#define FOO 1 // trailing comment
#define FOO (1 + 1)

#define MAX(a, b) \
((a) < (b) ? (b) : (a))
Expand All @@ -46,8 +47,8 @@
["macro", [
["directive-hash", "#"],
["directive", "define"],
["macro-name", "PG_locked"],
["expression", [
"PG_locked ",
["number", "0"]
]]
]],
Expand Down Expand Up @@ -117,9 +118,7 @@
["macro", [
["directive-hash", "#"],
["directive", "define"],
["expression", [
"FOO "
]],
["macro-name", "FOO"],
["comment", "/*\r\n comment\r\n*/"],
["expression", [
["number", "1"]
Expand All @@ -129,18 +128,30 @@
["macro", [
["directive-hash", "#"],
["directive", "define"],
["macro-name", "FOO"],
["expression", [
"FOO ",
["number", "1"]
]],
["comment", "// trailing comment"]
]],
["macro", [
["directive-hash", "#"],
["directive", "define"],
["macro-name", "FOO"],
["expression", [
["punctuation", "("],
["number", "1"],
["operator", "+"],
["number", "1"],
["punctuation", ")"]
]]
]],

["macro", [
["directive-hash", "#"],
["directive", "define"],
["macro-name", "MAX"],
["expression", [
["function", "MAX"],
["punctuation", "("],
"a",
["punctuation", ","],
Expand Down Expand Up @@ -172,8 +183,8 @@
["macro", [
["directive-hash", "#"],
["directive", "define"],
["macro-name", "BAR"],
["expression", [
["function", "BAR"],
["punctuation", "("],
"s",
["punctuation", ")"],
Expand All @@ -188,4 +199,4 @@

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

Checks for macros and paths inside include statements.
Checks for macros and paths inside include statements.
71 changes: 71 additions & 0 deletions tests/languages/opencl/builtin-type_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
_cl_command_queue
_cl_context
_cl_device_id
_cl_event
_cl_kernel
_cl_mem
_cl_platform_id
_cl_program
_cl_sampler
cl_image_format
cl_mem_fence_flags
clk_event_t
event_t
image1d_array_t
image1d_buffer_t
image1d_t
image2d_array_depth_t
image2d_array_msaa_depth_t
image2d_array_msaa_t
image2d_array_t
image2d_depth_t
image2d_msaa_depth_t
image2d_msaa_t
image2d_t
image3d_t
intptr_t
ndrange_t
ptrdiff_t
queue_t
reserve_id_t
sampler_t
size_t
uintptr_t

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

[
["builtin-type", "_cl_command_queue"],
["builtin-type", "_cl_context"],
["builtin-type", "_cl_device_id"],
["builtin-type", "_cl_event"],
["builtin-type", "_cl_kernel"],
["builtin-type", "_cl_mem"],
["builtin-type", "_cl_platform_id"],
["builtin-type", "_cl_program"],
["builtin-type", "_cl_sampler"],
["builtin-type", "cl_image_format"],
["builtin-type", "cl_mem_fence_flags"],
["builtin-type", "clk_event_t"],
["builtin-type", "event_t"],
["builtin-type", "image1d_array_t"],
["builtin-type", "image1d_buffer_t"],
["builtin-type", "image1d_t"],
["builtin-type", "image2d_array_depth_t"],
["builtin-type", "image2d_array_msaa_depth_t"],
["builtin-type", "image2d_array_msaa_t"],
["builtin-type", "image2d_array_t"],
["builtin-type", "image2d_depth_t"],
["builtin-type", "image2d_msaa_depth_t"],
["builtin-type", "image2d_msaa_t"],
["builtin-type", "image2d_t"],
["builtin-type", "image3d_t"],
["builtin-type", "intptr_t"],
["builtin-type", "ndrange_t"],
["builtin-type", "ptrdiff_t"],
["builtin-type", "queue_t"],
["builtin-type", "reserve_id_t"],
["builtin-type", "sampler_t"],
["builtin-type", "size_t"],
["builtin-type", "uintptr_t"]
]
Loading

0 comments on commit 129faf5

Please sign in to comment.