Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
CondParser Class Reference

#include <condparser.h>

Public Member Functions

 CondParser ()
 
bool parse (const char *fileName, int lineNr, const char *expr)
 

Private Types

enum  TOKENTYPE { NOTHING = -1, DELIMITER, VARIABLE, UNKNOWN }
 
enum  OPERATOR_ID { UNKNOWN_OP = -1, AND = 1, OR, NOT }
 

Private Member Functions

void getToken ()
 
bool parseLevel1 ()
 
bool parseLevel2 ()
 
bool parseLevel3 ()
 
bool parseVar ()
 
bool evalOperator (const int opId, bool lhs, bool rhs)
 
bool evalVariable (const char *varName)
 
int getOperatorId (const QCString &opName)
 

Private Attributes

QCString m_err
 error state More...
 
QCString m_expr
 holds the expression More...
 
const char * m_e
 points to a character in expr More...
 
QCString m_token
 holds the token More...
 
TOKENTYPE m_tokenType
 type of the token More...
 

Detailed Description

Copyright (C) 1997-2015 by Dimitri van Heesch.

Permission to use, copy, modify, and distribute this software and its documentation under the terms of the GNU General Public License is hereby granted. No representations are made about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty. See the GNU General Public License for more details.

Documents produced by Doxygen are derivative works derived from the input used in their production; they are not affected by this license.

C++ Expression parser for EXTABLED_SETIONS in Doxygen

Features used: Operators: && AND operator || OR operator ! NOT operator

Definition at line 27 of file condparser.h.

Member Enumeration Documentation

Enumerator
UNKNOWN_OP 
AND 
OR 
NOT 

Definition at line 43 of file condparser.h.

44  {
45  UNKNOWN_OP = -1,
46  AND = 1,
47  OR,
48  NOT
49  };
enum CondParser::TOKENTYPE
private
Enumerator
NOTHING 
DELIMITER 
VARIABLE 
UNKNOWN 

Definition at line 36 of file condparser.h.

Constructor & Destructor Documentation

CondParser::CondParser ( )
inline

Definition at line 31 of file condparser.h.

31 : m_e(0), m_tokenType(NOTHING) {}
TOKENTYPE m_tokenType
type of the token
Definition: condparser.h:59
const char * m_e
points to a character in expr
Definition: condparser.h:56

Member Function Documentation

bool CondParser::evalOperator ( const int  opId,
bool  lhs,
bool  rhs 
)
private

evaluate an operator for given valuess

Definition at line 288 of file condparser.cpp.

289 {
290  switch (opId)
291  {
292  // level 2
293  case AND: return lhs && rhs;
294  case OR: return lhs || rhs;
295  }
296 
297  m_err = "Internal error unknown operator: id="+QCString().setNum(opId);
298  return FALSE;
299 }
const bool FALSE
Definition: qglobal.h:370
QCString & setNum(short n)
Definition: qcstring.cpp:469
QCString m_err
error state
Definition: condparser.h:54
bool CondParser::evalVariable ( const char *  varName)
private

evaluate a variable

Definition at line 304 of file condparser.cpp.

305 {
306  if (Config_getList("ENABLED_SECTIONS").find(varName)==-1) return FALSE;
307  return TRUE;
308 }
static QCString varName
const bool FALSE
Definition: qglobal.h:370
#define Config_getList(val)
Definition: config.cpp:662
const bool TRUE
Definition: qglobal.h:371
int CondParser::getOperatorId ( const QCString opName)
private

returns the id of the given operator returns -1 if the operator is not recognized

Definition at line 112 of file condparser.cpp.

113 {
114  // level 2
115  if (opName=="&&") { return AND; }
116  if (opName=="||") { return OR; }
117 
118  // not operator
119  if (opName=="!") { return NOT; }
120 
121  return UNKNOWN_OP;
122 }
void CondParser::getToken ( )
private

Get next token in the current string expr. Uses the data in m_expr pointed to by m_e to produce m_tokenType and m_token, set m_err in case of an error

Definition at line 129 of file condparser.cpp.

