Doxygen
Loading...
Searching...
No Matches
CondParser Class Reference

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

#include <src/condparser.h>

Collaboration diagram for CondParser:

Public Member Functions

 CondParser ()
bool parse (const DString &fileName, int lineNr, const DString &expr)
 parses and evaluates the given expression.

Private Types

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

Private Member Functions

void getToken ()
 Get next token in the current string expr.
bool parseLevel1 ()
 conditional operators AND and OR
bool parseLevel2 ()
 NOT.
bool parseLevel3 ()
 parenthesized expression or variable
bool parseVar ()
bool evalOperator (const int opId, bool lhs, bool rhs)
 evaluate an operator for given values
bool evalVariable (const DString &varName)
 evaluate a variable
int getOperatorId (const DString &opName)
 returns the id of the given operator returns -1 if the operator is not recognized

Private Attributes

DString m_err
 error state
DString m_expr
 holds the expression
const char * m_e
 points to a character in expr
DString 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 ENABLED_SECTIONS in Doxygen

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

Definition at line 27 of file condparser.h.

Member Enumeration Documentation

◆ OPERATOR_ID

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 };

◆ TOKENTYPE

enum CondParser::TOKENTYPE
private
Enumerator
NOTHING 
DELIMITER 
VARIABLE 
UNKNOWN 

Definition at line 36 of file condparser.h.

37 {
38 NOTHING = -1,
42 };

Constructor & Destructor Documentation

◆ CondParser()

CondParser::CondParser ( )
inline

Definition at line 31 of file condparser.h.

31: m_e(nullptr), 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

References m_e, m_tokenType, and NOTHING.

Member Function Documentation

◆ evalOperator()

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

evaluate an operator for given values

Definition at line 268 of file condparser.cpp.

269{
270 switch (opId)
271 {
272 // level 2
273 case AND: return lhs && rhs;
274 case OR: return lhs || rhs;
275 }
276
277 m_err = "Internal error unknown operator: id="+DString().setNum(opId);
278 return false;
279}
DString m_err
error state
Definition condparser.h:54
DString & setNum(short n)
Definition dstring.h:541
DString()=default

References AND, DString::DString(), m_err, OR, and DString::setNum().

Referenced by parseLevel1().

◆ evalVariable()

bool CondParser::evalVariable ( const DString & varName)
private

evaluate a variable

Definition at line 284 of file condparser.cpp.

285{
286 if (varName == "YES") return true;
287 if (varName == "NO") return false;
288 StringVector list = Config_getList(ENABLED_SECTIONS);
289 return std::find(list.begin(),list.end(),varName.str())!=list.end();
290}
const std::string & str() const
Definition dstring.h:634
#define Config_getList(name)
Definition config.h:38
std::vector< std::string > StringVector
Definition containers.h:33

References Config_getList, and DString::str().

Referenced by parseVar().

◆ getOperatorId()

int CondParser::getOperatorId ( const DString & opName)
private

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

Definition at line 95 of file condparser.cpp.

96{
97 // level 2
98 if (opName=="&&") { return AND; }
99 if (opName=="||") { return OR; }
100
101 // not operator
102 if (opName=="!") { return NOT; }
103
104 return UNKNOWN_OP;
105}

References AND, NOT, OR, and UNKNOWN_OP.

Referenced by parseLevel1(), and parseLevel2().

◆ getToken()

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 112 of file condparser.cpp.

113{
115 m_token.clear();
116
117 //printf("\tgetToken e:{%c}, ascii=%i, col=%i\n", *e, *e, e-expr);
118
119 // skip over whitespaces
120 while (*m_e == ' ' || *m_e == '\t' || *m_e == '\n') // space or tab or newline
121 {
122 m_e++;
123 }
124
125 // check for end of expression
126 if (*m_e=='\0')
127 {
128 // token is still empty
130 return;
131 }
132
133 // check for parentheses
134 if (*m_e == '(' || *m_e == ')')
135 {
137 m_token += *m_e++;
138 return;
139 }
140
141 // check for operators (delimiters)
142 if (isDelimiter(*m_e))
143 {
145 while (isDelimiter(*m_e))
146 {
147 m_token += *m_e++;
148 }
149 return;
150 }
151
152 // check for variables
153 if (isAlpha(*m_e))
154 {
156 while (isAlphaNumSpec(*m_e))
157 {
158 m_token += *m_e++;
159 }
160 return;
161 }
162
163 // something unknown is found, wrong characters -> a syntax error
165 while (*m_e)
166 {
167 m_token += *m_e++;
168 }
169 m_err = DString("Syntax error in part '")+m_token+"'";
170 return;
171}
DString m_token
holds the token
Definition condparser.h:58
void clear()
Definition dstring.h:219
static bool isDelimiter(const char c)
checks if the given char c is a delimiter minus is checked apart, can be unary minus
static bool isAlphaNumSpec(const char c)
static bool isAlpha(const char c)
checks if the given char c is a letter or underscore

