#include "ComponentRepeat.h"
#include "buildstate.h"
#include "nfagraph/ng_builder.h"
#include "parse_error.h"
#include "Parser.h"
#include "position.h"
#include "position_dump.h"
#include "position_info.h"
#include "ue2common.h"
#include "util/make_unique.h"
#include <algorithm>
#include <cassert>
using namespace std;
namespace ue2 {
static constexpr u32 MAX_REPEAT = 32767;
static constexpr u32 MAX_POSITIONS_EXPANDED = 500000;
ComponentRepeat::ComponentRepeat(unique_ptr<Component> sub_comp_in, u32 min,
u32 max, enum RepeatType t)
: type(t), sub_comp(move(sub_comp_in)), m_min(min), m_max(max),
posFirst(GlushkovBuildState::POS_UNINITIALIZED),
posLast(GlushkovBuildState::POS_UNINITIALIZED) {
assert(sub_comp);
assert(max > 0);
assert(m_min <= m_max);
if (m_min > MAX_REPEAT) {
throw ParseError("Bounded repeat is too large.");
}
if (m_max != NoLimit && m_max > MAX_REPEAT) {
throw ParseError("Bounded repeat is too large.");
}
}
ComponentRepeat::~ComponentRepeat() {}
ComponentRepeat *ComponentRepeat::clone() const {
return new ComponentRepeat(*this);
}
ComponentRepeat::ComponentRepeat(const ComponentRepeat &other)
: Component(other),
type(other.type), sub_comp(unique_ptr<Component>(other.sub_comp->clone())),
m_min(other.m_min), m_max(other.m_max),
m_firsts(other.m_firsts), m_lasts(other.m_lasts),
posFirst(other.posFirst), posLast(other.posLast) {}
bool ComponentRepeat::empty() const {
return m_min == 0 || sub_comp->empty();
}
bool ComponentRepeat::repeatable() const {
return false;
}
static
void addBase(Position base, vector<PositionInfo> &firsts,
vector<PositionInfo> &lasts) {
for (auto &e : firsts) {
if (e.pos != GlushkovBuildState::POS_EPSILON) {
e.pos += base;
}
}
for (auto &e : lasts) {
e.pos += base;
}
}
static
void checkPositions(vector<PositionInfo> &v, const GlushkovBuildState &bs) {
const NFABuilder& builder = bs.getBuilder();
for (const auto &e : v) {
if (builder.isSpecialState(e.pos)) {
throw ParseError("Embedded anchors not supported.");
}
}
}
void ComponentRepeat::notePositions(GlushkovBuildState &bs) {
assert(m_max > 0);
assert(m_max == NoLimit || m_max < MAX_REPEAT);
posFirst = bs.getBuilder().numVertices();
sub_comp->notePositions(bs);
u32 copies = m_max < NoLimit ? m_max : MAX(m_min, 1);
DEBUG_PRINTF("building %u copies of repeated region\n", copies);
m_firsts.clear();
m_lasts.clear();
m_firsts.resize(copies);
m_lasts.resize(copies);
m_firsts[0] = sub_comp->first();
m_lasts[0] = sub_comp->last();
postSubNotePositionHook();
posLast = bs.getBuilder().numVertices() - 1;
u32 vcount = posLast + 1 - posFirst;
if (copies > 1) {
checkPositions(m_firsts[0], bs);
checkPositions(m_lasts[0], bs);
}
if (vcount * copies > MAX_POSITIONS_EXPANDED) {
throw ParseError("Bounded repeat is too large.");
}
size_t copyPositions = vcount * (copies - 1);
bs.getBuilder().makePositions(copyPositions);
for (u32 i = 1; i < copies; ++i) {
m_firsts[i] = m_firsts[0];
m_lasts[i] = m_lasts[0];
u32 base = i * vcount;
addBase(base, m_firsts[i], m_lasts[i]);
}
recordPosBounds(posFirst, bs.getBuilder().numVertices());
for (u32 i = m_min; i < m_firsts.size(); i++) {
m_firsts[i].push_back(GlushkovBuildState::POS_EPSILON);
}
}
vector<PositionInfo> ComponentRepeat::first() const {
if (!m_max) {
return {};
}
assert(!m_firsts.empty()); const vector<PositionInfo> &firsts = m_firsts.front();
DEBUG_PRINTF("firsts = %s\n",
dumpPositions(begin(firsts), end(firsts)).c_str());
return firsts;
}
void ComponentRepeat::buildFollowSet(GlushkovBuildState &bs,
const vector<PositionInfo> &lastPos) {
if (!m_max) {
return;
}
DEBUG_PRINTF("enter\n");
DEBUG_PRINTF("initial repeat\n");
sub_comp->buildFollowSet(bs, lastPos);
u32 copies = m_firsts.size();
DEBUG_PRINTF("cloning %u copies of repeat\n", copies - 1);
for (u32 rep = 1; rep < copies; rep++) {
u32 offset = (posLast + 1 - posFirst) * rep;
if (offset > 0) {
bs.cloneFollowSet(posFirst, posLast, offset);
}
}
wireRepeats(bs);
DEBUG_PRINTF("leave\n");
}
void ComponentRepeat::optimise(bool connected_to_sds) {
DEBUG_PRINTF("opt %d\n", (int)connected_to_sds);
if (!connected_to_sds) {
return;
}
DEBUG_PRINTF("setting m_max to %u\n", m_min);
m_max = m_min;
}
bool ComponentRepeat::vacuous_everywhere() const {
return !m_min || sub_comp->vacuous_everywhere();
}
bool ComponentRepeat::checkEmbeddedStartAnchor(bool at_start) const {
at_start = sub_comp->checkEmbeddedStartAnchor(at_start);
if (m_max > 1) {
at_start = sub_comp->checkEmbeddedStartAnchor(at_start);
}
return at_start;
}
bool ComponentRepeat::checkEmbeddedEndAnchor(bool at_end) const {
at_end = sub_comp->checkEmbeddedEndAnchor(at_end);
if (m_max > 1) {
at_end = sub_comp->checkEmbeddedEndAnchor(at_end);
}
return at_end;
}
Component *ComponentRepeat::accept(ComponentVisitor &v) {
Component *c = v.visit(this);
if (c != this) {
v.post(this);
return c;
}
c = sub_comp->accept(v);
if (c != sub_comp.get()) {
sub_comp.reset(c);
}
v.post(this);
return !sub_comp ? nullptr : this;
}
void ComponentRepeat::accept(ConstComponentVisitor &v) const {
v.pre(*this);
sub_comp->accept(v);
v.post(*this);
}
vector<PositionInfo> ComponentRepeat::last() const {
vector<PositionInfo> lasts;
if (!m_max) {
return lasts;
}
assert(!m_firsts.empty()); assert(!m_lasts.empty());
const auto &l = m_min ? m_lasts[m_min - 1] : m_lasts[0];
lasts.insert(lasts.end(), l.begin(), l.end());
if (!m_min || m_min != m_lasts.size()) {
lasts.insert(lasts.end(), m_lasts.back().begin(), m_lasts.back().end());
}
DEBUG_PRINTF("lasts = %s\n",
dumpPositions(lasts.begin(), lasts.end()).c_str());
return lasts;
}
void ComponentRepeat::wireRepeats(GlushkovBuildState &bs) {
u32 copies = m_firsts.size();
const bool isEmpty = sub_comp->empty();
const vector<PositionInfo> &optLasts =
m_min ? m_lasts[m_min - 1] : m_lasts[0];
if (!copies) {
goto inf_check;
}
DEBUG_PRINTF("wiring up %u mand repeats\n", m_min);
for (u32 rep = 1; rep < m_min; rep++) {
bs.connectRegions(m_lasts[rep - 1], m_firsts[rep]);
if (isEmpty) {
m_lasts[rep].insert(m_lasts[rep].end(), m_lasts[rep - 1].begin(),
m_lasts[rep - 1].end());
}
}
DEBUG_PRINTF("wiring up %d optional repeats\n", copies - m_min);
for (u32 rep = MAX(m_min, 1); rep < copies; rep++) {
vector<PositionInfo> lasts = m_lasts[rep - 1];
if (rep != m_min) {
lasts.insert(lasts.end(), optLasts.begin(), optLasts.end());
sort(lasts.begin(), lasts.end());
lasts.erase(unique(lasts.begin(), lasts.end()), lasts.end());
}
bs.connectRegions(lasts, m_firsts[rep]);
}
inf_check:
if (m_max == NoLimit) {
DEBUG_PRINTF("final repeat self-loop\n");
bs.connectRegions(m_lasts.back(), m_firsts.back());
}
}
static
bool hasPositionFlags(const Component &c) {
for (const auto &e : c.first()) {
if (e.flags) {
return true;
}
}
return false;
}
void ComponentRepeat::postSubNotePositionHook() {
if (!hasPositionFlags(*sub_comp) && sub_comp->empty()) {
m_min = 0;
}
}
unique_ptr<ComponentRepeat> makeComponentRepeat(unique_ptr<Component> sub_comp,
u32 min, u32 max,
ComponentRepeat::RepeatType t) {
return ue2::make_unique<ComponentRepeat>(move(sub_comp), min, max, t);
}
}