diff --git a/src/highlight.js b/src/highlight.js index b0da8f6159..24fb7d3395 100644 --- a/src/highlight.js +++ b/src/highlight.js @@ -210,7 +210,7 @@ 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]; @@ -218,7 +218,7 @@ const HLJS = function(hljs) { } 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; } } @@ -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; diff --git a/src/lib/mode_compiler.js b/src/lib/mode_compiler.js index 2916099550..10fd000483 100644 --- a/src/lib/mode_compiler.js +++ b/src/lib/mode_compiler.js @@ -103,7 +103,7 @@ export function compileLanguage(language) { this.count = 0; this.lastIndex = 0; - this.startAt = 0; + this.regexIndex = 0; } getMatcher(index) { @@ -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; } } @@ -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