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
|
#include "pattern"
/*
The PerlSetFSA is a Finite State Automaton converting the special
character set characters as used by Perl to their corresponding
set-notations.
The special perl-set characters may be used inside and outside of
character classes. When used in a class, it adds the set to the current
set. If the class starts with a caret, then the meaning of the set is
reversed:
\s <-> \S
\d <-> \D
(etc).
The character sets s, d, and w are currently supported. \b and \B are
already part of the standard regex implementation.
*/
#include <iostream>
#include <vector>
#include <memory>
#include <bobcat/fswap>
#include <bobcat/ranger>
using namespace std;
namespace FBB
{
class PerlSetFSA
{
class Validator;
friend class Validator;
enum State
{
Start,
Bs,
Set,
NegatedSet,
SetBs,
NestedSet,
InsideASet,
NegatedSetBs,
NegatedNestedSet,
InsideANegatedSet,
nStates_
};
struct TransitionMatrix
{
State d_state;
int d_input;
State d_next;
void (PerlSetFSA::*d_action)();
};
static TransitionMatrix s_stateTransitions[];
static TransitionMatrix *s_stateTransitions_end;
class Validator
{
vector<bool> d_used;
int d_last;
State d_state;
bool d_valid;
size_t d_element;
public:
Validator()
:
d_used(nStates_),
d_last(0),
d_state(nStates_),
d_valid(true)
{}
void operator()(TransitionMatrix const &state);
operator bool() const
{
return d_valid;
}
};
// s_transition holds as many elements as there are states,
// first: points to first element of corresponding state
// in s_transition
// second: points just beyond the last element of corresponding
// state in s_transition
typedef pair<TransitionMatrix *,
TransitionMatrix *> statePair;
static vector<statePair> s_transition;
string d_target;
string::iterator d_next;
public:
//static void setup();
PerlSetFSA();
void convert(string &pattern);
private:
static void initialize(TransitionMatrix &stateDescription);
void nop()
{};
void copy();
void copybs();
void w_Set();
void W_Set();
void d_Set();
void D_Set();
void s_Set();
void S_Set();
void w_Nest();
void d_Nest();
void s_Nest();
};
inline Pattern::Regex::~Regex()
{
regfree(&d_regex);
}
inline void Pattern::newRegex(string const &pattern, int options)
{
d_regex = new Regex(pattern, options);
}
} // FBB
using namespace FBB;
|