130 {
132  m_token.resize(0);
133 
134  //printf("\tgetToken e:{%c}, ascii=%i, col=%i\n", *e, *e, e-expr);
135 
136  // skip over whitespaces
137  while (*m_e == ' ' || *m_e == '\t') // space or tab
138  {
139  m_e++;
140  }
141 
142  // check for end of expression
143  if (*m_e=='\0')
144  {
145  // token is still empty
147  return;
148  }
149 
150  // check for parentheses
151  if (*m_e == '(' || *m_e == ')')
152  {
154  m_token += *m_e++;
155  return;
156  }
157 
158  // check for operators (delimeters)
159  if (isDelimiter(*m_e))
160  {
162  while (isDelimiter(*m_e))
163  {
164  m_token += *m_e++;
165  }
166  return;
167  }
168 
169  // check for variables
170  if (isAlpha(*m_e))
171  {
173  while (isAlphaNum(*m_e))
174  {
175  m_token += *m_e++;
176  }
177  return;
178  }
179 
180  // something unknown is found, wrong characters -> a syntax error
182  while (*m_e)
183  {
184  m_token += *m_e++;
185  }
186  m_err = QCString("Syntax error in part '")+m_token+"'";
187  return;
188 }
TOKENTYPE m_tokenType
type of the token
Definition: condparser.h:59
bool resize(uint newlen)
Definition: qcstring.h:225
static bool isAlpha(const char c)
Definition: condparser.cpp:98
static bool isDelimiter(const char c)
Definition: condparser.cpp:90
QCString m_token
holds the token
Definition: condparser.h:58
static bool isAlphaNum(const char c)
Definition: condparser.cpp:103
QCString m_err
error state
Definition: condparser.h:54
const char * m_e
points to a character in expr
Definition: condparser.h:56
bool CondParser::parse ( const char *  fileName,
int  lineNr,
const char *  expr 
)

Copyright (C) 1997-2015 by Dimitri van Heesch.

Permission to use, copy, modify, and distribute this software and its documentation under the terms of the GNU General Public License is hereby granted. No representations are made about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty. See the GNU General Public License for more details.

Documents produced by Doxygen are derivative works derived from the input used in their production; they are not affected by this license.

C++ Expression parser for ENABLED_SECTIONS in Doxygen

Features used: Operators: && AND operator || OR operator ! NOT operator parses and evaluates the given expression.

Returns
  • On error, an error message is returned.
  • On success, the result of the expression is either "1" or "0".

Definition at line 34 of file condparser.cpp.

35 {
36  m_expr = expr;
38 
39  // initialize all variables
40  m_e = m_expr; // let m_e point to the start of the expression
41 
42  bool answer=FALSE;
43  getToken();
45  {
46  // empty expression: answer==FALSE
47  }
48  else if (m_err.isEmpty())
49  {
50  answer = parseLevel1();
51 
52 #if 0
53  // check for garbage at the end of the expression
54  // an expression ends with a character '\0' and token_type = delimeter
56  {
57  if (m_tokenType == DELIMITER)
58  {
59  if (m_token=="(" || m_token==")")
60  {
61  m_err=QCString("Unexpected parenthesis ")+m_token+"'";
62  }
63  else
64  {
65  // user entered a not existing operator like "//"
66  m_err=QCString("Unexpected operator ")+m_token+"'";
67  }
68  }
69  else
70  {
71  m_err=QCString("Unexpected part '")+m_token+"'";
72  }
73  }
74 #endif
75  }
76  if (m_err)
77  {
78  warn(fileName,lineNr,"problem evaluating expression '%s': %s",
79  expr,m_err.data());
80  }
81  //printf("expr='%s' answer=%d\n",expr,answer);
82  return answer;
83 }
TOKENTYPE m_tokenType
type of the token
Definition: condparser.h:59
bool isEmpty() const
Definition: qcstring.h:189
const bool FALSE
Definition: qglobal.h:370
QCString m_token
holds the token
Definition: condparser.h:58
fileName
Definition: dumpTree.py:9
const char * data() const
Definition: qcstring.h:207
void warn(const char *file, int line, const char *fmt,...)
Definition: message.cpp:183
bool parseLevel1()
Definition: condparser.cpp:194
void getToken()
Definition: condparser.cpp:129
QCString m_expr
holds the expression
Definition: condparser.h:55
QCString m_err
error state
Definition: condparser.h:54
const char * m_e
points to a character in expr
Definition: condparser.h:56
bool CondParser::parseLevel1 ( )
private

