Skip to content

Commit

Permalink
Improve C language (#1697)
Browse files Browse the repository at this point in the history
This PR adds support for [hexadecimal floating-point literals](http://www.cplusplus.com/doc/tutorial/structures/), and [`enum`](https://www.geeksforgeeks.org/enumeration-enum-c/) and [`struct`](https://www.tutorialspoint.com/cprogramming/c_structures.htm) class names.
  • Loading branch information
RunDevelopment authored and mAAdhaTTah committed Jan 21, 2019
1 parent e2d3bc7 commit 7eccea5
Show file tree
Hide file tree
Showing 12 changed files with 281 additions and 244 deletions.
7 changes: 5 additions & 2 deletions components/prism-c.js
@@ -1,7 +1,11 @@
Prism.languages.c = Prism.languages.extend('clike', {
'class-name': {
pattern: /(\b(?:enum|struct)\s+)\w+/,
lookbehind: true
},
'keyword': /\b(?:_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/,
'operator': />>=?|<<=?|->|([-+&|:])\1|[?:~]|[-+*/%&|^!=<>]=?/,
'number': /(?:\b0x[\da-f]+|(?:\b\d+\.?\d*|\B\.\d+)(?:e[+-]?\d+)?)[ful]*/i
'number': /(?:\b0x(?:[\da-f]+\.?[\da-f]*|\.[\da-f]+)(?:p[+-]?\d+)?|(?:\b\d+\.?\d*|\B\.\d+)(?:e[+-]?\d+)?)[ful]*/i
});

Prism.languages.insertBefore('c', 'string', {
Expand Down Expand Up @@ -29,5 +33,4 @@ Prism.languages.insertBefore('c', 'string', {
'constant': /\b(?:__FILE__|__LINE__|__DATE__|__TIME__|__TIMESTAMP__|__func__|EOF|NULL|SEEK_CUR|SEEK_END|SEEK_SET|stdin|stdout|stderr)\b/
});

delete Prism.languages.c['class-name'];
delete Prism.languages.c['boolean'];
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.

11 changes: 4 additions & 7 deletions components/prism-cpp.js
@@ -1,16 +1,13 @@
Prism.languages.cpp = Prism.languages.extend('c', {
'class-name': {
pattern: /(\b(?:class|enum|struct)\s+)\w+/,
lookbehind: true
},
'keyword': /\b(?:alignas|alignof|asm|auto|bool|break|case|catch|char|char16_t|char32_t|class|compl|const|constexpr|const_cast|continue|decltype|default|delete|do|double|dynamic_cast|else|enum|explicit|export|extern|float|for|friend|goto|if|inline|int|int8_t|int16_t|int32_t|int64_t|uint8_t|uint16_t|uint32_t|uint64_t|long|mutable|namespace|new|noexcept|nullptr|operator|private|protected|public|register|reinterpret_cast|return|short|signed|sizeof|static|static_assert|static_cast|struct|switch|template|this|thread_local|throw|try|typedef|typeid|typename|union|unsigned|using|virtual|void|volatile|wchar_t|while)\b/,
'boolean': /\b(?:true|false)\b/,
'operator': />>=?|<<=?|->|([-+&|:])\1|[?:~]|[-+*/%&|^!=<>]=?|\b(?:and|and_eq|bitand|bitor|not|not_eq|or|or_eq|xor|xor_eq)\b/
});

Prism.languages.insertBefore('cpp', 'keyword', {
'class-name': {
pattern: /(class\s+)\w+/i,
lookbehind: true
}
});

Prism.languages.insertBefore('cpp', 'string', {
'raw-string': {
pattern: /R"([^()\\ ]{0,16})\([\s\S]*?\)\1"/,
Expand Down
2 changes: 1 addition & 1 deletion components/prism-cpp.min.js

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

2 changes: 2 additions & 0 deletions components/prism-objectivec.js
Expand Up @@ -3,3 +3,5 @@ Prism.languages.objectivec = Prism.languages.extend('c', {
'string': /("|')(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1|@"(?:\\(?:\r\n|[\s\S])|[^"\\\r\n])*"/,
'operator': /-[->]?|\+\+?|!=?|<<?=?|>>?=?|==?|&&?|\|\|?|[~^%?*\/@]/
});

delete Prism.languages.objectivec['class-name'];
2 changes: 1 addition & 1 deletion components/prism-objectivec.min.js

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

17 changes: 17 additions & 0 deletions tests/languages/c/class-name_feature.test
@@ -0,0 +1,17 @@
struct foo
enum bar

struct foo var;

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

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

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

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

Checks for structs and enums.
18 changes: 11 additions & 7 deletions tests/languages/c/keyword_feature.test
Expand Up @@ -3,27 +3,31 @@ _Complex _Generic _Imaginary
_Noreturn _Static_assert _Thread_local
asm typeof inline auto break
case char const continue default
do double else enum extern
do double else enum;
extern
float for goto if int
long register return short signed
sizeof static struct switch typedef
sizeof static struct;
switch typedef
union unsigned void volatile while

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

[
["keyword", "_Alignas"], ["keyword", "_Alignof"], ["keyword", "_Atomic"], ["keyword", "_Bool"],
["keyword", "_Complex"], ["keyword", "_Generic"], ["keyword", "_Imaginary"],
["keyword", "_Noreturn"], ["keyword", "_Static_assert"], ["keyword", "_Thread_local"],
["keyword", "_Complex"], ["keyword", "_Generic"], ["keyword", "_Imaginary"],
["keyword", "_Noreturn"], ["keyword", "_Static_assert"], ["keyword", "_Thread_local"],
["keyword", "asm"], ["keyword", "typeof"], ["keyword", "inline"], ["keyword", "auto"], ["keyword", "break"],
["keyword", "case"], ["keyword", "char"], ["keyword", "const"], ["keyword", "continue"], ["keyword", "default"],
["keyword", "do"], ["keyword", "double"], ["keyword", "else"], ["keyword", "enum"], ["keyword", "extern"],
["keyword", "do"], ["keyword", "double"], ["keyword", "else"], ["keyword", "enum"], ["punctuation", ";"],
["keyword", "extern"],
["keyword", "float"], ["keyword", "for"], ["keyword", "goto"], ["keyword", "if"], ["keyword", "int"],
["keyword", "long"], ["keyword", "register"], ["keyword", "return"], ["keyword", "short"], ["keyword", "signed"],
["keyword", "sizeof"], ["keyword", "static"], ["keyword", "struct"], ["keyword", "switch"], ["keyword", "typedef"],
["keyword", "sizeof"], ["keyword", "static"], ["keyword", "struct"], ["punctuation", ";"],
["keyword", "switch"], ["keyword", "typedef"],
["keyword", "union"], ["keyword", "unsigned"], ["keyword", "void"], ["keyword", "volatile"], ["keyword", "while"]
]

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

Checks for all keywords.
Checks for all keywords.
8 changes: 7 additions & 1 deletion tests/languages/c/number_feature.test
Expand Up @@ -5,6 +5,9 @@
0.4e+2
0xbabe
0xBABE
0x1.2
0x0.3p-3
0x0.3p4L
42f
42F
42u
Expand All @@ -22,6 +25,9 @@
["number", "0.4e+2"],
["number", "0xbabe"],
["number", "0xBABE"],
["number", "0x1.2"],
["number", "0x0.3p-3"],
["number", "0x0.3p4L"],
["number", "42f"],
["number", "42F"],
["number", "42u"],
Expand All @@ -32,4 +38,4 @@

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

Checks for decimal numbers and hexadecimal numbers.
Checks for decimal numbers and hexadecimal numbers.
8 changes: 6 additions & 2 deletions tests/languages/cpp/class-name_feature.test
@@ -1,13 +1,17 @@
class Foo
class Foo_bar
struct foo
enum bar

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

[
["keyword", "class"], ["class-name", "Foo"],
["keyword", "class"], ["class-name", "Foo_bar"]
["keyword", "class"], ["class-name", "Foo_bar"],
["keyword", "struct"], ["class-name", "foo"],
["keyword", "enum"], ["class-name", "bar"]
]

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

Checks for class names.
Checks for class names.
14 changes: 9 additions & 5 deletions tests/languages/cpp/keyword_feature.test
Expand Up @@ -4,14 +4,16 @@ class;
compl const constexpr
const_cast continue decltype default
delete do double dynamic_cast
else enum explicit export extern
else enum;
explicit export extern
float for friend goto if
inline int long mutable namespace
new noexcept nullptr operator
private protected public register
reinterpret_cast return short
signed sizeof static static_assert
static_cast struct switch template
static_cast struct;
switch template
this thread_local throw try
typedef typeid typename union
unsigned using virtual void
Expand All @@ -28,14 +30,16 @@ uint8_t uint16_t uint32_t uint64_t
["keyword", "compl"], ["keyword", "const"], ["keyword", "constexpr"],
["keyword", "const_cast"], ["keyword", "continue"], ["keyword", "decltype"], ["keyword", "default"],
["keyword", "delete"], ["keyword", "do"], ["keyword", "double"], ["keyword", "dynamic_cast"],
["keyword", "else"], ["keyword", "enum"], ["keyword", "explicit"], ["keyword", "export"], ["keyword", "extern"],
["keyword", "else"], ["keyword", "enum"], ["punctuation", ";"],
["keyword", "explicit"], ["keyword", "export"], ["keyword", "extern"],
["keyword", "float"], ["keyword", "for"], ["keyword", "friend"], ["keyword", "goto"], ["keyword", "if"],
["keyword", "inline"], ["keyword", "int"], ["keyword", "long"], ["keyword", "mutable"], ["keyword", "namespace"],
["keyword", "new"], ["keyword", "noexcept"], ["keyword", "nullptr"], ["keyword", "operator"],
["keyword", "private"], ["keyword", "protected"], ["keyword", "public"], ["keyword", "register"],
["keyword", "reinterpret_cast"], ["keyword", "return"], ["keyword", "short"],
["keyword", "signed"], ["keyword", "sizeof"], ["keyword", "static"], ["keyword", "static_assert"],
["keyword", "static_cast"], ["keyword", "struct"], ["keyword", "switch"], ["keyword", "template"],
["keyword", "static_cast"], ["keyword", "struct"], ["punctuation", ";"],
["keyword", "switch"], ["keyword", "template"],
["keyword", "this"], ["keyword", "thread_local"], ["keyword", "throw"], ["keyword", "try"],
["keyword", "typedef"], ["keyword", "typeid"], ["keyword", "typename"], ["keyword", "union"],
["keyword", "unsigned"], ["keyword", "using"], ["keyword", "virtual"], ["keyword", "void"],
Expand All @@ -46,4 +50,4 @@ uint8_t uint16_t uint32_t uint64_t

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

Checks for all keywords
Checks for all keywords.

0 comments on commit 7eccea5

Please sign in to comment.