Skip to content

Commit

Permalink
simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
joshgoebel committed Feb 27, 2020
1 parent 6f6c06e commit bebc9b7
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/lib/mode_compiler.js
Expand Up @@ -44,25 +44,22 @@ export function compileLanguage(language) {
}

compile() {
if (this.regexes.length === 0) {
// avoids the need to check length every time exec is called
this.exec = () => null;
}
let terminators = this.regexes.map(el => el[1]);
this.matcherRe = langRe(regex.join(terminators, '|'), true);
this.lastIndex = 0;
}

exec(s) {
var matchData;
if (this.regexes.length === 0) return null;

this.matcherRe.lastIndex = this.lastIndex;
let match = this.matcherRe.exec(s);
if (!match) { return null; }

for(var i = 0; i<match.length; i++) {
if (match[i] != undefined && this.matchIndexes[i]) {
matchData = this.matchIndexes[i];
break;
}
}
let i = match.findIndex((el, i) => i>0 && el!=undefined);
let matchData = this.matchIndexes[i];

return Object.assign(match, matchData);
}
Expand Down

0 comments on commit bebc9b7

Please sign in to comment.