Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed highlight C++ std types in C #4107

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Core Grammars:
- enh(csharp) add Contextual keywords `file`, `args`, `dynamic`, `record`, `required` and `scoped` [Alvin Joy][]
- fix(c) - Fixed hex numbers with decimals [Dxuian]
- fix(ruby) - fix `|=` operator false positives (as block arguments) [Aboobacker MK]
- fix(sql) - Fixed sql primary key and foreign key spacing issue [Dxuian]

New Grammars:

Expand Down
147 changes: 135 additions & 12 deletions src/languages/c.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,19 +180,142 @@ export default function(hljs) {
const KEYWORDS = {
keyword: C_KEYWORDS,
type: C_TYPES,
literal: 'true false NULL',
// TODO: apply hinting work similar to what was done in cpp.js
built_in: 'std string wstring cin cout cerr clog stdin stdout stderr stringstream istringstream ostringstream '
+ 'auto_ptr deque list queue stack vector map set pair bitset multiset multimap unordered_set '
+ 'unordered_map unordered_multiset unordered_multimap priority_queue make_pair array shared_ptr abort terminate abs acos '
+ 'asin atan2 atan calloc ceil cosh cos exit exp fabs floor fmod fprintf fputs free frexp '
+ 'fscanf future isalnum isalpha iscntrl isdigit isgraph islower isprint ispunct isspace isupper '
+ 'isxdigit tolower toupper labs ldexp log10 log malloc realloc memchr memcmp memcpy memset modf pow '
+ 'printf putchar puts scanf sinh sin snprintf sprintf sqrt sscanf strcat strchr strcmp '
+ 'strcpy strcspn strlen strncat strncmp strncpy strpbrk strrchr strspn strstr tanh tan '
+ 'vfprintf vprintf vsprintf endl initializer_list unique_ptr',
literal:
'true '
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should now be an array (one item per line), not a large string.

+ 'false '
+ 'NULL',
built_in:
'stdin '
+ 'stdout '
+ 'stderr '
+ 'abs '
+ 'acos '
+ 'asin '
+ 'atan '
+ 'atan2 '
+ 'ceil '
+ 'cos '
+ 'cosh '
+ 'exp '
+ 'fabs '
+ 'floor '
+ 'fmod '
+ 'frexp '
+ 'ldexp '
+ 'log '
+ 'log10 '
+ 'pow '
+ 'sin '
+ 'sinh '
+ 'sqrt '
+ 'tan '
+ 'tanh '
+ 'printf '
+ 'scanf '
+ 'puts '
+ 'putchar '
+ 'getchar '
+ 'gets '
+ 'fgets '
+ 'fputs '
+ 'fprintf '
+ 'fscanf '
+ 'sscanf '
+ 'sprintf '
+ 'snprintf '
+ 'malloc '
+ 'calloc '
+ 'realloc '
+ 'free '
+ 'memcpy '
+ 'memmove '
+ 'memset '
+ 'memcmp '
+ 'memchr '
+ 'strcpy '
+ 'strncpy '
+ 'strcat '
+ 'strncat '
+ 'strcmp '
+ 'strncmp '
+ 'strchr '
+ 'strrchr '
+ 'strstr '
+ 'strlen '
+ 'atoi '
+ 'atol '
+ 'atoll '
+ 'strtol '
+ 'strtoll '
+ 'strtoul '
+ 'strtoull '
+ 'rand '
+ 'srand '
+ 'exit '
+ 'abort '
+ 'assert '
+ 'time '
+ 'clock '
+ 'difftime '
+ 'mktime '
+ 'asctime '
+ 'ctime '
+ 'gmtime '
+ 'localtime '
+ 'strftime '
+ 'fopen '
+ 'fclose '
+ 'fread '
+ 'fwrite '
+ 'fseek '
+ 'ftell '
+ 'rewind '
+ 'fflush '
+ 'isalnum '
+ 'isalpha '
+ 'iscntrl '
+ 'isdigit '
+ 'isgraph '
+ 'islower '
+ 'isprint '
+ 'ispunct '
+ 'isspace '
+ 'isupper '
+ 'isxdigit '
+ 'tolower '
+ 'toupper '
+ 'qsort '
+ 'bsearch '
+ 'setjmp '
+ 'longjmp '
+ 'va_start '
+ 'va_arg '
+ 'va_end '
+ 'offsetof '
+ 'static_assert '
+ 'alignas '
+ 'alignof '
+ '_Generic '
+ 'atomic_bool '
+ 'atomic_char '
+ 'atomic_schar '
+ 'atomic_uchar '
+ 'atomic_short '
+ 'atomic_ushort '
+ 'atomic_int '
+ 'atomic_uint '
+ 'atomic_long '
+ 'atomic_ulong '
+ 'atomic_llong '
+ 'atomic_ullong '
+ 'atomic_intptr_t '
+ 'atomic_uintptr_t '
+ 'atomic_size_t '
+ 'atomic_ptrdiff_t '
+ 'atomic_intmax_t '
+ 'atomic_uintmax_t'
};

const EXPRESSION_CONTAINS = [
PREPROCESSOR,
TYPES,
Expand Down
40 changes: 30 additions & 10 deletions src/languages/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,35 @@ export default function(hljs) {
keywords: { built_in: FUNCTIONS }
};

//COMBOS generator
// const regexPatterns = COMBOS.map(phrase => {
// const escapedPhrase = phrase.replace(/ /g, "\\s+"); // Replace spaces with \s+ to match any whitespace
// return new RegExp(escapedPhrase, "gi"); // Create case-insensitive regex
// });

const COMBOSLIST = {
className: 'type',
variants: [
{ match: /\bcreate\s+table\b/ },
{ match: /\binsert\s+into\b/ },
{ match: /\bprimary\s+key\b/ },
{ match: /\bforeign\s+key\b/ },
{ match: /\bnot\s+null\b/ },
{ match: /\balter\s+table\b/ },
{ match: /\badd\s+constraint\b/ },
{ match: /\bgrouping\s+sets\b/ },
{ match: /\bon\s+overflow\b/ },
{ match: /\bcharacter\s+set\b/ },
{ match: /\brespect\s+nulls\b/ },
{ match: /\bignore\s+nulls\b/ },
{ match: /\bnulls\s+first\b/ },
{ match: /\bnulls\s+last\b/ },
{ match: /\bdepth\s+first\b/ },
{ match: /\bbreadth\s+first\b/ }
],
relevance: 0
};

// keywords with less than 3 letters are reduced in relevancy
function reduceRelevancy(list, {
exceptions, when
Expand Down Expand Up @@ -653,20 +682,11 @@ export default function(hljs) {
built_in: POSSIBLE_WITHOUT_PARENS
},
contains: [
{
begin: regex.either(...COMBOS),
relevance: 0,
keywords: {
$pattern: /[\w\.]+/,
keyword: KEYWORDS.concat(COMBOS),
literal: LITERALS,
type: TYPES
},
},
{
className: "type",
begin: regex.either(...MULTI_WORD_TYPES)
},
COMBOSLIST,
FUNCTION_CALL,
VARIABLE,
STRING,
Expand Down