<commit_before>/*
 * ExpressionParser.h -- This file is part of ExpressionParser
 *
 * Copyright (C) 2011 <NAME>, <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

#ifndef EXPRESSIONPARSER_H
#define EXPRESSIONPARSER_H

#include <exception>
#include <string>
#include <sstream>
#include <stack>
#include <cstddef> /* std::size_t */
#include <cctype>  /* std::isspace(int) */
#include <cassert>

/**
 * ExpressionParser.
 * @brief   ExpressionParser.h is a simple C++ operator precedence
 *          parser with infix notation for integer arithmetic
 *          expressions. Its eval(const std::string&) function
 *          evaluates an arithmetic expression passed as string
 *          argument, getResult() returns the corresponding result.
 * @see     http://expressionparser.googlecode.com
 * @author  <NAME>, <<EMAIL>>
 * @version 1.2
 * @date    July, 02 2011
 *
 * == Supported operators ==
 *
 *     OPERATOR     NAME                     ASSOCIATIVITY     PRECEDENCE
 *
 *     or,  OR      Bitwise Inclusive OR     Left               4
 *     xor, XOR     Bitwise Exclusive OR     Left               5
 *     and, AND     Bitwise AND              Left               6
 *     not, NOT     Unary complement         Left              99
 *     shl, SHL     Shift Left               Left               9
 *     shr, SHR     Shift Right              Left               9
 *     +            Addition                 Left              10
 *     -            Subtraction              Left              10
 *     *            Multiplication           Left              20
 *     /            Division                 Left              20
 *     %            Modulo                   Left              20
 *     ^, **        Raise to power           Right             30
 *     e, E         Scientific notation      Right             40
 *
 * The operator precedence has been set according to (uses the C and
 * C++ operator precedence):
 * http://en.wikipedia.org/wiki/Order_of_operations
 * Operators with higher precedence are evaluated before operators
 * with relatively lower precedence.
 *
 * NOTE: Unary operators are set to have the highest precedence. This
 *       is not strictly correct for the power operator
 *       e.g. "-3^2" = 9 but a lot of software tools (Microsoft
 *       Excel, Bash shell, GNU bc, ...) use the same convention.
 *
 * == Examples of valid expressions ==
 *
 *     "2^16"                                  = 65536
 *     "2^16 shr 15"                           = 2
 *     "(0 + 0xdf234 - 1000) * 3 / 2 % 999"    = 828
 *     "-(2^2^2^2)"                            = -65536
 *     "(0 + not (0xDF234 and 1000) * 3) /-2"  = 817
 *     "(2^16) + (1 SHL 16) shr 0X5"           = 4096
 *     "5*-(2^(9+7))/3+5*(1 AND 0xFf123)"      = -109221
 *
 * == About the algorithm used ==
 *
 * ExpressionParser has its roots in a JavaScript parser published at:
 * http://stackoverflow.com/questions/28256/equation-expression-parser-with-precedence/114961#114961
 *
 * The same author has also published an article about his operator
 * precedence algorithm at PerlMonks:
 * http://www.perlmonks.org/?node_id=554516
 */
template<typename T>
class ExpressionParser {
private:
  enum {
    OPERATOR_NULL,
    OPERATOR_BITWISE_OR,      /// or,  OR
    OPERATOR_BITWISE_XOR,     /// xor, XOR
    OPERATOR_BITWISE_AND,     /// and, AND
    OPERATOR_BITWISE_SHL,     /// shl, SHL
    OPERATOR_BITWISE_SHR,     /// shr, SHR
    OPERATOR_ADDITION,        /// +
    OPERATOR_SUBTRACTION,     /// -
    OPERATOR_MULTIPLICATION,  /// *
    OPERATOR_DIVISION,        /// /
    OPERATOR_MODULO,          /// %
    OPERATOR_POWER,           /// ^, **
    OPERATOR_EXPONENT         /// e, E
  };
  
  enum {
    /// default max length (32 KB of characters) for the expression string
    EXPRESSION_MAX_LENGTH = 32767
  };

  class parser_error : public std::exception {
  public:
    parser_error(const std::string& msg) : msg_(msg) {}
    parser_error(const std::string& expr, std::size_t offset) {
      std::ostringstream error;
      error << "Syntax error: unexpected token \""
            << expr.substr(offset, expr.length() - offset)
            << "\" at index "
            << offset;
      msg_ = error.str();
    }
    virtual ~parser_error() throw() {}
    const char* what() const throw() { return msg_.c_str(); }
  private:
    std::string msg_;
  };

