Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Cache moo lexer instances #9697

Merged
merged 2 commits into from Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 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 Expand Up @@ -155,6 +155,8 @@ function parseContent(content: string): string[] {
token = lexer.next();
}

lexer.reset();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
lexer.reset();

I think this is not needed. 🤔

return result;
}

Expand Down
6 changes: 3 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 All @@ -67,5 +66,6 @@ export function extractPackageFile(content: string): PackageFile {
}
token = lexer.next();
}
lexer.reset();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
lexer.reset();

return { deps };
}
9 changes: 5 additions & 4 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,11 +213,12 @@ function filterTokens({ type }: Token): boolean {
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
lexer.reset();

return result;
}

export function processTokens(tokens: Token[]): Token[] {
Expand Down