Changeset 6170


Ignore:
Timestamp:
Oct 2, 2018, 10:46:20 AM (3 months ago)
Author:
cameron
Message:

RE Transformation names and printing

Location:
icGREP/icgrep-devel/icgrep
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/UCD/CaseFolding.cpp

    r5782 r6170  
    1 /*
     1    /*
    22 *  Copyright (c) 2014 International Characters, Inc.
    33 *  This software is licensed to the public under the Open Software License 3.0.
  • icGREP/icgrep-devel/icgrep/re/casing.cpp

    r6163 r6170  
    1717#include <llvm/Support/Casting.h>  // for dyn_cast, isa
    1818#include <llvm/Support/ErrorHandling.h>
    19 #include <re/re_utility.h>
     19#include <re/re_toolchain.h>
    2020
    2121using namespace llvm;
     
    2929    RE * transformGroup(Group * g) override;
    3030
    31     ResolveCaseInsensitiveMode(const bool globallyCaseInsensitive) : inCaseInsensitiveMode(globallyCaseInsensitive) { }
     31    ResolveCaseInsensitiveMode(const bool globallyCaseInsensitive) :
     32       RE_Transformer("CaseInsensitize"), inCaseInsensitiveMode(globallyCaseInsensitive) { }
    3233
    3334private:
     
    7172RE * resolveCaseInsensitiveMode(RE * re, const bool globallyCaseInsensitive) {
    7273    ResolveCaseInsensitiveMode R(globallyCaseInsensitive);
    73     return R.transform(re);
     74    return R.transformRE(re);
    7475}
    7576
  • icGREP/icgrep-devel/icgrep/re/exclude_CC.cpp

    r6161 r6170  
    1919#include <re/re_intersect.h>
    2020#include <re/re_assertion.h>
    21 #include <re/re_utility.h>
     21#include <re/re_toolchain.h>
    2222#include <llvm/Support/Casting.h>
    2323#include <llvm/Support/ErrorHandling.h>
     
    2929class CC_Remover : public RE_Transformer {
    3030public:
    31     CC_Remover(CC * toExclude) : RE_Transformer(), mExcludedCC(toExclude) {}
     31    CC_Remover(CC * toExclude) : RE_Transformer("Exclude"), mExcludedCC(toExclude) {}
    3232    RE * transformCC (CC * cc) override;
    3333    RE * transformName (Name * name) override;
     
    6262   
    6363RE * exclude_CC(RE * re, CC * cc) {
    64     return CC_Remover(cc).transform(re);
     64    return CC_Remover(cc).transformRE(re);
    6565}
    6666}
  • icGREP/icgrep-devel/icgrep/re/printer_re.cpp

    r6169 r6170  
    4747        retVal += re_cc->canonicalName();
    4848        retVal += "\" ";
    49 
    50         for (const auto & i : *re_cc) {
    51             retVal += "[";
    52             retVal += std::to_string(lo_codepoint(i));
    53             if (hi_codepoint(i) != lo_codepoint(i))
    54                 retVal += "-" + std::to_string(hi_codepoint(i));
    55             retVal += "]";
    56         }
    57         retVal += "/" + re_cc->getAlphabet()->getName();
    5849    } else if (const Name* re_name = dyn_cast<const Name>(re)) {
    5950        retVal = "Name \"";
  • icGREP/icgrep-devel/icgrep/re/re_analysis.cpp

    r6160 r6170  
    501501    if (const CC * cc = dyn_cast<CC>(re)) {
    502502        if (cc->getAlphabet() == &cc::Unicode) {
    503             gatherTests(UTF8_Transformer().transform(re));
     503            gatherTests(UTF8_Transformer().transformRE(re));
    504504        } else {
    505505            for (const auto range : *cc) {
  • icGREP/icgrep-devel/icgrep/re/re_compiler.cpp

    r6160 r6170  
    129129        return makeMarker(FinalMatchUnit, pb.createAnd(nextPos, pb.createInFile(mCCCompiler.compileCC(cc, pb))));
    130130    } else if (a == &cc::Unicode) {
    131         MarkerType m = compile(UTF8_Transformer().transform(cc), pb);
     131        MarkerType m = compile(UTF8_Transformer().transformRE(cc), pb);
    132132        if (isByteLength(cc)) {
    133133            if (marker.pos == FinalMatchUnit) {
  • icGREP/icgrep-devel/icgrep/re/re_local.cpp

    r6160 r6170  
    1010#include <re/re_analysis.h>
    1111#include <re/re_nullable.h>
    12 #include <re/re_utility.h>
     12#include <re/re_toolchain.h>
    1313#include <boost/container/flat_map.hpp>
    1414#include <boost/range/adaptor/reversed.hpp>
  • icGREP/icgrep-devel/icgrep/re/re_multiplex.cpp

    r6167 r6170  
    1212#include <re/re_memoizer.hpp>
    1313#include <re/printer_re.h>
     14#include <re/re_toolchain.h>
    1415#include <UCD/ucd_compiler.hpp>
    1516#include <UCD/resolve_properties.h>
     
    3031class CC_multiplexer : public RE_Transformer {
    3132public:
    32     CC_multiplexer(cc::MultiplexedAlphabet * mpx) : RE_Transformer(), mMultiplexedAlphabet(mpx) {}
     33    CC_multiplexer(cc::MultiplexedAlphabet * mpx) :
     34        RE_Transformer("Multiplex_" + mpx->getName()), mMultiplexedAlphabet(mpx) {}
    3335    RE * transformCC(CC *) override;
    3436    RE * transformName(Name *) override;
     
    5860
    5961RE * transformCCs(cc::MultiplexedAlphabet * mpx, RE * re) {
    60     return CC_multiplexer(mpx).transform(re);
     62    return CC_multiplexer(mpx).transformRE(re);
    6163}
    6264
  • icGREP/icgrep-devel/icgrep/re/re_name_resolve.cpp

    r6161 r6170  
    1515#include <re/re_end.h>
    1616#include <re/re_any.h>
    17 #include <re/re_utility.h>
     17#include <re/re_toolchain.h>
    1818#include <re/re_memoizer.hpp>
    1919#include <UCD/resolve_properties.h>
     
    3030class UnicodeNameResolver : public RE_Transformer {
    3131public:
    32     UnicodeNameResolver() : RE_Transformer() {}
     32    UnicodeNameResolver() : RE_Transformer("UnicodeNames") {}
    3333    RE * transformName(Name * name) override;
    3434private:
     
    5757
    5858RE * resolveUnicodeNames(RE * re) {
    59     return UnicodeNameResolver().transform(re);
     59    return UnicodeNameResolver().transformRE(re);
    6060}
    6161
     
    100100
    101101RE * resolveAnchors(RE * r, RE * breakRE) {
    102     return AnchorResolution(breakRE).transform(r);
     102    return AnchorResolution(breakRE).transformRE(r);
    103103}
    104104                                                       
  • icGREP/icgrep-devel/icgrep/re/re_star_normal.h

    r6160 r6170  
    22#define RE_STAR_NORMAL_H
    33
    4 #include <re/re_utility.h>
     4#include <re/re_toolchain.h>
    55
    66namespace re {
     
    2020
    2121class RE_Star_Normal : public RE_Transformer {
    22    
    2322public:
     23    RE_Star_Normal() : RE_Transformer("StarNormal") {}
    2424    RE * transformRep(Rep * rep) override;
    2525};
  • icGREP/icgrep-devel/icgrep/re/re_toolchain.cpp

    r6161 r6170  
    88#include <grep_interface.h>
    99#include <re/re_toolchain.h>
     10#include <re/re_any.h>
     11#include <re/re_name.h>
     12#include <re/re_cc.h>
     13#include <re/re_start.h>
     14#include <re/re_end.h>
     15#include <re/re_alt.h>
     16#include <re/re_seq.h>
     17#include <re/re_diff.h>
     18#include <re/re_intersect.h>
     19#include <re/re_group.h>
     20#include <re/re_range.h>
     21#include <re/re_assertion.h>
    1022#include <cc/cc_compiler.h>            // for CC_Compiler
    1123#include <llvm/Support/CommandLine.h>  // for clEnumVal, clEnumValEnd, Optio...
     
    7284    }
    7385    r = re::resolveUnicodeNames(r);
    74     if (PrintOptions.isSet(ShowAllREs)) {
    75         errs() << "resolveUnicodeNames:\n" << Printer_RE::PrintRE(r) << '\n';
    76     }
    7786    r = resolveCaseInsensitiveMode(r, globallyCaseInsensitive);
    78     if (PrintOptions.isSet(ShowAllREs)) {
    79         errs() << "resolveCaseInsensitiveMode:\n" << Printer_RE::PrintRE(r) << '\n';
    80     }
    8187    return r;
    8288}
     
    101107        errs() << "RemoveNullableSuffix:\n" << Printer_RE::PrintRE(r) << '\n';
    102108    }
    103     r = RE_Star_Normal().transform(r);
    104     if (PrintOptions.isSet(ShowAllREs) || PrintOptions.isSet(ShowSimplifiedREs)) {
    105         //Print to the terminal the AST that was transformed to the star normal form.
    106         errs() << "Star_Normal_Form:\n" << Printer_RE::PrintRE(r) << '\n';
    107     }
     109    r = RE_Star_Normal().transformRE(r);
    108110    if (codegen::OptLevel > 1) {
    109111        r = RE_Minimizer::minimize(r);
     
    121123    return r;
    122124}
    123 
    124 }
     125RE * RE_Transformer::transformRE(RE * re) {
     126    RE * initialRE = re;
     127    RE * finalRE = transform(re);
     128    if (PrintOptions.isSet(ShowAllREs) && (initialRE != finalRE) && (mTransformationName != "")) {
     129        errs() << mTransformationName << ":\n" << Printer_RE::PrintRE(finalRE) << '\n';
     130    }
     131    return finalRE;
     132}
     133
     134RE * RE_Transformer::transform(RE * re) {
     135    if (llvm::isa<CC>(re)) return transformCC(llvm::cast<CC>(re));
     136    else if (llvm::isa<Start>(re)) return transformStart(llvm::cast<Start>(re));
     137    else if (llvm::isa<End>(re)) return transformEnd(llvm::cast<End>(re));
     138    else if (llvm::isa<Name>(re)) return transformName(llvm::cast<Name>(re));
     139    else if (llvm::isa<Seq>(re)) return transformSeq(llvm::cast<Seq>(re));
     140    else if (llvm::isa<Alt>(re)) return transformAlt(llvm::cast<Alt>(re));
     141    else if (llvm::isa<Rep>(re)) return transformRep(llvm::cast<Rep>(re));
     142    else if (llvm::isa<Intersect>(re)) return transformIntersect(llvm::cast<Intersect>(re));
     143    else if (llvm::isa<Diff>(re)) return transformDiff(llvm::cast<Diff>(re));
     144    else if (llvm::isa<Range>(re)) return transformRange(llvm::cast<Range>(re));
     145    else if (llvm::isa<Group>(re)) return transformGroup(llvm::cast<Group>(re));
     146    else if (llvm::isa<Assertion>(re)) return transformAssertion(llvm::cast<Assertion>(re));
     147    else {
     148        llvm_unreachable("Unknown RE type");
     149        return nullptr;
     150    }
     151}
     152
     153RE * RE_Transformer::transformName(Name * nm) {
     154    if (mNameTransform == NameTransformationMode::None) return nm;
     155    RE * d = nm->getDefinition();
     156    if (d) return transform(d);
     157    UndefinedNameError(nm);
     158    return nullptr;
     159}
     160
     161RE * RE_Transformer::transformCC(CC * cc) {
     162    return cc;
     163}
     164
     165RE * RE_Transformer::transformStart(Start * s) {
     166    return s;
     167}
     168
     169RE * RE_Transformer::transformEnd(End * e) {
     170    return e;
     171}
     172
     173RE * RE_Transformer::transformSeq(Seq * seq) {
     174    std::vector<RE *> elems;
     175    elems.reserve(seq->size());
     176    bool any_changed = false;
     177    for (RE * e : *seq) {
     178        RE * e1 = transform(e);
     179        if (e1 != e) any_changed = true;
     180        elems.push_back(e1);
     181    }
     182    if (!any_changed) return seq;
     183    return makeSeq(elems.begin(), elems.end());
     184}
     185
     186RE * RE_Transformer::transformAlt(Alt * alt) {
     187    std::vector<RE *> elems;
     188    elems.reserve(alt->size());
     189    bool any_changed = false;
     190    for (RE * e : *alt) {
     191        RE * e1 = transform(e);
     192        if (e1 != e) any_changed = true;
     193        elems.push_back(e1);
     194    }
     195    if (!any_changed) return alt;
     196    return makeAlt(elems.begin(), elems.end());
     197}
     198
     199RE * RE_Transformer::transformRep(Rep * r) {
     200    RE * x0 = r->getRE();
     201    RE * x = transform(x0);
     202    if (x == x0) {
     203        return r;
     204    } else {
     205        return makeRep(x, r->getLB(), r->getUB());
     206    }
     207}
     208
     209RE * RE_Transformer::transformIntersect(Intersect * ix) {
     210    RE * x0 = ix->getLH();
     211    RE * y0 = ix->getRH();
     212    RE * x = transform(x0);
     213    RE * y = transform(y0);
     214    if ((x == x0) && (y == y0)) {
     215        return ix;
     216    } else {
     217        return makeIntersect(x, y);
     218    }
     219}
     220
     221RE * RE_Transformer::transformDiff(Diff * d) {
     222    RE * x0 = d->getLH();
     223    RE * y0 = d->getRH();
     224    RE * x = transform(x0);
     225    RE * y = transform(y0);
     226    if ((x == x0) && (y == y0)) {
     227        return d;
     228    } else {
     229        return makeDiff(x, y);
     230    }
     231}
     232
     233RE * RE_Transformer::transformRange(Range * rg) {
     234    RE * x0 = rg->getLo();
     235    RE * y0 = rg->getHi();
     236    RE * x = transform(x0);
     237    RE * y = transform(y0);
     238    if ((x == x0) && (y == y0)) {
     239        return rg;
     240    } else {
     241        return makeRange(x, y);
     242    }
     243}
     244
     245RE * RE_Transformer::transformGroup(Group * g) {
     246    RE * x0 = g->getRE();
     247    RE * x = transform(x0);
     248    if (x == x0) {
     249        return g;
     250    } else {
     251        return makeGroup(g->getMode(), x, g->getSense());
     252    }
     253}
     254
     255RE * RE_Transformer::transformAssertion(Assertion * a) {
     256    RE * x0 = a->getAsserted();
     257    RE * x = transform(x0);
     258    if (x == x0) {
     259        return a;
     260    } else {
     261        return makeAssertion(x, a->getKind(), a->getSense());
     262    }
     263}
     264
     265}
  • icGREP/icgrep-devel/icgrep/re/re_toolchain.h

    r5945 r6170  
    77#ifndef RE_TOOLCHAIN_H
    88#define RE_TOOLCHAIN_H
     9#include <string>
    910#include <llvm/Support/Compiler.h>
    1011namespace llvm { namespace cl { class OptionCategory; } }
    1112namespace pablo { class PabloKernel; class PabloAST; }
    1213namespace re { class RE; class CC;}
     14namespace re { class Name; class Start; class End; class CC; class Seq; class Alt;
     15    class Rep; class Intersect; class Diff; class Range; class Group;
     16    class Assertion;
     17}
    1318
    1419namespace re {
     
    2833
    2934const llvm::cl::OptionCategory * LLVM_READONLY re_toolchain_flags();
     35   
     36   
     37void UndefinedNameError (const Name * n);
     38
     39enum class NameTransformationMode {None, TransformDefinition};
     40
     41class RE_Transformer {
     42public:
     43    RE_Transformer(std::string transformationName = "",
     44                   NameTransformationMode m = NameTransformationMode::None) :
     45    mTransformationName(transformationName), mNameTransform(m) {}
     46    RE * transformRE(RE * r);
     47protected:
     48    RE * transform(RE * r);
     49    virtual RE * transformName(Name * n);
     50    virtual RE * transformStart(Start * s);
     51    virtual RE * transformEnd(End * e);
     52    virtual RE * transformCC(CC * cc);
     53    virtual RE * transformSeq(Seq * s);
     54    virtual RE * transformAlt(Alt * a);
     55    virtual RE * transformRep(Rep * rep);
     56    virtual RE * transformIntersect(Intersect * e);
     57    virtual RE * transformDiff(Diff * d);
     58    virtual RE * transformRange(Range * rg);
     59    virtual RE * transformGroup(Group * g);
     60    virtual RE * transformAssertion(Assertion * a);
     61   
     62    std::string mTransformationName;
     63    NameTransformationMode mNameTransform;
     64};
    3065
    3166RE * resolveModesAndExternalSymbols(RE * r, bool globallyCaseInsensitive = false);
  • icGREP/icgrep-devel/icgrep/re/re_utility.cpp

    r6160 r6170  
    7272}
    7373   
    74    
    75 RE * RE_Transformer::transform(RE * re) {
    76     if (llvm::isa<CC>(re)) return transformCC(llvm::cast<CC>(re));
    77     else if (llvm::isa<Start>(re)) return transformStart(llvm::cast<Start>(re));
    78     else if (llvm::isa<End>(re)) return transformEnd(llvm::cast<End>(re));
    79     else if (llvm::isa<Name>(re)) return transformName(llvm::cast<Name>(re));
    80     else if (llvm::isa<Seq>(re)) return transformSeq(llvm::cast<Seq>(re));
    81     else if (llvm::isa<Alt>(re)) return transformAlt(llvm::cast<Alt>(re));
    82     else if (llvm::isa<Rep>(re)) return transformRep(llvm::cast<Rep>(re));
    83     else if (llvm::isa<Intersect>(re)) return transformIntersect(llvm::cast<Intersect>(re));
    84     else if (llvm::isa<Diff>(re)) return transformDiff(llvm::cast<Diff>(re));
    85     else if (llvm::isa<Range>(re)) return transformRange(llvm::cast<Range>(re));
    86     else if (llvm::isa<Group>(re)) return transformGroup(llvm::cast<Group>(re));
    87     else if (llvm::isa<Assertion>(re)) return transformAssertion(llvm::cast<Assertion>(re));
    88     else {
    89         llvm_unreachable("Unknown RE type");
    90         return nullptr;
    91     }
    9274}
    93    
    94 RE * RE_Transformer::transformName(Name * nm) {
    95     if (mNameTransform == NameTransformationMode::None) return nm;
    96     RE * d = nm->getDefinition();
    97     if (d) return transform(d);
    98     UndefinedNameError(nm);
    99     return nullptr;
    100 }
    101  
    102 RE * RE_Transformer::transformCC(CC * cc) {
    103     return cc;
    104 }
    105 
    106 RE * RE_Transformer::transformStart(Start * s) {
    107     return s;
    108 }
    109 
    110 RE * RE_Transformer::transformEnd(End * e) {
    111     return e;
    112 }
    113 
    114 RE * RE_Transformer::transformSeq(Seq * seq) {
    115     std::vector<RE *> elems;
    116     elems.reserve(seq->size());
    117     bool any_changed = false;
    118     for (RE * e : *seq) {
    119         RE * e1 = transform(e);
    120         if (e1 != e) any_changed = true;
    121         elems.push_back(e1);
    122     }
    123     if (!any_changed) return seq;
    124     return makeSeq(elems.begin(), elems.end());
    125 }
    126 
    127 RE * RE_Transformer::transformAlt(Alt * alt) {
    128     std::vector<RE *> elems;
    129     elems.reserve(alt->size());
    130     bool any_changed = false;
    131     for (RE * e : *alt) {
    132         RE * e1 = transform(e);
    133         if (e1 != e) any_changed = true;
    134         elems.push_back(e1);
    135     }
    136     if (!any_changed) return alt;
    137     return makeAlt(elems.begin(), elems.end());
    138 }
    139    
    140 RE * RE_Transformer::transformRep(Rep * r) {
    141     RE * x0 = r->getRE();
    142     RE * x = transform(x0);
    143     if (x == x0) {
    144         return r;
    145     } else {
    146         return makeRep(x, r->getLB(), r->getUB());
    147     }
    148 }
    149 
    150 RE * RE_Transformer::transformIntersect(Intersect * ix) {
    151     RE * x0 = ix->getLH();
    152     RE * y0 = ix->getRH();
    153     RE * x = transform(x0);
    154     RE * y = transform(y0);
    155     if ((x == x0) && (y == y0)) {
    156         return ix;
    157     } else {
    158         return makeIntersect(x, y);
    159     }
    160 }
    161 
    162 RE * RE_Transformer::transformDiff(Diff * d) {
    163     RE * x0 = d->getLH();
    164     RE * y0 = d->getRH();
    165     RE * x = transform(x0);
    166     RE * y = transform(y0);
    167     if ((x == x0) && (y == y0)) {
    168         return d;
    169     } else {
    170         return makeDiff(x, y);
    171     }
    172 }
    173 
    174 RE * RE_Transformer::transformRange(Range * rg) {
    175     RE * x0 = rg->getLo();
    176     RE * y0 = rg->getHi();
    177     RE * x = transform(x0);
    178     RE * y = transform(y0);
    179     if ((x == x0) && (y == y0)) {
    180         return rg;
    181     } else {
    182         return makeRange(x, y);
    183     }
    184 }
    185 
    186 RE * RE_Transformer::transformGroup(Group * g) {
    187     RE * x0 = g->getRE();
    188     RE * x = transform(x0);
    189     if (x == x0) {
    190         return g;
    191     } else {
    192         return makeGroup(g->getMode(), x, g->getSense());
    193     }
    194 }
    195 
    196 RE * RE_Transformer::transformAssertion(Assertion * a) {
    197     RE * x0 = a->getAsserted();
    198     RE * x = transform(x0);
    199     if (x == x0) {
    200         return a;
    201     } else {
    202         return makeAssertion(x, a->getKind(), a->getSense());
    203     }
    204 }
    205 
    206 }
  • icGREP/icgrep-devel/icgrep/re/re_utility.h

    r6160 r6170  
    88#define RE_UTILITY_H
    99
    10 namespace re { class RE; }
    11 namespace re { class Name; class Start; class End; class CC; class Seq; class Alt;
    12                class Rep; class Intersect; class Diff; class Range; class Group;
    13                class Assertion;
    14 }
     10namespace re { class RE; class Name;}
    1511
    1612namespace re {
     
    2723RE * makeUnicodeBreak();
    2824
    29 void UndefinedNameError (const Name * n);
    30 
    31 enum class NameTransformationMode {None, TransformDefinition};
    32 
    33 class RE_Transformer {
    34 public:
    35     RE_Transformer(NameTransformationMode m = NameTransformationMode::None) : mNameTransform(m) {}
    36     RE * transform(RE * r);
    37     virtual RE * transformName(Name * n);
    38     virtual RE * transformStart(Start * s);
    39     virtual RE * transformEnd(End * e);
    40     virtual RE * transformCC(CC * cc);
    41     virtual RE * transformSeq(Seq * s);
    42     virtual RE * transformAlt(Alt * a);
    43     virtual RE * transformRep(Rep * rep);
    44     virtual RE * transformIntersect(Intersect * e);
    45     virtual RE * transformDiff(Diff * d);
    46     virtual RE * transformRange(Range * rg);
    47     virtual RE * transformGroup(Group * g);
    48     virtual RE * transformAssertion(Assertion * a);
    49 protected:
    50     NameTransformationMode mNameTransform;
    51 };
    52 
    5325}
    5426#endif // RE_UTILITY_H
  • icGREP/icgrep-devel/icgrep/re/to_utf8.h

    r6160 r6170  
    99
    1010#include <re/re_re.h>
    11 #include <re/re_utility.h>
     11#include <re/re_toolchain.h>
    1212
    1313namespace re {
     
    1616class UTF8_Transformer : public RE_Transformer {
    1717public:
    18     UTF8_Transformer(NameTransformationMode m = NameTransformationMode::None) : RE_Transformer(m) {}
     18    UTF8_Transformer(NameTransformationMode m = NameTransformationMode::None) : RE_Transformer("ToUTF8", m) {}
    1919    RE * transformCC(CC * cc) override;
    2020};
    2121
    2222inline RE * toUTF8(RE * r, bool convertName = false) {
    23     return UTF8_Transformer(convertName ? NameTransformationMode::TransformDefinition : NameTransformationMode::None ).transform(r);}
     23    return UTF8_Transformer(convertName ? NameTransformationMode::TransformDefinition : NameTransformationMode::None ).transformRE(r);}
    2424}
    2525#endif // TO_UTF8_H
Note: See TracChangeset for help on using the changeset viewer.