|
1 | 1 | import { encodeAtom } from '../encoder/encoder';
|
2 | 2 | import type { EncodeResult } from '../encoder/types';
|
3 |
| -import { ensureArray } from '../utils/elements'; |
4 |
| -import type { RegexConstruct, RegexElement, RegexSequence } from '../types'; |
| 3 | +import type { RegexSequence } from '../types'; |
5 | 4 |
|
6 | 5 | export interface QuantifierOptions {
|
7 | 6 | greedy?: boolean;
|
8 | 7 | }
|
9 |
| -export interface ZeroOrMore extends RegexConstruct { |
10 |
| - type: 'zeroOrMore'; |
11 |
| - children: RegexElement[]; |
12 |
| - options?: QuantifierOptions; |
13 |
| -} |
14 |
| - |
15 |
| -export interface OneOrMore extends RegexConstruct { |
16 |
| - type: 'oneOrMore'; |
17 |
| - children: RegexElement[]; |
18 |
| - options?: QuantifierOptions; |
19 |
| -} |
20 |
| - |
21 |
| -export interface Optional extends RegexConstruct { |
22 |
| - type: 'optional'; |
23 |
| - children: RegexElement[]; |
24 |
| - options?: QuantifierOptions; |
25 |
| -} |
26 |
| - |
27 |
| -export function zeroOrMore(sequence: RegexSequence, options?: QuantifierOptions): ZeroOrMore { |
28 |
| - return { |
29 |
| - type: 'zeroOrMore', |
30 |
| - children: ensureArray(sequence), |
31 |
| - options, |
32 |
| - encode: encodeZeroOrMore, |
33 |
| - }; |
34 |
| -} |
35 |
| - |
36 |
| -export function oneOrMore(sequence: RegexSequence, options?: QuantifierOptions): OneOrMore { |
37 |
| - return { |
38 |
| - type: 'oneOrMore', |
39 |
| - children: ensureArray(sequence), |
40 |
| - options, |
41 |
| - encode: encodeOneOrMore, |
42 |
| - }; |
43 |
| -} |
44 |
| - |
45 |
| -export function optional(sequence: RegexSequence, options?: QuantifierOptions): Optional { |
46 |
| - return { |
47 |
| - type: 'optional', |
48 |
| - children: ensureArray(sequence), |
49 |
| - options, |
50 |
| - encode: encodeOptional, |
51 |
| - }; |
52 |
| -} |
53 | 8 |
|
54 |
| -function encodeZeroOrMore(this: ZeroOrMore): EncodeResult { |
| 9 | +export function zeroOrMore(sequence: RegexSequence, options?: QuantifierOptions): EncodeResult { |
55 | 10 | return {
|
56 | 11 | precedence: 'sequence',
|
57 |
| - pattern: `${encodeAtom(this.children).pattern}*${this.options?.greedy === false ? '?' : ''}`, |
| 12 | + pattern: `${encodeAtom(sequence).pattern}*${options?.greedy === false ? '?' : ''}`, |
58 | 13 | };
|
59 | 14 | }
|
60 | 15 |
|
61 |
| -function encodeOneOrMore(this: OneOrMore): EncodeResult { |
| 16 | +export function oneOrMore(sequence: RegexSequence, options?: QuantifierOptions): EncodeResult { |
62 | 17 | return {
|
63 | 18 | precedence: 'sequence',
|
64 |
| - pattern: `${encodeAtom(this.children).pattern}+${this.options?.greedy === false ? '?' : ''}`, |
| 19 | + pattern: `${encodeAtom(sequence).pattern}+${options?.greedy === false ? '?' : ''}`, |
65 | 20 | };
|
66 | 21 | }
|
67 | 22 |
|
68 |
| -function encodeOptional(this: Optional): EncodeResult { |
| 23 | +export function optional(sequence: RegexSequence, options?: QuantifierOptions): EncodeResult { |
69 | 24 | return {
|
70 | 25 | precedence: 'sequence',
|
71 |
| - pattern: `${encodeAtom(this.children).pattern}?${this.options?.greedy === false ? '?' : ''}`, |
| 26 | + pattern: `${encodeAtom(sequence).pattern}?${options?.greedy === false ? '?' : ''}`, |
72 | 27 | };
|
73 | 28 | }
|
0 commit comments