source: icGREP/icgrep-devel/icgrep/re/casing.cpp @ 5775

Last change on this file since 5775 was 5770, checked in by cameron, 20 months ago

Restructure to eliminate unnecessary dependencies on RegExpCompiler? and UCDLIB

File size: 3.3 KB
Line 
1#include <re/casing.h>
2#include <re/re_cc.h>
3#include <UCD/unicode_set.h>
4#include <UCD/CaseFolding.h>
5#include <re/re_alt.h>             // for Alt, makeAlt
6#include <re/re_any.h>             // for makeAny, Any
7#include <re/re_assertion.h>       // for Assertion, Assertion::Sense, Asser...
8#include <re/re_start.h>
9#include <re/re_end.h>
10#include <re/re_diff.h>            // for Diff, makeDiff
11#include <re/re_group.h>
12#include <re/re_intersect.h>       // for Intersect
13#include <re/re_name.h>            // for Name
14#include <re/re_rep.h>             // for Rep, makeRep
15#include <re/re_seq.h>             // for Seq, makeSeq
16#include <vector>                  // for vector, allocator
17#include <llvm/Support/Casting.h>  // for dyn_cast, isa
18#include <llvm/Support/ErrorHandling.h>
19
20
21using namespace llvm;
22
23namespace re {
24RE * resolveCaseInsensitiveMode(RE * re, bool inCaseInsensitiveMode) {
25    if (isa<CC>(re)) {
26        if (inCaseInsensitiveMode) {
27            UCD::UnicodeSet * cased = caseInsensitize(cast<CC>(re));
28            return makeCC(std::move(*cased));
29        }
30        else return re;
31    }
32    else if (Name * name = dyn_cast<Name>(re)) {
33        if (!inCaseInsensitiveMode || (name->getDefinition() == nullptr)) return re;
34        RE * r = resolveCaseInsensitiveMode(name->getDefinition(), true);
35        Name * n = makeName(name->getNamespace(), name->getName() + "/i", name->getType());
36        n->setDefinition(r);
37        return n;
38    }
39    else if (Seq * seq = dyn_cast<Seq>(re)) {
40        std::vector<RE*> list;
41        for (auto i = seq->begin(); i != seq->end(); ++i) {
42            list.push_back(resolveCaseInsensitiveMode(*i, inCaseInsensitiveMode));
43        }
44        return makeSeq(list.begin(), list.end());
45    } else if (Group * g = dyn_cast<Group>(re)) {
46        if (g->getMode() == Group::Mode::CaseInsensitiveMode) {
47            return resolveCaseInsensitiveMode(g->getRE(), g->getSense() == Group::Sense::On);
48        }
49        else {
50            return makeGroup(g->getMode(), resolveCaseInsensitiveMode(g->getRE(), inCaseInsensitiveMode), g->getSense());
51        }
52    } else if (Alt * alt = dyn_cast<Alt>(re)) {
53        std::vector<RE*> list;
54        for (auto i = alt->begin(); i != alt->end(); ++i) {
55            list.push_back(resolveCaseInsensitiveMode(*i, inCaseInsensitiveMode));
56        }
57        return makeAlt(list.begin(), list.end());
58    } else if (Rep * rep = dyn_cast<Rep>(re)) {
59        return makeRep(resolveCaseInsensitiveMode(rep->getRE(), inCaseInsensitiveMode), rep->getLB(), rep->getUB());
60    } else if (const Diff * diff = dyn_cast<const Diff>(re)) {
61        return makeDiff(resolveCaseInsensitiveMode(diff->getLH(), inCaseInsensitiveMode),
62                        resolveCaseInsensitiveMode(diff->getRH(), inCaseInsensitiveMode));
63    } else if (const Intersect * e = dyn_cast<const Intersect>(re)) {
64        return makeIntersect(resolveCaseInsensitiveMode(e->getLH(), inCaseInsensitiveMode),
65                             resolveCaseInsensitiveMode(e->getRH(), inCaseInsensitiveMode));
66    } else if (const Assertion * a = dyn_cast<Assertion>(re)) {
67        return makeAssertion(resolveCaseInsensitiveMode(a->getAsserted(), inCaseInsensitiveMode), a->getKind(), a->getSense());
68    } else if (isa<Start>(re) || isa<End>(re)) {
69        return re;
70    } else llvm_unreachable("Unknown RE type");
71}
72
73}
Note: See TracBrowser for help on using the repository browser.