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 Lookahead/lookbehind zero-width assertions.
31  */
32 #include "ComponentAssertion.h"
33 #include "buildstate.h"
34 #include "position.h"
35 #include "position_info.h"
36 #include "ue2common.h"
37 
38 #include <cassert>
39 #include <algorithm>
40 
41 using namespace std;
42 
43 namespace ue2 {
44 
ComponentAssertion(enum Direction dir,enum Sense sense)45 ComponentAssertion::ComponentAssertion(enum Direction dir, enum Sense sense)
46     : m_dir(dir), m_sense(sense) {}
47 
~ComponentAssertion()48 ComponentAssertion::~ComponentAssertion() { }
49 
clone() const50 ComponentAssertion *ComponentAssertion::clone() const {
51     return new ComponentAssertion(*this);
52 }
53 
accept(ComponentVisitor & v)54 Component * ComponentAssertion::accept(ComponentVisitor &v) {
55     Component *c = v.visit(this);
56     if (c != this) {
57         v.post(this);
58         return c;
59     }
60 
61     for (auto i = children.begin(), e = children.end(); i != e; ++i) {
62         Component *child = i->get();
63         c = (*i)->accept(v);
64         if (c != child) {
65             // Child has been replaced (new Component pointer) or we've been
66             // instructed to delete it (null).
67             i->reset(c);
68         }
69     }
70 
71     // Remove deleted children.
72     children.erase(remove(children.begin(), children.end(), nullptr),
73                    children.end());
74 
75     v.post(this);
76     return this;
77 }
78 
accept(ConstComponentVisitor & v) const79 void ComponentAssertion::accept(ConstComponentVisitor &v) const {
80     v.pre(*this);
81     for (auto i = children.begin(), e = children.end(); i != e; ++i) {
82         (*i)->accept(v);
83         if (i + 1 != e) {
84             v.during(*this);
85         }
86     }
87 
88     v.post(*this);
89 }
90 
first() const91 vector<PositionInfo> ComponentAssertion::first() const {
92     assert(0);
93     return vector<PositionInfo>();
94 }
95 
last() const96 vector<PositionInfo> ComponentAssertion::last() const {
97     assert(0);
98     return vector<PositionInfo>();
99 }
100 
empty() const101 bool ComponentAssertion::empty() const {
102     return true;
103 }
104 
notePositions(GlushkovBuildState &)105 void ComponentAssertion::notePositions(GlushkovBuildState &) {
106     assert(0);
107 }
108 
buildFollowSet(GlushkovBuildState &,const vector<PositionInfo> &)109 void ComponentAssertion::buildFollowSet(GlushkovBuildState &,
110                                         const vector<PositionInfo> &) {
111     assert(0);
112 }
113 
repeatable() const114 bool ComponentAssertion::repeatable() const {
115     // If this assertion has no children (it's an empty sequence, like that
116     // produced by '(?!)') then PCRE would throw a "nothing to repeat" error.
117     // So we do as well.
118     return !children.empty();
119 }
120 
121 } // namespace ue2
122