Changeset 6208 for icGREP


Ignore:
Timestamp:
Nov 25, 2018, 10:03:30 AM (5 months ago)
Author:
cameron
Message:

Unneeded capture removal

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

Legend:

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

    r6185 r6208  
    6363}
    6464
     65using ReferenceSet = boost::container::flat_set<std::string>;
     66
     67struct ReferenceCollector  final : public RE_Inspector {
     68    ReferenceCollector(ReferenceSet & references)
     69    : RE_Inspector(InspectionMode::IgnoreNonUnique), mReferences(references) {}
     70   
     71    void inspectName(Name * n) final {
     72        if (n->getType() == Name::Type::Reference) {
     73            mReferences.insert(n->getName());
     74        }
     75    }
     76   
     77private:
     78    ReferenceSet & mReferences;
     79};
     80
     81struct UnneededCaptureRemoval final : public RE_Transformer {
     82    UnneededCaptureRemoval(ReferenceSet & references)
     83    : RE_Transformer("UnneededCaptureRemoval"), mReferences(references) {}
     84   
     85    RE * transformName(Name * n) final {
     86        if (n->getType() == Name::Type::Capture) {
     87            if (mReferences.count(n->getName()) == 0) {
     88                return n->getDefinition();
     89            }
     90        }
     91        return n;
     92    }
     93   
     94private:
     95    ReferenceSet & mReferences;
     96};
     97
     98RE * removeUnneededCaptures(RE * r) {
     99    ReferenceSet refs;
     100    ReferenceCollector(refs).inspectRE(r);
     101    return UnneededCaptureRemoval(refs).transformRE(r);
    65102}
     103}
  • icGREP/icgrep-devel/icgrep/re/re_simplifier.h

    r6185 r6208  
    1010RE * simplifyRE(RE * re);
    1111
     12RE * removeUnneededCaptures(RE * r);
     13
    1214}
    1315
  • icGREP/icgrep-devel/icgrep/re/re_toolchain.cpp

    r6195 r6208  
    8888        errs() << "Parser:\n" << Printer_RE::PrintRE(r) << '\n';
    8989    }
     90    r = removeUnneededCaptures(r);
    9091    r = resolveGraphemeMode(r, false /* not in grapheme mode at top level*/);
    9192    r = re::resolveUnicodeNames(r);
Note: See TracChangeset for help on using the changeset viewer.