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

Last change on this file was 6170, checked in by cameron, 13 months ago

RE Transformation names and printing

File size: 2.5 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#include <re/re_toolchain.h>
20
21using namespace llvm;
22
23namespace re {
24
25class ResolveCaseInsensitiveMode final : public RE_Transformer {
26public:
27    RE * transformCC(CC * cc) override;
28    RE * transformName(Name * name) override;
29    RE * transformGroup(Group * g) override;
30
31    ResolveCaseInsensitiveMode(const bool globallyCaseInsensitive) :
32       RE_Transformer("CaseInsensitize"), inCaseInsensitiveMode(globallyCaseInsensitive) { }
33
34private:
35    bool inCaseInsensitiveMode;
36};
37
38RE * ResolveCaseInsensitiveMode::transformCC(CC * cc) {
39    if (inCaseInsensitiveMode) {
40        return makeCC(caseInsensitize(*cc));
41    }
42    return cc;
43}
44
45RE * ResolveCaseInsensitiveMode::transformName(Name * name) {
46    if (!inCaseInsensitiveMode || (name->getDefinition() == nullptr)) {
47        return name;
48    }
49    Name * n = nullptr;
50    if (name->hasNamespace()) {
51        n = makeName(name->getNamespace(), name->getName() + "/i", name->getType());
52    } else {
53        n = makeName(name->getName() + "/i", name->getType());
54    }
55    n->setDefinition(transform(name->getDefinition()));
56    return n;
57}
58
59RE * ResolveCaseInsensitiveMode::transformGroup(Group * g) {
60    if (g->getMode() == Group::Mode::CaseInsensitiveMode) {
61        const auto wasInCaseInsensitiveMode = inCaseInsensitiveMode;
62        inCaseInsensitiveMode = g->getSense() == Group::Sense::On;
63        RE * const re = transform(g->getRE());
64        inCaseInsensitiveMode = wasInCaseInsensitiveMode;
65        return re;
66    } else {
67        return RE_Transformer::transformGroup(g);
68    }
69}
70
71
72RE * resolveCaseInsensitiveMode(RE * re, const bool globallyCaseInsensitive) {
73    ResolveCaseInsensitiveMode R(globallyCaseInsensitive);
74    return R.transformRE(re);
75}
76
77}
Note: See TracBrowser for help on using the repository browser.