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: optimize grouping #23

Merged
merged 6 commits into from
Dec 14, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { buildPattern, buildRegex } from '../compiler';
import { buildPattern, buildRegex } from '..';
import { one, oneOrMore, optionally, zeroOrMore } from '../quantifiers/base';
import { repeat } from '../quantifiers/repeat';

Expand Down Expand Up @@ -56,3 +56,9 @@ test('buildRegex throws error on unknown element', () => {
buildRegex({ type: 'unknown' })
).toThrowErrorMatchingInlineSnapshot(`"Unknown elements type unknown"`);
});

test('buildPattern throws on empty text', () => {
expect(() => buildPattern('')).toThrowErrorMatchingInlineSnapshot(
`"\`encodeText\`: received text should not be empty"`
);
});
23 changes: 14 additions & 9 deletions src/character-classes/__tests__/any-of.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
import { buildPattern as p } from '../../compiler';
import { buildPattern } from '../..';
import { oneOrMore } from '../../quantifiers/base';
import { anyOf } from '../any-of';

test('"anyOf" base cases', () => {
expect(p(anyOf(''))).toBe('');
expect(p(anyOf('a'))).toBe('a');
expect(p(anyOf('abc'))).toBe('[abc]');
expect(buildPattern(anyOf('a'))).toBe('a');
expect(buildPattern(anyOf('abc'))).toBe('[abc]');
});

test('"anyOf" in context', () => {
expect(p('x', anyOf('a'), 'x')).toBe('xax');
expect(p('x', anyOf('abc'), 'x')).toBe('x[abc]x');
expect(p('x', oneOrMore(anyOf('abc')), 'x')).toBe('x(?:[abc])+x');
expect(buildPattern('x', anyOf('a'), 'x')).toBe('xax');
expect(buildPattern('x', anyOf('abc'), 'x')).toBe('x[abc]x');
expect(buildPattern('x', oneOrMore(anyOf('abc')), 'x')).toBe('x[abc]+x');
});

test('"anyOf" escapes special characters', () => {
expect(p(anyOf('abc-+.'))).toBe('[-abc\\+\\.]');
expect(buildPattern(anyOf('abc-+.'))).toBe('[-abc\\+\\.]');
});

test('"anyOf" moves hyphen to the first position', () => {
expect(p(anyOf('a-bc'))).toBe('[-abc]');
expect(buildPattern(anyOf('a-bc'))).toBe('[-abc]');
});

test('`anyOf` throws on empty text', () => {
expect(() => anyOf('')).toThrowErrorMatchingInlineSnapshot(
`"\`anyOf\` should received at least one character"`
);
});
4 changes: 2 additions & 2 deletions src/character-classes/__tests__/base.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { any, digit, whitespace, word } from '../base';
import { buildPattern } from '../../compiler';
import { buildPattern } from '../..';
import { one } from '../../quantifiers/base';
import { any, digit, whitespace, word } from '../base';

