-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathregex.js
More file actions
407 lines (363 loc) · 8.66 KB
/
regex.js
File metadata and controls
407 lines (363 loc) · 8.66 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// http://www.ccs.neu.edu/home/turon/re-deriv.pdf
import {
dt, symbolToString, zip,
valid, explain, getName,
specize, conform
} from './util'
import { Nilable } from './spec/nilable'
import { invalid } from './symbols'
import * as p from './predicates'
// ops
const amp = Symbol('amp &')
const kleene = Symbol('kleene *')
const alt = Symbol('alt |')
const cat = Symbol('cat ·')
const acc = Symbol('accepted')
/**
* Wraps value in regex object of with operation "accepted".
*/
function accept(x) {
return {
op: acc,
ret: x
}
}
/**
* Returns true iff value.op equals accepted.
*/
function accepted(x) {
return x && x.op && x.op === acc
}
export function isRegex(x) {
// TODO could use map spec for this?
return p.obj(x) && p.symbol(x.op)
}
/**
* Returns derivative for cat operation with regard to x.
*
* ∂a (r · s) = ∂a r · s + ν(r) · ∂a s
*/
function catDeriv(regex, x) {
const [p0, ...prest] = regex.ps
const [k0, ...krest] = regex.ks
const derivations = [
pcat({
ps: [deriv(p0, x), ...prest],
ks: regex.ks,
ret: regex.ret
})
]
if (acceptsNil(p0)) {
derivations.push(
deriv(
pcat({
ps: prest,
ks: krest,
ret: [...regex.ret, k0 ? {
[k0]: null
} : null]
})))
} else {
}
return palt({
ps: derivations
})
}
/**
* Returns derivative for alt operation with regard to x.
*
* ∂a (r + s) = ∂a r + ∂a s
*/
function altDeriv(regex, x) {
return palt({
ps: regex.ps.map(p => deriv(p, x)),
ks: regex.ks,
ret: regex.ret
})
}
function kleeneDeriv(regex, x) {
const { p1, p2, ret } = regex;
let ps = [
pkleene({ p1: deriv(p1, x), p2, ret })
];
if (acceptsNil(p1)) {
ps = ps.concat([
deriv(pkleene({ p1: p2, p2, ret: ret.concat([getReturn(p1)]) }), x)
]);
}
return ps.length > 1 ? palt({ ps }) : ps[0];
}
/**
* Calculates derivative of regex with respect to x.
*
* @param {[type]} regex [description]
* @param {[type]} x [description]
* @return {[type]} [description]
*/
export function deriv(regex, x) {
if (!isRegex(regex)) {
if (regex === null) {
return null
}
const ret = dt(regex, x)
return ret === invalid ? null : accept(ret)
}
switch (regex.op) {
case acc:
return null
case alt:
return altDeriv(regex, x)
case cat:
return catDeriv(regex, x)
case kleene:
return kleeneDeriv(regex, x)
}
throw new Error(`Cannot derive unknown operation "${symbolToString(op)}"`)
}
function getReturn(regex) {
switch (regex.op) {
case cat:
const catProcessed = regex.ret.reduce((agg, val) => Object.assign(agg, val), {})
// unprocessed values
regex.ks.forEach(k => catProcessed[k] = null)
return catProcessed
case acc:
case alt:
return regex.ret;
case kleene:
const p1ret = accepted(regex.p1) ? getReturn(regex.p1) : null;
return regex.ret.concat(p1ret ? [p1ret] : []);
}
throw new Error(`Return for ${symbolToString(regex.op)} not implemented`)
}
/**
* Conform for regex objects. Returns conformed value when regex
* matches data, invalid otherwise.
*/
export function regexConform(regex, data) {
if (data !== null && !Array.isArray(data)) {
return invalid;
}
const [x0, ...xrest] = data
if (!data.length) {
if (acceptsNil(regex)) {
return getReturn(regex);
} else {
return invalid;
}
} else {
const dx = deriv(regex, x0);
return dx ? regexConform(dx, xrest) : invalid;
}
}
export function regexExplain(regex, path, via, value) {
// no value provided, so no problems can occur
if (p.nil(value)) {
return null
}
if (valid(regex, value)) {
return null
}
// regex is not a regex, defer to spec explain implementation
if (!isRegex(regex)) {
return explain(regex, path, via, value)
}
// it's a regex, but value is not a collection(???)
if (!Array.isArray(value)) {
return [{
path,
via,
value,
predicate: Array.isArray
}]
}
switch (regex.op) {
case cat:
if (value.length < regex.ps.length) {
return [{
path,
via: [...via, getName(regex)],
value,
predicate: function hasLength(val) {
return val.length >= regex.ps.length
}
}]
}
return regex.ps
.map((p, i) => [valid(p, value[i]), p, i])
.filter(([valid]) => !valid)
.map(([_, p, i]) => explain(p, [...path, i], [...via, getName(regex), regex.ks[i]], value[i]))
case alt:
// if value does not match alt, ALL alternatives have to yield a problem
return regex.ps.map((p, i) => explain(p, [...path, i], [...via, getName(regex), regex.ks[i]], value))
case acc:
return null
case kleene:
return value
.reduce((errs, val, i) => {
let r = kleeneDeriv(regex, val);
if (r.p1 === null) {
r = Object.assign({}, r, { i, value: val });
return errs.concat([r]);
}
return errs;
}, [])
.map(regex => ({
predicate: regex.p2,
path: [...path, regex.i],
via,
value: regex.value,
}));
}
}
function acceptsNil(regex) {
regex = regex || {};
switch (regex.op) {
case acc:
return true;
case alt:
return regex.ps.some(p => acceptsNil(p))
case cat:
return regex.ps.every(p => acceptsNil(p))
case kleene:
return regex.p1 === regex.p2 || acceptsNil(regex.p1);
default:
// TODO
return false
}
}
/**
* Returns regex object for cat. Do not use directly, use catImpl.
*/
function pcat(opts = {}) {
const {ps = [], ks = [], ret = []} = opts
const [p0, ...prest] = ps
const [k0, ...krest] = ks
if (accepted(p0)) {
const pret = [...ret, k0 ? {
[k0]: p0.ret
} : p0.ret]
if (prest.length > 0) {
return pcat({
ps: prest,
ks: krest,
ret: pret
})
}
// there are no more values
// we convert the array of matches to a single map and return that
return accept(getReturn(pcat({
ret: pret
})))
}
return {
op: cat,
ps,
ks,
ret
}
}
/**
* Used to filter predicates according to a function.
*/
function filter(ps, ks, fn) {
if (ks.length > 0) {
const filtered = zip(ps, ks).filter(([p]) => fn(p))
if (filtered.length > 0) {
return zip(...filtered)
}
return [[], []]
}
return [ps.filter(p => fn(p)), []]
}
/**
* Returns regex object for alt. Do not use directly, use altImpl.
*/
function palt(opts = {}) {
var {ps = [], ks = [], ret = {}} = opts;
// we need to remove null (= not matching) predicates
[ps, ks] = filter(ps, ks, x => !!x)
const [p0, ...prest] = ps
const [k0, ...krest] = ks
const _alt = {
op: alt,
ps,
ks,
ret
}
// if any of the alternatives is accepted, return that
const acceptIdx = ps.findIndex(p => accepted(p))
if (acceptIdx !== -1) {
if (ks[acceptIdx]) {
return accept({
[ks[acceptIdx]]: ps[acceptIdx].ret
})
}
return accept(ps[acceptIdx].ret)
}
// return first predicate if there are no keys and no other alternatives
if (prest.length === 0 && !k0) {
return p0
}
// else just return the whole regex object
return _alt
}
// xy
export function catImpl(...predicates) {
if (p.odd(predicates.length)) {
throw new Error(`Must provide an even number of arguments to cat. Provided: ${predicates.length}`)
}
const ks = predicates.filter((_, i) => p.even(i))
const ps = predicates.filter((_, i) => p.odd(i))
return pcat({
ps,
ks
})
}
// (x | y)
export function altImpl(...predicates) {
if (p.odd(predicates.length)) {
throw new Error(`Must provide an even number of arguments to alt. Provided: ${predicates.length}`)
}
const ks = predicates.filter((_, i) => p.even(i))
const ps = predicates.filter((_, i) => p.odd(i))
return palt({
ps,
ks
})
}
function pkleene(opts) {
let { p1, p2, ret = [] } = opts;
const r = { op: kleene, p1, p2, ret };
return accepted(p1)
? Object.assign({}, r, { p1: p2, ret: ret.concat([getReturn(p1)]) })
: r;
}
// x*
export function kleeneImpl(predicate) {
return pkleene({
op: kleene,
p1: predicate,
p2: predicate,
});
}
// x+
export function plusImpl() {
}
// x?
// TODO should there be a name parameter for maybe?
export function maybeImpl(name, predicate) {
if (p.nil(name) || !p.string(name)) {
throw new Error(`Must provide a name to maybe.`)
}
if (p.nil(predicate)) {
throw new Error(`Must provide a predicate to maybe.`)
}
return palt({
ps: [predicate, p.nil],
ks: [name, name]
})
}
//????
export function ampImpl() {
}