Skip to main content

The CondParser Class Reference

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

Declaration

class CondParser { ... }

Included Headers

#include <src/condparser.h>

Enumerations Index

enumTOKENTYPE { ... }
enumOPERATOR_ID { ... }

Public Constructors Index

CondParser ()

Public Member Functions Index

boolparse (const QCString &fileName, int lineNr, const QCString &expr)

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

Private Member Functions Index

voidgetToken ()

Get next token in the current string expr. More...

boolparseLevel1 ()

conditional operators AND and OR More...

boolparseLevel2 ()

NOT. More...

boolparseLevel3 ()

parenthesized expression or variable More...

boolparseVar ()
boolevalOperator (const int opId, bool lhs, bool rhs)

evaluate an operator for given values More...

boolevalVariable (const QCString &varName)

evaluate a variable More...

intgetOperatorId (const QCString &opName)

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

Private Member Attributes Index

QCStringm_err

error state More...

QCStringm_expr

holds the expression More...

const char *m_e

points to a character in expr More...

QCStringm_token

holds the token More...

TOKENTYPEm_tokenType

type of the token More...

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.

Enumerations

OPERATOR_ID

enum CondParser::OPERATOR_ID
Enumeration values
UNKNOWN_OP (= -1)
AND (= 1)
OR
NOT

Definition at line 43 of file condparser.h.

44 {
46 AND = 1,
49 };

TOKENTYPE

enum CondParser::TOKENTYPE
Enumeration values
NOTHING (= -1)
DELIMITER
VARIABLE
UNKNOWN

Definition at line 36 of file condparser.h.

37 {
38 NOTHING = -1,
42 };

Public Constructors

CondParser()

CondParser::CondParser ()
inline

Definition at line 31 of file condparser.h.

31 CondParser() : m_e(nullptr), m_tokenType(NOTHING) {}

References m_e, m_tokenType and NOTHING.

Public Member Functions

parse()

