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
|
// -*- C++ -*-
// Copyright (C) 2012-2013 Red Hat Inc.
//
// This file is part of systemtap, and is free software. You can
// redistribute it and/or modify it under the terms of the GNU General
// Public License (GPL); either version 2, or (at your option) any
// later version.
//
// ---
//
// This file incorporates code from the re2c project; please see
// the file README.stapregex for details.
#ifndef STAPREGEX_TREE_H
#define STAPREGEX_TREE_H
#include <string>
#include <deque>
#include <utility>
#include <stdexcept>
#include "stapregex-defines.h"
// Numbering scheme for tags representing start and end of n'th subexpression:
#define TAG_START(n) (2*(n))
#define TAG_END(n) (2*(n)+1)
namespace stapregex {
typedef std::pair<rchar, rchar> segment;
struct range {
std::deque<segment> segments; // -- [lb, ub], XXX sorted ascending
range () {} // -- empty range
range (rchar lb, rchar ub); // -- a segment [lb, ub]
range (const std::string& str); // -- character class (no named entities)
void print(std::ostream& o) const;
};
std::ostream& operator<< (std::ostream&, const range&);
std::ostream& operator<< (std::ostream&, const range*);
// NB: be sure to deallocate the old ranges if they are no longer used
range *range_union(range *a, range *b);
range *range_invert(range *ran);
// ------------------------------------------------------------------------
/* For the NFA representation, re2c uses an assembler-like notation,
which should be easy enough to understand based on the meaning of
FORK. An NFA is considered to be a tagged-NFA if it uses the TAG opcode.
The only tricky thing here is instituting sensible defaults for
tagged-NFA transition priorities. This is done by setting i->param = 0
on a FORK instruction if the FORK-target has lower priority, and
i->param = 1 if it has higher priority. FORK is the only place
where discriminating between transitions needs to be done. */
/* Opcodes for the assembly notation: */
const unsigned CHAR = 0; // -- match character set (one successful outcome)
const unsigned GOTO = 1;
const unsigned FORK = 2; // -- nondeterministic choice; param marks priority
const unsigned ACCEPT = 3; // -- final states; param marks success/fail
const unsigned TAG = 4; // -- subexpression tracking; param marks tag #
const unsigned INIT = 5; // -- opcode for ^ operator
/* To represent an NFA, allocate a continuous array of these ins units: */
union ins {
struct {
unsigned int tag:3; // -- opcode
unsigned int marked:1; // -- internal use; for algorithmic manipulation
unsigned int param:8; // -- numerical operand, e.g. tag #
void *link; // -- other instruction, e.g. FORK/GOTO target
} i;
/* For the CHAR opcodes, we follow the instruction with a sequence of
these special character-matching units, in ascending order: */
struct {
rchar value; // -- character to match
unsigned short bump; // -- relative address of success-outcome insn
} 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; }
/* Helper function for printing out one ins element in a sequence: */
const ins* show_ins(std::ostream &o, const ins *i, const ins *base);
/* Perform the obvious optimization/compression on an ins node
(namely, collapsing chained GOTOs).
NB: This function sets 'marked' flags on the node; the caller is
responsible for clearing these flags for subsequent use. */
void ins_optimize(ins *i);
// ------------------------------------------------------------------------
struct regexp {
int num_tags; // -- number of tag_op id's used in expression, -1 if unknown
int size; // -- number of instructions required for ins representation
regexp () : num_tags(-1), size(-1) {}
virtual ~regexp () {}
virtual const std::string type_of() const = 0;
/* Is regexp left-anchored? This function is used for optimization
purposes, so it's always safe to return false. */
virtual bool anchored() const { return false; }
/* Length of array needed for ins array representation: */
virtual void calc_size() = 0;
unsigned ins_size() { if (size < 0) calc_size(); return size; }
/* Compile to (part of) an already-allocated ins array: */
virtual void compile(ins *i) = 0;
/* Allocate a fresh ins array and compile: */
ins *compile();
/* Print out, with a careful eye as to bracketing:
priority == 0 -- don't bracket anything
priority == 1 -- bracket alt_op, but not cat_op
priority == 2 -- bracket all compound operators */
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) const {
o << "{null}"; // XXX: pick a better pseudo-notation?
}
};
struct anchor_op : public regexp {
rchar type;
anchor_op (rchar 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) 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) 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) 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: doesn't catch all cases, but that's all right
}
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) const {
re->print(o, 2); o << "+";
}
};
struct closev_op : public regexp {
regexp *re;
int nmin, nmax; // -- use -1 to denote unboundedness in that direction
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) const {
re->print(o, 2); o << "{" << nmin << "," << nmax << "}";
}
};
/* The following is somewhat generalized to allow implementing support
for multiple distinct success outcomes, like in the original re2c: */
struct rule_op : public regexp {
regexp *re;
unsigned outcome; // -- 0 -> failure; 1 -> success; prefer success outcomes
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) 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; // -1 denotes error at unknown/indeterminate position
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
/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
|