2 * Copyright (C) 1997-2015 by Dimitri van Heesch.
4 * Permission to use, copy, modify, and distribute this software and its
5 * documentation under the terms of the GNU General Public License is hereby
6 * granted. No representations are made about the suitability of this software
7 * for any purpose. It is provided "as is" without express or implied warranty.
8 * See the GNU General Public License for more details.
10 * Documents produced by Doxygen are derivative works derived from the
11 * input used in their production; they are not affected by this license.
13 * C++ Expression parser for ENABLED_SECTIONS in Doxygen
22 #include "condparser.h"
29 * parses and evaluates the given expression.
31 * - On error, an error message is returned.
32 * - On success, the result of the expression is either "1" or "0".
34 bool CondParser::parse(const char *fileName,int lineNr,const char *expr)
37 m_tokenType = NOTHING;
39 // initialize all variables
40 m_e = m_expr; // let m_e point to the start of the expression
44 if (m_tokenType==DELIMITER && m_token.isEmpty())
46 // empty expression: answer==FALSE
48 else if (m_err.isEmpty())
50 answer = parseLevel1();
53 // check for garbage at the end of the expression
54 // an expression ends with a character '\0' and token_type = delimiter
55 if (m_tokenType!=DELIMITER || !m_token.isEmpty())
57 if (m_tokenType == DELIMITER)
59 if (m_token=="(" || m_token==")")
61 m_err=QCString("Unexpected parenthesis ")+m_token+"'";
65 // user entered a not existing operator like "//"
66 m_err=QCString("Unexpected operator ")+m_token+"'";
71 m_err=QCString("Unexpected part '")+m_token+"'";
78 warn(fileName,lineNr,"problem evaluating expression '%s': %s",
81 //printf("expr='%s' answer=%d\n",expr,answer);
87 * checks if the given char c is a delimiter
88 * minus is checked apart, can be unary minus
90 static bool isDelimiter(const char c)
92 return c=='&' || c=='|' || c=='!';
96 * checks if the given char c is a letter or underscore
98 static bool isAlpha(const char c)
100 return (c>='A' && c<='Z') || (c>='a' && c<='z') || c=='_';
103 static bool isAlphaNum(const char c)
105 return isAlpha(c) || (c>='0' && c<='9');
109 * returns the id of the given operator
110 * returns -1 if the operator is not recognized
112 int CondParser::getOperatorId(const QCString &opName)
115 if (opName=="&&") { return AND; }
116 if (opName=="||") { return OR; }
119 if (opName=="!") { return NOT; }
125 * Get next token in the current string expr.
126 * Uses the data in m_expr pointed to by m_e to
127 * produce m_tokenType and m_token, set m_err in case of an error
129 void CondParser::getToken()
131 m_tokenType = NOTHING;
134 //printf("\tgetToken e:{%c}, ascii=%i, col=%i\n", *e, *e, e-expr);
136 // skip over whitespaces
137 while (*m_e == ' ' || *m_e == '\t') // space or tab
142 // check for end of expression
145 // token is still empty
146 m_tokenType = DELIMITER;
150 // check for parentheses
151 if (*m_e == '(' || *m_e == ')')
153 m_tokenType = DELIMITER;
158 // check for operators (delimiters)
159 if (isDelimiter(*m_e))
161 m_tokenType = DELIMITER;
162 while (isDelimiter(*m_e))
169 // check for variables
172 m_tokenType = VARIABLE;
173 while (isAlphaNum(*m_e))
180 // something unknown is found, wrong characters -> a syntax error
181 m_tokenType = UNKNOWN;
186 m_err = QCString("Syntax error in part '")+m_token+"'";
192 * conditional operators AND and OR
194 bool CondParser::parseLevel1()
196 bool ans = parseLevel2();
197 int opId = getOperatorId(m_token);
199 while (opId==AND || opId==OR)
202 ans = evalOperator(opId, ans, parseLevel2());
203 opId = getOperatorId(m_token);
212 bool CondParser::parseLevel2()
215 int opId = getOperatorId(m_token);
219 ans = !parseLevel3();
231 * parenthesized expression or variable
233 bool CondParser::parseLevel3()
235 // check if it is a parenthesized expression
236 if (m_tokenType == DELIMITER)
241 int ans = parseLevel1();
242 if (m_tokenType!=DELIMITER || m_token!=")")
244 m_err="Parenthesis ) missing";
252 // if not parenthesized then the expression is a variable
257 bool CondParser::parseVar()
263 // this is a variable
264 ans = evalVariable(m_token);
269 // syntax error or unexpected end of expression
270 if (m_token.isEmpty())
272 m_err="Unexpected end of expression";
277 m_err="Value expected";
286 * evaluate an operator for given valuess
288 bool CondParser::evalOperator(int opId, bool lhs, bool rhs)
293 case AND: return lhs && rhs;
294 case OR: return lhs || rhs;
297 m_err = "Internal error unknown operator: id="+QCString().setNum(opId);
302 * evaluate a variable
304 bool CondParser::evalVariable(const char *varName)
306 if (Config_getList(ENABLED_SECTIONS).find(varName)==-1) return FALSE;