conditional operators AND and OR

Definition at line 194 of file condparser.cpp.

195 {
196  bool ans = parseLevel2();
197  int opId = getOperatorId(m_token);
198 
199  while (opId==AND || opId==OR)
200  {
201  getToken();
202  ans = evalOperator(opId, ans, parseLevel2());
203  opId = getOperatorId(m_token);
204  }
205 
206  return ans;
207 }
bool parseLevel2()
Definition: condparser.cpp:212
bool evalOperator(const int opId, bool lhs, bool rhs)
Definition: condparser.cpp:288
QCString m_token
holds the token
Definition: condparser.h:58
void getToken()
Definition: condparser.cpp:129
int getOperatorId(const QCString &opName)
Definition: condparser.cpp:112
bool CondParser::parseLevel2 ( )
private

NOT

Definition at line 212 of file condparser.cpp.

213 {
214  bool ans;
215  int opId = getOperatorId(m_token);
216  if (opId == NOT)
217  {
218  getToken();
219  ans = !parseLevel3();
220  }
221  else
222  {
223  ans = parseLevel3();
224  }
225 
226  return ans;
227 }
QCString m_token
holds the token
Definition: condparser.h:58
bool parseLevel3()
Definition: condparser.cpp:233
void getToken()
Definition: condparser.cpp:129
int getOperatorId(const QCString &opName)
Definition: condparser.cpp:112
bool CondParser::parseLevel3 ( )
private

parenthesized expression or variable

Definition at line 233 of file condparser.cpp.

234 {
235  // check if it is a parenthesized expression
236  if (m_tokenType == DELIMITER)
237  {
238  if (m_token=="(")
239  {
240  getToken();
241  int ans = parseLevel1();
242  if (m_tokenType!=DELIMITER || m_token!=")")
243  {
244  m_err="Parenthesis ) missing";
245  return FALSE;
246  }
247  getToken();
248  return ans;
249  }
250  }
251 
252  // if not parenthesized then the expression is a variable
253  return parseVar();
254 }
TOKENTYPE m_tokenType
type of the token
Definition: condparser.h:59
const bool FALSE
Definition: qglobal.h:370
bool parseVar()
Definition: condparser.cpp:257
QCString m_token
holds the token
Definition: condparser.h:58
bool parseLevel1()
Definition: condparser.cpp:194
void getToken()
Definition: condparser.cpp:129
QCString m_err
error state
Definition: condparser.h:54
bool CondParser::parseVar ( )
private

Definition at line 257 of file condparser.cpp.

258 {
259  bool ans = 0;
260  switch (m_tokenType)
261  {
262  case VARIABLE:
263  // this is a variable
264  ans = evalVariable(m_token);
265  getToken();
266  break;
267 
268  default:
269  // syntax error or unexpected end of expression
270  if (m_token.isEmpty())
271  {
272  m_err="Unexpected end of expression";
273  return FALSE;
274  }
275  else
276  {
277  m_err="Value expected";
278  return FALSE;
279  }
280  break;
281  }
282  return ans;
283 }
TOKENTYPE m_tokenType
type of the token
Definition: condparser.h:59
bool isEmpty() const
Definition: qcstring.h:189
const bool FALSE
Definition: qglobal.h:370
QCString m_token
holds the token
Definition: condparser.h:58
void getToken()
Definition: condparser.cpp:129
bool evalVariable(const char *varName)
Definition: condparser.cpp:304
QCString m_err
error state
Definition: condparser.h:54

Member Data Documentation

const char* CondParser::m_e
private

points to a character in expr

Definition at line 56 of file condparser.h.

QCString CondParser::m_err
private

error state

Definition at line 54 of file condparser.h.

QCString CondParser::m_expr
private

holds the expression

Definition at line 55 of file condparser.h.

QCString CondParser::m_token
private

holds the token

Definition at line 58 of file condparser.h.

TOKENTYPE CondParser::m_tokenType
private

type of the token

Definition at line 59 of file condparser.h.


The documentation for this class was generated from the following files: