-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathinternal.ts
155 lines (151 loc) · 7.21 KB
/
internal.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { exactly } from './inputs'
import type { GetValue } from './types/escape'
import type { GetCapturedGroupsArr, InputSource } from './types/sources'
import { IfUnwrapped, wrap } from './wrap'
const GROUPED_AS_REPLACE_RE = /^(?:\(\?:(.+)\)|(\(?.+\)?))$/
const GROUPED_REPLACE_RE = /^(?:\(\?:(.+)\)([?+*]|{[\d,]+})?|(.+))$/
export interface Input<
in V extends string,
G extends string = never,
C extends (string | undefined)[] = []
> {
/** this adds a new pattern to the current input */
and: {
<I extends InputSource>(input: I): Input<
`${V}${GetValue<I>}`,
G | (I extends Input<any, infer NewGroups> ? NewGroups : never),
[...C, ...GetCapturedGroupsArr<I>]
>
/** this adds a new pattern to the current input, with the pattern reference to a named group. */
referenceTo: <N extends G>(groupName: N) => Input<`${V}\\k<${N}>`, G, C>
}
/** this provides an alternative to the current input */
or: <I extends InputSource>(
input: I
) => Input<
`(?:${V}|${GetValue<I>})`,
G | (I extends Input<any, infer NewGroups> ? NewGroups : never),
[...C, ...GetCapturedGroupsArr<I>]
>
/** this is a positive lookbehind. Make sure to check [browser support](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#browser_compatibility) as not all browsers support lookbehinds (notably Safari) */
after: <I extends InputSource>(
input: I
) => Input<`(?<=${GetValue<I>})${V}`, G, [...GetCapturedGroupsArr<I>, ...C]>
/** this is a positive lookahead */
before: <I extends InputSource>(
input: I
) => Input<`${V}(?=${GetValue<I>})`, G, [...C, ...GetCapturedGroupsArr<I>]>
/** these is a negative lookbehind. Make sure to check [browser support](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#browser_compatibility) as not all browsers support lookbehinds (notably Safari) */
notAfter: <I extends InputSource>(
input: I
) => Input<`(?<!${GetValue<I>})${V}`, G, [...GetCapturedGroupsArr<I, true>, ...C]>
/** this is a negative lookahead */
notBefore: <I extends InputSource>(
input: I
) => Input<`${V}(?!${GetValue<I>})`, G, [...C, ...GetCapturedGroupsArr<I, true>]>
/** repeat the previous pattern an exact number of times */
times: {
<N extends number>(number: N): Input<IfUnwrapped<V, `(?:${V}){${N}}`, `${V}{${N}}`>, G, C>
/** specify that the expression can repeat any number of times, _including none_ */
any: () => Input<IfUnwrapped<V, `(?:${V})*`, `${V}*`>, G, C>
/** (Lazy Mode) specify that the expression can repeat any number of times, _including none_ */
anyLazy: () => Input<IfUnwrapped<V, `(?:${V})*?`, `${V}*?`>, G, C>
/** specify that the expression must occur at least `N` times */
atLeast: <N extends number>(
number: N
) => Input<IfUnwrapped<V, `(?:${V}){${N},}`, `${V}{${N},}`>, G, C>
/** (Lazy Mode) specify that the expression must occur at least `N` times */
atLeastLazy: <N extends number>(
number: N
) => Input<IfUnwrapped<V, `(?:${V}){${N},}?`, `${V}{${N},}?`>, G, C>
/** specify that the expression must occur at most `N` times */
atMost: <N extends number>(
number: N
) => Input<IfUnwrapped<V, `(?:${V}){0,${N}}`, `${V}{0,${N}}`>, G, C>
/** (Lazy Mode) specify that the expression must occur at most `N` times */
atMostLazy: <N extends number>(
number: N
) => Input<IfUnwrapped<V, `(?:${V}){0,${N}}?`, `${V}{0,${N}}?`>, G, C>
/** specify a range of times to repeat the previous pattern */
between: <Min extends number, Max extends number>(
min: Min,
max: Max
) => Input<IfUnwrapped<V, `(?:${V}){${Min},${Max}}`, `${V}{${Min},${Max}}`>, G, C>
/** (Lazy Mode) specify a range of times to repeat the previous pattern */
betweenLazy: <Min extends number, Max extends number>(
min: Min,
max: Max
) => Input<IfUnwrapped<V, `(?:${V}){${Min},${Max}}?`, `${V}{${Min},${Max}}?`>, G, C>
}
/** this defines the entire input so far as a named capture group. You will get type safety when using the resulting RegExp with `String.match()`. Alias for `groupedAs` */
as: <K extends string>(
key: K
) => Input<
V extends `(?:${infer S})` ? `(?<${K}>${S})` : `(?<${K}>${V})`,
G | K,
[V extends `(?:${infer S})` ? `(?<${K}>${S})` : `(?<${K}>${V})`, ...C]
>
/** this defines the entire input so far as a named capture group. You will get type safety when using the resulting RegExp with `String.match()` */
groupedAs: <K extends string>(
key: K
) => Input<
V extends `(?:${infer S})` ? `(?<${K}>${S})` : `(?<${K}>${V})`,
G | K,
[V extends `(?:${infer S})` ? `(?<${K}>${S})` : `(?<${K}>${V})`, ...C]
>
/** this capture the entire input so far as an anonymous group */
grouped: () => Input<
V extends `(?:${infer S})${infer E}` ? `(${S})${E}` : `(${V})`,
G,
[V extends `(?:${infer S})${'' | '?' | '+' | '*' | `{${string}}`}` ? `(${S})` : `(${V})`, ...C]
>
/** this allows you to match beginning/ends of lines with `at.lineStart()` and `at.lineEnd()` */
at: {
lineStart: () => Input<`^${V}`, G, C>
lineEnd: () => Input<`${V}$`, G, C>
}
/** this allows you to mark the input so far as optional */
optionally: () => Input<IfUnwrapped<V, `(?:${V})?`, `${V}?`>, G, C>
optionallyLazy: () => Input<IfUnwrapped<V, `(?:${V})??`, `${V}??`>, G, C>
toString: () => string
}
export const createInput = <
Value extends string,
Groups extends string = never,
CaptureGroupsArr extends (string | undefined)[] = []
>(
s: Value | Input<Value, Groups, CaptureGroupsArr>
): Input<Value, Groups, CaptureGroupsArr> => {
const groupedAsFn = (key: string) =>
createInput(`(?<${key}>${`${s}`.replace(GROUPED_AS_REPLACE_RE, '$1$2')})`)
return {
toString: () => s.toString(),
and: Object.assign((input: InputSource) => createInput(`${s}${exactly(input)}`), {
referenceTo: (groupName: string) => createInput(`${s}\\k<${groupName}>`),
}),
or: input => createInput(`(?:${s}|${exactly(input)})`),
after: input => createInput(`(?<=${exactly(input)})${s}`),
before: input => createInput(`${s}(?=${exactly(input)})`),
notAfter: input => createInput(`(?<!${exactly(input)})${s}`),
notBefore: input => createInput(`${s}(?!${exactly(input)})`),
times: Object.assign((number: number) => createInput(`${wrap(s)}{${number}}`) as any, {
any: () => createInput(`${wrap(s)}*`),
anyLazy: () => createInput(`${wrap(s)}*?`),
atLeast: (min: number) => createInput(`${wrap(s)}{${min},}`),
atLeastLazy: (min: number) => createInput(`${wrap(s)}{${min},}?`),
atMost: (max: number) => createInput(`${wrap(s)}{0,${max}}`),
atMostLazy: (max: number) => createInput(`${wrap(s)}{0,${max}}?`),
between: (min: number, max: number) => createInput(`${wrap(s)}{${min},${max}}`),
betweenLazy: (min: number, max: number) => createInput(`${wrap(s)}{${min},${max}}?`),
}),
optionally: () => createInput(`${wrap(s)}?`) as any,
optionallyLazy: () => createInput(`${wrap(s)}??`) as any,
as: groupedAsFn,
groupedAs: groupedAsFn,
grouped: () => createInput(`${s}`.replace(GROUPED_REPLACE_RE, '($1$3)$2')),
at: {
lineStart: () => createInput(`^${s}`),
lineEnd: () => createInput(`${s}$`),
},
}
}