Changeset 6177 for icGREP


Ignore:
Timestamp:
Oct 24, 2018, 2:58:57 PM (4 months ago)
Author:
cameron
Message:

GraphemeModeTransformer?, PrintREs

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

Legend:

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

    r6173 r6177  
    1616#include <re/printer_re.h>
    1717#include <re/re_name_resolve.h>
     18#include <re/re_toolchain.h>
    1819
    1920#include <vector>                  // for vector, allocator
     
    7273}
    7374
    74 RE * resolveGraphemeMode(RE * re, bool inGraphemeMode) {
    75     if (isa<Name>(re)) {
    76         if (inGraphemeMode && (cast<Name>(re)->getName() == ".")) {
     75class GraphemeModeTransformer : public RE_Transformer {
     76public:
     77    GraphemeModeTransformer(bool inGraphemeMode = true) : RE_Transformer("ResolveGraphemeMode"), mGraphemeMode(inGraphemeMode) {}
     78   
     79    RE * transformName(Name * n) override {
     80        if (mGraphemeMode && (n->getName() == ".")) {
    7781            RE * GCB = makeZeroWidth("\\b{g}");
    7882            RE * nonGCB = makeDiff(makeSeq({}), GCB);
    7983            return makeSeq({makeAny(), makeRep(makeSeq({nonGCB, makeAny()}), 0, Rep::UNBOUNDED_REP), GCB});
    8084        }
    81         else return re;
     85        return n;
    8286    }
    83     else if (isa<CC>(re) || isa<Range>(re)) {
    84         if (inGraphemeMode) return makeSeq({re, makeZeroWidth("\\b{g}")});
    85         else return re;
     87   
     88    RE * transformCC(CC * cc) override {
     89        if (mGraphemeMode) return makeSeq({cc, makeZeroWidth("\\b{g}")});
     90        return cc;
    8691    }
    87     else if (Seq * seq = dyn_cast<Seq>(re)) {
     92   
     93    RE * transformRange(Range * rg) override {
     94        if (mGraphemeMode) return makeSeq({rg, makeZeroWidth("\\b{g}")});
     95        return rg;
     96    }
     97   
     98    RE * transformGroup(Group * g) override {
     99        if (g->getMode() == Group::Mode::GraphemeMode) {
     100            RE * r = g->getRE();
     101            bool modeSave = mGraphemeMode;
     102            mGraphemeMode = g->getSense() == Group::Sense::On;
     103            RE * t = transform(r);
     104            mGraphemeMode = modeSave;
     105            return t;
     106        } else {
     107            return RE_Transformer::transformGroup(g);
     108        }
     109    }
     110   
     111    RE * transformSeq(Seq * seq) override {
    88112        std::vector<RE*> list;
    89113        bool afterSingleChar = false;
     114        bool changed = false;
    90115        for (auto i = seq->begin(); i != seq->end(); ++i) {
    91             bool atSingleChar = isa<CC>(re) && (cast<CC>(re)->count() == 1);
    92             if (afterSingleChar && inGraphemeMode && !atSingleChar)
     116            bool atSingleChar = isa<CC>(*i) && (cast<CC>(*i)->count() == 1);
     117            if (afterSingleChar && mGraphemeMode && !atSingleChar) {
    93118                list.push_back(makeZeroWidth("\\b{g}"));
    94             if (isa<CC>(re)) list.push_back(*i);
    95             else {
    96                 list.push_back(resolveGraphemeMode(*i, inGraphemeMode));
     119                changed = true;
     120            }
     121            if (isa<CC>(*i)) {
     122                list.push_back(*i);
     123            } else {
     124                RE * t = transform(*i);
     125                if (*i != t) changed = true;
     126                list.push_back(t);
    97127            }
    98128            afterSingleChar = atSingleChar;
    99129        }
    100         if (afterSingleChar && inGraphemeMode) list.push_back(makeZeroWidth("\\b{g}"));
     130        if (afterSingleChar && mGraphemeMode) {
     131            list.push_back(makeZeroWidth("\\b{g}"));
     132            changed = true;
     133        }
     134        if (!changed) return seq;
    101135        return makeSeq(list.begin(), list.end());
    102     } else if (Group * g = dyn_cast<Group>(re)) {
    103         if (g->getMode() == Group::Mode::GraphemeMode) {
    104             return resolveGraphemeMode(g->getRE(), g->getSense() == Group::Sense::On);
    105         }
    106         else {
    107             return makeGroup(g->getMode(), resolveGraphemeMode(g->getRE(), inGraphemeMode), g->getSense());
    108         }
    109     } else if (Alt * alt = dyn_cast<Alt>(re)) {
    110         std::vector<RE*> list;
    111         for (auto i = alt->begin(); i != alt->end(); ++i) {
    112             list.push_back(resolveGraphemeMode(*i, inGraphemeMode));
    113         }
    114         return makeAlt(list.begin(), list.end());
    115     } else if (Rep * rep = dyn_cast<Rep>(re)) {
    116         return makeRep(resolveGraphemeMode(rep->getRE(), inGraphemeMode), rep->getLB(), rep->getUB());
    117     } else if (const Diff * diff = dyn_cast<const Diff>(re)) {
    118         return makeDiff(resolveGraphemeMode(diff->getLH(), inGraphemeMode),
    119                         resolveGraphemeMode(diff->getRH(), inGraphemeMode));
    120     } else if (const Intersect * e = dyn_cast<const Intersect>(re)) {
    121         return makeIntersect(resolveGraphemeMode(e->getLH(), inGraphemeMode),
    122                              resolveGraphemeMode(e->getRH(), inGraphemeMode));
    123     } else if (const Assertion * a = dyn_cast<Assertion>(re)) {
    124         return makeAssertion(resolveGraphemeMode(a->getAsserted(), inGraphemeMode), a->getKind(), a->getSense());
    125     } else if (isa<Start>(re) || isa<End>(re)) {
    126         return re;
    127     } else llvm_unreachable("Unknown RE type");
     136    }
     137
     138private:
     139    bool mGraphemeMode;
     140};
     141
     142RE * resolveGraphemeMode(RE * re, bool inGraphemeMode) {
     143    return GraphemeModeTransformer(inGraphemeMode).transformRE(re);
    128144}
    129 
    130145
    131146#define Behind(x) makeLookBehindAssertion(x)
  • icGREP/icgrep-devel/icgrep/re/re_toolchain.cpp

    r6176 r6177  
    5151
    5252static cl::bits<RE_PrintFlags>
    53     PrintOptions(cl::values(clEnumVal(ShowREs, "Print parsed or generated regular expressions"),
    54                             clEnumVal(ShowAllREs, "Print all regular expression passes"),
    55                             clEnumVal(ShowStrippedREs, "Print REs with nullable prefixes/suffixes removed"),
    56                             clEnumVal(ShowSimplifiedREs, "Print final simplified REs")
     53    PrintOptions(cl::values(clEnumVal(ShowREs, "Show parsed regular expressions and transformations that change them"),
     54                            clEnumVal(ShowAllREs, "Print all regular expression passes")
    5755                            CL_ENUM_VAL_SENTINEL), cl::cat(RegexOptions));
    5856
     
    8078    }
    8179    r = resolveGraphemeMode(r, false /* not in grapheme mode at top level*/);
    82     if (PrintOptions.isSet(ShowAllREs)) {
    83         errs() << "resolveGraphemeMode:\n" << Printer_RE::PrintRE(r) << '\n';
    84     }
    8580    r = re::resolveUnicodeNames(r);
    8681    r = resolveCaseInsensitiveMode(r, globallyCaseInsensitive);
     
    259254    RE * initialRE = re;
    260255    RE * finalRE = transform(re);
    261     if (PrintOptions.isSet(ShowAllREs) && (initialRE != finalRE) && (mTransformationName != "")) {
     256    if ((mTransformationName != "") && (PrintOptions.isSet(ShowAllREs) || (PrintOptions.isSet(ShowREs) && (initialRE != finalRE)))) {
    262257        errs() << mTransformationName << ":\n" << Printer_RE::PrintRE(finalRE) << '\n';
    263258    }
Note: See TracChangeset for help on using the changeset viewer.