-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathre.ts
183 lines (149 loc) · 3.82 KB
/
re.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { Set, Record, List } from 'immutable';
export interface TypedBody<S extends string, B> {
type: S;
body: B;
}
export interface TypedRecord<S extends string, B>
extends Record<TypedBody<S, B>>,
Readonly<TypedBody<S, B>> {}
function factory<R extends TypedRecord<string, any>>(type: R['type']) {
const inner = Record({ type, body: undefined });
return (body: R['body']) => inner({ body }) as R;
}
export type Re = Chars | Empty | Concat | Kleene | Or | And | Not;
export type Class = Set<number>;
export interface Chars extends TypedRecord<'Chars', Class> {}
const Chars = factory<Chars>('Chars');
export function chars(allowedChars: string) {
return Chars(
Set().withMutations(set => {
for (let i = 0; i < allowedChars.length; i++) {
set.add(allowedChars.charCodeAt(i));
}
})
);
}
export const NONE = Chars(Set());
export interface Empty extends Record<{ type: 'Empty' }>, Readonly<{ type: 'Empty' }> {}
export const EMPTY: Empty = Record({ type: 'Empty' as 'Empty' })();
export type ConcatItem = Chars | Kleene | Or | And | Not;
export interface Concat extends TypedRecord<'Concat', List<ConcatItem>> {}
const Concat = factory<Concat>('Concat');
export function concat(...regexps: Re[]) {
let newList = List<ConcatItem>();
for (let re of regexps) {
switch (re.type) {
case 'Empty': {
break;
}
case 'Concat': {
newList = newList.concat(re.body);
break;
}
default: {
if (re.equals(NONE)) {
return NONE;
}
newList = newList.push(re);
break;
}
}
}
if (newList.size === 0) {
return EMPTY;
}
if (newList.size === 1) {
return newList.first()!;
}
return Concat(newList);
}
export type KleeneBody = Chars | Concat | Or | And | Not;
export interface Kleene extends TypedRecord<'Kleene', KleeneBody> {}
const Kleene = factory<Kleene>('Kleene');
export function kleene(body: Re) {
if (body.type === 'Empty' || body.type === 'Kleene') return body;
if (body.equals(NONE)) return EMPTY;
return Kleene(body);
}
export type OrItem = Chars | Empty | Concat | Kleene | And | Not;
export interface Or extends TypedRecord<'Or', Set<OrItem>> {}
const Or = factory<Or>('Or');
export function or(...regexps: Re[]) {
let newSet = Set<OrItem>();
let chars = Set<number>();
for (let re of regexps) {
switch (re.type) {
case 'Chars': {
chars = chars.union(re.body);
break;
}
case 'Or': {
newSet = newSet.union(re.body);
break;
}
default: {
if (re.equals(NOT_NONE)) {
return NOT_NONE;
}
newSet = newSet.add(re);
break;
}
}
}
if (!chars.isEmpty()) {
newSet = newSet.add(Chars(chars));
}
if (newSet.size === 0) {
return NONE;
}
if (newSet.size === 1) {
return newSet.first()!;
}
return Or(newSet);
}
export type AndItem = Chars | Empty | Concat | Kleene | Or | Not;
export interface And extends TypedRecord<'And', Set<AndItem>> {}
const And = factory<And>('And');
export function and(...regexps: Re[]) {
let newSet = Set<AndItem>();
let chars: Set<number> | undefined;
for (let re of regexps) {
switch (re.type) {
case 'Chars': {
chars = chars === undefined ? re.body : chars.intersect(re.body);
if (chars.isEmpty()) {
return NONE;
}
break;
}
case 'And': {
newSet = newSet.union(re.body);
break;
}
default: {
if (!re.equals(NOT_NONE)) {
newSet = newSet.add(re);
}
break;
}
}
}
if (chars !== undefined) {
newSet = newSet.add(Chars(chars));
}
if (newSet.size === 0) {
return NOT_NONE;
}
if (newSet.size === 1) {
return newSet.first()!;
}
return And(newSet);
}
export type NotBody = Chars | Empty | Concat | Kleene | Or | And;
export interface Not extends TypedRecord<'Not', NotBody> {}
const Not = factory<Not>('Not');
export function not(body: Re) {
if (body.type === 'Not') return body.body;
return Not(body);
}
export const NOT_NONE = Not(NONE);