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

cpp: Fully support C++11 raw strings. #1897

Merged
merged 3 commits into from
Feb 3, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
45 changes: 43 additions & 2 deletions src/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,47 @@ https://highlightjs.org/
);
}

// joinRe logically computes regexps.join(separator), but fixes the
// backreferences so they continue to match.
function joinRe(regexps, separator) {
// backreferenceRe matches an open parenthesis or backreference. To avoid
// an incorrect parse, it additionally matches the following:
// - [...] elements, where the meaning of parentheses and escapes change
// - other escape sequences, so we do not misparse escape sequences as
// interesting elements
// - non-matching or lookahead parentheses, which do not capture. These
// follow the '(' with a '?'.
var backreferenceRe = /\[(?:[^\\\]]|\\.)*\]|\(\??|\\([1-9][0-9]*)|\\./;
var numCaptures = 0;
var ret = '';
for (var i = 0; i < regexps.length; i++) {
var offset = numCaptures;
var re = reStr(regexps[i]);
if (i > 0) {
ret += separator;
}
while (re.length > 0) {
var match = backreferenceRe.exec(re);
if (match == null) {
ret += re;
break;
}
ret += re.substring(0, match.index);
re = re.substring(match.index + match[0].length);
if (match[0][0] == '\\' && match[1]) {
// Adjust the backreference.
ret += '\\' + String(Number(match[1]) + offset);
} else {
ret += match[0];
if (match[0] == '(') {
numCaptures++;
}
}
}
}
return ret;
}

function compileMode(mode, parent) {
if (mode.compiled)
return;
Expand Down Expand Up @@ -302,12 +343,12 @@ https://highlightjs.org/

var terminators =
mode.contains.map(function(c) {
return c.beginKeywords ? '\\.?(' + c.begin + ')\\.?' : c.begin;
return c.beginKeywords ? '\\.?(?:' + c.begin + ')\\.?' : c.begin;
})
.concat([mode.terminator_end, mode.illegal])
.map(reStr)
.filter(Boolean);
mode.terminators = terminators.length ? langRe(terminators.join('|'), true) : {exec: function(/*s*/) {return null;}};
mode.terminators = terminators.length ? langRe(joinRe(terminators, '|'), true) : {exec: function(/*s*/) {return null;}};
}

compileMode(language);
Expand Down
8 changes: 1 addition & 7 deletions src/languages/cpp.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ function(hljs) {
illegal: '\\n',
contains: [hljs.BACKSLASH_ESCAPE]
},
{
// TODO: This does not handle raw string literals with prefixes. Using
// a single regex with backreferences would work (note to use *?
// instead of * to make it non-greedy), but the mode.terminators
// computation in highlight.js breaks the counting.
begin: '(u8?|U|L)?R"\\(', end: '\\)"',
},
{ begin: /(?:u8?|U|L)?R"([^()\\ ]{0,16})\((?:.|\n)*?\)\1"/ },
{
begin: '\'\\\\?.', end: '\'',
illegal: '.'
Expand Down
34 changes: 30 additions & 4 deletions test/markup/cpp/string-literals.expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,47 @@
<span class="hljs-comment">// Raw string literals (multiline)</span>
<span class="hljs-keyword">auto</span> char_multi = <span class="hljs-string">R"(Hello
"normal"
muliline
multiline
string.)"</span>;
<span class="hljs-keyword">auto</span> utf8_multi = <span class="hljs-string">u8R"(Hello
"utf-8"
muliline
multiline
string)"</span>;
<span class="hljs-keyword">auto</span> utf16_multi = <span class="hljs-string">uR"(Hello
"utf-16"
muliline
multiline
string)"</span>;
<span class="hljs-keyword">auto</span> utf32_multi = <span class="hljs-string">UR"(Hello
"utf-32"
muliline
multiline
string)"</span>;

<span class="hljs-comment">// Raw string literals with delimiter (multiline)</span>
<span class="hljs-keyword">auto</span> char_multi = <span class="hljs-string">R"blah1(Hello
"normal"
multiline
)"
)blah"
string.)blah1"</span>;
<span class="hljs-keyword">auto</span> utf8_multi = <span class="hljs-string">u8R"blah2(Hello
"utf-8"
multiline
)"
)blah"
string)blah2"</span>;
<span class="hljs-keyword">auto</span> utf16_multi = <span class="hljs-string">uR"blah3(Hello
"utf-16"
multiline
)"
)blah"
string)blah3"</span>;
<span class="hljs-keyword">auto</span> utf32_multi = <span class="hljs-string">UR"blah4(Hello
"utf-32"
multiline
)"
)blah"
string)blah4"</span>;

<span class="hljs-comment">// Meta strings</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"lib.h"</span></span>
34 changes: 30 additions & 4 deletions test/markup/cpp/string-literals.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,47 @@ auto wide_char = L"Hello wchar_t string";
// Raw string literals (multiline)
auto char_multi = R"(Hello
"normal"
muliline
multiline
string.)";
auto utf8_multi = u8R"(Hello
"utf-8"
muliline
multiline
string)";
auto utf16_multi = uR"(Hello
"utf-16"
muliline
multiline
string)";
auto utf32_multi = UR"(Hello
"utf-32"
muliline
multiline
string)";

// Raw string literals with delimiter (multiline)
auto char_multi = R"blah1(Hello
"normal"
multiline
)"
)blah"
string.)blah1";
auto utf8_multi = u8R"blah2(Hello
"utf-8"
multiline
)"
)blah"
string)blah2";
auto utf16_multi = uR"blah3(Hello
"utf-16"
multiline
)"
)blah"
string)blah3";
auto utf32_multi = UR"blah4(Hello
"utf-32"
multiline
)"
)blah"
string)blah4";

// Meta strings
#include <stdio>
#include "lib.h"