This source file includes following definitions.
- Mk
- Deref
- Append
- NullFrag
- AllocInst
- Trim
- NoMatch
- IsNoMatch
- Cat
- Alt
- Star
- Plus
- Quest
- ByteRange
- Nop
- Match
- EmptyWidth
- Capture
- MaxRune
- BeginRange
- UncachedRuneByteSuffix
- RuneByteSuffix
- AddSuffix
- EndRange
- AddRuneRange
- AddRuneRangeLatin1
- Add_80_10ffff
- AddRuneRangeUTF8
- Copy
- ShortVisit
- PreVisit
- Literal
- PostVisit
- IsAnchorStart
- IsAnchorEnd
- Setup
- Finish
- CompileToProg
- CompileToReverseProg
- DotStar
- CompileSet
- CompileSet
#include "re2/prog.h"
#include "re2/re2.h"
#include "re2/regexp.h"
#include "re2/walker-inl.h"
namespace re2 {
struct PatchList {
uint32 p;
static PatchList Mk(uint32 p);
static void Patch(Prog::Inst *inst0, PatchList l, uint32 v);
static PatchList Deref(Prog::Inst *inst0, PatchList l);
static PatchList Append(Prog::Inst *inst0, PatchList l1, PatchList l2);
};
static PatchList nullPatchList = { 0 };
PatchList PatchList::Mk(uint32 p) {
PatchList l;
l.p = p;
return l;
}
PatchList PatchList::Deref(Prog::Inst* inst0, PatchList l) {
Prog::Inst* ip = &inst0[l.p>>1];
if (l.p&1)
l.p = ip->out1();
else
l.p = ip->out();
return l;
}
void PatchList::Patch(Prog::Inst *inst0, PatchList l, uint32 val) {
while (l.p != 0) {
Prog::Inst* ip = &inst0[l.p>>1];
if (l.p&1) {
l.p = ip->out1();
ip->out1_ = val;
} else {
l.p = ip->out();
ip->set_out(val);
}
}
}
PatchList PatchList::Append(Prog::Inst* inst0, PatchList l1, PatchList l2) {
if (l1.p == 0)
return l2;
if (l2.p == 0)
return l1;
PatchList l = l1;
for (;;) {
PatchList next = PatchList::Deref(inst0, l);
if (next.p == 0)
break;
l = next;
}
Prog::Inst* ip = &inst0[l.p>>1];
if (l.p&1)
ip->out1_ = l2.p;
else
ip->set_out(l2.p);
return l1;
}
struct Frag {
uint32 begin;
PatchList end;
Frag() : begin(0) { end.p = 0; }
Frag(uint32 begin, PatchList end) : begin(begin), end(end) {}
};
static Frag NullFrag() {
return Frag();
}
enum Encoding {
kEncodingUTF8 = 1,
kEncodingLatin1,
};
class Compiler : public Regexp::Walker<Frag> {
public:
explicit Compiler();
~Compiler();
static Prog *Compile(Regexp* re, bool reversed, int64 max_mem);
static Prog* CompileSet(const RE2::Options& options, RE2::Anchor anchor,
Regexp* re);
Frag PreVisit(Regexp* re, Frag parent_arg, bool* stop);
Frag PostVisit(Regexp* re, Frag parent_arg, Frag pre_arg, Frag* child_args,
int nchild_args);
Frag ShortVisit(Regexp* re, Frag parent_arg);
Frag Copy(Frag arg);
Frag Plus(Frag a, bool nongreedy);
Frag Star(Frag a, bool nongreedy);
Frag Quest(Frag a, bool nongreedy);
Frag Capture(Frag a, int n);
Frag Cat(Frag a, Frag b);
Frag Alt(Frag a, Frag b);
Frag NoMatch();
Frag Match(int32 id);
Frag Nop();
Frag ByteRange(int lo, int hi, bool foldcase);
Frag EmptyWidth(EmptyOp op);
int AllocInst(int n);
void Trim();
void BeginRange();
void AddRuneRange(Rune lo, Rune hi, bool foldcase);
void AddRuneRangeLatin1(Rune lo, Rune hi, bool foldcase);
void AddRuneRangeUTF8(Rune lo, Rune hi, bool foldcase);
void Add_80_10ffff();
int RuneByteSuffix(uint8 lo, uint8 hi, bool foldcase, int next);
int UncachedRuneByteSuffix(uint8 lo, uint8 hi, bool foldcase, int next);
void AddSuffix(int id);
Frag EndRange();
Frag Literal(Rune r, bool foldcase);
void Setup(Regexp::ParseFlags, int64, RE2::Anchor);
Prog* Finish();
Frag DotStar();
private:
Prog* prog_;
bool failed_;
Encoding encoding_;
bool reversed_;
int max_inst_;
Prog::Inst* inst_;
int inst_len_;
int inst_cap_;
int64 max_mem_;
map<uint64, int> rune_cache_;
Frag rune_range_;
RE2::Anchor anchor_;
DISALLOW_EVIL_CONSTRUCTORS(Compiler);
};
Compiler::Compiler() {
prog_ = new Prog();
failed_ = false;
encoding_ = kEncodingUTF8;
reversed_ = false;
inst_ = NULL;
inst_len_ = 0;
inst_cap_ = 0;
max_inst_ = 1;
max_mem_ = 0;
int fail = AllocInst(1);
inst_[fail].InitFail();
max_inst_ = 0;
}
Compiler::~Compiler() {
delete prog_;
delete[] inst_;
}
int Compiler::AllocInst(int n) {
if (failed_ || inst_len_ + n > max_inst_) {
failed_ = true;
return -1;
}
if (inst_len_ + n > inst_cap_) {
if (inst_cap_ == 0)
inst_cap_ = 8;
while (inst_len_ + n > inst_cap_)
inst_cap_ *= 2;
Prog::Inst* ip = new Prog::Inst[inst_cap_];
memmove(ip, inst_, inst_len_ * sizeof ip[0]);
memset(ip + inst_len_, 0, (inst_cap_ - inst_len_) * sizeof ip[0]);
delete[] inst_;
inst_ = ip;
}
int id = inst_len_;
inst_len_ += n;
return id;
}
void Compiler::Trim() {
if (inst_len_ < inst_cap_) {
Prog::Inst* ip = new Prog::Inst[inst_len_];
memmove(ip, inst_, inst_len_ * sizeof ip[0]);
delete[] inst_;
inst_ = ip;
inst_cap_ = inst_len_;
}
}
Frag Compiler::NoMatch() {
return Frag(0, nullPatchList);
}
static bool IsNoMatch(Frag a) {
return a.begin == 0;
}
Frag Compiler::Cat(Frag a, Frag b) {
if (IsNoMatch(a) || IsNoMatch(b))
return NoMatch();
Prog::Inst* begin = &inst_[a.begin];
if (begin->opcode() == kInstNop &&
a.end.p == (a.begin << 1) &&
begin->out() == 0) {
PatchList::Patch(inst_, a.end, b.begin);
return b;
}
if (reversed_) {
PatchList::Patch(inst_, b.end, a.begin);
return Frag(b.begin, a.end);
}
PatchList::Patch(inst_, a.end, b.begin);
return Frag(a.begin, b.end);
}
Frag Compiler::Alt(Frag a, Frag b) {
if (IsNoMatch(a))
return b;
if (IsNoMatch(b))
return a;
int id = AllocInst(1);
if (id < 0)
return NoMatch();
inst_[id].InitAlt(a.begin, b.begin);
return Frag(id, PatchList::Append(inst_, a.end, b.end));
}
Frag Compiler::Star(Frag a, bool nongreedy) {
int id = AllocInst(1);
if (id < 0)
return NoMatch();
inst_[id].InitAlt(0, 0);
PatchList::Patch(inst_, a.end, id);
if (nongreedy) {
inst_[id].out1_ = a.begin;
return Frag(id, PatchList::Mk(id << 1));
} else {
inst_[id].set_out(a.begin);
return Frag(id, PatchList::Mk((id << 1) | 1));
}
}
Frag Compiler::Plus(Frag a, bool nongreedy) {
Frag f = Star(a, nongreedy);
return Frag(a.begin, f.end);
}
Frag Compiler::Quest(Frag a, bool nongreedy) {
int id = AllocInst(1);
if (id < 0)
return NoMatch();
PatchList pl;
if (nongreedy) {
inst_[id].InitAlt(0, a.begin);
pl = PatchList::Mk(id << 1);
} else {
inst_[id].InitAlt(a.begin, 0);
pl = PatchList::Mk((id << 1) | 1);
}
return Frag(id, PatchList::Append(inst_, pl, a.end));
}
Frag Compiler::ByteRange(int lo, int hi, bool foldcase) {
int id = AllocInst(1);
if (id < 0)
return NoMatch();
inst_[id].InitByteRange(lo, hi, foldcase, 0);
prog_->byte_inst_count_++;
prog_->MarkByteRange(lo, hi);
if (foldcase && lo <= 'z' && hi >= 'a') {
if (lo < 'a')
lo = 'a';
if (hi > 'z')
hi = 'z';
if (lo <= hi)
prog_->MarkByteRange(lo + 'A' - 'a', hi + 'A' - 'a');
}
return Frag(id, PatchList::Mk(id << 1));
}
Frag Compiler::Nop() {
int id = AllocInst(1);
if (id < 0)
return NoMatch();
inst_[id].InitNop(0);
return Frag(id, PatchList::Mk(id << 1));
}
Frag Compiler::Match(int32 match_id) {
int id = AllocInst(1);
if (id < 0)
return NoMatch();
inst_[id].InitMatch(match_id);
return Frag(id, nullPatchList);
}
Frag Compiler::EmptyWidth(EmptyOp empty) {
int id = AllocInst(1);
if (id < 0)
return NoMatch();
inst_[id].InitEmptyWidth(empty, 0);
if (empty & (kEmptyBeginLine|kEmptyEndLine))
prog_->MarkByteRange('\n', '\n');
if (empty & (kEmptyWordBoundary|kEmptyNonWordBoundary)) {
int j;
for (int i = 0; i < 256; i = j) {
for (j = i+1; j < 256 && Prog::IsWordChar(i) == Prog::IsWordChar(j); j++)
;
prog_->MarkByteRange(i, j-1);
}
}
return Frag(id, PatchList::Mk(id << 1));
}
Frag Compiler::Capture(Frag a, int n) {
int id = AllocInst(2);
if (id < 0)
return NoMatch();
inst_[id].InitCapture(2*n, a.begin);
inst_[id+1].InitCapture(2*n+1, 0);
PatchList::Patch(inst_, a.end, id+1);
return Frag(id, PatchList::Mk((id+1) << 1));
}
static int MaxRune(int len) {
int b;
if (len == 1)
b = 7;
else
b = 8-(len+1) + 6*(len-1);
return (1<<b) - 1;
}
void Compiler::BeginRange() {
rune_cache_.clear();
rune_range_.begin = 0;
rune_range_.end = nullPatchList;
}
int Compiler::UncachedRuneByteSuffix(uint8 lo, uint8 hi, bool foldcase,
int next) {
Frag f = ByteRange(lo, hi, foldcase);
if (next != 0) {
PatchList::Patch(inst_, f.end, next);
} else {
rune_range_.end = PatchList::Append(inst_, rune_range_.end, f.end);
}
return f.begin;
}
int Compiler::RuneByteSuffix(uint8 lo, uint8 hi, bool foldcase, int next) {
if (encoding_ == kEncodingLatin1 ||
(encoding_ == kEncodingUTF8 &&
!reversed_ &&
!(0x80 <= lo && hi <= 0xbf))) {
return UncachedRuneByteSuffix(lo, hi, foldcase, next);
}
uint64 key = ((uint64)next << 17) | (lo<<9) | (hi<<1) | (foldcase ? 1ULL : 0ULL);
map<uint64, int>::iterator it = rune_cache_.find(key);
if (it != rune_cache_.end())
return it->second;
int id = UncachedRuneByteSuffix(lo, hi, foldcase, next);
rune_cache_[key] = id;
return id;
}
void Compiler::AddSuffix(int id) {
if (rune_range_.begin == 0) {
rune_range_.begin = id;
return;
}
int alt = AllocInst(1);
if (alt < 0) {
rune_range_.begin = 0;
return;
}
inst_[alt].InitAlt(rune_range_.begin, id);
rune_range_.begin = alt;
}
Frag Compiler::EndRange() {
return rune_range_;
}
void Compiler::AddRuneRange(Rune lo, Rune hi, bool foldcase) {
switch (encoding_) {
default:
case kEncodingUTF8:
AddRuneRangeUTF8(lo, hi, foldcase);
break;
case kEncodingLatin1:
AddRuneRangeLatin1(lo, hi, foldcase);
break;
}
}
void Compiler::AddRuneRangeLatin1(Rune lo, Rune hi, bool foldcase) {
if (lo > hi || lo > 0xFF)
return;
if (hi > 0xFF)
hi = 0xFF;
AddSuffix(RuneByteSuffix(lo, hi, foldcase, 0));
}
static struct ByteRangeProg {
int next;
int lo;
int hi;
} prog_80_10ffff[] = {
{ -1, 0x80, 0xBF, },
{ 0, 0xC2, 0xDF, },
{ 0, 0xA0, 0xBF, },
{ 2, 0xE0, 0xE0, },
{ 0, 0x80, 0xBF, },
{ 4, 0xE1, 0xEF, },
{ 4, 0x90, 0xBF, },
{ 6, 0xF0, 0xF0, },
{ 4, 0x80, 0xBF, },
{ 8, 0xF1, 0xF3, },
{ 4, 0x80, 0x8F, },
{ 10, 0xF4, 0xF4, },
};
void Compiler::Add_80_10ffff() {
int inst[arraysize(prog_80_10ffff)] = { 0 };
for (int i = 0; i < arraysize(prog_80_10ffff); i++) {
const ByteRangeProg& p = prog_80_10ffff[i];
int next = 0;
if (p.next >= 0)
next = inst[p.next];
inst[i] = UncachedRuneByteSuffix(p.lo, p.hi, false, next);
if ((p.lo & 0xC0) != 0x80)
AddSuffix(inst[i]);
}
}
void Compiler::AddRuneRangeUTF8(Rune lo, Rune hi, bool foldcase) {
if (lo > hi)
return;
if (lo == 0x80 && hi == 0x10ffff && !reversed_) {
Add_80_10ffff();
return;
}
for (int i = 1; i < UTFmax; i++) {
Rune max = MaxRune(i);
if (lo <= max && max < hi) {
AddRuneRangeUTF8(lo, max, foldcase);
AddRuneRangeUTF8(max+1, hi, foldcase);
return;
}
}
if (hi < Runeself) {
AddSuffix(RuneByteSuffix(lo, hi, foldcase, 0));
return;
}
for (int i = 1; i < UTFmax; i++) {
uint m = (1<<(6*i)) - 1;
if ((lo & ~m) != (hi & ~m)) {
if ((lo & m) != 0) {
AddRuneRangeUTF8(lo, lo|m, foldcase);
AddRuneRangeUTF8((lo|m)+1, hi, foldcase);
return;
}
if ((hi & m) != m) {
AddRuneRangeUTF8(lo, (hi&~m)-1, foldcase);
AddRuneRangeUTF8(hi&~m, hi, foldcase);
return;
}
}
}
uint8 ulo[UTFmax], uhi[UTFmax];
int n = runetochar(reinterpret_cast<char*>(ulo), &lo);
int m = runetochar(reinterpret_cast<char*>(uhi), &hi);
(void)m;
DCHECK_EQ(n, m);
int id = 0;
if (reversed_) {
for (int i = 0; i < n; i++)
id = RuneByteSuffix(ulo[i], uhi[i], false, id);
} else {
for (int i = n-1; i >= 0; i--)
id = RuneByteSuffix(ulo[i], uhi[i], false, id);
}
AddSuffix(id);
}
Frag Compiler::Copy(Frag arg) {
LOG(DFATAL) << "Compiler::Copy called!";
failed_ = true;
return NoMatch();
}
Frag Compiler::ShortVisit(Regexp* re, Frag) {
failed_ = true;
return NoMatch();
}
Frag Compiler::PreVisit(Regexp* re, Frag, bool* stop) {
if (failed_)
*stop = true;
return NullFrag();
}
Frag Compiler::Literal(Rune r, bool foldcase) {
switch (encoding_) {
default:
return NullFrag();
case kEncodingLatin1:
return ByteRange(r, r, foldcase);
case kEncodingUTF8: {
if (r < Runeself)
return ByteRange(r, r, foldcase);
uint8 buf[UTFmax];
int n = runetochar(reinterpret_cast<char*>(buf), &r);
Frag f = ByteRange((uint8)buf[0], buf[0], false);
for (int i = 1; i < n; i++)
f = Cat(f, ByteRange((uint8)buf[i], buf[i], false));
return f;
}
}
}
Frag Compiler::PostVisit(Regexp* re, Frag, Frag, Frag* child_frags,
int nchild_frags) {
if (failed_)
return NoMatch();
switch (re->op()) {
case kRegexpRepeat:
break;
case kRegexpNoMatch:
return NoMatch();
case kRegexpEmptyMatch:
return Nop();
case kRegexpHaveMatch: {
Frag f = Match(re->match_id());
if (anchor_ != RE2::ANCHOR_BOTH)
f = Cat(DotStar(), Cat(EmptyWidth(kEmptyEndText), f));
return f;
}
case kRegexpConcat: {
Frag f = child_frags[0];
for (int i = 1; i < nchild_frags; i++)
f = Cat(f, child_frags[i]);
return f;
}
case kRegexpAlternate: {
Frag f = child_frags[0];
for (int i = 1; i < nchild_frags; i++)
f = Alt(f, child_frags[i]);
return f;
}
case kRegexpStar:
return Star(child_frags[0], re->parse_flags()&Regexp::NonGreedy);
case kRegexpPlus:
return Plus(child_frags[0], re->parse_flags()&Regexp::NonGreedy);
case kRegexpQuest:
return Quest(child_frags[0], re->parse_flags()&Regexp::NonGreedy);
case kRegexpLiteral:
return Literal(re->rune(), re->parse_flags()&Regexp::FoldCase);
case kRegexpLiteralString: {
if (re->nrunes() == 0)
return Nop();
Frag f;
for (int i = 0; i < re->nrunes(); i++) {
Frag f1 = Literal(re->runes()[i], re->parse_flags()&Regexp::FoldCase);
if (i == 0)
f = f1;
else
f = Cat(f, f1);
}
return f;
}
case kRegexpAnyChar:
BeginRange();
AddRuneRange(0, Runemax, false);
return EndRange();
case kRegexpAnyByte:
return ByteRange(0x00, 0xFF, false);
case kRegexpCharClass: {
CharClass* cc = re->cc();
if (cc->empty()) {
LOG(DFATAL) << "No ranges in char class";
failed_ = true;
return NoMatch();
}
bool foldascii = cc->FoldsASCII();
BeginRange();
for (CharClass::iterator i = cc->begin(); i != cc->end(); ++i) {
if (foldascii && 'A' <= i->lo && i->hi <= 'Z')
continue;
bool fold = foldascii;
if ((i->lo <= 'A' && 'z' <= i->hi) || i->hi < 'A' || 'z' < i->lo)
fold = false;
AddRuneRange(i->lo, i->hi, fold);
}
return EndRange();
}
case kRegexpCapture:
if (re->cap() < 0)
return child_frags[0];
return Capture(child_frags[0], re->cap());
case kRegexpBeginLine:
return EmptyWidth(reversed_ ? kEmptyEndLine : kEmptyBeginLine);
case kRegexpEndLine:
return EmptyWidth(reversed_ ? kEmptyBeginLine : kEmptyEndLine);
case kRegexpBeginText:
return EmptyWidth(reversed_ ? kEmptyEndText : kEmptyBeginText);
case kRegexpEndText:
return EmptyWidth(reversed_ ? kEmptyBeginText : kEmptyEndText);
case kRegexpWordBoundary:
return EmptyWidth(kEmptyWordBoundary);
case kRegexpNoWordBoundary:
return EmptyWidth(kEmptyNonWordBoundary);
}
LOG(DFATAL) << "Missing case in Compiler: " << re->op();
failed_ = true;
return NoMatch();
}
static bool IsAnchorStart(Regexp** pre, int depth) {
Regexp* re = *pre;
Regexp* sub;
if (re == NULL || depth >= 4)
return false;
switch (re->op()) {
default:
break;
case kRegexpConcat:
if (re->nsub() > 0) {
sub = re->sub()[0]->Incref();
if (IsAnchorStart(&sub, depth+1)) {
Regexp** subcopy = new Regexp*[re->nsub()];
subcopy[0] = sub;
for (int i = 1; i < re->nsub(); i++)
subcopy[i] = re->sub()[i]->Incref();
*pre = Regexp::Concat(subcopy, re->nsub(), re->parse_flags());
delete[] subcopy;
re->Decref();
return true;
}
sub->Decref();
}
break;
case kRegexpCapture:
sub = re->sub()[0]->Incref();
if (IsAnchorStart(&sub, depth+1)) {
*pre = Regexp::Capture(sub, re->parse_flags(), re->cap());
re->Decref();
return true;
}
sub->Decref();
break;
case kRegexpBeginText:
*pre = Regexp::LiteralString(NULL, 0, re->parse_flags());
re->Decref();
return true;
}
return false;
}
static bool IsAnchorEnd(Regexp** pre, int depth) {
Regexp* re = *pre;
Regexp* sub;
if (re == NULL || depth >= 4)
return false;
switch (re->op()) {
default:
break;
case kRegexpConcat:
if (re->nsub() > 0) {
sub = re->sub()[re->nsub() - 1]->Incref();
if (IsAnchorEnd(&sub, depth+1)) {
Regexp** subcopy = new Regexp*[re->nsub()];
subcopy[re->nsub() - 1] = sub;
for (int i = 0; i < re->nsub() - 1; i++)
subcopy[i] = re->sub()[i]->Incref();
*pre = Regexp::Concat(subcopy, re->nsub(), re->parse_flags());
delete[] subcopy;
re->Decref();
return true;
}
sub->Decref();
}
break;
case kRegexpCapture:
sub = re->sub()[0]->Incref();
if (IsAnchorEnd(&sub, depth+1)) {
*pre = Regexp::Capture(sub, re->parse_flags(), re->cap());
re->Decref();
return true;
}
sub->Decref();
break;
case kRegexpEndText:
*pre = Regexp::LiteralString(NULL, 0, re->parse_flags());
re->Decref();
return true;
}
return false;
}
void Compiler::Setup(Regexp::ParseFlags flags, int64 max_mem,
RE2::Anchor anchor) {
prog_->set_flags(flags);
if (flags & Regexp::Latin1)
encoding_ = kEncodingLatin1;
max_mem_ = max_mem;
if (max_mem <= 0) {
max_inst_ = 100000;
} else if (max_mem <= sizeof(Prog)) {
max_inst_ = 0;
} else {
int64 m = (max_mem - sizeof(Prog)) / sizeof(Prog::Inst);
if (m >= 1<<24)
m = 1<<24;
if (m > Prog::Inst::kMaxInst)
m = Prog::Inst::kMaxInst;
max_inst_ = m;
}
anchor_ = anchor;
}
Prog* Compiler::Compile(Regexp* re, bool reversed, int64 max_mem) {
Compiler c;
c.Setup(re->parse_flags(), max_mem, RE2::ANCHOR_BOTH );
c.reversed_ = reversed;
Regexp* sre = re->Simplify();
if (sre == NULL)
return NULL;
bool is_anchor_start = IsAnchorStart(&sre, 0);
bool is_anchor_end = IsAnchorEnd(&sre, 0);
Frag f = c.WalkExponential(sre, NullFrag(), 2*c.max_inst_);
sre->Decref();
if (c.failed_)
return NULL;
c.reversed_ = false;
Frag all = c.Cat(f, c.Match(0));
c.prog_->set_start(all.begin);
if (reversed) {
c.prog_->set_anchor_start(is_anchor_end);
c.prog_->set_anchor_end(is_anchor_start);
} else {
c.prog_->set_anchor_start(is_anchor_start);
c.prog_->set_anchor_end(is_anchor_end);
}
if (c.prog_->anchor_start()) {
c.prog_->set_start_unanchored(c.prog_->start());
} else {
Frag unanchored = c.Cat(c.DotStar(), all);
c.prog_->set_start_unanchored(unanchored.begin);
}
c.prog_->set_reversed(reversed);
return c.Finish();
}
Prog* Compiler::Finish() {
if (failed_)
return NULL;
if (prog_->start() == 0 && prog_->start_unanchored() == 0) {
inst_len_ = 1;
}
Trim();
prog_->inst_ = inst_;
prog_->size_ = inst_len_;
inst_ = NULL;
prog_->ComputeByteMap();
prog_->Optimize();
if (max_mem_ <= 0) {
prog_->set_dfa_mem(1<<20);
} else {
int64 m = max_mem_ - sizeof(Prog) - inst_len_*sizeof(Prog::Inst);
if (m < 0)
m = 0;
prog_->set_dfa_mem(m);
}
Prog* p = prog_;
prog_ = NULL;
return p;
}
Prog* Regexp::CompileToProg(int64 max_mem) {
return Compiler::Compile(this, false, max_mem);
}
Prog* Regexp::CompileToReverseProg(int64 max_mem) {
return Compiler::Compile(this, true, max_mem);
}
Frag Compiler::DotStar() {
return Star(ByteRange(0x00, 0xff, false), true);
}
Prog* Compiler::CompileSet(const RE2::Options& options, RE2::Anchor anchor,
Regexp* re) {
Compiler c;
Regexp::ParseFlags pf = static_cast<Regexp::ParseFlags>(options.ParseFlags());
c.Setup(pf, options.max_mem(), anchor);
Frag all = c.WalkExponential(re, NullFrag(), 2*c.max_inst_);
re->Decref();
if (c.failed_)
return NULL;
if (anchor == RE2::UNANCHORED) {
all = c.Cat(c.DotStar(), all);
}
c.prog_->set_start(all.begin);
c.prog_->set_start_unanchored(all.begin);
c.prog_->set_anchor_start(true);
c.prog_->set_anchor_end(true);
Prog* prog = c.Finish();
if (prog == NULL)
return NULL;
bool failed;
StringPiece sp = "hello, world";
prog->SearchDFA(sp, sp, Prog::kAnchored, Prog::kManyMatch,
NULL, &failed, NULL);
if (failed) {
delete prog;
return NULL;
}
return prog;
}
Prog* Prog::CompileSet(const RE2::Options& options, RE2::Anchor anchor,
Regexp* re) {
return Compiler::CompileSet(options, anchor, re);
}
}