forked from boostorg/website-v2-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp-highlight.js
More file actions
199 lines (181 loc) · 5.96 KB
/
Copy pathcpp-highlight.js
File metadata and controls
199 lines (181 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
* cpp-highlight.js - Lightweight C++ syntax highlighter
*
* Categories:
* - keyword : Language keywords (const, while, if, etc.)
* - string : String and character literals
* - preprocessor: Preprocessor directives (#include, #define, etc.)
* - comment : C and C++ style comments
*/
const CppHighlight = (function () {
'use strict'
const KEYWORDS = new Set([
// Storage class
'auto', 'register', 'static', 'extern', 'mutable', 'thread_local',
// Type qualifiers
'const', 'volatile', 'constexpr', 'consteval', 'constinit',
// Type specifiers
'void', 'bool', 'char', 'short', 'int', 'long', 'float', 'double',
'signed', 'unsigned', 'wchar_t', 'char8_t', 'char16_t', 'char32_t',
// Complex types
'class', 'struct', 'union', 'enum', 'typename', 'typedef',
// Control flow
'if', 'else', 'switch', 'case', 'default', 'for', 'while', 'do',
'break', 'continue', 'return', 'goto',
// Exception handling
'try', 'catch', 'throw', 'noexcept',
// OOP
'public', 'private', 'protected', 'virtual', 'override', 'final',
'friend', 'this', 'operator', 'new', 'delete',
// Templates
'template', 'concept', 'requires',
// Namespace
'namespace', 'using',
// Other
'sizeof', 'alignof', 'alignas', 'decltype', 'typeid',
'static_cast', 'dynamic_cast', 'const_cast', 'reinterpret_cast',
'static_assert', 'inline', 'explicit', 'export', 'module', 'import',
'co_await', 'co_yield', 'co_return',
// Literals
'true', 'false', 'nullptr', 'NULL',
])
function escapeHtml (text) {
return text
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
}
function span (cls, content) {
return `<span class="cpp-${cls}">${escapeHtml(content)}</span>`
}
function highlight (code) {
const result = []
let i = 0
const n = code.length
while (i < n) {
// Line comment
if (code[i] === '/' && code[i + 1] === '/') {
let end = i + 2
while (end < n && code[end] !== '\n') end++
result.push(span('comment', code.slice(i, end)))
i = end
continue
}
// Block comment
if (code[i] === '/' && code[i + 1] === '*') {
let end = i + 2
while (end < n - 1 && !(code[end] === '*' && code[end + 1] === '/')) end++
end += 2
result.push(span('comment', code.slice(i, end)))
i = end
continue
}
// Preprocessor directive (at start of line or after whitespace)
if (code[i] === '#') {
// Check if # is at line start (allow leading whitespace)
let checkPos = i - 1
while (checkPos >= 0 && (code[checkPos] === ' ' || code[checkPos] === '\t')) checkPos--
if (checkPos < 0 || code[checkPos] === '\n') {
let end = i + 1
// Handle line continuation with backslash
while (end < n) {
if (code[end] === '\n') {
if (code[end - 1] === '\\') {
end++
continue
}
break
}
end++
}
result.push(span('preprocessor', code.slice(i, end)))
i = end
continue
}
}
// Raw string literal R"delimiter(...)delimiter"
if (code[i] === 'R' && code[i + 1] === '"') {
let delimEnd = i + 2
while (delimEnd < n && code[delimEnd] !== '(') delimEnd++
const delimiter = code.slice(i + 2, delimEnd)
const endMarker = ')' + delimiter + '"'
let end = delimEnd + 1
while (end < n) {
if (code.slice(end, end + endMarker.length) === endMarker) {
end += endMarker.length
break
}
end++
}
result.push(span('string', code.slice(i, end)))
i = end
continue
}
// String literal (with optional prefix)
if (code[i] === '"' ||
((code[i] === 'L' || code[i] === 'u' || code[i] === 'U') && code[i + 1] === '"') ||
(code[i] === 'u' && code[i + 1] === '8' && code[i + 2] === '"')) {
const start = i
if (code[i] === 'u' && code[i + 1] === '8') i += 2
else if (code[i] !== '"') i++
i++ // skip opening quote
while (i < n && code[i] !== '"') {
if (code[i] === '\\' && i + 1 < n) i += 2
else i++
}
i++ // skip closing quote
result.push(span('string', code.slice(start, i)))
continue
}
// Character literal
if (code[i] === '\'' ||
((code[i] === 'L' || code[i] === 'u' || code[i] === 'U') && code[i + 1] === '\'') ||
(code[i] === 'u' && code[i + 1] === '8' && code[i + 2] === '\'')) {
const start = i
if (code[i] === 'u' && code[i + 1] === '8') i += 2
else if (code[i] !== '\'') i++
i++ // skip opening quote
while (i < n && code[i] !== '\'') {
if (code[i] === '\\' && i + 1 < n) i += 2
else i++
}
i++ // skip closing quote
result.push(span('string', code.slice(start, i)))
continue
}
// Identifier or keyword
if (/[a-zA-Z_]/.test(code[i])) {
let end = i + 1
while (end < n && /[a-zA-Z0-9_]/.test(code[end])) end++
const word = code.slice(i, end)
if (KEYWORDS.has(word)) {
result.push(span('keyword', word))
} else {
result.push(escapeHtml(word))
}
i = end
continue
}
// Default: single character
result.push(escapeHtml(code[i]))
i++
}
return result.join('')
}
function highlightElement (el) {
el.innerHTML = highlight(el.textContent)
}
function highlightAll (selector = 'code.cpp, code.c++, pre.cpp, pre.c++') {
document.querySelectorAll(selector).forEach(highlightElement)
}
return {
highlight,
highlightElement,
highlightAll,
KEYWORDS,
}
})()
// CommonJS / ES module support
if (typeof module !== 'undefined' && module.exports) {
module.exports = CppHighlight
}