Blame src/condparser.cpp

Packit Service 50c9f2
/**
Packit Service 50c9f2
 * Copyright (C) 1997-2015 by Dimitri van Heesch.
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * Permission to use, copy, modify, and distribute this software and its
Packit Service 50c9f2
 * documentation under the terms of the GNU General Public License is hereby 
Packit Service 50c9f2
 * granted. No representations are made about the suitability of this software 
Packit Service 50c9f2
 * for any purpose. It is provided "as is" without express or implied warranty.
Packit Service 50c9f2
 * See the GNU General Public License for more details.
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * Documents produced by Doxygen are derivative works derived from the
Packit Service 50c9f2
 * input used in their production; they are not affected by this license.
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * C++ Expression parser for ENABLED_SECTIONS in Doxygen
Packit Service 50c9f2
 *
Packit Service 50c9f2
 * Features used:
Packit Service 50c9f2
 *     Operators:
Packit Service 50c9f2
 *         &&    AND operator
Packit Service 50c9f2
 *         ||    OR  operator
Packit Service 50c9f2
 *         !     NOT operator
Packit Service 50c9f2
 */
Packit Service 50c9f2
Packit Service 50c9f2
#include "condparser.h"
Packit Service 50c9f2
#include "config.h"
Packit Service 50c9f2
#include "message.h"
Packit Service 50c9f2
Packit Service 50c9f2
// declarations
Packit Service 50c9f2
Packit Service 50c9f2
/**
Packit Service 50c9f2
 * parses and evaluates the given expression.
Packit Service 50c9f2
 * @returns 
Packit Service 50c9f2
 * - On error, an error message is returned.
Packit Service 50c9f2
 * - On success, the result of the expression is either "1" or "0".
Packit Service 50c9f2
 */
