Skip to content

Commit

Permalink
refactor: Cache moo lexer instances (#9697)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Apr 23, 2021
1 parent 37a8e28 commit 5716077
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
6 changes: 3 additions & 3 deletions lib/manager/bazel/extract.ts
Expand Up @@ -53,7 +53,7 @@ function parseUrl(urlString: string): UrlParsedResult | null {
return null;
}

const dummyLexer = {
const lexer = moo.states({
main: {
lineComment: { match: /#.*?$/ },
leftParen: { match: '(' },
Expand Down Expand Up @@ -103,11 +103,11 @@ const dummyLexer = {
stringFinish: { match: "'", pop: 1 },
char: { match: /[^]/, lineBreaks: true },
},
};
});

function parseContent(content: string): string[] {
const lexer = moo.states(dummyLexer);
lexer.reset(content);

let balance = 0;

let def: null | string = null;
Expand Down
5 changes: 2 additions & 3 deletions lib/manager/cake/index.ts
Expand Up @@ -10,7 +10,7 @@ export const defaultConfig = {
fileMatch: ['\\.cake$'],
};

const lexerStates = {
const lexer = moo.states({
main: {
lineComment: { match: /\/\/.*?$/ },
multiLineComment: { match: /\/\*[^]*?\*\//, lineBreaks: true },
Expand All @@ -23,7 +23,7 @@ const lexerStates = {
},
unknown: { match: /[^]/, lineBreaks: true },
},
};
});

function parseDependencyLine(line: string): PackageDependency | null {
try {
Expand Down Expand Up @@ -54,7 +54,6 @@ function parseDependencyLine(line: string): PackageDependency | null {

export function extractPackageFile(content: string): PackageFile {
const deps = [];
const lexer = moo.states(lexerStates);
lexer.reset(content);
let token = lexer.next();
while (token) {
Expand Down
5 changes: 2 additions & 3 deletions lib/manager/gradle-lite/tokenizer.ts
Expand Up @@ -19,7 +19,7 @@ const escapedChars = {
},
};

export const rawLexer = {
const lexer = moo.states({
// Top-level Groovy lexemes
main: {
[TokenType.LineComment]: { match: /\/\/.*?$/ },
Expand Down Expand Up @@ -104,7 +104,7 @@ export const rawLexer = {
[TokenType.RightBrace]: { match: '}', pop: 1 },
[TokenType.UnknownLexeme]: { match: /[^]/, lineBreaks: true },
},
};
});

/*
Turn UnknownLexeme chars to UnknownFragment strings
Expand Down Expand Up @@ -213,7 +213,6 @@ function filterTokens({ type }: Token): boolean {
}

export function extractRawTokens(input: string): Token[] {
const lexer = moo.states(rawLexer);
lexer.reset(input);
return Array.from(lexer).map(
({ type, offset, value }) => ({ type, offset, value } as Token)
Expand Down

0 comments on commit 5716077

Please sign in to comment.