Skip to content

Commit

Permalink
properly support lookahead expresions in matches
Browse files Browse the repository at this point in the history
- passes the FULL remaining buffer to `processLexeme` so
  that when it attemps to detemine the next mode it can
  run the ruleset against the FULL buffer, not just the
  terminator which resulted in a match (which will not
  be sufficient enough since lookaheads aren't included
  in the actual match)
  • Loading branch information
joshgoebel committed Sep 23, 2019
1 parent 83cc34a commit 2313427
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ https://highlightjs.org/
top = Object.create(mode, {parent: {value: top}});
}

function processLexeme(buffer, lexeme) {
function processLexeme(buffer, lexeme, remaining_buffer) {

mode_buffer += buffer;

Expand All @@ -496,7 +496,11 @@ https://highlightjs.org/
return 0;
}

var new_mode = subMode(lexeme, top);
// we use remaining_buffer here rather than lexeme so that
// positive/negative lookaheads can work, even though the
// lookahead content would not be included in the lexeme itself
// (as it's technically NOT part of the match)
var new_mode = subMode(remaining_buffer, top);
if (new_mode) {
if (new_mode.skip) {
mode_buffer += lexeme;
Expand Down Expand Up @@ -573,14 +577,18 @@ https://highlightjs.org/
}
var mode_buffer = '';
var relevance = 0;
var remaining_buffer, skipped_buffer;
try {
var match, count, index = 0;
while (true) {
top.terminators.lastIndex = index;
match = top.terminators.exec(value);
if (!match)
break;
count = processLexeme(value.substring(index, match.index), match[0]);
// buffer between the last match and the current match
skipped_buffer = value.substring(index, match.index)
remaining_buffer = value.substr(match.index)
count = processLexeme(skipped_buffer, match[0], remaining_buffer);
index = match.index + count;
}
processLexeme(value.substr(index));
Expand Down

0 comments on commit 2313427

Please sign in to comment.