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 Visitor base class for working with the component tree.
31 */
32
33#ifndef COMPONENTVISITOR_H
34#define COMPONENTVISITOR_H
35
36namespace ue2 {
37
38class AsciiComponentClass;
39class Component;
40class ComponentAlternation;
41class ComponentAssertion;
42class ComponentAtomicGroup;
43class ComponentBackReference;
44class ComponentBoundary;
45class ComponentByte;
46class ComponentClass;
47class ComponentCondReference;
48class ComponentEmpty;
49class ComponentEUS;
50class ComponentRepeat;
51class ComponentSequence;
52class ComponentWordBoundary;
53class UTF8ComponentClass;
54
55/**
56 * \brief Visitor base class for working with the component tree.
57 *
58 * Our approach to implementing the visitor pattern for traversing (and
59 * optionally mutating) the Component tree for a pattern. Each _visit_ function
60 * takes a Component subclass pointer in and returns a Component pointer. That
61 * pointer can have several values, dictating what the containing Component
62 * should do:
63 *
64 * 1. If ptr == c, then do nothing.
65 * 2. If ptr == nullptr, then remove c from the tree.
66 * 3. If ptr != c && ptr != nullptr, then replace c with ptr.
67 *
68 * Traversal order is pre-order.
69 *
70 * After a Component's subcomponents have been visited, the _post_ function for
71 * that Component will be called.
72 */
73class ComponentVisitor {
74public:
75 virtual ~ComponentVisitor();
76
77 virtual Component *visit(AsciiComponentClass *c) = 0;
78 virtual Component *visit(ComponentAlternation *c) = 0;
79 virtual Component *visit(ComponentAssertion *c) = 0;
80 virtual Component *visit(ComponentAtomicGroup *c) = 0;
81 virtual Component *visit(ComponentBackReference *c) = 0;
82 virtual Component *visit(ComponentBoundary *c) = 0;
83 virtual Component *visit(ComponentByte *c) = 0;
84 virtual Component *visit(ComponentCondReference *c) = 0;
85 virtual Component *visit(ComponentEmpty *c) = 0;
86 virtual Component *visit(ComponentEUS *c) = 0;
87 virtual Component *visit(ComponentRepeat *c) = 0;
88 virtual Component *visit(ComponentSequence *c) = 0;
89 virtual Component *visit(ComponentWordBoundary *c) = 0;
90 virtual Component *visit(UTF8ComponentClass *c) = 0;
91
92 virtual void post(AsciiComponentClass *c) = 0;
93 virtual void post(ComponentAlternation *c) = 0;
94 virtual void post(ComponentAssertion *c) = 0;
95 virtual void post(ComponentAtomicGroup *c) = 0;
96 virtual void post(ComponentBackReference *c) = 0;
97 virtual void post(ComponentBoundary *c) = 0;
98 virtual void post(ComponentByte *c) = 0;
99 virtual void post(ComponentCondReference *c) = 0;
100 virtual void post(ComponentEmpty *c) = 0;
101 virtual void post(ComponentEUS *c) = 0;
102 virtual void post(ComponentRepeat *c) = 0;
103 virtual void post(ComponentSequence *c) = 0;
104 virtual void post(ComponentWordBoundary *c) = 0;
105 virtual void post(UTF8ComponentClass *c) = 0;
106};
107
108/**
109 * \brief Concrete subclass of ComponentVisitor with default behaviour,
110 * allowing you to just implement the member functions you need.
111 */
112class DefaultComponentVisitor : public ComponentVisitor {
113public:
114 DefaultComponentVisitor();
115 ~DefaultComponentVisitor() override;
116
117 Component *visit(AsciiComponentClass *c) override;
118 Component *visit(ComponentAlternation *c) override;
119 Component *visit(ComponentAssertion *c) override;
120 Component *visit(ComponentAtomicGroup *c) override;
121 Component *visit(ComponentBackReference *c) override;
122 Component *visit(ComponentBoundary *c) override;
123 Component *visit(ComponentByte *c) override;
124 Component *visit(ComponentCondReference *c) override;
125 Component *visit(ComponentEmpty *c) override;
126 Component *visit(ComponentEUS *c) override;
127 Component *visit(ComponentRepeat *c) override;
128 Component *visit(ComponentSequence *c) override;
129 Component *visit(ComponentWordBoundary *c) override;
130 Component *visit(UTF8ComponentClass *c) override;
131
132 void post(AsciiComponentClass *c) override;
133 void post(ComponentAlternation *c) override;
134 void post(ComponentAssertion *c) override;
135 void post(ComponentAtomicGroup *c) override;
136 void post(ComponentBackReference *c) override;
137 void post(ComponentBoundary *c) override;
138 void post(ComponentByte *c) override;
139 void post(ComponentCondReference *c) override;
140 void post(ComponentEmpty *c) override;
141 void post(ComponentEUS *c) override;
142 void post(ComponentRepeat *c) override;
143 void post(ComponentSequence *c) override;
144 void post(ComponentWordBoundary *c) override;
145 void post(UTF8ComponentClass *c) override;
146};
147
148} // namespace ue2
149
150#endif // COMPONENTVISITOR_H
151