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: rename RegexComponent to RegexElement #12

Merged
merged 1 commit into from
Dec 6, 2023
Merged
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
38 changes: 19 additions & 19 deletions src/compiler.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
import type { RegexComponent } from './types';
import type { RegexElement } from './types';
import { compilers as quantifiers } from './quantifiers';

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

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

// Recursive compilation

function compileList(components: RegexComponent[]): string {
return components.map((c) => compileSingle(c)).join('');
function compileList(elements: RegexElement[]): string {
return elements.map((c) => compileSingle(c)).join('');
}

function compileSingle(component: RegexComponent): string {
if (typeof component === 'string') {
return component;
function compileSingle(elements: RegexElement): string {
if (typeof elements === 'string') {
return elements;
}

const componentCompiler = quantifiers[component.type];
if (!componentCompiler) {
throw new Error(`Unknown component type ${component.type}`);
const elementCompiler = quantifiers[elements.type];
if (!elementCompiler) {
throw new Error(`Unknown elements type ${elements.type}`);
}

const children = compileList(component.children);
return componentCompiler(children);
const children = compileList(elements.children);
return elementCompiler(children);
}
10 changes: 5 additions & 5 deletions src/quantifiers.ts
Original file line number Diff line number Diff line change
@@ -2,34 +2,34 @@ import type {
One,
OneOrMore,
Optionally,
RegexComponent,
RegexElement,
ZeroOrMore,
} from './types';
import type { CompilerMap } from './types-internal';
import { wrapGroup } from './utils';

export function oneOrMore(...children: RegexComponent[]): OneOrMore {
export function oneOrMore(...children: RegexElement[]): OneOrMore {
return {
type: 'oneOrMore',
children,
};
}

export function optionally(...children: RegexComponent[]): Optionally {
export function optionally(...children: RegexElement[]): Optionally {
return {
type: 'optionally',
children,
};
}

export function one(...children: RegexComponent[]): One {
export function one(...children: RegexElement[]): One {
return {
type: 'one',
children,
};
}

export function zeroOrMore(...children: RegexComponent[]): ZeroOrMore {
export function zeroOrMore(...children: RegexElement[]): ZeroOrMore {
return {
type: 'zeroOrMore',
children,
4 changes: 2 additions & 2 deletions src/types-internal.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Compilation
export type ComponentCompiler = (compiledChildren: string) => string;
export type CompilerMap = Record<string, ComponentCompiler>;
export type ElementCompiler = (compiledChildren: string) => string;
export type CompilerMap = Record<string, ElementCompiler>;
10 changes: 5 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
export type RegexComponent = string | RegexQuantifier;
export type RegexElement = string | RegexQuantifier;

export type RegexQuantifier = One | OneOrMore | Optionally | ZeroOrMore;

// Quantifiers
export type One = {
type: 'one';
children: RegexComponent[];
children: RegexElement[];
};

export type OneOrMore = {
type: 'oneOrMore';
children: RegexComponent[];
children: RegexElement[];
};

export type Optionally = {
type: 'optionally';
children: RegexComponent[];
children: RegexElement[];
};

export type ZeroOrMore = {
type: 'zeroOrMore';
children: RegexComponent[];
children: RegexElement[];
};