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 Rose compile-time analysis for lookaround masks.
31 */
32#ifndef ROSE_ROSE_BUILD_LOOKAROUND_H
33#define ROSE_ROSE_BUILD_LOOKAROUND_H
34
35#include "rose_graph.h"
36#include "util/hash.h"
37
38#include <vector>
39
40/** \brief Max path number for multi-path lookaround. */
41#define MAX_LOOKAROUND_PATHS 8
42
43namespace ue2 {
44
45class CharReach;
46class RoseBuildImpl;
47
48/** \brief Lookaround entry prototype, describing the reachability at a given
49 * distance from the end of a role match. */
50struct LookEntry {
51 LookEntry() : offset(0) {}
52 LookEntry(s8 offset_in, const CharReach &reach_in)
53 : offset(offset_in), reach(reach_in) {}
54 s8 offset; //!< offset from role match location.
55 CharReach reach; //!< reachability at given offset.
56
57 bool operator==(const LookEntry &other) const {
58 return offset == other.offset && reach == other.reach;
59 }
60};
61
62void findLookaroundMasks(const RoseBuildImpl &tbi, const RoseVertex v,
63 std::vector<LookEntry> &look_more);
64
65/**
66 * \brief If possible, render the prefix of the given vertex as a lookaround.
67 *
68 * Given a prefix, returns true (and fills the lookaround vector) if
69 * it can be satisfied with a lookaround alone.
70 */
71bool makeLeftfixLookaround(const RoseBuildImpl &build, const RoseVertex v,
72 std::vector<std::vector<LookEntry>> &lookaround);
73
74void mergeLookaround(std::vector<LookEntry> &lookaround,
75 const std::vector<LookEntry> &more_lookaround);
76
77} // namespace ue2
78
79namespace std {
80
81template<>
82struct hash<ue2::LookEntry> {
83 size_t operator()(const ue2::LookEntry &l) const {
84 return ue2::hash_all(l.offset, l.reach);
85 }
86};
87
88} // namespace std
89
90#endif // ROSE_ROSE_BUILD_LOOKAROUND_H
91