| 1 |
/* |
| 2 |
* Copyright (c) 2021-2025 Symas Corporation |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions are |
| 6 |
* met: |
| 7 |
* |
| 8 |
* * Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* * Redistributions in binary form must reproduce the above |
| 11 |
* copyright notice, this list of conditions and the following disclaimer |
| 12 |
* in the documentation and/or other materials provided with the |
| 13 |
* distribution. |
| 14 |
* * Neither the name of the Symas Corporation nor the names of its |
| 15 |
* contributors may be used to endorse or promote products derived from |
| 16 |
* this software without specific prior written permission. |
| 17 |
* |
| 18 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
*/ |
| 30 |
#ifndef INSPECT_H |
| 31 |
#define INSPECT_H |
| 32 |
#include <algorithm> |
| 33 |
#include <cstddef> |
| 34 |
#include <cstring> |
| 35 |
#include <cstdio> |
| 36 |
|
| 37 |
/* |
| 38 |
* INSPECT has 3 repeating elements: |
| 39 |
* |
| 40 |
* 1. cbl_inspect_t |
| 41 |
* Tally (identifier-2). parser_inspect takes N of these. |
| 42 |
* Because REPLACING has no such loop, N == 1 for REPLACING. |
| 43 |
* |
| 44 |
* 2. cbl_inspect_oper_t |
| 45 |
* The CHARACTERS/ALL/LEADING/FIRST phrase (type of match) |
| 46 |
* Has N match/replace operands (or both) |
| 47 |
* |
| 48 |
* 3. cbl_inspect_match_t and cbl_inspect_replace_t |
| 49 |
* The CHARACTERS/ALL/LEADING/FIRST operands |
| 50 |
* Has N tuples of identifier-3 + [BEFORE and/or AFTER] |
| 51 |
*/ |
| 52 |
|
| 53 |
static inline bool |
| 54 |
is_active( const cbl_refer_t& refer ) { return NULL != refer.field; } |
| 55 |
|
| 56 |
struct cbl_inspect_qual_t { |
| 57 |
bool initial; |
| 58 |
cbl_refer_t identifier_4; |
| 59 |
|
| 60 |
cbl_inspect_qual_t() : initial(false), identifier_4(cbl_refer_t()) {} |
| 61 |
cbl_inspect_qual_t( bool initial, const cbl_refer_t& identifier_4 ) |
| 62 |
: initial(initial), identifier_4(identifier_4) |
| 63 |
{} |
| 64 |
cbl_inspect_qual_t( const cbl_inspect_qual_t& that ) |
| 65 |
: initial(that.initial) |
| 66 |
, identifier_4(that.identifier_4) |
| 67 |
{} |
| 68 |
|
| 69 |
cbl_inspect_qual_t& operator=( const cbl_inspect_qual_t& that ) { |
| 70 |
initial = that.initial; |
| 71 |
identifier_4 = that.identifier_4; |
| 72 |
return *this; |
| 73 |
} |
| 74 |
|
| 75 |
bool active() const { return is_active(identifier_4); } |
| 76 |
}; |
| 77 |
|
| 78 |
/* |
| 79 |
* Data for INSPECT X TALLYING Y FOR. Captures information for operands of |
| 80 |
* CHARACTERS/ALL/LEADING. The CHARACTERS/ALL/LEADING control is kept at the |
| 81 |
* next higher level, and may be repeated for each tally. |
| 82 |
* |
| 83 |
* cbl_inspect_match_t::matching is not used with CHARACTERS |
| 84 |
*/ |
| 85 |
|
| 86 |
class cbl_inspect_match_t { |
| 87 |
friend void dump_inspect_match( const cbl_inspect_match_t& M ); |
| 88 |
cbl_refer_t match; // identifier-3/5 or literal-1/3 |
| 89 |
cbl_refer_t tally; // collected too soon, belongs to next phrase |
| 90 |
public: |
| 91 |
cbl_inspect_qual_t before, after; // phrase 1 |
| 92 |
|
| 93 |
cbl_inspect_match_t() {} |
| 94 |
explicit |
| 95 |
cbl_inspect_match_t( const cbl_refer_t& matching, |
| 96 |
const cbl_inspect_qual_t& before = cbl_inspect_qual_t(), |
| 97 |
const cbl_inspect_qual_t& after = cbl_inspect_qual_t() ) |
| 98 |
: match(matching) |
| 99 |
, before(before) |
| 100 |
, after(after) |
| 101 |
{} |
| 102 |
// match all characters |
| 103 |
bool match_any() const { return !(before.active() || after.active()); } |
| 104 |
|
| 105 |
void save_premature_tally( const cbl_refer_t& tally ) { |
| 106 |
this->tally = tally; // put it here temporarily |
| 107 |
} |
| 108 |
cbl_refer_t premature_tally() { |
| 109 |
if( !tally.field ) { std::swap(match, tally); } |
| 110 |
return tally; |
| 111 |
} |
| 112 |
|
| 113 |
const cbl_refer_t& matching( const cbl_refer_t& match ) { |
| 114 |
return this->match = match; |
| 115 |
} |
| 116 |
const cbl_refer_t& matching() const { return match; } |
| 117 |
|
| 118 |
bool empty() const { |
| 119 |
return !is_active(match) && !before.active() && !after.active(); |
| 120 |
} |
| 121 |
}; |
| 122 |
|
| 123 |
/* |
| 124 |
* Data for INSPECT X REPLACING. The CHARACTERS/ALL/LEADING/FIRST control is |
| 125 |
* kept at the next higher level, and may be repeated. |
| 126 |
*/ |
| 127 |
struct cbl_inspect_replace_t : public cbl_inspect_match_t { |
| 128 |
cbl_refer_t replacement; |
| 129 |
|
| 130 |
cbl_inspect_replace_t() {} |
| 131 |
cbl_inspect_replace_t( const cbl_refer_t& matching, |
| 132 |
const cbl_refer_t& replacement, |
| 133 |
const cbl_inspect_qual_t& before, |
| 134 |
const cbl_inspect_qual_t& after ) |
| 135 |
: cbl_inspect_match_t(matching, before, after) |
| 136 |
, replacement(replacement) |
| 137 |
{} |
| 138 |
}; |
| 139 |
|
| 140 |
// One partial tally or substitution. |
| 141 |
struct cbl_inspect_oper_t { |
| 142 |
cbl_inspect_bound_t bound; // CHARACTERS/ALL/LEADING/FIRST |
| 143 |
// either tallies or replaces is empty |
| 144 |
std::vector<cbl_inspect_match_t> matches; |
| 145 |
std::vector<cbl_inspect_replace_t> replaces; |
| 146 |
|
| 147 |
cbl_inspect_oper_t() : bound(bound_characters_e) {} |
| 148 |
|
| 149 |
explicit cbl_inspect_oper_t( const cbl_inspect_match_t& match, |
| 150 |
cbl_inspect_bound_t bound = bound_characters_e ) |
| 151 |
: bound(bound) |
| 152 |
{ |
| 153 |
matches.push_back(match); |
| 154 |
} |
| 155 |
explicit cbl_inspect_oper_t( const cbl_inspect_replace_t& replace, |
| 156 |
cbl_inspect_bound_t bound = bound_characters_e ) |
| 157 |
: bound(bound) |
| 158 |
{ |
| 159 |
replaces.push_back(replace); |
| 160 |
} |
| 161 |
|
| 162 |
cbl_inspect_oper_t( cbl_inspect_bound_t bound, |
| 163 |
const std::vector<cbl_inspect_match_t>& matches ) |
| 164 |
: bound(bound) |
| 165 |
, matches(matches) |
| 166 |
{} |
| 167 |
|
| 168 |
cbl_inspect_oper_t( cbl_inspect_bound_t bound, |
| 169 |
const std::vector<cbl_inspect_replace_t>& replaces ) |
| 170 |
: bound(bound) |
| 171 |
, replaces(replaces) |
| 172 |
{} |
| 173 |
|
| 174 |
// N matches/replaces |
| 175 |
size_t n_identifier_3() const { |
| 176 |
return std::max( matches.size(), replaces.size() ); |
| 177 |
} |
| 178 |
|
| 179 |
bool is_valid() const { // only one or the other, never both |
| 180 |
bool invalid = !matches.empty() && !replaces.empty(); |
| 181 |
return ! invalid; |
| 182 |
} |
| 183 |
}; |
| 184 |
|
| 185 |
// One whole tally or substitution. For REPLACING, nbound == 1 |
| 186 |
// FOR and REPLACING start with a cbl_inspect_bound_t |
| 187 |
struct cbl_inspect_t : public std::vector<cbl_inspect_oper_t> { |
| 188 |
cbl_refer_t tally; // field is NULL for REPLACING |
| 189 |
cbl_inspect_t() {} |
| 190 |
cbl_inspect_t( size_t n, const cbl_inspect_oper_t& oper ) |
| 191 |
: std::vector<cbl_inspect_oper_t>(n, oper) |
| 192 |
{} |
| 193 |
cbl_inspect_t( const cbl_refer_t& tally, |
| 194 |
const std::vector<cbl_inspect_oper_t>& opers ) |
| 195 |
: std::vector<cbl_inspect_oper_t>(opers) |
| 196 |
, tally(tally) |
| 197 |
{} |
| 198 |
|
| 199 |
size_t nbound() const { return size(); } |
| 200 |
}; |
| 201 |
|
| 202 |
typedef std::vector<cbl_inspect_t> cbl_inspect_opers_t; |
| 203 |
|
| 204 |
/* |
| 205 |
* Runtime |
| 206 |
*/ |
| 207 |
|
| 208 |
void parser_inspect( const cbl_refer_t& input, |
| 209 |
bool backward, |
| 210 |
cbl_inspect_opers_t& inspects ); |
| 211 |
|
| 212 |
void parser_inspect_conv( cbl_refer_t input, bool backward, |
| 213 |
cbl_refer_t original, |
| 214 |
cbl_refer_t replacement, |
| 215 |
cbl_inspect_qual_t before = cbl_inspect_qual_t(), |
| 216 |
cbl_inspect_qual_t after = cbl_inspect_qual_t() ); |
| 217 |
|
| 218 |
#endif // INSPECT_H |