bool CondParser::parse (const QCString & fileName, int lineNr, const QCString & 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".

Declaration at line 32 of file condparser.h, definition at line 36 of file condparser.cpp.

36bool CondParser::parse(const QCString &fileName,int lineNr,const QCString &expr)
37{
38 if (expr.isEmpty()) return false;
39 m_expr = expr;
41
42 // initialize all variables
43 m_e = m_expr.data(); // let m_e point to the start of the expression
44
45 bool answer=false;
46 getToken();
47 if (m_tokenType==DELIMITER && m_token.isEmpty())
48 {
49 // empty expression: answer==FALSE
50 }
51 else if (m_err.isEmpty())
52 {
53 answer = parseLevel1();
54 }
55 if (!m_err.isEmpty())
56 {
57 warn(fileName,lineNr,"problem evaluating expression '{}': {}", expr, m_err);
58 }
59 //printf("expr='%s' answer=%d\n",expr,answer);
60 return answer;
61}

References DELIMITER, getToken, QCString::isEmpty, m_e, m_err, m_expr, m_token, m_tokenType, NOTHING, parseLevel1 and warn.

Referenced by handleGuard, startCondSection and startCondSection.

Private Member Functions

evalOperator()

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

evaluate an operator for given values

Declaration at line 70 of file condparser.h, definition at line 263 of file condparser.cpp.

263bool CondParser::evalOperator(int opId, bool lhs, bool rhs)
264{
265 switch (opId)
266 {
267 // level 2
268 case AND: return lhs && rhs;
269 case OR: return lhs || rhs;
270 }
271
272 m_err = "Internal error unknown operator: id="+QCString().setNum(opId);
273 return FALSE;
274}

References AND, FALSE, m_err, OR and QCString::setNum.

Referenced by parseLevel1.

evalVariable()

bool CondParser::evalVariable (const QCString & varName)

evaluate a variable

Declaration at line 71 of file condparser.h, definition at line 279 of file condparser.cpp.

280{
281 const StringVector &list = Config_getList(ENABLED_SECTIONS);
282 return std::find(list.begin(),list.end(),varName.str())!=list.end();
283}

References Config_getList and QCString::str.

Referenced by parseVar.

getOperatorId()

int CondParser::getOperatorId (const QCString & opName)

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

Declaration at line 72 of file condparser.h, definition at line 90 of file condparser.cpp.

91{
92 // level 2
93 if (opName=="&&") { return AND; }
94 if (opName=="||") { return OR; }
95
96 // not operator
97 if (opName=="!") { return NOT; }
98
99 return UNKNOWN_OP;
100}

References AND, NOT, OR and UNKNOWN_OP.

Referenced by parseLevel1 and parseLevel2.

getToken()

void CondParser::getToken ()

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

Declaration at line 63 of file condparser.h, definition at line 107 of file condparser.cpp.

108{
110 m_token.clear();
111
112 //printf("\tgetToken e:{%c}, ascii=%i, col=%i\n", *e, *e, e-expr);
113
114 // skip over whitespaces
115 while (*m_e == ' ' || *m_e == '\t' || *m_e == '\n') // space or tab or newline
116 {
117 m_e++;
118 }
119
120 // check for end of expression
121 if (*m_e=='\0')
122 {
123 // token is still empty
125 return;
126 }
127
128 // check for parentheses
129 if (*m_e == '(' || *m_e == ')')
130 {
132 m_token += *m_e++;
133 return;
134 }
135
136 // check for operators (delimiters)
137 if (isDelimiter(*m_e))
138 {
140 while (isDelimiter(*m_e))
141 {
142 m_token += *m_e++;
143 }
144 return;
145 }
146
147 // check for variables
148 if (isAlpha(*m_e))
149 {
151 while (isAlphaNumSpec(*m_e))
152 {
153 m_token += *m_e++;
154 }
155 return;
156 }
157
158 // something unknown is found, wrong characters -> a syntax error
160 while (*m_e)
161 {
162 m_token += *m_e++;
163 }
164 m_err = QCString("Syntax error in part '")+m_token+"'";
165 return;
166}

References DELIMITER, isAlpha, isAlphaNumSpec, isDelimiter, m_e, m_err, m_token, m_tokenType, NOTHING, UNKNOWN and VARIABLE.

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

parseLevel1()

bool CondParser::parseLevel1 ()

conditional operators AND and OR

Declaration at line 65 of file condparser.h, definition at line 172 of file condparser.cpp.

173{
174 bool ans = parseLevel2();
175 int opId = getOperatorId(m_token);
176
177 while (opId==AND || opId==OR)
178 {
179 getToken();
180 ans = evalOperator(opId, ans, parseLevel2());
181 opId = getOperatorId(m_token);
182 }
183
184 return ans;
185}

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

Referenced by parse and parseLevel3.

parseLevel2()

bool CondParser::parseLevel2 ()

NOT.

Declaration at line 66 of file condparser.h, definition at line 190 of file condparser.cpp.

191{
192 int opId = getOperatorId(m_token);
193 if (opId == NOT)
194 {
195 getToken();
196 return !parseLevel3();
197 }
198 else
199 {
200 return parseLevel3();
201 }
202}

References getOperatorId, getToken, m_token, NOT and parseLevel3.

Referenced by parseLevel1.

parseLevel3()

bool CondParser::parseLevel3 ()

parenthesized expression or variable

Declaration at line 67 of file condparser.h, definition at line 208 of file condparser.cpp.

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

References DELIMITER, FALSE, getToken, m_err, m_token, m_tokenType, parseLevel1 and parseVar.

Referenced by parseLevel2.

parseVar()

bool CondParser::parseVar ()

Declaration at line 68 of file condparser.h, definition at line 232 of file condparser.cpp.

233{
234 bool ans = false;
235 switch (m_tokenType)
236 {
237 case VARIABLE:
238 // this is a variable
239 ans = evalVariable(m_token);
240 getToken();
241 break;
242
243 default:
244 // syntax error or unexpected end of expression
245 if (m_token.isEmpty())
246 {
247 m_err="Unexpected end of expression";
248 return FALSE;
249 }
250 else
251 {
252 m_err="Value expected";
253 return FALSE;
254 }
255 break;
256 }
257 return ans;
258}

References evalVariable, FALSE, getToken, m_err, m_token, m_tokenType and VARIABLE.

Referenced by parseLevel3.

Private Member Attributes

m_e

const char* CondParser::m_e

points to a character in expr

Definition at line 56 of file condparser.h.

56 const char *m_e; //!< points to a character in expr

Referenced by CondParser, getToken and parse.

m_err

QCString CondParser::m_err

error state

Definition at line 54 of file condparser.h.

54 QCString m_err; //!< error state

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

m_expr

QCString CondParser::m_expr

holds the expression

Definition at line 55 of file condparser.h.

55 QCString m_expr; //!< holds the expression

Referenced by parse.

m_token

QCString CondParser::m_token

holds the token

Definition at line 58 of file condparser.h.

58 QCString m_token; //!< holds the token

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

m_tokenType

TOKENTYPE CondParser::m_tokenType

type of the token

Definition at line 59 of file condparser.h.

59 TOKENTYPE m_tokenType; //!< type of the token

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


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


Generated via doxygen2docusaurus by Doxygen 1.14.0.