Packit Service 50c9f2
bool CondParser::parse(const char *fileName,int lineNr,const char *expr)
Packit Service 50c9f2
{
Packit Service 50c9f2
  m_expr      = expr;
Packit Service 50c9f2
  m_tokenType = NOTHING;
Packit Service 50c9f2
Packit Service 50c9f2
  // initialize all variables
Packit Service 50c9f2
  m_e = m_expr;    // let m_e point to the start of the expression
Packit Service 50c9f2
Packit Service 50c9f2
  bool answer=FALSE;
Packit Service 50c9f2
  getToken();
Packit Service 50c9f2
  if (m_tokenType==DELIMITER && m_token.isEmpty())
Packit Service 50c9f2
  {
Packit Service 50c9f2
    // empty expression: answer==FALSE
Packit Service 50c9f2
  }
Packit Service 50c9f2
  else if (m_err.isEmpty())
Packit Service 50c9f2
  {
Packit Service 50c9f2
    answer = parseLevel1();
Packit Service 50c9f2
Packit Service 50c9f2
#if 0
Packit Service 50c9f2
    // check for garbage at the end of the expression
Packit Service 50c9f2
    // an expression ends with a character '\0' and token_type = delimiter
Packit Service 50c9f2
    if (m_tokenType!=DELIMITER || !m_token.isEmpty())
Packit Service 50c9f2
    {
Packit Service 50c9f2
      if (m_tokenType == DELIMITER)
Packit Service 50c9f2
      {
Packit Service 50c9f2
        if (m_token=="(" || m_token==")")
Packit Service 50c9f2
        {
Packit Service 50c9f2
          m_err=QCString("Unexpected parenthesis ")+m_token+"'";
Packit Service 50c9f2
        }
Packit Service 50c9f2
        else
Packit Service 50c9f2
        {
Packit Service 50c9f2
          // user entered a not existing operator like "//"
Packit Service 50c9f2
          m_err=QCString("Unexpected operator ")+m_token+"'";
Packit Service 50c9f2
        }
Packit Service 50c9f2
      }
Packit Service 50c9f2
      else
Packit Service 50c9f2
      {
Packit Service 50c9f2
        m_err=QCString("Unexpected part '")+m_token+"'";
Packit Service 50c9f2
      }
Packit Service 50c9f2
    }
Packit Service 50c9f2
#endif
Packit Service 50c9f2
  }
Packit Service 50c9f2
  if (m_err)
Packit Service 50c9f2
  {
Packit Service 50c9f2
    warn(fileName,lineNr,"problem evaluating expression '%s': %s",
Packit Service 50c9f2
        expr,m_err.data());
Packit Service 50c9f2
  }
Packit Service 50c9f2
  //printf("expr='%s' answer=%d\n",expr,answer);
Packit Service 50c9f2
  return answer;
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
Packit Service 50c9f2
/**
Packit Service 50c9f2
 * checks if the given char c is a delimiter
Packit Service 50c9f2
 * minus is checked apart, can be unary minus
Packit Service 50c9f2
 */
Packit Service 50c9f2
static bool isDelimiter(const char c)
Packit Service 50c9f2
{
Packit Service 50c9f2
  return c=='&' || c=='|' || c=='!';
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/**
Packit Service 50c9f2
 * checks if the given char c is a letter or underscore
Packit Service 50c9f2
 */
Packit Service 50c9f2
static bool isAlpha(const char c)
Packit Service 50c9f2
{
Packit Service 50c9f2
  return (c>='A' && c<='Z') || (c>='a' && c<='z') || c=='_';
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
static bool isAlphaNum(const char c)
Packit Service 50c9f2
{
Packit Service 50c9f2
  return isAlpha(c) || (c>='0' && c<='9');
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/**
Packit Service 50c9f2
 * returns the id of the given operator
Packit Service 50c9f2
 * returns -1 if the operator is not recognized
Packit Service 50c9f2
 */
Packit Service 50c9f2
int CondParser::getOperatorId(const QCString &opName)
Packit Service 50c9f2
{
Packit Service 50c9f2
  // level 2
Packit Service 50c9f2
  if (opName=="&&") { return AND; }
Packit Service 50c9f2
  if (opName=="||") { return OR;  }
Packit Service 50c9f2
Packit Service 50c9f2
  // not operator
Packit Service 50c9f2
  if (opName=="!")  { return NOT; }
Packit Service 50c9f2
Packit Service 50c9f2
  return UNKNOWN_OP;
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/**
Packit Service 50c9f2
 * Get next token in the current string expr.
Packit Service 50c9f2
 * Uses the data in m_expr pointed to by m_e to 
Packit Service 50c9f2
 * produce m_tokenType and m_token, set m_err in case of an error
Packit Service 50c9f2
 */
Packit Service 50c9f2
void CondParser::getToken()
Packit Service 50c9f2
{
Packit Service 50c9f2
  m_tokenType = NOTHING;
Packit Service 50c9f2
  m_token.resize(0);     
Packit Service 50c9f2
Packit Service 50c9f2
  //printf("\tgetToken e:{%c}, ascii=%i, col=%i\n", *e, *e, e-expr);
Packit Service 50c9f2
Packit Service 50c9f2
  // skip over whitespaces
Packit Service 50c9f2
  while (*m_e == ' ' || *m_e == '\t')     // space or tab
Packit Service 50c9f2
  {
Packit Service 50c9f2
    m_e++;
Packit Service 50c9f2
  }
Packit Service 50c9f2
Packit Service 50c9f2
  // check for end of expression
Packit Service 50c9f2
  if (*m_e=='\0')
Packit Service 50c9f2
  {
Packit Service 50c9f2
    // token is still empty
Packit Service 50c9f2
    m_tokenType = DELIMITER;
Packit Service 50c9f2
    return;
Packit Service 50c9f2
  }
Packit Service 50c9f2
Packit Service 50c9f2
  // check for parentheses
Packit Service 50c9f2
  if (*m_e == '(' || *m_e == ')')
Packit Service 50c9f2
  {
Packit Service 50c9f2
    m_tokenType = DELIMITER;
Packit Service 50c9f2
    m_token += *m_e++;
Packit Service 50c9f2
    return;
Packit Service 50c9f2
  }
Packit Service 50c9f2
Packit Service 50c9f2
  // check for operators (delimiters)
Packit Service 50c9f2
  if (isDelimiter(*m_e))
Packit Service 50c9f2
  {
Packit Service 50c9f2
    m_tokenType = DELIMITER;
Packit Service 50c9f2
    while (isDelimiter(*m_e))
Packit Service 50c9f2
    {
Packit Service 50c9f2
      m_token += *m_e++;
Packit Service 50c9f2
    }
Packit Service 50c9f2
    return;
Packit Service 50c9f2
  }
Packit Service 50c9f2
Packit Service 50c9f2
  // check for variables
Packit Service 50c9f2
  if (isAlpha(*m_e))
Packit Service 50c9f2
  {
Packit Service 50c9f2
    m_tokenType = VARIABLE;
Packit Service 50c9f2
    while (isAlphaNum(*m_e))
Packit Service 50c9f2
    {
Packit Service 50c9f2
      m_token += *m_e++;
Packit Service 50c9f2
    }
Packit Service 50c9f2
    return;
Packit Service 50c9f2
  }
Packit Service 50c9f2
Packit Service 50c9f2
  // something unknown is found, wrong characters -> a syntax error
Packit Service 50c9f2
  m_tokenType = UNKNOWN;
Packit Service 50c9f2
  while (*m_e)
Packit Service 50c9f2
  {
Packit Service 50c9f2
    m_token += *m_e++;
Packit Service 50c9f2
  }
Packit Service 50c9f2
  m_err = QCString("Syntax error in part '")+m_token+"'";
Packit Service 50c9f2
  return;
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
Packit Service 50c9f2
/**
Packit Service 50c9f2
 * conditional operators AND and OR
Packit Service 50c9f2
 */
Packit Service 50c9f2
bool CondParser::parseLevel1()
Packit Service 50c9f2
{
Packit Service 50c9f2
  bool ans = parseLevel2();
Packit Service 50c9f2
  int opId = getOperatorId(m_token);
Packit Service 50c9f2
Packit Service 50c9f2
  while (opId==AND || opId==OR)
Packit Service 50c9f2
  {
Packit Service 50c9f2
    getToken();
Packit Service 50c9f2
    ans = evalOperator(opId, ans, parseLevel2());
Packit Service 50c9f2
    opId = getOperatorId(m_token);
Packit Service 50c9f2
  }
Packit Service 50c9f2
Packit Service 50c9f2
  return ans;
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/**
Packit Service 50c9f2
 * NOT
Packit Service 50c9f2
 */
Packit Service 50c9f2
bool CondParser::parseLevel2()
Packit Service 50c9f2
{
Packit Service 50c9f2
  bool ans;
Packit Service 50c9f2
  int opId = getOperatorId(m_token);
Packit Service 50c9f2
  if (opId == NOT)
Packit Service 50c9f2
  {
Packit Service 50c9f2
    getToken();
Packit Service 50c9f2
    ans = !parseLevel3();
Packit Service 50c9f2
  }
Packit Service 50c9f2
  else
Packit Service 50c9f2
  {
Packit Service 50c9f2
    ans = parseLevel3();
Packit Service 50c9f2
  }
Packit Service 50c9f2
Packit Service 50c9f2
  return ans;
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
Packit Service 50c9f2
/**
Packit Service 50c9f2
 * parenthesized expression or variable
Packit Service 50c9f2
 */
Packit Service 50c9f2
bool CondParser::parseLevel3()
Packit Service 50c9f2
{
Packit Service 50c9f2
  // check if it is a parenthesized expression
Packit Service 50c9f2
  if (m_tokenType == DELIMITER)
Packit Service 50c9f2
  {
Packit Service 50c9f2
    if (m_token=="(")
Packit Service 50c9f2
    {
Packit Service 50c9f2
      getToken();
Packit Service 50c9f2
      int ans = parseLevel1();
Packit Service 50c9f2
      if (m_tokenType!=DELIMITER || m_token!=")")
Packit Service 50c9f2
      {
Packit Service 50c9f2
        m_err="Parenthesis ) missing";
Packit Service 50c9f2
        return FALSE;
Packit Service 50c9f2
      }
Packit Service 50c9f2
      getToken();
Packit Service 50c9f2
      return ans;
Packit Service 50c9f2
    }
Packit Service 50c9f2
  }
Packit Service 50c9f2
Packit Service 50c9f2
  // if not parenthesized then the expression is a variable
Packit Service 50c9f2
  return parseVar();
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
Packit Service 50c9f2
bool CondParser::parseVar()
Packit Service 50c9f2
{
Packit Service 50c9f2
  bool ans = 0;
Packit Service 50c9f2
  switch (m_tokenType)
Packit Service 50c9f2
  {
Packit Service 50c9f2
    case VARIABLE:
Packit Service 50c9f2
      // this is a variable
Packit Service 50c9f2
      ans = evalVariable(m_token);
Packit Service 50c9f2
      getToken();
Packit Service 50c9f2
      break;
Packit Service 50c9f2
Packit Service 50c9f2
    default:
Packit Service 50c9f2
      // syntax error or unexpected end of expression
Packit Service 50c9f2
      if (m_token.isEmpty())
Packit Service 50c9f2
      {
Packit Service 50c9f2
        m_err="Unexpected end of expression";
Packit Service 50c9f2
        return FALSE;
Packit Service 50c9f2
      }
Packit Service 50c9f2
      else
Packit Service 50c9f2
      {
Packit Service 50c9f2
        m_err="Value expected";
Packit Service 50c9f2
        return FALSE;
Packit Service 50c9f2
      }
Packit Service 50c9f2
      break;
Packit Service 50c9f2
  }
Packit Service 50c9f2
  return ans;
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/**
Packit Service 50c9f2
 * evaluate an operator for given valuess
Packit Service 50c9f2
 */
Packit Service 50c9f2
bool CondParser::evalOperator(int opId, bool lhs, bool rhs)
Packit Service 50c9f2
{
Packit Service 50c9f2
  switch (opId)
Packit Service 50c9f2
  {
Packit Service 50c9f2
    // level 2
Packit Service 50c9f2
    case AND: return lhs && rhs;
Packit Service 50c9f2
    case OR:  return lhs || rhs;
Packit Service 50c9f2
  }
Packit Service 50c9f2
Packit Service 50c9f2
  m_err = "Internal error unknown operator: id="+QCString().setNum(opId);
Packit Service 50c9f2
  return FALSE;
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
/**
Packit Service 50c9f2
 * evaluate a variable
Packit Service 50c9f2
 */
Packit Service 50c9f2
bool CondParser::evalVariable(const char *varName)
Packit Service 50c9f2
{
Packit Service 50c9f2
  if (Config_getList(ENABLED_SECTIONS).find(varName)==-1) return FALSE;
Packit Service 50c9f2
  return TRUE;
Packit Service 50c9f2
}
Packit Service 50c9f2