1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
|
#ifndef _RE2C_LIB_REGEX_IMPL_
#define _RE2C_LIB_REGEX_IMPL_
#include <stddef.h>
#include <string.h>
#include <map>
#include <vector>
#include <queue>
#include "regex.h"
#include "src/dfa/dfa.h"
#include "src/dfa/determinization.h"
#include "src/nfa/nfa.h"
#include "src/util/check.h"
namespace re2c {
namespace libre2c {
using tag_path_t = std::vector<tag_info_t>;
using conf_t = clos_t;
struct ran_or_fin_t {
inline bool operator()(const conf_t& c);
};
using confset_t = std::vector<conf_t>;
using confiter_t = confset_t::iterator;
using cconfiter_t = confset_t::const_iterator;
using rcconfiter_t = confset_t::const_reverse_iterator;
template<typename history_type_t>
struct simctx_t {
using conf_t = clos_t;
using confset_t = std::vector<conf_t>;
using confiter_t = confset_t::iterator;
using cconfiter_t = confset_t::const_iterator;
using rconfiter_t = confset_t::reverse_iterator;
using rcconfiter_t = confset_t::const_reverse_iterator;
using history_t = history_type_t;
const Tnfa& nfa;
const size_t nsub;
const int flags;
const std::vector<Tag>& tags;
history_t history;
int32_t hidx;
uint32_t step;
size_t rule;
const char* cursor;
const char* marker;
regoff_t* offsets1;
regoff_t* offsets2;
regoff_t* offsets3;
bool* done;
int32_t* newprectbl;
int32_t* oldprectbl;
size_t oldprecdim;
histleaf_t* histlevel;
std::vector<uint32_t> sortcores;
std::vector<uint32_t> fincount;
std::vector<int32_t> worklist;
std::vector<cconfiter_t> stateiters;
confset_t reach;
confset_t state;
std::vector<TnfaState*> gor1_topsort;
std::vector<TnfaState*> gor1_linear;
closure_stats_t clstats;
simctx_t(const Tnfa& nfa, size_t re_nsub, int flags);
~simctx_t();
FORBID_COPY(simctx_t);
};
// tag history for lazy disambiguation (both POSIX and leftmost greedy)
struct zhistory_t {
struct node_t {
tag_info_t info;
hidx_t pred;
uint32_t orig;
uint32_t step;
inline node_t(tag_info_t info, hidx_t pred, uint32_t orig, uint32_t step)
: info(info), pred(pred), orig(orig), step(step) {}
};
struct cache_entry_t {
int32_t prec1;
int32_t prec2;
int32_t prec;
};
using cache_t = std::map<uint64_t, cache_entry_t>;
std::vector<node_t> nodes;
cache_t cache;
inline zhistory_t(): nodes(), cache() { init(); }
inline void init();
inline node_t& node(hidx_t i) { return nodes[static_cast<uint32_t>(i)]; }
inline const node_t& node(hidx_t i) const { return nodes[static_cast<uint32_t>(i)]; }
template<typename ctx_t> inline hidx_t link(ctx_t& ctx, const typename ctx_t::conf_t& conf);
template<typename ctx_t> static int32_t precedence(ctx_t& ctx,
const typename ctx_t::conf_t& x,
const typename ctx_t::conf_t& y,
int32_t& prec1,
int32_t& prec2);
FORBID_COPY(zhistory_t);
};
using psimctx_t = simctx_t<phistory_t>;
using lsimctx_t = simctx_t<lhistory_t>;
using pzsimctx_t = simctx_t<zhistory_t>;
using lzsimctx_t = simctx_t<zhistory_t>;
// regexec functions
using regexec_t = int (const regex_t*, const char*, size_t, regmatch_t[], int);
regexec_t regexec_dfa;
template<typename ctx_t> regexec_t regexec_dfa_multipass;
regexec_t regexec_nfa_posix;
regexec_t regexec_nfa_posix_trie;
regexec_t regexec_nfa_leftmost;
regexec_t regexec_nfa_leftmost_trie;
// regparse functions (non-standard)
using regparse_t = subhistory_t* (const regex_t*, const char*, size_t);
regparse_t regparse_dfa;
template<typename ctx_t> regparse_t regparse_dfa_multipass;
// regtstring function (non-standard)
template<typename ctx_t>
const tstring_t* regtstring_dfa_multipass(const regex_t*, const char*);
template<typename history_t>
simctx_t<history_t>::simctx_t(const Tnfa& nfa, size_t re_nsub, int flags)
: nfa(nfa),
nsub(2 * (re_nsub - 1)),
flags(flags),
tags(nfa.tags),
history(),
hidx(HROOT),
step(0),
rule(Rule::NONE),
cursor(nullptr),
marker(nullptr),
offsets1(nullptr),
offsets2(nullptr),
offsets3(nullptr),
done(nullptr),
newprectbl(nullptr),
oldprectbl(nullptr),
oldprecdim(0),
histlevel(nullptr),
sortcores(),
fincount(),
worklist(),
stateiters(),
reach(),
state(),
gor1_topsort(),
gor1_linear(),
clstats() {
const size_t
ntags = nfa.tags.size(),
nstates = nfa.nstates,
ncores = nfa.ncores;
state.reserve(nstates);
reach.reserve(nstates);
done = new bool[ntags];
offsets3 = new regoff_t[ntags];
if (!(flags & REG_TRIE)) {
offsets1 = new regoff_t[ntags * ncores];
offsets2 = new regoff_t[ntags * ncores];
}
if (!(flags & REG_LEFTMOST) && !(flags & REG_TRIE)) {
const size_t dim = ncores;
newprectbl = new int32_t[ncores * dim];
oldprectbl = new int32_t[ncores * dim];
histlevel = new histleaf_t[ncores];
sortcores.reserve(ncores);
fincount.resize(ncores + 1);
worklist.reserve(nstates);
}
gor1_topsort.reserve(nstates);
gor1_linear.reserve(nstates);
}
template<typename history_t>
simctx_t<history_t>::~simctx_t() {
delete[] done;
delete[] offsets3;
if (!(flags & REG_TRIE)) {
delete[] offsets1;
delete[] offsets2;
}
if (!(flags & REG_LEFTMOST) && !(flags & REG_TRIE)) {
delete[] newprectbl;
delete[] oldprectbl;
delete[] histlevel;
}
}
template<typename history_t>
void init(simctx_t<history_t>& ctx, const char* string) {
ctx.reach.clear();
ctx.state.clear();
ctx.history.init();
ctx.hidx = HROOT;
ctx.step = 0;
ctx.rule = Rule::NONE;
ctx.cursor = ctx.marker = string;
ctx.sortcores.clear();
DCHECK(ctx.worklist.empty());
DCHECK(ctx.gor1_topsort.empty());
DCHECK(ctx.gor1_linear.empty());
}
static inline regoff_t* offs_addr(regmatch_t pmatch[], size_t t) {
regmatch_t* m = &pmatch[t / 2 + 1];
return t % 2 == 0 ? &m->rm_so : &m->rm_eo;
}
struct getoff_nfa_t {
const regoff_t* offsets;
inline regoff_t operator()(size_t idx) const {
return offsets[idx];
}
};
struct getoff_dfa_t {
const Tdfa* dfa;
const regoff_t* regs;
const regoff_t len;
regoff_t operator()(size_t idx) const {
const Tag& tag = dfa->tags[idx];
regoff_t off;
if (!fixed(tag)) {
off = regs[dfa->finvers[idx]];
} else {
off = tag.base == Tag::RIGHTMOST ? len : regs[dfa->finvers[tag.base]];
if (off != -1) {
off -= static_cast<regoff_t>(tag.dist);
}
}
return off;
}
};
template<typename getoff_t>
void tags_to_submatch(const std::vector<Tag>& tags,
size_t nmatch,
regmatch_t pmatch[],
regoff_t len,
const getoff_t& getoff) {
const size_t ntags = tags.size();
regmatch_t* m = pmatch, *e = pmatch + nmatch;
m->rm_so = 0;
m->rm_eo = len;
++m;
for (size_t t = 0; t < ntags && m < e; t += 2) {
const Tag& tag = tags[t];
if (!fictive(tag)) {
const regoff_t so = getoff(t);
const regoff_t eo = getoff(t + 1);
for (size_t j = tag.lsub; j <= tag.hsub && m < e; j += 2, ++m) {
DCHECK(m - 1 == &pmatch[j / 2]);
m->rm_so = so;
m->rm_eo = eo;
}
}
}
}
template<typename history_t>
int finalize(const simctx_t<history_t>& ctx,
const char* string,
size_t nmatch,
regmatch_t pmatch[]) {
if (ctx.rule == Rule::NONE) {
return REG_NOMATCH;
}
const std::vector<Tag>& tags = ctx.nfa.tags;
const size_t ntags = tags.size();
bool* done = ctx.done;
memset(done, 0, ntags * sizeof(bool));
for (int32_t i = ctx.hidx; i != HROOT; ) {
const typename history_t::node_t& n = ctx.history.node(i);
i = n.pred;
const size_t t = n.info.idx;
// If already updated, skip.
if (done[t]) continue;
if (!n.info.neg) {
// Update positive tag.
done[t] = true;
ctx.offsets3[t] = static_cast<regoff_t>(n.step);
} else {
// Update negative tag together with its sibling and nested tags (if any).
const Tag& tag = tags[t];
for (size_t l = tag.lnest; l < tag.hnest; ++l) {
if (!done[l]) {
done[l] = true;
ctx.offsets3[l] = -1;
}
}
}
}
const getoff_nfa_t fn = { ctx.offsets3 };
tags_to_submatch(tags, nmatch, pmatch, ctx.marker - string - 1, fn);
return 0;
}
template<typename history_t>
void update_offsets(simctx_t<history_t>& ctx, const conf_t& c, uint32_t id) {
regoff_t* o;
const std::vector<Tag>& tags = ctx.nfa.tags;
const size_t ntags = tags.size();
TnfaState* s = c.state;
bool* done = ctx.done;
if (s->kind == TnfaState::Kind::FIN) {
ctx.marker = ctx.cursor;
ctx.rule = 0;
o = ctx.offsets3;
} else {
o = ctx.offsets1 + id * ntags;
}
memcpy(o, ctx.offsets2 + c.origin * ntags, ntags * sizeof(regoff_t));
memset(done, 0, ntags * sizeof(bool));
for (int32_t i = c.thist; i != HROOT; ) {
const typename history_t::node_t& n = ctx.history.node(i);
i = n.pred;
const size_t t = n.info.idx;
// If already updated, skip.
if (done[t]) continue;
if (!n.info.neg) {
// Update positive tag.
done[t] = true;
o[t] = static_cast<regoff_t>(ctx.step);
} else {
// Update negative tag together with its sibling and nested tags (if any).
const Tag& tag = tags[t];
for (size_t l = tag.lnest; l < tag.hnest; ++l) {
if (!done[l]) {
done[l] = true;
o[l] = -1;
}
}
}
}
}
bool ran_or_fin_t::operator()(const conf_t& c) {
return c.state->kind == TnfaState::Kind::RAN
|| c.state->kind == TnfaState::Kind::FIN;
}
void zhistory_t::init() {
nodes.clear();
nodes.push_back(node_t(NOINFO, -1, 0, 0));
cache.clear();
}
template<typename ctx_t>
hidx_t zhistory_t::link(ctx_t& ctx, const typename ctx_t::conf_t& conf) {
const int32_t i = static_cast<int32_t>(nodes.size());
nodes.push_back(node_t(conf.state->tag, conf.thist, conf.origin, ctx.step));
return i;
}
} // namespace libre2c
} // namespace re2c
#endif // _RE2C_LIB_REGEX_IMPL_
|