Skip to content

Commit

Permalink
refactor regrex
Browse files Browse the repository at this point in the history
  • Loading branch information
issacto committed Aug 2, 2023
1 parent ccef96e commit c9454e7
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions vs code extension/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,23 @@ async function validateTextDocument(textDocument: TextDocument): Promise<void> {
let text = textDocument.getText();
let m: RegExpExecArray | null;

// Find unclosed delimiters of each type.
//TODO: Make regex exclude keywords inside quotes.
//TODO: Make regex' more efficient. Somewhat slow on large files.
const delimiters =
[[/(?<!^[0-9]{6}\* *)(?=([^']*'[^']*')*[^']*$)(?=([^"]*"[^"]*")*[^"]*$)BEFORE(-| )EACH/, /(?<!^[0-9]{6}\* *)(?=([^']*'[^']*')*[^']*$)(?=([^"]*"[^"]*")*[^"]*$)END-BEFORE/],
[/(?<!^[0-9]{6}\* *)(?=([^']*'[^']*')*[^']*$)(?=([^"]*"[^"]*")*[^"]*$)(?<!END-)MOCK/, /(?<!^[0-9]{6}\* *)(?=([^']*'[^']*')*[^']*$)(?=([^"]*"[^"]*")*[^"]*$)END-MOCK/],
[/(?<!^[0-9]{6}\* *)(?=([^']*'[^']*')*[^']*$)(?=([^"]*"[^"]*")*[^"]*$)AFTER(-| )EACH/, /(?<!^[0-9]{6}\* *)(?=([^']*'[^']*')*[^']*$)(?=([^"]*"[^"]*")*[^"]*$)END-AFTER/]];
type RegexPair = [RegExp, RegExp];

const sixDigitsFollowedByAsterisk = "(?<!^[0-9]{6}\\* *)";
const notInSingleQuotes = "(?=([^']*'[^']*')*[^']*$)";
const notInDoubleQuotes = "(?=([^\"']*\"[^\"']*')*[^\"']*$)";

const createRegexPair = (start: string, end: string): RegexPair => [
new RegExp(`${sixDigitsFollowedByAsterisk}${notInSingleQuotes}${notInDoubleQuotes}${start}`),
new RegExp(`${sixDigitsFollowedByAsterisk}${notInSingleQuotes}${notInDoubleQuotes}${end}`)
];

const delimiters: RegexPair[] = [
createRegexPair("BEFORE(-| )EACH", "END-BEFORE"),
createRegexPair("(?<!END-)MOCK", "END-MOCK"),
createRegexPair("AFTER(-| )EACH", "END-AFTER")
];

const delimitererrors = [];
let error;
Expand Down

0 comments on commit c9454e7

Please sign in to comment.