1 | /* |
2 | * Copyright (c) 2015-2017, 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 Glushkov construction. |
31 | */ |
32 | |
33 | #ifndef BUILDSTATE_H |
34 | #define BUILDSTATE_H |
35 | |
36 | #include "ue2common.h" |
37 | #include "position.h" |
38 | #include "util/noncopyable.h" |
39 | |
40 | #include <memory> |
41 | #include <vector> |
42 | |
43 | namespace ue2 { |
44 | |
45 | class NFABuilder; |
46 | class PositionInfo; |
47 | |
48 | /** \brief Machinery for Glushkov construction. |
49 | * |
50 | * Abstract base class; use \ref makeGlushkovBuildState to get one of these you |
51 | * can use. */ |
52 | class GlushkovBuildState : noncopyable { |
53 | public: |
54 | /** \brief Represents an uninitialized state. */ |
55 | static const Position POS_UNINITIALIZED; |
56 | |
57 | /** \brief Represents an epsilon transition in the firsts of a component. */ |
58 | static const Position POS_EPSILON; |
59 | |
60 | virtual ~GlushkovBuildState(); |
61 | |
62 | /** \brief Returns a reference to the NFABuilder being used. */ |
63 | virtual NFABuilder &getBuilder() = 0; |
64 | |
65 | /** \brief Returns a const reference to the NFABuilder being used. */ |
66 | virtual const NFABuilder &getBuilder() const = 0; |
67 | |
68 | /** \brief Wire up edges from the lasts of one component to the firsts of |
69 | * another. */ |
70 | virtual void connectRegions(const std::vector<PositionInfo> &lasts, |
71 | const std::vector<PositionInfo> &firsts) = 0; |
72 | |
73 | /** \brief Wire the lasts of the main sequence to accepts. */ |
74 | virtual void connectAccepts(const std::vector<PositionInfo> &lasts) = 0; |
75 | |
76 | /** \brief Wire up a pair of positions. */ |
77 | virtual void addSuccessor(Position from, Position to) = 0; |
78 | |
79 | /** \brief Clone the vertex properties and edges of all vertices between |
80 | * two positions. */ |
81 | virtual void cloneFollowSet(Position from, Position to, u32 offset) = 0; |
82 | |
83 | /** \brief Build the prioritised list of edges out of our successor map. */ |
84 | virtual void buildEdges() = 0; |
85 | }; |
86 | |
87 | /** \brief Returns a new GlushkovBuildState object. */ |
88 | std::unique_ptr<GlushkovBuildState> makeGlushkovBuildState(NFABuilder &b, |
89 | bool prefilter); |
90 | |
91 | /** \brief Replace all epsilons with the given positions. */ |
92 | void replaceEpsilons(std::vector<PositionInfo> &target, |
93 | const std::vector<PositionInfo> &source); |
94 | |
95 | /** \brief Eliminate lower-priority duplicate PositionInfo entries. |
96 | * |
97 | * Scans through a list of positions and retains only the highest priority |
98 | * version of a given (position, flags) entry. */ |
99 | void cleanupPositions(std::vector<PositionInfo> &a); |
100 | |
101 | } // namespace ue2 |
102 | |
103 | #endif |
104 | |