/
EPattern.java
503 lines (418 loc) · 10.3 KB
/
EPattern.java
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/**
* This file is part of Erjang - A JVM-based Erlang VM
*
* Copyright (c) 2009 by Trifork
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package erjang.m.ets;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.regex.Pattern;
import com.trifork.clj_ds.ISeq;
import erjang.EAtom;
import erjang.EBitString;
import erjang.ECons;
import erjang.EFun;
import erjang.EList;
import erjang.ENumber;
import erjang.EObject;
import erjang.EPID;
import erjang.EPort;
import erjang.ERT;
import erjang.ERef;
import erjang.ESeq;
import erjang.ETuple;
/**
*
*/
public class EPattern {
ETermPattern matcher;
private final EObject spec;
private Integer[] out_vars;
private final int keyidx;
@Override
public String toString() {
return "#Pattern<keyidx=" + keyidx + ";" + spec.toString() + ">";
}
/**
* @param spec
*/
public EPattern(int keyidx, EObject spec) {
this.keyidx = keyidx;
this.spec = spec;
SortedSet<Integer> out = new TreeSet<Integer>();
matcher = spec.compileMatch(out);
this.out_vars = out.toArray(new Integer[out.size()]);
}
ESeq match_members(ESeq out, Collection<ETuple> in) {
for (EObject elm : in) {
EMatchContext res = new EMatchContext(out_vars, elm);
if (elm.match(matcher, res)) {
out = out.cons(elm);
}
}
return out;
}
ESeq match(ESeq out, Map<EObject, ETuple> in) {
for (ETuple elm : in.values()) {
ETuple val = elm.testTuple();
EMatchContext res = new EMatchContext(out_vars, val);
if (matcher.match(val, res)) {
out = out.cons(res.makeList());
}
}
return out;
}
/** return list of matching members */
ESeq match_members(ESeq out, Map<EObject, ETuple> in) {
for (ETuple elm : in.values()) {
EMatchContext res = new EMatchContext(out_vars, elm);
if (matcher.match(elm, res)) {
out = out.cons(elm);
}
}
return out;
}
/** return list of matching members */
ESeq match_members(ESeq out, ISeq in) {
while (in != null) {
ETuple elm = (ETuple) in.first();
if (elm == null)
break;
EMatchContext res = new EMatchContext(out_vars, elm);
if (matcher.match(elm, res)) {
out = out.cons(elm);
}
in = in.next();
}
return out;
}
/** return list of matching members' vars */
ESeq match_vars(ESeq out, ISeq in) {
while (in != null) {
ETuple elm = (ETuple) in.first();
if (elm == null)
break;
EMatchContext res = new EMatchContext(out_vars, elm);
if (matcher.match(elm, res)) {
out = out.cons(res.makeList());
}
in = in.next();
}
return out;
}
public static EPattern compile(int keyidx, ETuple spec) {
return new EPattern(keyidx, spec);
}
/** matcher for '_' */
static class AnyPattern extends ETermPattern {
public boolean match(ETuple t, EMatchContext r) {
return true;
}
public boolean match(ENumber n, EMatchContext r) {
return true;
}
public boolean match(EAtom a, EMatchContext r) {
return true;
}
public boolean match(ECons c, EMatchContext r) {
return true;
}
public boolean match(EPID p, EMatchContext r) {
return true;
}
public boolean match(EPort p, EMatchContext r) {
return true;
}
public boolean match(EBitString bs, EMatchContext r) {
return true;
}
}
/** matcher for '$N' */
static class VarPattern extends ETermPattern {
private Integer name;
private boolean free;
/**
* @param idx2
* @param out
*/
public VarPattern(int idx1, Set<Integer> out) {
this.name = idx1;
if (this.free = !out.contains(this.name)) {
out.add(this.name);
}
}
public boolean match(ETuple t, EMatchContext r) {
if (free) {
r.vars.put(name, t);
return true;
} else {
return t.equalsExactly(r.vars.get(name));
}
}
public boolean match(ENumber t, EMatchContext r) {
if (free) {
r.vars.put(name, t);
return true;
} else {
return t.equalsExactly(r.vars.get(name));
}
}
public boolean match(EAtom t, EMatchContext r) {
if (free) {
r.vars.put(name, t);
return true;
} else {
return t.equalsExactly(r.vars.get(name));
}
}
public boolean match(EFun fu, EMatchContext r) {
if (free) {
r.vars.put(name, fu);
return true;
} else {
return fu.equalsExactly(r.vars.get(name));
}
}
public boolean match(ERef fu, EMatchContext r) {
if (free) {
r.vars.put(name, fu);
return true;
} else {
return fu.equalsExactly(r.vars.get(name));
}
}
public boolean match(ECons t, EMatchContext r) {
if (free) {
r.vars.put(name, t);
return true;
} else {
return t.equalsExactly(r.vars.get(name));
}
}
public boolean match(EPID t, EMatchContext r) {
if (free) {
r.vars.put(name, t);
return true;
} else {
return t.equalsExactly(r.vars.get(name));
}
}
public boolean match(EPort t, EMatchContext r) {
if (free) {
r.vars.put(name, t);
return true;
} else {
return t.equalsExactly(r.vars.get(name));
}
}
public boolean match(EBitString t, EMatchContext r) {
if (free) {
r.vars.put(name, t);
return true;
} else {
return t.equalsExactly(r.vars.get(name));
}
}
}
static class TuplePattern extends ETermPattern {
int arity;
ETermPattern[] elems;
/**
* @param tup
* @param out
*/
public TuplePattern(ETuple tup, Set<Integer> out) {
arity = tup.arity();
elems = new ETermPattern[arity];
for (int idx1 = 1; idx1 <= arity; idx1++) {
elems[idx1 - 1] = tup.elm(idx1).compileMatch(out);
}
}
public boolean match(EObject elm, EMatchContext res) {
ETuple tup;
if ((tup = elm.testTuple()) == null)
return false;
return match(tup, res);
}
public boolean match(ETuple t, EMatchContext r) {
if (t.arity() != arity)
return false;
for (int idx1 = 1; idx1 < elems.length + 1; idx1++) {
if (!t.elm(idx1).match(elems[idx1 - 1], r))
return false;
}
return true;
}
}
static class NilPattern extends ETermPattern {
@Override
public boolean match(ECons c, EMatchContext r) {
return c.isNil();
}
}
static class ConsPattern extends ETermPattern {
ETermPattern head, tail;
/**
* @param cons
* @param out
*/
public ConsPattern(ECons cons, Set<Integer> out) {
head = cons.head().compileMatch(out);
tail = cons.tail().compileMatch(out);
}
public boolean match(ECons c, EMatchContext r) {
return (!c.isNil()) && c.head().match(head, r) && c.tail().match(tail, r);
}
}
/** matcher for all the non-recursive types */
static class ValuePattern extends ETermPattern {
final EObject value;
/**
* @param epid
*/
public ValuePattern(EObject val) {
this.value = val;
}
public boolean match(EPID pid, EMatchContext r) {
return pid.equalsExactly(value);
}
public boolean match(ERef ref, EMatchContext r) {
return ref.equalsExactly(value);
}
public boolean match(EPort port, EMatchContext r) {
return port.equalsExactly(value);
}
public boolean match(EAtom port, EMatchContext r) {
return port.equalsExactly(value);
}
public boolean match(EFun fu, EMatchContext r) {
return fu.equalsExactly(value);
}
public boolean match(EBitString port, EMatchContext r) {
return port.equalsExactly(value);
}
public boolean match(ENumber num, EMatchContext r) {
return num.equalsExactly(value);
}
public boolean match(ETuple t, EMatchContext r) {
return t.equalsExactly(value);
}
@Override
public boolean match(ECons c, EMatchContext r) {
return c.equalsExactly(value);
}
}
/**
* @param eTuple
* @param out
* @return
*/
public static ETermPattern compilePattern(ETuple tup, Set<Integer> out) {
TuplePattern tp = new TuplePattern(tup, out);
for (int i = 0; i < tp.elems.length; i++) {
if (!(tp.elems[i] instanceof ValuePattern)) {
return tp;
}
}
return new ValuePattern(tup);
}
/** generic compare-equals matcher */
public static ETermPattern compilePattern(EObject epid, Set<Integer> out) {
return new ValuePattern(epid);
}
/**
* @param eCons
* @param out
* @return
*/
public static ETermPattern compilePattern(ECons cons, Set<Integer> out) {
if (cons.isNil()) {
return new NilPattern();
} else {
return new ConsPattern(cons, out);
}
}
static Pattern VAR = Pattern.compile("^\\$[0-9]+$");
static EAtom am_ANY = EAtom.intern("_");
/**
* @param eAtom
* @param out
* @return
*/
public static ETermPattern compilePattern(EAtom am, Set<Integer> out) {
String name = am.getName();
if (VAR.matcher(name).matches()) {
int idx1 = Integer.parseInt(name.substring(1));
return new VarPattern(idx1, out);
} else if (am == am_ANY) {
return new AnyPattern();
} else {
return new ValuePattern(am);
}
}
/**
* @param keypos1
* @return
*/
public EObject getKey(int keypos1) {
if (matcher instanceof TuplePattern) {
TuplePattern tm = (TuplePattern) matcher;
if (keypos1 < 1 || keypos1 > tm.elems.length) {
return null;
}
ETermPattern m = tm.elems[keypos1 - 1];
if (m instanceof ValuePattern) {
ValuePattern vm = (ValuePattern) m;
return vm.value;
}
} else if (matcher instanceof ValuePattern) {
ValuePattern vp = (ValuePattern) matcher;
if (vp.value instanceof ETuple) {
ETuple et = (ETuple) vp.value;
if (keypos1 < 1 || keypos1 > et.arity())
return null;
return et.elm(keypos1);
}
}
return null;
}
/**
* @param res
* @param candidate
* @return
*/
ESeq match(ESeq out, ETuple val) {
EMatchContext res = new EMatchContext(out_vars, val);
if (matcher.match(val, res)) {
out = out.cons(res.makeList());
}
return out;
}
ESeq match_members(ESeq out, ETuple val) {
EMatchContext res = new EMatchContext(out_vars, val);
if (matcher.match(val, res)) {
out = out.cons(val);
}
return out;
}
boolean match(ETuple val) {
EMatchContext res = new EMatchContext(out_vars, val);
return matcher.match(val, res);
}
}