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

Last change on this file was 5725, checked in by cameron, 3 weeks ago

Utility functions for regular expression transformations

File size: 2.7 KB
Line 
1/*
2 *  Copyright (c) 2017 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#ifndef RE_ASSERTION_H
8#define RE_ASSERTION_H
9
10#include <re/re_re.h>
11#include <re/re_nullable.h>
12#include <re/re_seq.h>
13
14namespace re {
15
16class Assertion : public RE {
17public:
18    static inline bool classof(const RE * re) {
19        return re->getClassTypeId() == ClassTypeId::Assertion;
20    }
21    static inline bool classof(const void *) {
22        return false;
23    }
24    enum class Kind {Lookbehind, Lookahead, Boundary};
25    enum class Sense {Positive, Negative};
26
27    RE * getAsserted() const {return mAsserted;}
28    Assertion::Kind getKind() const {return mKind;}
29    Assertion::Sense getSense() const {return mSense;}
30    void setAsserted(RE * r) {mAsserted = r;}
31
32    static Assertion::Kind reverseKind(Assertion::Kind k);
33    static Assertion::Sense negateSense(Assertion::Sense s);
34
35protected:
36    friend RE * makeAssertion(RE * asserted, Kind k, Sense s);
37    Assertion(RE * r, Kind k, Sense s) : RE(ClassTypeId::Assertion), mAsserted(r), mKind(k), mSense(s) {}
38    virtual ~Assertion() {}
39
40private:
41    RE * mAsserted;
42    Kind mKind;
43    Sense mSense;
44};
45
46inline Assertion::Kind Assertion::reverseKind(Assertion::Kind k) {
47    if (k == Assertion::Kind::Boundary) return k;
48    return k == Assertion::Kind::Lookahead ? Assertion::Kind::Lookbehind : Assertion::Kind::Lookahead;
49}
50
51inline Assertion::Sense Assertion::negateSense(Assertion::Sense s) {
52    return s == Assertion::Sense::Positive ? Assertion::Sense::Negative : Assertion::Sense::Positive;
53}
54
55inline RE * makeAssertion(RE * asserted, Assertion::Kind k, Assertion::Sense s) {
56    if (RE_Nullable::isNullable(asserted)) return makeSeq();
57    return new Assertion(asserted, k, s);
58}
59
60inline RE * makeLookAheadAssertion(RE * r) {
61    return makeAssertion(r, Assertion::Kind::Lookahead, Assertion::Sense::Positive);
62}
63
64inline RE * makeNegativeLookAheadAssertion(RE * r) {
65    return makeAssertion(r, Assertion::Kind::Lookahead, Assertion::Sense::Negative);
66}
67
68inline RE * makeLookBehindAssertion(RE * r) {
69    return makeAssertion(r, Assertion::Kind::Lookbehind, Assertion::Sense::Positive);
70}
71
72inline RE * makeNegativeLookBehindAssertion(RE * r) {
73    return makeAssertion(r, Assertion::Kind::Lookbehind, Assertion::Sense::Negative);
74}
75
76inline RE * makeBoundaryAssertion(RE * r) {
77    return makeAssertion(r, Assertion::Kind::Boundary, Assertion::Sense::Positive);
78}
79
80inline RE * makeNegativeBoundaryAssertion(RE * r) {
81    return makeAssertion(r, Assertion::Kind::Boundary, Assertion::Sense::Negative);
82}
83
84RE * expandBoundaryAssertion(RE * r);
85}
86
87#endif // RE_ASSERTION_H
88
Note: See TracBrowser for help on using the repository browser.