  struct Operator {<commit_msg>improved code readability<commit_after>/*
 * ExpressionParser.h -- This file is part of ExpressionParser
 *
 * Copyright (C) 2011 <NAME>, <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

#ifndef EXPRESSIONPARSER_H
#define EXPRESSIONPARSER_H

#include <exception>
#include <string>
#include <sstream>
#include <stack>
#include <cstddef> /* std::size_t */
#include <cctype>  /* std::isspace(int) */
#include <cassert>

/**
 * ExpressionParser.
 * @brief   ExpressionParser.h is a simple C++ operator precedence
 *          parser with infix notation for integer arithmetic
 *          expressions. Its eval(const std::string&) function
 *          evaluates an arithmetic expression passed as string
 *          argument, getResult() returns the corresponding result.
 * @see     http://expressionparser.googlecode.com
 * @author  <NAME>, <<EMAIL>>
 * @version 1.2
 * @date    July, 02 2011
 *
 * == Supported operators ==
 *
 *     OPERATOR     NAME                     ASSOCIATIVITY     PRECEDENCE
 *
 *     or,  OR      Bitwise Inclusive OR     Left               4
 *     xor, XOR     Bitwise Exclusive OR     Left               5
 *     and, AND     Bitwise AND              Left               6
 *     not, NOT     Unary complement         Left              99
 *     shl, SHL     Shift Left               Left               9
 *     shr, SHR     Shift Right              Left               9
 *     +            Addition                 Left              10
 *     -            Subtraction              Left              10
 *     *            Multiplication           Left              20
 *     /            Division                 Left              20
 *     %            Modulo                   Left              20
 *     ^, **        Raise to power           Right             30
 *     e, E         Scientific notation      Right             40
 *
 * The operator precedence has been set according to (uses the C and
 * C++ operator precedence):
 * http://en.wikipedia.org/wiki/Order_of_operations
 * Operators with higher precedence are evaluated before operators
 * with relatively lower precedence.
 *
 * NOTE: Unary operators are set to have the highest precedence. This
 *       is not strictly correct for the power operator
 *       e.g. "-3^2" = 9 but a lot of software tools (Microsoft
 *       Excel, Bash shell, GNU bc, ...) use the same convention.
 *
 * == Examples of valid expressions ==
 *
 *     "2^16"                                  = 65536
 *     "2^16 shr 15"                           = 2
 *     "(0 + 0xdf234 - 1000) * 3 / 2 % 999"    = 828
 *     "-(2^2^2^2)"                            = -65536
 *     "(0 + not (0xDF234 and 1000) * 3) /-2"  = 817
 *     "(2^16) + (1 SHL 16) shr 0X5"           = 4096
 *     "5*-(2^(9+7))/3+5*(1 AND 0xFf123)"      = -109221
 *
 * == About the algorithm used ==
 *
 * ExpressionParser has its roots in a JavaScript parser published at:
 * http://stackoverflow.com/questions/28256/equation-expression-parser-with-precedence/114961#114961
 *
 * The same author has also published an article about his operator
 * precedence algorithm at PerlMonks:
 * http://www.perlmonks.org/?node_id=554516
 */
template<typename T>
class ExpressionParser {
private:
  enum {
    OPERATOR_NULL,
    OPERATOR_BITWISE_OR,      /// or,  OR
    OPERATOR_BITWISE_XOR,     /// xor, XOR
    OPERATOR_BITWISE_AND,     /// and, AND
    OPERATOR_BITWISE_SHL,     /// shl, SHL
    OPERATOR_BITWISE_SHR,     /// shr, SHR
    OPERATOR_ADDITION,        /// +
    OPERATOR_SUBTRACTION,     /// -
    OPERATOR_MULTIPLICATION,  /// *
    OPERATOR_DIVISION,        /// /
    OPERATOR_MODULO,          /// %
    OPERATOR_POWER,           /// ^, **
    OPERATOR_EXPONENT         /// e, E
  };
  
  enum {
    /// default max length (32 KB of characters) for the expression string
    EXPRESSION_MAX_LENGTH = 32767
  };

  class parser_error : public std::exception {
  public:
    parser_error(const std::string& msg) : msg_(msg) {}
    parser_error(const std::string& expr, std::size_t offset) {
      std::ostringstream error;
      error << "Syntax error: unexpected token \""
            << expr.substr(offset, expr.length() - offset)
            << "\" at index "
            << offset;
      msg_ = error.str();
    }
    virtual ~parser_error() throw() {}
    virtual const char* what() const throw() { return msg_.c_str(); }
  private:
    std::string msg_;
  };

  struct Operator {
{'fraction_non_alphanumeric': 0.09922449791210976, 'fraction_numerical': 0.029827003380393718, 'mean_word_length': 1.8674458380843786, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4500604', 'n_tokens_mistral': 3071, 'n_tokens_neox': 2787, 'n_words': 945}