1 | static new L<S> rules; |
2 | static new L<S> statements; |
3 | |
4 | // result of matchStatements ordered by variable |
5 | static MultiMap<S, S> matchAll(S pat) { |
6 | new MultiMap<S, S> map; |
7 | L<Map<S, S>> l = matchStatements(pat); |
8 | print("[" + l.size() + " matches for " + quote(pat) + "]"); |
9 | for (Map<S, S> m : l) { |
10 | for (Map.Entry<S, S> e : m.entrySet()) |
11 | map.put(e.getKey(), e.getValue()); |
12 | } |
13 | ret map; |
14 | } |
15 | |
16 | static void process() { |
17 | for (S rule : rules) { |
18 | new Matches m; |
19 | if (match3("* <=> *", rule, m)) { |
20 | S l = m.unq(0), r = m.unq(1); |
21 | rewrite(l, r); |
22 | rewrite(r, l); |
23 | } |
24 | } |
25 | } |
26 | |
27 | static void rewrite(S l, S r) { |
28 | for (Map<S, S> map : matchStatements(l)) |
29 | addStatement(convert(r, map)); |
30 | } |
31 | |
32 | static L<Map<S, S>> matchStatements(S pat) { |
33 | L<Map<S, S>> l = new ArrayList<Map<S, S>>(); |
34 | for (S s : statements) { |
35 | Map<S, S> m = match4(pat, s); |
36 | if (m != null) l.add(m); |
37 | } |
38 | ret l; |
39 | } |
40 | |
41 | static Map<S, S> match4(S pat, S s) { |
42 | new Map<S, S> m; |
43 | ret match4(pat, s, m) ? m : null; |
44 | } |
45 | |
46 | static boolean match4(S pat, S s, Map<S, S> m) { |
47 | L<S> tok1 = parse3(pat), tok2 = parse3(s); |
48 | if (tok1.size() != tok2.size()) ret false; |
49 | m.clear(); |
50 | for (int i = 1; i < tok1.size(); i += 2) { |
51 | S t1 = tok1.get(i), t2 = tok2.get(i); |
52 | if (isVar(t1)) |
53 | m.put(t1, t2); |
54 | else if (!tokEq(t1, t2)) |
55 | ret false; |
56 | } |
57 | ret true; |
58 | } |
59 | |
60 | static boolean tokEq(S a, S b) { |
61 | a = unquote(a); |
62 | b = unquote(b); |
63 | ret a.equalsIgnoreCase(b); |
64 | } |
65 | |
66 | static boolean isVar(S s) { |
67 | ret s.startsWith("$") && s.length() > 1; |
68 | } |
69 | |
70 | static S convert(S r, Map<S, S> matches) { |
71 | L<S> tok = javaTokPlusPeriod(r); |
72 | for (int i = 1; i < tok.size(); i += 2) { |
73 | if (tok.get(i).startsWith("$")) { |
74 | S m = matches.get(tok.get(i)); |
75 | if (m != null) |
76 | tok.set(i, m); |
77 | } |
78 | } |
79 | ret join(tok); |
80 | } |
81 | |
82 | static void addStatement(S s) { |
83 | for (S st : statements) |
84 | if (match3(st, s)) |
85 | ret; |
86 | statements.add(s); |
87 | } |
88 | |
89 | !include #2000515 // unquote |
90 | !include #1001497 // parseBoolean |
91 | !include #1000709 // formatSnippetID |
92 | |
93 | !include #1001296 // MultiMap |
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1001509 |
Snippet name: | Rules & statements (include) |
Eternal ID of this version: | #1001509/1 |
Text MD5: | 21b0c045d60bda846f5301b2ad5573b4 |
Author: | stefan |
Category: | |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-10-21 16:15:03 |
Source code size: | 2316 bytes / 93 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 524 / 1098 |
Referenced in: | [show references] |