Skip to content

Commit f9f5a70

Browse files
committed
(chore) Create c-like and c/cpp depend on that now
- chore(parser): effectively rename `cpp.js` to `c-like.js` [Josh Goebel][] - chore(parser): create new `c.js` (C), depends on `c-like` now [Josh Goebel][] - chore(parser): create new `cpp.js` (C), depends on `c-like` now [Josh Goebel][] - This will allow us to clean up C/C++ in the future without another breaking change by getting this require change out of the way early. (#2146)
1 parent 188a198 commit f9f5a70

File tree

4 files changed

+34
-187
lines changed

4 files changed

+34
-187
lines changed

VERSION_10_BREAKING.md

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ Incompatibilities:
66
Renamed Language Files:
77
- chore(parser): rename `cs.js` to `csharp.js` [Josh Goebel][]
88
- chore(parser): rename `tex.js` to `latex.js` [Josh Goebel][]
9+
- chore(parser): effectively rename `cpp.js` to `c-like.js` [Josh Goebel][]
10+
- chore(parser): create new `c.js` (C), depends on `c-like` now [Josh Goebel][]
11+
- chore(parser): create new `cpp.js` (C), depends on `c-like` now [Josh Goebel][]
12+
- This will allow us to clean up C/C++ in the future without another breaking change
13+
by getting this require change out of the way early.
14+
(https://github.com/highlightjs/highlight.js/issues/2146)
915

1016
Legacy Browser Issues:
1117
- **We're now using ES2015 features in the codebase. Internet Explorer 11 is no longer supported.**

src/languages/c.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
Language: C
3+
Category: common, system
4+
Website: https://en.wikipedia.org/wiki/C_(programming_language)
5+
Requires: c-like.js
6+
*/
7+
8+
function(hljs) {
9+
10+
var lang = hljs.getLanguage('c-like').rawDefinition();
11+
// Until C is markedly different than C++ there is no reason to auto-detect
12+
// C as it's own language since it would just fail auto-detect testing
13+
// lang.disableAutodetect = false;
14+
return lang;
15+
16+
}

src/languages/cpp.js

+4-187
Original file line numberDiff line numberDiff line change
@@ -1,197 +1,14 @@
11
/*
22
Language: C++
3-
Author: Ivan Sagalaev <maniac@softwaremaniacs.org>
4-
Contributors: Evgeny Stepanischev <imbolk@gmail.com>, Zaven Muradyan <megalivoithos@gmail.com>, Roel Deckers <admin@codingcat.nl>, Sam Wu <samsam2310@gmail.com>, Jordi Petit <jordi.petit@gmail.com>, Pieter Vantorre <pietervantorre@gmail.com>, Google Inc. (David Benjamin) <davidben@google.com>
53
Category: common, system
64
Website: https://isocpp.org
5+
Requires: c-like.js
76
*/
87

98
function(hljs) {
10-
var CPP_PRIMITIVE_TYPES = {
11-
className: 'keyword',
12-
begin: '\\b[a-z\\d_]*_t\\b'
13-
};
149

15-
// https://en.cppreference.com/w/cpp/language/escape
16-
// \\ \x \xFF \u2837 \u00323747 \374
17-
var CHARACTER_ESCAPES = '\\\\(x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4,8}|[0-7]{3}|\\S)'
18-
var STRINGS = {
19-
className: 'string',
20-
variants: [
21-
{
22-
begin: '(u8?|U|L)?"', end: '"',
23-
illegal: '\\n',
24-
contains: [hljs.BACKSLASH_ESCAPE]
25-
},
26-
{
27-
begin: '(u8?|U|L)?\'(' + CHARACTER_ESCAPES + "|.)", end: '\'',
28-
illegal: '.'
29-
},
30-
{ begin: /(?:u8?|U|L)?R"([^()\\ ]{0,16})\((?:.|\n)*?\)\1"/ }
31-
]
32-
};
10+
var lang = hljs.getLanguage('c-like').rawDefinition();
11+
lang.disableAutodetect = false;
12+
return lang;
3313

34-
var NUMBERS = {
35-
className: 'number',
36-
variants: [
37-
{ begin: '\\b(0b[01\']+)' },
38-
{ begin: '(-?)\\b([\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)(u|U|l|L|ul|UL|f|F|b|B)' },
39-
{ begin: '(-?)(\\b0[xX][a-fA-F0-9\']+|(\\b[\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)([eE][-+]?[\\d\']+)?)' }
40-
],
41-
relevance: 0
42-
};
43-
44-
var PREPROCESSOR = {
45-
className: 'meta',
46-
begin: /#\s*[a-z]+\b/, end: /$/,
47-
keywords: {
48-
'meta-keyword':
49-
'if else elif endif define undef warning error line ' +
50-
'pragma _Pragma ifdef ifndef include'
51-
},
52-
contains: [
53-
{
54-
begin: /\\\n/, relevance: 0
55-
},
56-
hljs.inherit(STRINGS, {className: 'meta-string'}),
57-
{
58-
className: 'meta-string',
59-
begin: /<.*?>/, end: /$/,
60-
illegal: '\\n',
61-
},
62-
hljs.C_LINE_COMMENT_MODE,
63-
hljs.C_BLOCK_COMMENT_MODE
64-
]
65-
};
66-
67-
var FUNCTION_TITLE = hljs.IDENT_RE + '\\s*\\(';
68-
69-
var CPP_KEYWORDS = {
70-
keyword: 'int float while private char char8_t char16_t char32_t catch import module export virtual operator sizeof ' +
71-
'dynamic_cast|10 typedef const_cast|10 const for static_cast|10 union namespace ' +
72-
'unsigned long volatile static protected bool template mutable if public friend ' +
73-
'do goto auto void enum else break extern using asm case typeid wchar_t' +
74-
'short reinterpret_cast|10 default double register explicit signed typename try this ' +
75-
'switch continue inline delete alignas alignof constexpr consteval constinit decltype ' +
76-
'concept co_await co_return co_yield requires ' +
77-
'noexcept static_assert thread_local restrict final override ' +
78-
'atomic_bool atomic_char atomic_schar ' +
79-
'atomic_uchar atomic_short atomic_ushort atomic_int atomic_uint atomic_long atomic_ulong atomic_llong ' +
80-
'atomic_ullong new throw return ' +
81-
'and and_eq bitand bitor compl not not_eq or or_eq xor xor_eq',
82-
built_in: 'std string wstring cin cout cerr clog stdin stdout stderr stringstream istringstream ostringstream ' +
83-
'auto_ptr deque list queue stack vector map set bitset multiset multimap unordered_set ' +
84-
'unordered_map unordered_multiset unordered_multimap array shared_ptr abort terminate abs acos ' +
85-
'asin atan2 atan calloc ceil cosh cos exit exp fabs floor fmod fprintf fputs free frexp ' +
86-
'fscanf future isalnum isalpha iscntrl isdigit isgraph islower isprint ispunct isspace isupper ' +
87-
'isxdigit tolower toupper labs ldexp log10 log malloc realloc memchr memcmp memcpy memset modf pow ' +
88-
'printf putchar puts scanf sinh sin snprintf sprintf sqrt sscanf strcat strchr strcmp ' +
89-
'strcpy strcspn strlen strncat strncmp strncpy strpbrk strrchr strspn strstr tanh tan ' +
90-
'vfprintf vprintf vsprintf endl initializer_list unique_ptr _Bool complex _Complex imaginary _Imaginary',
91-
literal: 'true false nullptr NULL'
92-
};
93-
94-
var EXPRESSION_CONTAINS = [
95-
CPP_PRIMITIVE_TYPES,
96-
hljs.C_LINE_COMMENT_MODE,
97-
hljs.C_BLOCK_COMMENT_MODE,
98-
NUMBERS,
99-
STRINGS
100-
];
101-
102-
return {
103-
aliases: ['c', 'cc', 'h', 'c++', 'h++', 'hpp', 'hh', 'hxx', 'cxx'],
104-
keywords: CPP_KEYWORDS,
105-
illegal: '</',
106-
contains: EXPRESSION_CONTAINS.concat([
107-
PREPROCESSOR,
108-
{
109-
begin: '\\b(deque|list|queue|stack|vector|map|set|bitset|multiset|multimap|unordered_map|unordered_set|unordered_multiset|unordered_multimap|array)\\s*<', end: '>',
110-
keywords: CPP_KEYWORDS,
111-
contains: ['self', CPP_PRIMITIVE_TYPES]
112-
},
113-
{
114-
begin: hljs.IDENT_RE + '::',
115-
keywords: CPP_KEYWORDS
116-
},
117-
{
118-
// This mode covers expression context where we can't expect a function
119-
// definition and shouldn't highlight anything that looks like one:
120-
// `return some()`, `else if()`, `(x*sum(1, 2))`
121-
variants: [
122-
{begin: /=/, end: /;/},
123-
{begin: /\(/, end: /\)/},
124-
{beginKeywords: 'new throw return else', end: /;/}
125-
],
126-
keywords: CPP_KEYWORDS,
127-
contains: EXPRESSION_CONTAINS.concat([
128-
{
129-
begin: /\(/, end: /\)/,
130-
keywords: CPP_KEYWORDS,
131-
contains: EXPRESSION_CONTAINS.concat(['self']),
132-
relevance: 0
133-
}
134-
]),
135-
relevance: 0
136-
},
137-
{
138-
className: 'function',
139-
begin: '(' + hljs.IDENT_RE + '[\\*&\\s]+)+' + FUNCTION_TITLE,
140-
returnBegin: true, end: /[{;=]/,
141-
excludeEnd: true,
142-
keywords: CPP_KEYWORDS,
143-
illegal: /[^\w\s\*&]/,
144-
contains: [
145-
{
146-
begin: FUNCTION_TITLE, returnBegin: true,
147-
contains: [hljs.TITLE_MODE],
148-
relevance: 0
149-
},
150-
{
151-
className: 'params',
152-
begin: /\(/, end: /\)/,
153-
keywords: CPP_KEYWORDS,
154-
relevance: 0,
155-
contains: [
156-
hljs.C_LINE_COMMENT_MODE,
157-
hljs.C_BLOCK_COMMENT_MODE,
158-
STRINGS,
159-
NUMBERS,
160-
CPP_PRIMITIVE_TYPES,
161-
// Count matching parentheses.
162-
{
163-
begin: /\(/, end: /\)/,
164-
keywords: CPP_KEYWORDS,
165-
relevance: 0,
166-
contains: [
167-
'self',
168-
hljs.C_LINE_COMMENT_MODE,
169-
hljs.C_BLOCK_COMMENT_MODE,
170-
STRINGS,
171-
NUMBERS,
172-
CPP_PRIMITIVE_TYPES
173-
]
174-
}
175-
]
176-
},
177-
hljs.C_LINE_COMMENT_MODE,
178-
hljs.C_BLOCK_COMMENT_MODE,
179-
PREPROCESSOR
180-
]
181-
},
182-
{
183-
className: 'class',
184-
beginKeywords: 'class struct', end: /[{;:]/,
185-
contains: [
186-
{begin: /</, end: />/, contains: ['self']}, // skip generic stuff
187-
hljs.TITLE_MODE
188-
]
189-
}
190-
]),
191-
exports: {
192-
preprocessor: PREPROCESSOR,
193-
strings: STRINGS,
194-
keywords: CPP_KEYWORDS
195-
}
196-
};
19714
}

tools/tasks.js

+8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ tasks.reorderDeps = function(options, blobs, done) {
4444
if(buf.Requires) {
4545
_.each(buf.Requires, function(language) {
4646
object = buffer[language];
47+
// go a second level deep for dependencies
48+
// (new build system will eliminate this)
49+
if (object.Requires) {
50+
_.each(object.Requires, function(language) {
51+
let object = buffer[language];
52+
pushInBlob(object);
53+
});
54+
}
4755
pushInBlob(object);
4856
});
4957
}

0 commit comments

Comments
 (0)