Skip to content

Commit

Permalink
more generic implementation (though not perfect yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshgoebel committed Nov 12, 2019
1 parent 41bde38 commit e087bd3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
54 changes: 45 additions & 9 deletions src/highlight.js
Expand Up @@ -108,7 +108,7 @@ https://highlightjs.org/
}
}

function inherit(parent) { // inherit(parent, override_obj, override_obj, ...)
var inherit = function(parent) { // inherit(parent, override_obj, override_obj, ...)
var key;
var result = {};
var objects = Array.prototype.slice.call(arguments, 1);
Expand All @@ -122,6 +122,15 @@ https://highlightjs.org/
return result;
}

// ES2015 version (we would hope it's faster?)
if (Object.assign) {
inherit = function() {
Array.prototype.unshift.call(arguments, {})
return Object.assign.apply(null, arguments)
}
}


/* Stream merging */

function nodeStream(node) {
Expand Down Expand Up @@ -418,15 +427,27 @@ https://highlightjs.org/
for(var i = 0; i<match.length; i++) {
if (match[i] != undefined && matchIndexes["" +i] != undefined ) {
rule = matchIndexes[""+i];
match.splice(0, i) // trim off the extra matches
break;
}
}

// raw (but cooked slight, to fixed the indexes) match is passed to
// callbacks as `raw` and avoids exposing too much of our internal
// workings to callbacks

// we do this selectively with begin [performance], but for `end` because
// we could potentially be matching a parent end rule we always
// include a copy of the raw match

// illegal or end match
if (typeof rule === "string") {
match.raw = inherit(match)
match.type = rule;
match.extra = [mode.illegal, mode.terminator_end];
} else {
if (hasBeginCallbacks(rule))
match.raw = inherit(match)
match.type = "begin";
match.rule = rule;
}
Expand All @@ -436,6 +457,14 @@ https://highlightjs.org/
return matcher;
}

function hasBeginCallbacks(rule) {
return !!rule.onBegin;
}

function hasCallbacks(rule) {
return rule.onBegin || rule.endWhen;
}

function compileMode(mode, parent) {
if (mode.compiled)
return;
Expand Down Expand Up @@ -501,19 +530,16 @@ https://highlightjs.org/
return new RegExp(value.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'm');
}

function endOfMode(mode, matchPlusRemainder, lexeme) {
function endOfMode(mode, matchPlusRemainder) {
var modeEnded = testRe(mode.endRe, matchPlusRemainder);
if (modeEnded && mode.endFilter) {
modeEnded = mode.endFilter(mode.beginValue, lexeme);
}
if (modeEnded) {
while (mode.endsParent && mode.parent) {
mode = mode.parent;
}
return mode;
}
if (mode.endsWithParent) {
return endOfMode(mode.parent, matchPlusRemainder, lexeme);
return endOfMode(mode.parent, matchPlusRemainder);
}
}

Expand Down Expand Up @@ -591,13 +617,15 @@ https://highlightjs.org/

function startNewMode(mode, lexeme) {
result += mode.className? buildSpan(mode.className, '', true): '';
top = Object.create(mode, {parent: {value: top}, beginValue: {value: lexeme}});
top = Object.create(mode, {parent: {value: top}, userState: {value: {}}})
return top;
}


function doBeginMatch(match) {
var lexeme = match[0];
var new_mode = match.rule;
var mode;

if (new_mode && new_mode.endSameAsBegin) {
new_mode.endRe = escapeRe( lexeme );
Expand All @@ -614,16 +642,24 @@ https://highlightjs.org/
mode_buffer = lexeme;
}
}
startNewMode(new_mode, lexeme);
mode = startNewMode(new_mode, lexeme);
if (mode.onBegin) {
mode.onBegin(match.raw, mode.userState)
}
return new_mode.returnBegin ? 0 : lexeme.length;
}

function doEndMatch(match) {
var lexeme = match[0];
var matchPlusRemainder = value.substr(match.index);
var end_mode = endOfMode(top, matchPlusRemainder, lexeme);
var end_mode = endOfMode(top, matchPlusRemainder);
if (!end_mode) { return; }

if (end_mode.endWhen) {
var doEnd = end_mode.endWhen(match.raw, end_mode.userState)
if (doEnd === false) { return; }
}

var origin = top;
if (origin.skip) {
mode_buffer += lexeme;
Expand Down
12 changes: 4 additions & 8 deletions src/languages/cpp.js
Expand Up @@ -28,14 +28,10 @@ function(hljs) {
illegal: '.'
},
{
begin: /(?:u8?|U|L)?R"[^()\\ ]{0,16}\(/,
end: /\)[^()\\ ]{0,16}"/,
endFilter: function(begin, end) {
var quote = begin.indexOf('"');
var beginDelimiter = begin.substring(quote + 1, begin.length - 1);
var endDelimiter = end.substring(1, end.length - 1);
return beginDelimiter == endDelimiter;
},
begin: /(?:u8?|U|L)?R"([^()\\ ]{0,16})\(/,
end: /\)([^()\\ ]{0,16})"/,
onBegin: function(m, state) { state.heredoc = m[1] },
endWhen: function(m, state) { return state.heredoc === m[1] },
}
]
};
Expand Down

0 comments on commit e087bd3

Please sign in to comment.