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

Last change on this file was 6264, checked in by cameron, 9 months ago

Restructured contextual assertion simplifier

File size: 2.6 KB
Line 
1/*
2 *  Copyright (c) 2018 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
7#include "re_assertion.h"
8#include "re_cc.h"
9#include "re_start.h"
10#include "re_end.h"
11#include "re_any.h"
12#include "re_seq.h"
13#include "re_alt.h"
14#include "re_diff.h"
15#include "re_nullable.h"
16#include <re/re_toolchain.h>
17
18using namespace llvm;
19
20namespace re {
21   
22RE * makeSOT () {
23    //return makeNegativeLookBehindAssertion(makeByte(0x00,0xFF));
24    return makeStart();
25}
26
27RE * makeEOT () {
28    //return makeNegativeLookAheadAssertion(makeByte(0x00,0xFF));
29    return makeEnd();
30}
31
32RE * expandBoundaryAssertion (RE * re) {
33    if (Assertion * a = dyn_cast<Assertion>(re)) {
34        if (a->getKind() == Assertion::Kind::Boundary) {
35            RE * asserted = a->getAsserted();
36            RE * behindP = makeAssertion(asserted, Assertion::Kind::Lookbehind, Assertion::Sense::Positive);
37            RE * behindN = makeAssertion(asserted, Assertion::Kind::Lookbehind, Assertion::Sense::Negative);
38            RE * aheadP = makeAssertion(asserted, Assertion::Kind::Lookahead, Assertion::Sense::Positive);
39            RE * aheadN = makeAssertion(asserted, Assertion::Kind::Lookahead, Assertion::Sense::Negative);
40            if (a->getSense() == Assertion::Sense::Positive) {
41                return makeAlt({makeSeq({behindP, aheadN}), makeSeq({behindN, aheadP})});
42            } else {
43                return makeAlt({makeSeq({behindP, aheadP}), makeSeq({behindN, aheadN})});
44            }
45        }
46    }
47    return re;
48}
49   
50   
51struct FinalLookaheadPromotion : public RE_Transformer {
52    FinalLookaheadPromotion() : RE_Transformer("FinalLookaheadPromotion") {}
53    RE * transformSeq(Seq * s) override {
54        if (s->empty()) return s;
55        RE * s_last = s->back();
56        RE * t = transform(s_last);
57        if (s_last == t) return s;
58        std::vector<RE *> elems;
59        for (unsigned i = 0; i < s->size() - 1; i++) {
60            elems.push_back((*s)[i]);
61        }
62        elems.push_back(t);
63        return makeSeq(elems.begin(), elems.end());
64    }
65    RE * transformDiff(Diff * d) override { return d;}
66    RE * transformRep(Rep * r) override { return r;}
67    RE * transformAssertion(Assertion * a) override {
68        if ((a->getKind() == Assertion::Kind::Lookahead) && (a->getSense() == Assertion::Sense::Positive)) {
69            return transform(a->getAsserted());
70        }
71        return a;
72    }
73};
74   
75RE * lookaheadPromotion(RE * r) {
76    return FinalLookaheadPromotion().transformRE(r);
77}
78}
Note: See TracBrowser for help on using the repository browser.