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

Copyright (C) 1997-2026 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-2026 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 28 of file condparser.h.

Member Enumeration Documentation

◆ OPERATOR_ID

Enumerator
UNKNOWN_OP 
AND 
OR 
NOT 

Definition at line 44 of file condparser.h.

45 {
46 UNKNOWN_OP = -1,
47 AND = 1,
48 OR,
49 NOT
50 };

◆ TOKENTYPE

enum CondParser::TOKENTYPE
private
Enumerator
NOTHING 
DELIMITER 
VARIABLE 
UNKNOWN 

Definition at line 37 of file condparser.h.

38 {
39 NOTHING = -1,
43 };

Constructor & Destructor Documentation

◆ CondParser()

CondParser::CondParser ( )
inline

Definition at line 32 of file condparser.h.

32: m_e(nullptr), m_tokenType(NOTHING) {}
TOKENTYPE m_tokenType
type of the token
Definition condparser.h:60
const char * m_e
points to a character in expr
Definition condparser.h:57

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

273{
274 switch (opId)
275 {
276 // level 2
277 case AND: return lhs && rhs;
278 case OR: return lhs || rhs;
279 }
280
281 m_err = "Internal error unknown operator: id="+DString().setNum(opId);
282 return false;
283}
DString m_err
error state
Definition condparser.h:55
DString & setNum(short n)
Definition dstring.h:552

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

Referenced by parseLevel1().

◆ evalVariable()

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

evaluate a variable

Definition at line 288 of file condparser.cpp.

289{
290 if (varName == "YES") return true;
291 if (varName == "NO") return false;
292 StringVector list = Config_getList(ENABLED_SECTIONS);
293 return std::find(list.begin(),list.end(),varName.str())!=list.end();
294}
const std::string & str() const
Definition dstring.h:645
#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 99 of file condparser.cpp.

100{
101 // level 2
102 if (opName=="&&") { return AND; }
103 if (opName=="||") { return OR; }
104
105 // not operator
106 if (opName=="!") { return NOT; }
107
108 return UNKNOWN_OP;
109}

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

117{
119 m_token.clear();
120
121 //printf("\tgetToken e:{%c}, ascii=%i, col=%i\n", *e, *e, e-expr);
122
123 // skip over whitespaces
124 while (*m_e == ' ' || *m_e == '\t' || *m_e == '\n') // space or tab or newline
125 {
126 m_e++;
127 }
128
129 // check for end of expression
130 if (*m_e=='\0')
131 {
132 // token is still empty
134 return;
135 }
136
137 // check for parentheses
138 if (*m_e == '(' || *m_e == ')')
139 {
141 m_token += *m_e++;
142 return;
143 }
144
145 // check for operators (delimiters)
146 if (isDelimiter(*m_e))
147 {
149 while (isDelimiter(*m_e))
150 {
151 m_token += *m_e++;
152 }
153 return;
154 }
155
156 // check for variables
157 if (isAlpha(*m_e))
158 {
160 while (isAlphaNumSpec(*m_e))
161 {
162 m_token += *m_e++;
163 }
164 return;
165 }
166
167 // something unknown is found, wrong characters -> a syntax error
169 while (*m_e)
170 {
171 m_token += *m_e++;
172 }
173 m_err = DString("Syntax error in part '")+m_token+"'";
174 return;
175}
DString m_token
holds the token
Definition condparser.h:59
void clear()
Definition dstring.h:214
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, 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 45 of file condparser.cpp.

46{
47 if (expr.empty()) return false;
49 m_expr = resolveConfig(fileName, lineNr, expr);
50
51 // initialize all variables
52 m_e = m_expr.data(); // let m_e point to the start of the expression
53
54 bool answer=false;
55 getToken();
57 {
58 // empty expression: answer==false
59 }
60 else if (m_err.empty())
61 {
62 answer = parseLevel1();
63 }
64 if (!m_err.empty())
65 {
66 warn(fileName,lineNr,"problem evaluating expression '{}': {}", expr, m_err);
67 }
68 //printf("expr='%s' answer=%d\n",expr,answer);
69 return answer;
70}
DString m_expr
holds the expression
Definition condparser.h:56
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:148
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:157
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 181 of file condparser.cpp.

182{
183 bool ans = parseLevel2();
184 int opId = getOperatorId(m_token);
185
186 while (opId==AND || opId==OR)
187 {
188 getToken();
189 ans = evalOperator(opId, ans, parseLevel2());
190 opId = getOperatorId(m_token);
191 }
192
193 return ans;
194}
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 199 of file condparser.cpp.

200{
201 int opId = getOperatorId(m_token);
202 if (opId == NOT)
203 {
204 getToken();
205 return !parseLevel3();
206 }
207 else
208 {
209 return parseLevel3();
210 }
211}
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 217 of file condparser.cpp.

218{
219 // check if it is a parenthesized expression
220 if (m_tokenType == DELIMITER)
221 {
222 if (m_token=="(")
223 {
224 getToken();
225 bool ans = parseLevel1();
226 if (m_tokenType!=DELIMITER || m_token!=")")
227 {
228 m_err="Parenthesis ) missing";
229 return false;
230 }
231 getToken();
232 return ans;
233 }
234 }
235
236 // if not parenthesized then the expression is a variable
237 return parseVar();
238}
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 241 of file condparser.cpp.

242{
243 bool ans = false;
244 switch (m_tokenType)
245 {
246 case VARIABLE:
247 // this is a variable
248 ans = evalVariable(m_token);
249 getToken();
250 break;
251
252 default:
253 // syntax error or unexpected end of expression
254 if (m_token.empty())
255 {
256 m_err="Unexpected end of expression";
257 return false;
258 }
259 else
260 {
261 m_err="Value expected";
262 return false;
263 }
264 break;
265 }
266 return ans;
267}
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 57 of file condparser.h.

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

◆ m_err

DString CondParser::m_err
private

error state

Definition at line 55 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 56 of file condparser.h.

Referenced by parse().

◆ m_token

DString CondParser::m_token
private

holds the token

Definition at line 59 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 60 of file condparser.h.

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


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