-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e02b07
commit b583a4f
Showing
4 changed files
with
31 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]; | ||
}; |