My Project
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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
 
QCString m_expr
 holds the expression
 
const char * m_e
 points to a character in expr
 
QCString m_token
 holds the token
 
TOKENTYPE m_tokenType
 type of the token
 

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.

{
UNKNOWN_OP = -1,
AND = 1,
OR,
};
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.

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.

References AND, m_err, and OR.

Referenced by parseLevel1().

{
switch (opId)
{
// level 2
case AND: return lhs && rhs;
case OR: return lhs || rhs;
}
m_err = "Internal error unknown operator: id="+QCString().setNum(opId);
return FALSE;
}
bool CondParser::evalVariable ( const char *  varName)
private

evaluate a variable

Definition at line 304 of file condparser.cpp.

References Config_getList.

Referenced by parseVar().

{
if (Config_getList(ENABLED_SECTIONS).find(varName)==-1) return FALSE;
return TRUE;
}
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.

References AND, NOT, OR, and UNKNOWN_OP.

Referenced by parseLevel1(), and parseLevel2().

{
// level 2
if (opName=="&&") { return AND; }
if (opName=="||") { return OR; }
// not operator
if (opName=="!") { return NOT; }
return UNKNOWN_OP;
}
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.

References DELIMITER, isAlpha(), isAlphaNum(), isDelimiter(), m_e, m_err, m_token, m_tokenType, NOTHING, UNKNOWN, and VARIABLE.

Referenced by parse(), parseLevel1(), parseLevel2(), parseLevel3(), and parseVar().

{
m_token.resize(0);
//printf("\tgetToken e:{%c}, ascii=%i, col=%i\n", *e, *e, e-expr);
// skip over whitespaces
while (*m_e == ' ' || *m_e == '\t') // space or tab
{
m_e++;
}
// check for end of expression
if (*m_e=='\0')
{
// token is still empty
return;
}
// check for parentheses
if (*m_e == '(' || *m_e == ')')
{
m_token += *m_e++;
return;
}
// check for operators (delimeters)
{
while (isDelimiter(*m_e))
{
m_token += *m_e++;
}
return;
}
// check for variables
if (isAlpha(*m_e))
{
while (isAlphaNum(*m_e))
{
m_token += *m_e++;
}
return;
}
// something unknown is found, wrong characters -> a syntax error
while (*m_e)
{
m_token += *m_e++;
}
m_err = QCString("Syntax error in part '")+m_token+"'";
return;
}
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.

References DELIMITER, getToken(), m_e, m_err, m_expr, m_token, m_tokenType, NOTHING, parseLevel1(), and warn().

{
m_expr = expr;
// initialize all variables
m_e = m_expr; // let m_e point to the start of the expression
bool answer=FALSE;
if (m_tokenType==DELIMITER && m_token.isEmpty())
{
// empty expression: answer==FALSE
}
else if (m_err.isEmpty())
{
answer = parseLevel1();
#if 0
// check for garbage at the end of the expression
// an expression ends with a character '\0' and token_type = delimeter
if (m_tokenType!=DELIMITER || !m_token.isEmpty())
{
{
if (m_token=="(" || m_token==")")
{
m_err=QCString("Unexpected parenthesis ")+m_token+"'";
}
else
{
// user entered a not existing operator like "//"
m_err=QCString("Unexpected operator ")+m_token+"'";
}
}
else
{
m_err=QCString("Unexpected part '")+m_token+"'";
}
}
#endif
}
if (m_err)
{
warn(fileName,lineNr,"problem evaluating expression '%s': %s",
expr,m_err.data());
}
//printf("expr='%s' answer=%d\n",expr,answer);
return answer;
}
bool CondParser::parseLevel1 ( )
private

conditional operators AND and OR

Definition at line 194 of file condparser.cpp.

References AND, evalOperator(), getOperatorId(), getToken(), m_token, OR, and parseLevel2().

Referenced by parse(), and parseLevel3().

{
bool ans = parseLevel2();
int opId = getOperatorId(m_token);
while (opId==AND || opId==OR)
{
ans = evalOperator(opId, ans, parseLevel2());
}
return ans;
}
bool CondParser::parseLevel2 ( )
private

NOT

Definition at line 212 of file condparser.cpp.

References getOperatorId(), getToken(), m_token, NOT, and parseLevel3().

Referenced by parseLevel1().

{
bool ans;
int opId = getOperatorId(m_token);
if (opId == NOT)
{
ans = !parseLevel3();
}
else
{
ans = parseLevel3();
}
return ans;
}
bool CondParser::parseLevel3 ( )
private

parenthesized expression or variable

Definition at line 233 of file condparser.cpp.

References DELIMITER, getToken(), m_err, m_token, m_tokenType, parseLevel1(), and parseVar().

Referenced by parseLevel2().

{
// check if it is a parenthesized expression
{
if (m_token=="(")
{
int ans = parseLevel1();
{
m_err="Parenthesis ) missing";
return FALSE;
}
return ans;
}
}
// if not parenthesized then the expression is a variable
return parseVar();
}
bool CondParser::parseVar ( )
private

Definition at line 257 of file condparser.cpp.

References evalVariable(), getToken(), m_err, m_token, m_tokenType, and VARIABLE.

Referenced by parseLevel3().

{
bool ans = 0;
switch (m_tokenType)
{
case VARIABLE:
// this is a variable
break;
default:
// syntax error or unexpected end of expression
if (m_token.isEmpty())
{
m_err="Unexpected end of expression";
return FALSE;
}
else
{
m_err="Value expected";
return FALSE;
}
break;
}
return ans;
}

Member Data Documentation

const char* CondParser::m_e
private

points to a character in expr

Definition at line 56 of file condparser.h.

Referenced by getToken(), and parse().

QCString CondParser::m_err
private

error state

Definition at line 54 of file condparser.h.

Referenced by evalOperator(), getToken(), parse(), parseLevel3(), and parseVar().

QCString CondParser::m_expr
private

holds the expression

Definition at line 55 of file condparser.h.

Referenced by parse().

QCString CondParser::m_token
private

holds the token

Definition at line 58 of file condparser.h.

Referenced by getToken(), parse(), parseLevel1(), parseLevel2(), parseLevel3(), and parseVar().

TOKENTYPE CondParser::m_tokenType
private

type of the token

Definition at line 59 of file condparser.h.

Referenced by getToken(), parse(), parseLevel3(), and parseVar().


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