1 | #include "re_multiplex.h" |
---|
2 | #include <re/re_name.h> |
---|
3 | #include <re/re_alt.h> |
---|
4 | #include <re/re_cc.h> |
---|
5 | #include <re/re_seq.h> |
---|
6 | #include <re/re_rep.h> |
---|
7 | #include <re/re_diff.h> |
---|
8 | #include <re/re_intersect.h> |
---|
9 | #include <re/re_assertion.h> |
---|
10 | #include <re/re_group.h> |
---|
11 | #include <re/re_analysis.h> |
---|
12 | #include <re/re_memoizer.hpp> |
---|
13 | #include <UCD/ucd_compiler.hpp> |
---|
14 | #include <UCD/resolve_properties.h> |
---|
15 | #include <boost/container/flat_set.hpp> |
---|
16 | #include <cc/multiplex_CCs.h> |
---|
17 | #include <sstream> |
---|
18 | #include <iostream> |
---|
19 | #include <functional> |
---|
20 | |
---|
21 | using namespace boost::container; |
---|
22 | using namespace llvm; |
---|
23 | |
---|
24 | namespace re { |
---|
25 | |
---|
26 | RE * multiplex(RE * const re, |
---|
27 | const std::vector<const CC *> & UnicodeSets, |
---|
28 | const std::vector<std::vector<unsigned>> & exclusiveSetIDs) { |
---|
29 | |
---|
30 | Memoizer memoizer; |
---|
31 | |
---|
32 | std::function<RE *(RE *)> multiplex = [&](RE * const re) -> RE * { |
---|
33 | if (CC * cc = dyn_cast<CC>(re)) { |
---|
34 | const auto index = find(UnicodeSets.begin(), UnicodeSets.end(), cc) - UnicodeSets.begin(); |
---|
35 | const auto exclusive_IDs = exclusiveSetIDs[index]; |
---|
36 | CC * CC_union = makeCC(); |
---|
37 | for (auto i : exclusive_IDs) { |
---|
38 | CC_union = makeCC(CC_union, makeCC(i)); |
---|
39 | } |
---|
40 | return CC_union; |
---|
41 | } else if (Name * name = dyn_cast<Name>(re)) { |
---|
42 | auto f = memoizer.find(name); |
---|
43 | if (f == memoizer.end()) { |
---|
44 | if (LLVM_LIKELY(name->getDefinition() != nullptr)) { |
---|
45 | if (CC * cc = dyn_cast<CC>(name->getDefinition())) { |
---|
46 | const auto index = find(UnicodeSets.begin(), UnicodeSets.end(), cc) - UnicodeSets.begin(); |
---|
47 | const auto exclusive_IDs = exclusiveSetIDs[index]; |
---|
48 | CC * CC_union = makeCC(); |
---|
49 | for (auto i : exclusive_IDs) { |
---|
50 | CC_union = makeCC(CC_union, makeCC(i)); |
---|
51 | } |
---|
52 | name->setDefinition(CC_union); |
---|
53 | } else { |
---|
54 | multiplex(name->getDefinition()); |
---|
55 | } |
---|
56 | } else { |
---|
57 | UndefinedNameError(name); |
---|
58 | } |
---|
59 | return memoizer.memoize(name); |
---|
60 | } else { |
---|
61 | return *f; |
---|
62 | } |
---|
63 | } else if (Seq * seq = dyn_cast<Seq>(re)) { |
---|
64 | for (auto si = seq->begin(); si != seq->end(); ++si) { |
---|
65 | *si = multiplex(*si); |
---|
66 | } |
---|
67 | } else if (Alt * alt = dyn_cast<Alt>(re)) { |
---|
68 | for (auto ai = alt->begin(); ai != alt->end(); ++ai) { |
---|
69 | *ai = multiplex(*ai); |
---|
70 | } |
---|
71 | } else if (Rep * rep = dyn_cast<Rep>(re)) { |
---|
72 | rep->setRE(multiplex(rep->getRE())); |
---|
73 | } else if (Assertion * a = dyn_cast<Assertion>(re)) { |
---|
74 | a->setAsserted(multiplex(a->getAsserted())); |
---|
75 | } else if (Diff * diff = dyn_cast<Diff>(re)) { |
---|
76 | diff->setLH(multiplex(diff->getLH())); |
---|
77 | diff->setRH(multiplex(diff->getRH())); |
---|
78 | } else if (Intersect * ix = dyn_cast<Intersect>(re)) { |
---|
79 | ix->setLH(multiplex(ix->getLH())); |
---|
80 | ix->setRH(multiplex(ix->getRH())); |
---|
81 | } else if (Group * g = dyn_cast<Group>(re)) { |
---|
82 | g->setRE(multiplex(g->getRE())); |
---|
83 | } |
---|
84 | return re; |
---|
85 | }; |
---|
86 | |
---|
87 | return multiplex(re); |
---|
88 | } |
---|
89 | |
---|
90 | } |
---|