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 284 285 286 287 288 289 290 291 292 293
|
/* Copyright (c) 2003-2006 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
#ifndef CPCD_PARSER_HPP
#define CPCD_PARSER_HPP
#include "Vector.hpp"
#include "Properties.hpp"
#include "InputStream.hpp"
#include "NdbOut.hpp"
class ParserImpl;
template<class T> struct ParserRow;
//#define PARSER_DEBUG
#ifdef PARSER_DEBUG
#define DEBUG(x) \
ndbout_c("%s:%d:%s", __FILE__, __LINE__, x);
#else
#define DEBUG(x)
#endif
/**
* A generic parser
*/
template<class T>
class Parser {
public:
/**
* Status for parser
*/
enum ParserStatus {
Ok = 0,
Eof = 1,
NoLine = 2,
EmptyLine = 3,
UnknownCommand = 4,
UnknownArgument = 5,
TypeMismatch = 6,
InvalidArgumentFormat = 7,
UnknownArgumentType = 8,
CommandWithoutFunction = 9,
ArgumentGivenTwice = 10,
ExternalStop = 11,
MissingMandatoryArgument = 12
};
/**
* Context for parse
*/
class Context {
public:
Context() { m_mutex= NULL; };
ParserStatus m_status;
const ParserRow<T> * m_currentCmd;
const ParserRow<T> * m_currentArg;
char * m_currentToken;
char m_tokenBuffer[512];
NdbMutex *m_mutex;
Vector<const ParserRow<T> *> m_aliasUsed;
};
/**
* Initialize parser
*/
Parser(const ParserRow<T> rows[], class InputStream & in = Stdin,
bool breakOnCommand = false,
bool breakOnEmptyLine = true,
bool breakOnInvalidArg = false);
~Parser();
/**
* Run parser
*/
bool run(Context &, T &, volatile bool * stop = 0) const;
/**
* Parse only one entry and return Properties object representing
* the message
*/
const Properties *parse(Context &, T &);
bool getBreakOnCommand() const;
void setBreakOnCommand(bool v);
bool getBreakOnEmptyLine() const;
void setBreakOnEmptyLine(bool v);
bool getBreakOnInvalidArg() const;
void setBreakOnInvalidArg(bool v);
private:
ParserImpl * impl;
};
template<class T>
struct ParserRow {
public:
enum Type { Cmd, Arg, CmdAlias, ArgAlias };
enum ArgType { String, Int, Properties };
enum ArgRequired { Mandatory, Optional };
enum ArgMinMax { CheckMinMax, IgnoreMinMax };
const char * name;
const char * realName;
Type type;
ArgType argType;
ArgRequired argRequired;
ArgMinMax argMinMax;
int minVal;
int maxVal;
void (T::* function)(typename Parser<T>::Context & ctx,
const class Properties& args);
const char * description;
void *user_value;
};
/**
* The void* equivalent implementation
*/
class ParserImpl {
public:
class Dummy {};
typedef ParserRow<Dummy> DummyRow;
typedef Parser<Dummy>::Context Context;
ParserImpl(const DummyRow rows[], class InputStream & in,
bool b_cmd, bool b_empty, bool b_iarg);
~ParserImpl();
bool run(Context *ctx, const class Properties **, volatile bool *) const ;
static const DummyRow* matchCommand(Context*, const char*, const DummyRow*);
static const DummyRow* matchArg(Context*, const char *, const DummyRow *);
static bool parseArg(Context*, char*, const DummyRow*, Properties*);
static bool checkMandatory(Context*, const Properties*);
private:
const DummyRow * const m_rows;
class ParseInputStream & input;
bool m_breakOnEmpty;
bool m_breakOnCmd;
bool m_breakOnInvalidArg;
};
template<class T>
inline
Parser<T>::Parser(const ParserRow<T> rows[], class InputStream & in,
bool b_cmd, bool b_empty, bool b_iarg){
impl = new ParserImpl((ParserImpl::DummyRow *)rows, in,
b_cmd, b_empty, b_iarg);
}
template<class T>
inline
Parser<T>::~Parser(){
delete impl;
}
template<class T>
inline
bool
Parser<T>::run(Context & ctx, T & t, volatile bool * stop) const {
const Properties * p;
DEBUG("Executing Parser<T>::run");
if(impl->run((ParserImpl::Context*)&ctx, &p, stop)){
const ParserRow<T> * cmd = ctx.m_currentCmd; // Cast to correct type
if(cmd == 0){
/**
* Should happen if run returns true
*/
abort();
}
for(unsigned i = 0; i<ctx.m_aliasUsed.size(); i++){
const ParserRow<T> * alias = ctx.m_aliasUsed[i];
if(alias->function != 0){
/**
* Report alias usage with callback (if specified by user)
*/
DEBUG("Alias usage with callback");
(t.* alias->function)(ctx, * p);
}
}
if(cmd->function == 0){
ctx.m_status = CommandWithoutFunction;
DEBUG("CommandWithoutFunction");
delete p;
return false;
}
(t.* cmd->function)(ctx, * p); // Call the function
delete p;
return true;
}
DEBUG("");
return false;
}
template<class T>
inline
const Properties *
Parser<T>::parse(Context &ctx, T &t) {
const Properties * p;
volatile bool stop = false;
DEBUG("Executing Parser<T>::parse");
if(impl->run((ParserImpl::Context*)&ctx, &p, &stop)){
const ParserRow<T> * cmd = ctx.m_currentCmd; // Cast to correct type
if(cmd == 0){
/**
* Should happen if run returns true
*/
abort();
}
for(unsigned i = 0; i<ctx.m_aliasUsed.size(); i++){
const ParserRow<T> * alias = ctx.m_aliasUsed[i];
if(alias->function != 0){
/**
* Report alias usage with callback (if specified by user)
*/
DEBUG("Alias usage with callback");
(t.* alias->function)(ctx, * p);
}
}
if(cmd->function == 0){
DEBUG("CommandWithoutFunction");
ctx.m_status = CommandWithoutFunction;
return p;
}
return p;
}
DEBUG("");
return NULL;
}
template<class T>
inline
bool
Parser<T>::getBreakOnCommand() const{
return impl->m_breakOnCmd;
}
template<class T>
inline
void
Parser<T>::setBreakOnCommand(bool v){
impl->m_breakOnCmd = v;
}
template<class T>
inline
bool
Parser<T>::getBreakOnEmptyLine() const{
return impl->m_breakOnEmpty;
}
template<class T>
inline
void
Parser<T>::setBreakOnEmptyLine(bool v){
impl->m_breakOnEmpty = v;
}
template<class T>
inline
bool
Parser<T>::getBreakOnInvalidArg() const{
return impl->m_breakOnInvalidArg;
}
template<class T>
inline
void
Parser<T>::setBreakOnInvalidArg(bool v){
impl->m_breakOnInvalidArg = v;
}
#endif
|