| 1 | /* |
| 2 | * Copyright (c) 2015, Intel Corporation |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * * Redistributions of source code must retain the above copyright notice, |
| 8 | * this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * * Neither the name of Intel Corporation nor the names of its contributors |
| 13 | * may be used to endorse or promote products derived from this software |
| 14 | * without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | * POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | /** \file |
| 30 | * \brief Conditional reference. |
| 31 | */ |
| 32 | #include "ComponentCondReference.h" |
| 33 | #include "ComponentAlternation.h" |
| 34 | #include "ComponentAssertion.h" |
| 35 | #include "parse_error.h" |
| 36 | #include "position_info.h" |
| 37 | |
| 38 | #include <algorithm> |
| 39 | #include <cassert> |
| 40 | #include <memory> |
| 41 | |
| 42 | using namespace std; |
| 43 | |
| 44 | namespace ue2 { |
| 45 | |
| 46 | ComponentCondReference::ComponentCondReference(unsigned ref) |
| 47 | : kind(CONDITION_NUMBER), ref_id(ref), hasBothBranches(false) {} |
| 48 | |
| 49 | ComponentCondReference::ComponentCondReference(const string &name) |
| 50 | : kind(CONDITION_NAME), ref_id(0), ref_name(name), hasBothBranches(false) {} |
| 51 | |
| 52 | ComponentCondReference::ComponentCondReference(unique_ptr<Component> c) |
| 53 | : kind(CONDITION_ASSERTION), ref_id(0), assertion(move(c)), |
| 54 | hasBothBranches(false) {} |
| 55 | |
| 56 | ComponentCondReference::~ComponentCondReference() {} |
| 57 | |
| 58 | ComponentCondReference::ComponentCondReference( |
| 59 | const ComponentCondReference &other) |
| 60 | : ComponentSequence(other), kind(other.kind), ref_id(other.ref_id), |
| 61 | ref_name(other.ref_name), hasBothBranches(other.hasBothBranches) { |
| 62 | if (kind == CONDITION_ASSERTION) { |
| 63 | assert(other.assertion); |
| 64 | assertion.reset(other.assertion->clone()); |
| 65 | } else { |
| 66 | assert(!other.assertion); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | ComponentCondReference *ComponentCondReference::clone() const { |
| 71 | return new ComponentCondReference(*this); |
| 72 | } |
| 73 | |
| 74 | Component *ComponentCondReference::accept(ComponentVisitor &v) { |
| 75 | Component *c = v.visit(this); |
| 76 | if (c != this) { |
| 77 | v.post(this); |
| 78 | return c; |
| 79 | } |
| 80 | |
| 81 | if (kind == CONDITION_ASSERTION) { |
| 82 | Component *a = assertion.get(); |
| 83 | c = assertion->accept(v); |
| 84 | if (c != a) { |
| 85 | assertion.reset(c); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | for (auto i = children.begin(), e = children.end(); i != e; ++i) { |
| 90 | Component *child = i->get(); |
| 91 | c = (*i)->accept(v); |
| 92 | if (c != child) { |
| 93 | // Child has been replaced (new Component pointer) or we've been |
| 94 | // instructed to delete it (null). |
| 95 | i->reset(c); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Remove deleted children. |
| 100 | children.erase(remove(children.begin(), children.end(), nullptr), |
| 101 | children.end()); |
| 102 | |
| 103 | v.post(this); |
| 104 | return this; |
| 105 | } |
| 106 | |
| 107 | void ComponentCondReference::accept(ConstComponentVisitor &v) const { |
| 108 | v.pre(*this); |
| 109 | |
| 110 | if (kind == CONDITION_ASSERTION) { |
| 111 | assertion->accept(v); |
| 112 | v.during(*this); // FIXME: a good idea? |
| 113 | } |
| 114 | |
| 115 | for (auto i = children.begin(), e = children.end(); i != e; ++i) { |
| 116 | (*i)->accept(v); |
| 117 | if (i + 1 != e) { |
| 118 | v.during(*this); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | v.post(*this); |
| 123 | } |
| 124 | |
| 125 | void ComponentCondReference::addAlternation() { |
| 126 | if (alternation) { |
| 127 | if (ref_name == "DEFINE" ) { |
| 128 | throw LocatedParseError("DEFINE conditional group with more than " |
| 129 | "one branch" ); |
| 130 | } |
| 131 | |
| 132 | if (alternation->numBranches() >= 2) { |
| 133 | throw LocatedParseError("Conditional with more than two branches" ); |
| 134 | } |
| 135 | } |
| 136 | hasBothBranches = true; |
| 137 | ComponentSequence::addAlternation(); |
| 138 | } |
| 139 | |
| 140 | vector<PositionInfo> ComponentCondReference::first() const { |
| 141 | assert(0); |
| 142 | return vector<PositionInfo>(); |
| 143 | } |
| 144 | |
| 145 | vector<PositionInfo> ComponentCondReference::last() const { |
| 146 | assert(0); |
| 147 | return vector<PositionInfo>(); |
| 148 | } |
| 149 | |
| 150 | bool ComponentCondReference::empty() const { return true; } |
| 151 | |
| 152 | void ComponentCondReference::notePositions(GlushkovBuildState &) { assert(0); } |
| 153 | |
| 154 | void ComponentCondReference::buildFollowSet(GlushkovBuildState &, |
| 155 | const vector<PositionInfo> &) { |
| 156 | assert(0); |
| 157 | } |
| 158 | |
| 159 | bool ComponentCondReference::repeatable() const { |
| 160 | // If this assertion has no children (it's an empty sequence, like that |
| 161 | // produced by '(?!)') then PCRE would throw a "nothing to repeat" error. |
| 162 | // So we do as well. |
| 163 | return !children.empty(); |
| 164 | } |
| 165 | |
| 166 | } // namespace ue2 |
| 167 | |