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

Last change on this file was 6256, checked in by cameron, 5 months ago

Fixes for contextual simplification

File size: 3.5 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 <UCD/resolve_properties.h>
19#include <cc/alphabet.h>
20#include <boost/container/flat_set.hpp>
21#include <llvm/Support/ErrorHandling.h>
22
23
24using namespace boost::container;
25using namespace llvm;
26
27namespace re {
28 
29class UnicodeNameResolver final : public RE_Transformer {
30public:
31    UnicodeNameResolver() : RE_Transformer("UnicodeNames") {}
32    RE * transformName(Name * name) override;
33    RE * transformRange(Range * rg) override;
34};
35   
36RE * UnicodeNameResolver::transformName(Name * name) {
37    if (LLVM_LIKELY(name->getDefinition() != nullptr)) {
38        name->setDefinition(transform(name->getDefinition()));
39    } else if (LLVM_LIKELY(name->getType() == Name::Type::UnicodeProperty || name->getType() == Name::Type::ZeroWidth)) {
40        if (UCD::resolvePropertyDefinition(name)) {
41            name->setDefinition(transform(name->getDefinition()));
42        } else {
43            name->setDefinition(makeCC(UCD::resolveUnicodeSet(name), &cc::Unicode));
44        }       
45    } else {
46        UndefinedNameError(name);
47    }
48    return name;
49}
50
51RE * getDefinitionOf(RE * const re) {
52    if (Name * const name = dyn_cast<Name>(re)) {
53        RE * const def = name->getDefinition();
54        if (LLVM_UNLIKELY(def == nullptr)) {
55            llvm::report_fatal_error(name->getFullName() + " could not be resolved.");
56        }
57        return def;
58    }
59    return re;
60}
61
62RE * UnicodeNameResolver::transformRange(Range * rg) {
63    RE * const x = getDefinitionOf(transform(rg->getLo()));
64    RE * const y = getDefinitionOf(transform(rg->getHi()));
65    return makeRange(x, y);
66}
67
68RE * resolveUnicodeNames(RE * re) {
69    return UnicodeNameResolver().transformRE(re);
70}
71
72 
73class AnchorResolution : public RE_Transformer {
74public:
75    AnchorResolution(RE * anchorRE);
76    RE * transformStart(Start * s) override;
77    RE * transformEnd(End * s) override;
78
79private:
80    RE * mAnchorRE;
81    bool mIsNegated;
82};
83 
84AnchorResolution::AnchorResolution(RE * breakRE)
85: RE_Transformer("Anchor Resolution") {
86    if (const CC * cc = dyn_cast<CC>(breakRE)) {
87        mIsNegated = true;
88        if (cc->getAlphabet() == &cc::Unicode) {
89            mAnchorRE = makeDiff(makeCC(0, 0x10FFFF), breakRE);
90        } else if (cc->getAlphabet() == &cc::Byte) {
91            mAnchorRE = makeDiff(makeByte(0, 0xFF), breakRE);
92        } else {
93            llvm::report_fatal_error("resolveAnchors: unexpected alphabet " + cc->getAlphabet()->getName());
94        }
95    } else {
96        mIsNegated = false;
97        mAnchorRE = breakRE;
98    }
99}
100
101RE * AnchorResolution::transformStart(Start * s) {
102    if (mIsNegated) return makeNegativeLookBehindAssertion(mAnchorRE);
103    RE * sot = makeNegativeLookBehindAssertion(makeByte(0x00,0xFF));
104    return makeAlt({sot, makeLookBehindAssertion(mAnchorRE)});
105}
106
107RE * AnchorResolution::transformEnd(End * e) {
108    if (mIsNegated) return makeNegativeLookAheadAssertion(mAnchorRE);
109    RE * eot = makeNegativeLookAheadAssertion(makeByte(0x00,0xFF));
110    return makeAlt({eot, makeLookAheadAssertion(mAnchorRE)});
111}
112
113RE * resolveAnchors(RE * r, RE * breakRE) {
114    return AnchorResolution(breakRE).transformRE(r);
115}
116                                                       
117}
Note: See TracBrowser for help on using the repository browser.