stapregex-tree.h - systemtap
Data types defined
Functions defined
Macros defined
Source code
#ifndef STAPREGEX_TREE_H
#define STAPREGEX_TREE_H
#include <string>
#include <deque>
#include <utility>
#include <stdexcept>
XXX#define NUM_REAL_CHARS 128
namespace stapregex {
typedef std::pair<char, char> segment;
struct range {
std::deque<segment> segments;
range () {} range (char lb, char ub); range (const std::string& str);
void print(std::ostream& o) const;
};
std::ostream& operator<< (std::ostream&, const range&);
std::ostream& operator<< (std::ostream&, const range*);
range *range_union(range *a, range *b);
range *range_invert(range *ran);
const unsigned CHAR = 0; const unsigned GOTO = 1;
const unsigned FORK = 2; const unsigned ACCEPT = 3; const unsigned TAG = 4; const unsigned INIT = 5;
union ins {
struct {
unsigned int tag:3; unsigned int marked:1; unsigned int param:8; void *link; } i;
struct {
char value; unsigned short bump; } c;
};
inline bool marked(ins *i) { return i->i.marked != 0; }
inline void mark(ins *i) { i->i.marked = 1; }
inline void unmark(ins *i) { i->i.marked = 0; }
const ins* show_ins(std::ostream &o, const ins *i, const ins *base);
struct regexp {
int num_tags; int size;
regexp () : num_tags(-1), size(-1) {}
virtual ~regexp () {}
virtual const std::string type_of() const = 0;
virtual bool anchored() const { return false; }
virtual void calc_size() = 0;
unsigned ins_size() { if (size < 0) calc_size(); return size; }
virtual void compile(ins *i) = 0;
ins *compile();
virtual void print(std::ostream& o, unsigned priority = 0) const = 0;
};
std::ostream& operator << (std::ostream &o, const regexp& re);
std::ostream& operator << (std::ostream &o, const regexp* re);
struct null_op : public regexp {
const std::string type_of() const { return "null_op"; }
void calc_size();
void compile(ins *i);
void print (std::ostream &o, unsigned priority) const {
o << "{null}"; XXX }
};
struct anchor_op : public regexp {
char type;
anchor_op (char type);
const std::string type_of() const { return "anchor_op"; }
bool anchored () const { return type == '^'; }
void calc_size();
void compile(ins *i);
void print (std::ostream &o, unsigned priority) const {
o << type;
}
};
struct tag_op : public regexp {
unsigned id;
tag_op (unsigned id);
const std::string type_of() const { return "tag_op"; }
void calc_size();
void compile(ins *i);
void print (std::ostream &o, unsigned priority) const {
o << "{t_" << id << "}";
}
};
struct match_op : public regexp {
range *ran;
match_op (range *ran);
const std::string type_of() const { return "match_op"; }
void calc_size();
void compile(ins *i);
void print (std::ostream &o, unsigned priority) const { o << ran; }
};
struct alt_op : public regexp {
regexp *a, *b;
bool prefer_second;
alt_op (regexp *a, regexp *b, bool prefer_second = false);
const std::string type_of() const { return "alt_op"; }
bool anchored () const { return a->anchored() && b->anchored(); }
void calc_size();
void compile(ins *i);
void print (std::ostream &o, unsigned priority) const {
if (priority >= 1) o << "(";
a->print(o, 0); o << "|"; b->print(o, 0);
if (priority >= 1) o << ")";
}
};
struct cat_op : public regexp {
regexp *a, *b;
cat_op (regexp *a, regexp *b);
const std::string type_of() const { return "cat_op"; }
bool anchored () const {
return a->anchored(); XXX }
void calc_size();
void compile(ins *i);
void print (std::ostream &o, unsigned priority) const {
if (priority >= 2) o << "(";
a->print(o, 1); b->print(o, 1);
if (priority >= 2) o << ")";
}
};
struct close_op : public regexp {
regexp *re;
bool prefer_shorter;
close_op (regexp *re, bool prefer_shorter = false);
const std::string type_of() const { return "close_op"; }
bool anchored () const { return re->anchored(); }
void calc_size();
void compile(ins *i);
void print (std::ostream &o, unsigned priority) const {
re->print(o, 2); o << "+";
}
};
struct closev_op : public regexp {
regexp *re;
int nmin, nmax; closev_op (regexp *re, int nmin, int nmax);
const std::string type_of() const { return "closev_op"; }
bool anchored () const { return nmin > 0 && re->anchored(); }
void calc_size();
void compile(ins *i);
void print (std::ostream &o, unsigned priority) const {
re->print(o, 2); o << "{" << nmin << "," << nmax << "}";
}
};
struct rule_op : public regexp {
regexp *re;
unsigned outcome; rule_op (regexp *re, unsigned outcome);
const std::string type_of() const { return "rule_op"; }
bool anchored () const { return re->anchored(); }
void calc_size();
void compile(ins *i);
void print (std::ostream &o, unsigned priority) const {
re->print(o, 1);
if (outcome) o << "{success_" << outcome << "}";
else o << "{failure_0}";
}
};
regexp *str_to_re(const std::string& str);
regexp *make_alt(regexp* a, regexp* b);
regexp *make_dot(bool allow_zero = false);
struct regex_error: public std::runtime_error
{
int pos; regex_error (const std::string& msg):
runtime_error(msg), pos(-1) {}
regex_error (const std::string& msg, int pos):
runtime_error(msg), pos(pos) {}
~regex_error () throw () {}
};
};
#endif