Skip to content
Snippets Groups Projects
Unverified Commit 5716077f authored by Sergei Zharinov's avatar Sergei Zharinov Committed by GitHub
Browse files

refactor: Cache moo lexer instances (#9697)

parent 37a8e282
Branches
No related tags found
No related merge requests found
......@@ -53,7 +53,7 @@ function parseUrl(urlString: string): UrlParsedResult | null {
return null;
}
const dummyLexer = {
const lexer = moo.states({
main: {
lineComment: { match: /#.*?$/ },
leftParen: { match: '(' },
......@@ -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;
......
......@@ -10,7 +10,7 @@ export const defaultConfig = {
fileMatch: ['\\.cake$'],
};
const lexerStates = {
const lexer = moo.states({
main: {
lineComment: { match: /\/\/.*?$/ },
multiLineComment: { match: /\/\*[^]*?\*\//, lineBreaks: true },
......@@ -23,7 +23,7 @@ const lexerStates = {
},
unknown: { match: /[^]/, lineBreaks: true },
},
};
});
function parseDependencyLine(line: string): PackageDependency | null {
try {
......@@ -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) {
......
......@@ -19,7 +19,7 @@ const escapedChars = {
},
};
export const rawLexer = {
const lexer = moo.states({
// Top-level Groovy lexemes
main: {
[TokenType.LineComment]: { match: /\/\/.*?$/ },
......@@ -104,7 +104,7 @@ export const rawLexer = {
[TokenType.RightBrace]: { match: '}', pop: 1 },
[TokenType.UnknownLexeme]: { match: /[^]/, lineBreaks: true },
},
};
});
/*
Turn UnknownLexeme chars to UnknownFragment strings
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment