source: icGREP/icgrep-devel/icgrep/re/re_name_resolve.cpp @ 6170

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

RE Transformation names and printing

File size: 3.0 KB
Line 
1#include <re/re_re.h>
2#include "re_name_resolve.h"
3#include <re/re_name.h>
4#include <re/re_alt.h>
5#include <re/re_cc.h>
6#include <re/re_seq.h>
7#include <re/re_rep.h>
8#include <re/re_range.h>
9#include <re/re_diff.h>
10#include <re/re_intersect.h>
11#include <re/re_assertion.h>
12#include <re/re_analysis.h>
13#include <re/re_group.h>
14#include <re/re_start.h>
15#include <re/re_end.h>
16#include <re/re_any.h>
17#include <re/re_toolchain.h>
18#include <re/re_memoizer.hpp>
19#include <UCD/resolve_properties.h>
20#include <cc/alphabet.h>
21#include <boost/container/flat_set.hpp>
22#include <llvm/Support/ErrorHandling.h>
23
24
25using namespace boost::container;
26using namespace llvm;
27
28namespace re {
29 
30class UnicodeNameResolver : public RE_Transformer {
31public:
32    UnicodeNameResolver() : RE_Transformer("UnicodeNames") {}
33    RE * transformName(Name * name) override;
34private:
35    Memoizer mMemoizer;
36};
37   
38RE * UnicodeNameResolver::transformName(Name * name) {
39    auto f = mMemoizer.find(name);
40    if (f == mMemoizer.end()) {
41        if (LLVM_LIKELY(name->getDefinition() != nullptr)) {
42            name->setDefinition(transform(name->getDefinition()));
43        } else if (LLVM_LIKELY(name->getType() == Name::Type::UnicodeProperty || name->getType() == Name::Type::ZeroWidth)) {
44            if (UCD::resolvePropertyDefinition(name)) {
45                name->setDefinition(transform(name->getDefinition()));
46            } else {
47                name->setDefinition(makeCC(UCD::resolveUnicodeSet(name), &cc::Unicode));
48            }
49        } else {
50            UndefinedNameError(name);
51        }
52        return mMemoizer.memoize(name);
53    } else {
54        return *f;
55    }
56}
57
58RE * resolveUnicodeNames(RE * re) {
59    return UnicodeNameResolver().transformRE(re);
60}
61
62 
63class AnchorResolution : public RE_Transformer {
64public:
65    AnchorResolution(RE * anchorRE);
66    RE * transformStart(Start * s) override;
67    RE * transformEnd(End * s) override;
68
69private:
70    RE * mAnchorRE;
71    bool mIsNegated;
72};
73 
74AnchorResolution::AnchorResolution(RE * breakRE)
75: RE_Transformer() {
76    if (const CC * cc = dyn_cast<CC>(breakRE)) {
77        mIsNegated = true;
78        if (cc->getAlphabet() == &cc::Unicode) {
79            mAnchorRE = makeDiff(makeCC(0, 0x10FFFF), breakRE);
80        } else if (cc->getAlphabet() == &cc::Byte) {
81            mAnchorRE = makeDiff(makeByte(0, 0xFF), breakRE);
82        } else {
83            llvm::report_fatal_error("resolveAnchors: unexpected alphabet " + cc->getAlphabet()->getName());
84        }
85    } else {
86        mIsNegated = false;
87        mAnchorRE = breakRE;
88    }
89}
90
91RE * AnchorResolution::transformStart(Start * s) {
92    if (mIsNegated) return makeNegativeLookBehindAssertion(mAnchorRE);
93    return makeAlt({makeSOT(), makeLookBehindAssertion(mAnchorRE)});
94}
95
96RE * AnchorResolution::transformEnd(End * e) {
97    if (mIsNegated) return makeNegativeLookAheadAssertion(mAnchorRE);
98    return makeAlt({makeEOT(), makeLookAheadAssertion(mAnchorRE)});
99}
100
101RE * resolveAnchors(RE * r, RE * breakRE) {
102    return AnchorResolution(breakRE).transformRE(r);
103}
104                                                       
105}
Note: See TracBrowser for help on using the repository browser.