Changeset 6163


Ignore:
Timestamp:
Sep 18, 2018, 9:07:49 PM (5 weeks ago)
Author:
nmedfort
Message:

updated casing function to use RE_Transformer

Location:
icGREP/icgrep-devel/icgrep/re
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/re/casing.cpp

    r5784 r6163  
    1717#include <llvm/Support/Casting.h>  // for dyn_cast, isa
    1818#include <llvm/Support/ErrorHandling.h>
    19 
     19#include <re/re_utility.h>
    2020
    2121using namespace llvm;
    2222
    2323namespace re {
    24 RE * resolveCaseInsensitiveMode(RE * re, const bool inCaseInsensitiveMode) {
    25     if (isa<CC>(re)) {
    26         if (inCaseInsensitiveMode) {
    27             return makeCC(caseInsensitize(*cast<CC>(re)));
    28         }
     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) : inCaseInsensitiveMode(globallyCaseInsensitive) { }
     32
     33private:
     34    bool inCaseInsensitiveMode;
     35};
     36
     37RE * ResolveCaseInsensitiveMode::transformCC(CC * cc) {
     38    if (inCaseInsensitiveMode) {
     39        return makeCC(caseInsensitize(*cc));
     40    }
     41    return cc;
     42}
     43
     44RE * ResolveCaseInsensitiveMode::transformName(Name * name) {
     45    if (!inCaseInsensitiveMode || (name->getDefinition() == nullptr)) {
     46        return name;
     47    }
     48    Name * n = nullptr;
     49    if (name->hasNamespace()) {
     50        n = makeName(name->getNamespace(), name->getName() + "/i", name->getType());
     51    } else {
     52        n = makeName(name->getName() + "/i", name->getType());
     53    }
     54    n->setDefinition(transform(name->getDefinition()));
     55    return n;
     56}
     57
     58RE * ResolveCaseInsensitiveMode::transformGroup(Group * g) {
     59    if (g->getMode() == Group::Mode::CaseInsensitiveMode) {
     60        const auto wasInCaseInsensitiveMode = inCaseInsensitiveMode;
     61        inCaseInsensitiveMode = g->getSense() == Group::Sense::On;
     62        RE * const re = transform(g->getRE());
     63        inCaseInsensitiveMode = wasInCaseInsensitiveMode;
    2964        return re;
    30     } else if (Name * name = dyn_cast<Name>(re)) {
    31         if (!inCaseInsensitiveMode || (name->getDefinition() == nullptr)) return re;
    32         RE * r = resolveCaseInsensitiveMode(name->getDefinition(), true);
    33         Name * n = nullptr;
    34         if (name->hasNamespace()) {
    35             n = makeName(name->getNamespace(), name->getName() + "/i", name->getType());
    36         } else {
    37             n = makeName(name->getName() + "/i", name->getType());
    38         }
    39         n->setDefinition(r);
    40         return n;
    41     } else if (Seq * seq = dyn_cast<Seq>(re)) {
    42         std::vector<RE*> list;
    43         for (auto i = seq->begin(); i != seq->end(); ++i) {
    44             list.push_back(resolveCaseInsensitiveMode(*i, inCaseInsensitiveMode));
    45         }
    46         return makeSeq(list.begin(), list.end());
    47     } else if (Group * g = dyn_cast<Group>(re)) {
    48         if (g->getMode() == Group::Mode::CaseInsensitiveMode) {
    49             return resolveCaseInsensitiveMode(g->getRE(), g->getSense() == Group::Sense::On);
    50         }
    51         else {
    52             return makeGroup(g->getMode(), resolveCaseInsensitiveMode(g->getRE(), inCaseInsensitiveMode), g->getSense());
    53         }
    54     } else if (Alt * alt = dyn_cast<Alt>(re)) {
    55         std::vector<RE*> list;
    56         for (auto i = alt->begin(); i != alt->end(); ++i) {
    57             list.push_back(resolveCaseInsensitiveMode(*i, inCaseInsensitiveMode));
    58         }
    59         return makeAlt(list.begin(), list.end());
    60     } else if (Rep * rep = dyn_cast<Rep>(re)) {
    61         return makeRep(resolveCaseInsensitiveMode(rep->getRE(), inCaseInsensitiveMode), rep->getLB(), rep->getUB());
    62     } else if (const Diff * diff = dyn_cast<const Diff>(re)) {
    63         return makeDiff(resolveCaseInsensitiveMode(diff->getLH(), inCaseInsensitiveMode),
    64                         resolveCaseInsensitiveMode(diff->getRH(), inCaseInsensitiveMode));
    65     } else if (const Intersect * e = dyn_cast<const Intersect>(re)) {
    66         return makeIntersect(resolveCaseInsensitiveMode(e->getLH(), inCaseInsensitiveMode),
    67                              resolveCaseInsensitiveMode(e->getRH(), inCaseInsensitiveMode));
    68     } else if (const Assertion * a = dyn_cast<Assertion>(re)) {
    69         return makeAssertion(resolveCaseInsensitiveMode(a->getAsserted(), inCaseInsensitiveMode), a->getKind(), a->getSense());
    70     } else if (isa<Start>(re) || isa<End>(re)) {
    71         return re;
    72     } else llvm_unreachable("Unknown RE type");
     65    } else {
     66        return RE_Transformer::transformGroup(g);
     67    }
     68}
     69
     70
     71RE * resolveCaseInsensitiveMode(RE * re, const bool globallyCaseInsensitive) {
     72    ResolveCaseInsensitiveMode R(globallyCaseInsensitive);
     73    return R.transform(re);
    7374}
    7475
  • icGREP/icgrep-devel/icgrep/re/casing.h

    r5782 r6163  
    66class RE;
    77
    8 RE * resolveCaseInsensitiveMode(RE * re, const bool inCaseInsensitiveMode);
     8RE * resolveCaseInsensitiveMode(RE * re, const bool globallyCaseInsensitive);
    99
    1010}
Note: See TracChangeset for help on using the changeset viewer.