Skip to content

Commit

Permalink
try to improve clarity with naming
Browse files Browse the repository at this point in the history
  • Loading branch information
joshgoebel committed Mar 2, 2020
1 parent e09c990 commit e137079
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
20 changes: 11 additions & 9 deletions src/highlight.js
Expand Up @@ -210,15 +210,15 @@ const HLJS = function(hljs) {
}

function doIgnore(lexeme) {
if (top.terminators.startAt === 0) {
if (top.matcher.regexIndex === 0) {
// no more regexs to potentially match here, so we move the cursor forward one
// space
mode_buffer += lexeme[0];
return 1;
} else {
// no need to move the cursor, we still have additional regexes to try and
// match at this very spot
findNextRegexMatch = true;
continueScanAtSamePosition = true;
return 0;
}
}
Expand Down Expand Up @@ -376,17 +376,19 @@ const HLJS = function(hljs) {
var match, processedCount, index = 0;

try {
var findNextRegexMatch = false;
top.terminators.startAt = 0;
var continueScanAtSamePosition = false;
top.matcher.considerAll();

while (true) {
top.terminators.lastIndex = index;
if (findNextRegexMatch) {
findNextRegexMatch = false;
if (continueScanAtSamePosition) {
continueScanAtSamePosition = false;
// only regexes not matched previously will now be
// considered for a potential match
} else {
top.terminators.startAt = 0;
top.matcher.lastIndex = index;
top.matcher.considerAll();
}
match = top.terminators.exec(codeToHighlight);
match = top.matcher.exec(codeToHighlight);
// console.log("match", match[0], match.rule && match.rule.begin)
if (!match)
break;
Expand Down
18 changes: 11 additions & 7 deletions src/lib/mode_compiler.js
Expand Up @@ -103,7 +103,7 @@ export function compileLanguage(language) {
this.count = 0;

this.lastIndex = 0;
this.startAt = 0;
this.regexIndex = 0;
}

getMatcher(index) {
Expand All @@ -116,22 +116,26 @@ export function compileLanguage(language) {
return matcher;
}

considerAll() {
this.regexIndex = 0;
}

addRule(re, opts) {
this.rules.push([re, opts]);
if (opts.type==="begin") this.count++;
}

exec(s) {
let m = this.getMatcher(this.startAt);
let m = this.getMatcher(this.regexIndex);
m.lastIndex = this.lastIndex;
let result = m.exec(s);
if (result) {
this.startAt += result.position + 1;
if (this.startAt === this.count) // wrap-around
this.startAt = 0;
this.regexIndex += result.position + 1;
if (this.regexIndex === this.count) // wrap-around
this.regexIndex = 0;
}

// this.startAt = 0;
// this.regexIndex = 0;
return result;
}
}
Expand Down Expand Up @@ -242,7 +246,7 @@ export function compileLanguage(language) {
compileMode(mode.starts, parent);
}

mode.terminators = buildModeRegex(mode);
mode.matcher = buildModeRegex(mode);
}

// self is not valid at the top-level
Expand Down

0 comments on commit e137079

Please sign in to comment.