test('"whitespace" character class', () => {
expect(buildPattern(whitespace)).toEqual(`\\s`);
Expand Down
12 changes: 12 additions & 0 deletions src/character-classes/__tests__/encoder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { encodeCharacterClass } from '../encoder';

test('buildPattern throws on empty text', () => {
expect(() =>
encodeCharacterClass({
type: 'characterClass',
characters: [],
})
).toThrowErrorMatchingInlineSnapshot(
`"Character class should contain at least one character"`
);
});
7 changes: 6 additions & 1 deletion src/character-classes/any-of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import type { CharacterClass } from '../types';
import { escapeText } from '../utils';

export function anyOf(characters: string): CharacterClass {
const charactersArray = characters.split('').map(escapeText);
if (charactersArray.length === 0) {
throw new Error('`anyOf` should received at least one character');
}

return {
type: 'characterClass',
characters: characters.split('').map(escapeText),
characters: charactersArray,
};
}
10 changes: 5 additions & 5 deletions src/character-classes/base.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import type { CharacterClass } from '../types';

export const any: CharacterClass = {
type: 'characterClass',
characters: ['.'],
};

export const whitespace: CharacterClass = {
type: 'characterClass',
characters: ['\\s'],
Expand All @@ -14,8 +19,3 @@ export const word: CharacterClass = {
type: 'characterClass',
characters: ['\\w'],
};

export const any: CharacterClass = {
type: 'characterClass',
characters: ['.'],
};
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import type { CharacterClass } from '../types';
import { EncoderPriority, type EncoderNode } from '../types-internal';

export function compileCharacterClass({ characters }: CharacterClass): string {
export function encodeCharacterClass({
characters,
}: CharacterClass): EncoderNode {
if (characters.length === 0) {
return '';
throw new Error('Character class should contain at least one character');
}

if (characters.length === 1) {
return characters[0]!;
return {
priority: EncoderPriority.Atom,
pattern: characters[0]!,
};
}

return `[${escapeHyphen(characters).join('')}]`;
return {
priority: EncoderPriority.Atom,
pattern: `[${reorderHyphen(characters).join('')}]`,
};
}

// If passed characters includes hyphen (`-`) it need to be moved to
// first (or last) place in order to treat it as hyphen character and not a range.
// See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Character_classes#types
function escapeHyphen(characters: string[]) {
function reorderHyphen(characters: string[]) {
if (characters.includes('-')) {
return ['-', ...characters.filter((c) => c !== '-')];
}
Expand Down
60 changes: 0 additions & 60 deletions src/compiler.ts

This file was deleted.

31 changes: 23 additions & 8 deletions src/components/__tests__/choiceOf.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
import { buildPattern } from '../../compiler';
import { buildPattern } from '../..';
import { oneOrMore, zeroOrMore } from '../../quantifiers/base';
import { repeat } from '../../quantifiers/repeat';
import { choiceOf } from '../choiceOf';

test('"choiceOf" using basic strings', () => {
expect(buildPattern(choiceOf('a'))).toEqual('a');
expect(buildPattern(choiceOf('a', 'b'))).toEqual('(?:a|b)');
expect(buildPattern(choiceOf('a', 'b', 'c'))).toEqual('(?:a|b|c)');
expect(buildPattern(choiceOf('a', 'b'))).toEqual('a|b');
expect(buildPattern(choiceOf('a', 'b', 'c'))).toEqual('a|b|c');
expect(buildPattern(choiceOf('aaa', 'bbb'))).toEqual('aaa|bbb');
});

test('"choiceOf" used in sequence', () => {
expect(buildPattern('x', choiceOf('a'), 'x')).toEqual('xax');
expect(buildPattern(choiceOf('a', 'b'), 'x')).toEqual('(?:a|b)x');
expect(buildPattern('x', choiceOf('a', 'b'))).toEqual('x(?:a|b)');

expect(buildPattern(choiceOf('aaa', 'bbb'))).toEqual('(?:aaa|bbb)');
expect(buildPattern(choiceOf('a', 'b', 'c'))).toEqual('a|b|c');
expect(buildPattern('x', choiceOf('a', 'b', 'c'))).toEqual('x(?:a|b|c)');
expect(buildPattern(choiceOf('a', 'b', 'c'), 'x')).toEqual('(?:a|b|c)x');

expect(buildPattern(choiceOf('aaa', 'bbb'))).toEqual('aaa|bbb');
});

test('"choiceOf" using nested regex', () => {
expect(buildPattern(choiceOf(oneOrMore('a'), zeroOrMore('b')))).toBe(
'(?:a+|b*)'
);
expect(buildPattern(choiceOf(oneOrMore('a'), zeroOrMore('b')))).toBe('a+|b*');
expect(
buildPattern(
choiceOf(repeat({ min: 1, max: 3 }, 'a'), repeat({ count: 5 }, 'bx'))
)
).toBe('(?:a{1,3}|(?:bx){5})');
).toBe('a{1,3}|(?:bx){5}');
});

test('`anyOf` throws on empty options', () => {
expect(() => choiceOf()).toThrowErrorMatchingInlineSnapshot(
`"\`choiceOf\` should receive at least one option"`
);
});
28 changes: 21 additions & 7 deletions src/components/choiceOf.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
import type { ChoiceOf, RegexElement } from '../types';
import type { CompileSingle } from '../types-internal';
import { wrapGroup } from '../utils';
import {
EncoderPriority,
type EncodeElement,
type EncoderNode,
} from '../types-internal';

export function choiceOf(...children: RegexElement[]): ChoiceOf {
if (children.length === 0) {
throw new Error('`choiceOf` should receive at least one option');
}

return {
type: 'choiceOf',
children,
};
}

export function compileChoiceOf(
export function encodeChoiceOf(
element: ChoiceOf,
compileSingle: CompileSingle
): string {
const compiledChildren = element.children.map(compileSingle);
return wrapGroup(compiledChildren.join('|'));
encodeElement: EncodeElement
): EncoderNode {
const encodedNodes = element.children.map(encodeElement);
if (encodedNodes.length === 1) {
return encodedNodes[0]!;
}

return {
priority: EncoderPriority.Alternation,
pattern: encodedNodes.map((n) => n.pattern).join('|'),
};
}
71 changes: 71 additions & 0 deletions src/encoder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import type { RegexElement } from './types';
import { EncoderPriority, type EncoderNode } from './types-internal';
import { encodeChoiceOf } from './components/choiceOf';
import { encodeCharacterClass } from './character-classes/encoder';
import {
encodeOne,
encodeOneOrMore,
encodeOptionally,
encodeZeroOrMore,
} from './quantifiers/base';
import { encodeRepeat } from './quantifiers/repeat';
import { concatNodes, escapeText } from './utils';

export function encodeSequence(elements: RegexElement[]): EncoderNode {
return concatNodes(elements.map((c) => encodeElement(c)));
}

export function encodeElement(element: RegexElement): EncoderNode {
if (typeof element === 'string') {
return encodeText(element);
}

if (element.type === 'characterClass') {
return encodeCharacterClass(element);
}

if (element.type === 'choiceOf') {
return encodeChoiceOf(element, encodeElement);
}

if (element.type === 'repeat') {
return encodeRepeat(element.config, encodeSequence(element.children));
}

if (element.type === 'one') {
return encodeOne(encodeSequence(element.children));
}

if (element.type === 'oneOrMore') {
return encodeOneOrMore(encodeSequence(element.children));
}

if (element.type === 'optionally') {
return encodeOptionally(encodeSequence(element.children));
}

if (element.type === 'zeroOrMore') {
return encodeZeroOrMore(encodeSequence(element.children));
}

// @ts-expect-error User passed incorrect type
throw new Error(`Unknown elements type ${element.type}`);
}

function encodeText(text: string): EncoderNode {
if (text.length === 0) {
throw new Error('`encodeText`: received text should not be empty');
}

if (text.length === 1) {
return {
priority: EncoderPriority.Atom,
pattern: escapeText(text),
};
}

return {
priority: EncoderPriority.Sequence,
pattern: escapeText(text),
};
}
25 changes: 23 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
export type * from './types';
import type { RegexElement } from './types';
import { encodeSequence } from './encoder';

export { buildRegex, buildPattern } from './compiler';
export type * from './types';

export { any, digit, whitespace, word } from './character-classes/base';
export { anyOf } from './character-classes/any-of';
export { one, oneOrMore, optionally, zeroOrMore } from './quantifiers/base';
export { repeat } from './quantifiers/repeat';
export { choiceOf } from './components/choiceOf';

/**
* Generate RegExp object for elements.
*
* @param elements
* @returns
*/
export function buildRegex(...elements: RegexElement[]): RegExp {
const pattern = encodeSequence(elements).pattern;
return new RegExp(pattern);
}

/**
* Generate regex pattern for elements.
* @param elements
* @returns
*/
export function buildPattern(...elements: RegexElement[]): string {
return encodeSequence(elements).pattern;
}
Loading