References DString::clear(), DELIMITER, DString::DString(), isAlpha(), isAlphaNumSpec(), isDelimiter(), m_e, m_err, m_token, m_tokenType, NOTHING, UNKNOWN, and VARIABLE.

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

◆ parse()

bool CondParser::parse ( const DString & fileName,
int lineNr,
const DString & expr )

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 41 of file condparser.cpp.

42{
43 if (expr.empty()) return false;
45 m_expr = resolveConfig(fileName, lineNr, expr);
46
47 // initialize all variables
48 m_e = m_expr.data(); // let m_e point to the start of the expression
49
50 bool answer=false;
51 getToken();
53 {
54 // empty expression: answer==false
55 }
56 else if (m_err.empty())
57 {
58 answer = parseLevel1();
59 }
60 if (!m_err.empty())
61 {
62 warn(fileName,lineNr,"problem evaluating expression '{}': {}", expr, m_err);
63 }
64 //printf("expr='%s' answer=%d\n",expr,answer);
65 return answer;
66}
DString m_expr
holds the expression
Definition condparser.h:55
bool parseLevel1()
conditional operators AND and OR
void getToken()
Get next token in the current string expr.
bool empty() const
Returns true iff the string is empty (std::string compatible alias for isEmpty()).
Definition dstring.h:153
const char * data() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string.
Definition dstring.h:162
static DString resolveConfig(const DString &fileName, int lineNr, const DString &expr)
#define warn(file, line, fmt,...)
Definition message.h:97

References DString::data(), DELIMITER, DString::empty(), getToken(), m_e, m_err, m_expr, m_token, m_tokenType, NOTHING, parseLevel1(), resolveConfig(), and warn.

Referenced by handleGuard(), startCondSection(), and startCondSection().

◆ parseLevel1()

bool CondParser::parseLevel1 ( )
private

conditional operators AND and OR

Definition at line 177 of file condparser.cpp.

178{
179 bool ans = parseLevel2();
180 int opId = getOperatorId(m_token);
181
182 while (opId==AND || opId==OR)
183 {
184 getToken();
185 ans = evalOperator(opId, ans, parseLevel2());
186 opId = getOperatorId(m_token);
187 }
188
189 return ans;
190}
bool parseLevel2()
NOT.
int getOperatorId(const DString &opName)
returns the id of the given operator returns -1 if the operator is not recognized
bool evalOperator(const int opId, bool lhs, bool rhs)
evaluate an operator for given values

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

Referenced by parse(), and parseLevel3().

◆ parseLevel2()

bool CondParser::parseLevel2 ( )
private

NOT.

Definition at line 195 of file condparser.cpp.

196{
197 int opId = getOperatorId(m_token);
198 if (opId == NOT)
199 {
200 getToken();
201 return !parseLevel3();
202 }
203 else
204 {
205 return parseLevel3();
206 }
207}
bool parseLevel3()
parenthesized expression or variable

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

Referenced by parseLevel1().

◆ parseLevel3()

bool CondParser::parseLevel3 ( )
private

parenthesized expression or variable

Definition at line 213 of file condparser.cpp.

214{
215 // check if it is a parenthesized expression
216 if (m_tokenType == DELIMITER)
217 {
218 if (m_token=="(")
219 {
220 getToken();
221 bool ans = parseLevel1();
222 if (m_tokenType!=DELIMITER || m_token!=")")
223 {
224 m_err="Parenthesis ) missing";
225 return false;
226 }
227 getToken();
228 return ans;
229 }
230 }
231
232 // if not parenthesized then the expression is a variable
233 return parseVar();
234}
bool parseVar()

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

Referenced by parseLevel2().

◆ parseVar()

bool CondParser::parseVar ( )
private

Definition at line 237 of file condparser.cpp.

238{
239 bool ans = false;
240 switch (m_tokenType)
241 {
242 case VARIABLE:
243 // this is a variable
244 ans = evalVariable(m_token);
245 getToken();
246 break;
247
248 default:
249 // syntax error or unexpected end of expression
250 if (m_token.empty())
251 {
252 m_err="Unexpected end of expression";
253 return false;
254 }
255 else
256 {
257 m_err="Value expected";
258 return false;
259 }
260 break;
261 }
262 return ans;
263}
bool evalVariable(const DString &varName)
evaluate a variable

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

Referenced by parseLevel3().

Member Data Documentation

◆ m_e

const char* CondParser::m_e
private

points to a character in expr

Definition at line 56 of file condparser.h.

Referenced by CondParser(), getToken(), and parse().

◆ m_err

DString CondParser::m_err
private

error state

Definition at line 54 of file condparser.h.

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

◆ m_expr

DString CondParser::m_expr
private

holds the expression

Definition at line 55 of file condparser.h.

Referenced by parse().

◆ m_token

DString CondParser::m_token
private

holds the token

Definition at line 58 of file condparser.h.

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

◆ m_tokenType

TOKENTYPE CondParser::m_tokenType
private

type of the token

Definition at line 59 of file condparser.h.

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


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