-
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.
feat: add basic character classes (#13)
Co-authored-by: Maciej Jastrzebski <[email protected]>
- Loading branch information
1 parent
3dd6dfe
commit e323eeb
Showing
12 changed files
with
121 additions
and
49 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 |
---|---|---|
|
@@ -30,3 +30,5 @@ buck-out/ | |
|
||
# generated by bob | ||
lib/ | ||
|
||
/.idea |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { any, digit, whitespace, word } from '../character-classes'; | ||
import { buildPattern } from '../compiler'; | ||
import { one } from '../quantifiers/base'; | ||
|
||
test('"whitespace" character class', () => { | ||
expect(buildPattern(whitespace)).toEqual(`\\s`); | ||
|
||
expect(buildPattern(one('ab'), whitespace)).toEqual(`ab\\s`); | ||
|
||
expect(buildPattern(one('ab'), whitespace, one('c'))).toEqual(`ab\\sc`); | ||
}); | ||
|
||
test('"digit" character class', () => { | ||
expect(buildPattern(digit)).toEqual(`\\d`); | ||
|
||
expect(buildPattern(one('ab'), digit)).toEqual(`ab\\d`); | ||
|
||
expect(buildPattern(one('ab'), digit, one('c'))).toEqual(`ab\\dc`); | ||
}); | ||
|
||
test('"word" character class', () => { | ||
expect(buildPattern(word)).toEqual(`\\w`); | ||
|
||
expect(buildPattern(one('ab'), word)).toEqual(`ab\\w`); | ||
|
||
expect(buildPattern(one('ab'), word, one('c'))).toEqual(`ab\\wc`); | ||
}); | ||
|
||
test('"any" character class', () => { | ||
expect(buildPattern(any)).toEqual(`.`); | ||
|
||
expect(buildPattern(one('ab'), any)).toEqual(`ab.`); | ||
|
||
expect(buildPattern(one('ab'), any, one('c'))).toEqual(`ab.c`); | ||
}); |
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
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { | ||
Any, | ||
CharacterClass, | ||
Digit, | ||
RegexElement, | ||
Whitespace, | ||
Word, | ||
} from './types'; | ||
|
||
export const whitespace: Whitespace = { type: 'whitespace' }; | ||
export const digit: Digit = { type: 'digit' }; | ||
export const word: Word = { type: 'word' }; | ||
export const any: Any = { type: 'any' }; | ||
|
||
export const characterClasses = { | ||
whitespace: '\\s', | ||
digit: '\\d', | ||
word: '\\w', | ||
any: '.', | ||
} as const satisfies Record<string, string>; | ||
|
||
export function isCharacterClass( | ||
element: Exclude<RegexElement, string> | ||
): element is CharacterClass { | ||
return element.type in characterClasses; | ||
} |
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,4 +1,5 @@ | ||
export type * from './types'; | ||
|
||
export { whitespace } from './character-classes'; | ||
export { buildRegex, buildPattern } from './compiler'; | ||
export { oneOrMore, optionally } from './quantifiers'; | ||
export { oneOrMore, optionally } from './quantifiers/base'; |
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,17 +1,24 @@ | ||
import type { RepeatConfig } from '../types'; | ||
import type { RegexElement, Repeat, RepeatConfig } from '../types'; | ||
import { wrapGroup } from '../utils'; | ||
|
||
export function repeat( | ||
config: RepeatConfig, | ||
...children: RegexElement[] | ||
): Repeat { | ||
return { | ||
type: 'repeat', | ||
children, | ||
config, | ||
}; | ||
} | ||
|
||
export function compileRepeat( | ||
config: RepeatConfig, | ||
compiledChildren: string | ||
): string { | ||
if ('count' in config && typeof config.count === 'number') { | ||
if ('count' in config) { | ||
return `${wrapGroup(compiledChildren)}{${config.count}}`; | ||
} | ||
|
||
if ('min' in config && typeof config.min === 'number') { | ||
return `${wrapGroup(compiledChildren)}{${config.min},${config?.max ?? ''}}`; | ||
} | ||
|
||
return `${wrapGroup(compiledChildren)}`; | ||
return `${wrapGroup(compiledChildren)}{${config.min},${config?.max ?? ''}}`; | ||
} |
This file was deleted.
Oops, something went wrong.
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