Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/reconstruct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ export const reconstruct = (token: Tokens): string => {
token.followedBy ? '?=' :
token.notFollowedBy ? '?!' :
'?:';
return `(${prefix}${createAlternate(token)})`;
let preprefix = '';
if (token.enableStack || token.disableStack) {
preprefix += '?';
if (token.enableStack) preprefix += reduceStack(token.enableStack);
if (token.dash) preprefix += '-';
if (token.disableStack) preprefix += reduceStack(token.disableStack);
preprefix += ':';
}
return `(${preprefix+prefix}${createAlternate(token)})`;
}
case types.REPETITION: {
const { min, max } = token;
Expand Down
61 changes: 61 additions & 0 deletions lib/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ const captureGroupFirstChar = /^[a-zA-Z_$]$/i;
*/
const captureGroupChars = /^[a-zA-Z0-9_$]$/i;

/**
* Valid characters for group modifiers.
*/
const modeModifiers = /^(i|m|s)$/i;

const digit = /\d/;

/**
Expand Down Expand Up @@ -42,6 +47,42 @@ export const tokenizer = (regexpStr: string): Root => {
);
};

const modifierErr = (idx: number) => {
throw new SyntaxError(
`Invalid regular expression: /${
regexpStr
}/: Unknown group modifier flag, expected 'i', or 'm', or 's', found` +
` '${str[idx]}' at column ${idx + 1}`,
);
}

const tokenizeModifiers = (group: Group, i: number, enable: boolean): number => {
group[enable ? 'enableStack' : 'disableStack'] = [];

while (i < str.length && modeModifiers.test(str[i])) {
const char: Char = {
type: types.CHAR,
value: str[i].charCodeAt(0),
};
group[enable ? 'enableStack' : 'disableStack'].push(char);
i++;
}

if (str[i] !== ':' && ((str[i] !== '-' && enable) || (!enable))) {
modifierErr(i);
}

return i;
}

const enableModifers = (group: Group, i: number): number => {
return tokenizeModifiers(group, i, true);
}

const disableModifers = (group: Group, i: number): number => {
return tokenizeModifiers(group, i, false);
}

// Decode a few escaped characters.
let str = util.strToChars(regexpStr);

Expand Down Expand Up @@ -222,6 +263,26 @@ export const tokenizer = (regexpStr: string): Root => {
i++;
} else if (c === ':') {
group.remember = false;
} else if (modeModifiers.test(c)) {
// the index logic is complicated
i = enableModifers(group, --i); // i is ahead of c so we decrement
if (str[i] === '-') {
group.dash = true;
i = disableModifers(group, ++i) +1; // increment move off of '-', add 1 move index
if (group.disableStack.length === 0) {
group.disableStack = undefined;
}
} else if (str[i] === ':') { // like (?i:)
i++;
} else {
modifierErr(i);
}
} else if (c === '-') {
group.dash = true;
i = disableModifers(group, i) +1; // no increment here because i is ahead of c
if (group.disableStack.length === 0) {
group.disableStack = undefined;
}
} else {
throw new SyntaxError(
`Invalid regular expression: /${
Expand Down
3 changes: 3 additions & 0 deletions lib/types/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export type Group = Base<types.GROUP, {
notFollowedBy?: boolean;
lookBehind?: boolean;
name?: string;
enableStack?: Token[];
disableStack?: Token[];
dash?: boolean;
}>

export type Set = Base<types.SET, {
Expand Down