<commit_before>#ifndef __STAN__GM__PARSER__TERM_GRAMMAR_DEF__HPP__
#define __STAN__GM__PARSER__TERM_GRAMMAR_DEF__HPP__

#include <cstddef>
#include <iomanip>
#include <iostream>
#include <istream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include <stdexcept>

#include <boost/spirit/include/qi.hpp>
// FIXME: get rid of unused include
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_function.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>

#include <boost/lexical_cast.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/qi_numeric.hpp>
#include <boost/spirit/include/classic_position_iterator.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_function.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/spirit/include/support_multi_pass.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <boost/variant/recursive_variant.hpp>

#include <stan/gm/ast.hpp>
#include <stan/gm/grammars/whitespace_grammar.hpp>
#include <stan/gm/grammars/term_grammar.hpp>
#include <stan/gm/grammars/expression_grammar.hpp>

BOOST_FUSION_ADAPT_STRUCT(stan::gm::index_op,
                          (stan::gm::expression, expr_)
                          (std::vector<std::vector<stan::gm::expression> >, 
                           dimss_) );

BOOST_FUSION_ADAPT_STRUCT(stan::gm::fun,
                          (std::string, name_)
                          (std::vector<stan::gm::expression>, args_) );

BOOST_FUSION_ADAPT_STRUCT(stan::gm::int_literal,
                          (int,val_)
                          (stan::gm::expr_type,type_));

BOOST_FUSION_ADAPT_STRUCT(stan::gm::double_literal,
                          (double,val_)
                          (stan::gm::expr_type,type_) );




namespace stan { 

  namespace gm {


    struct set_fun_type {
      template <typename T1, typename T2>
      struct result { typedef fun type; };

      fun operator()(fun& fun,
                     std::ostream& error_msgs) const {
        std::vector<expr_type> arg_types;
        for (size_t i = 0; i < fun.args_.size(); ++i)
          arg_types.push_back(fun.args_[i].expression_type());
        fun.type_ = function_signatures::instance().get_result_type(fun.name_,
                                                                    arg_types,
                                                                    error_msgs);
        return fun;
      }
    };
    boost::phoenix::function<set_fun_type> set_fun_type_f;


    struct set_fun_type_named {
      template <typename T1, typename T2, typename T3, typename T4, typename T5>
      struct result { typedef void type; };

      void operator()(expression& fun_result,
                      fun& fun,
                      const var_origin& var_origin,
                      bool& pass,
                      std::ostream& error_msgs) const {
        std::vector<expr_type> arg_types;
        for (size_t i = 0; i < fun.args_.size(); ++i)
          arg_types.push_back(fun.args_[i].expression_type());
        fun.type_ = function_signatures::instance().get_result_type(fun.name_,
                                                                    arg_types,
                                                                    error_msgs);


        if (has_rng_suffix(fun.name_)) {  
          if (!( var_origin == derived_origin
                 || var_origin == function_argument_origin_rng )) {
            error_msgs << "random number generators only allowed in"
                       << " generated quantities block or"
                       << " user-defined functions with names ending in _rng"
                       << "; found function=" << fun.name_
                       << " in block=";
          print_var_origin(error_msgs,var_origin);
          error_msgs << std::endl;
          pass = false;
          return;
          }
        }

        if (has_lp_suffix(fun.name_)) {
          if (!( var_origin == parameter_origin
                 || var_origin == transformed_parameter_origin
                 || var_origin == function_argument_origin
                 || var_origin == local_origin )) {
            error_msgs << "lp suffixed functions only allowed in"
                       << " transformed parameter, function argument, or model"
                       << " blocks;  found function=" << fun.name_ 
                       << " in block=";
            print_var_origin(error_msgs,var_origin);
            error_msgs << std::endl;
            pass = false;
            return;
          }
        }

        if (fun.name_ == "abs" 
            && fun.args_.size() > 0 
            && fun.args_[0].expression_type().is_primitive_double()) {
          error_msgs << "Warning: Function abs(real) is deprecated."
                     << std::endl
                     << "         It will be removed in a future release."
                     << std::endl
                     << "         Use fabs(real) instead."
                     << std::endl << std::endl;
        }

        fun_result = fun;
        pass = true;
      }
    };
    boost::phoenix::function<set_fun_type_named> set_fun_type_named_f;

 
    struct exponentiation_expr {
      template <typename T1, typename T2, typename T3, typename T4, typename T5>
      struct result { typedef void type; };

      void operator()(expression& expr1,
                      const expression& expr2,
                      const var_origin& var_origin,
                      bool& pass,
                      std::ostream& error_msgs) const {

        //        if (expr1.expression_type().is_vector() 
        //            && !expr2.expression_type().is_primitive()) {
        // handle raising each member of a vector to a power?
        // }
        print_var_origin(error_msgs,var_origin);
        if (!expr1.expression_type().is_primitive() 
            || !expr2.expression_type().is_primitive()) {
          error_msgs << "arguments to ^ must be primitive (real or int)"
                     << "; cannot exponentiate "
                     << expr1.expression_type()<commit_msg>Minor fix to operator ^<commit_after>#ifndef __STAN__GM__PARSER__TERM_GRAMMAR_DEF__HPP__
#define __STAN__GM__PARSER__TERM_GRAMMAR_DEF__HPP__

#include <cstddef>
#include <iomanip>
#include <iostream>
#include <istream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include <stdexcept>

#include <boost/spirit/include/qi.hpp>
// FIXME: get rid of unused include
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_function.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>

#include <boost/lexical_cast.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/qi_numeric.hpp>
#include <boost/spirit/include/classic_position_iterator.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_function.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/spirit/include/support_multi_pass.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <boost/variant/recursive_variant.hpp>

#include <stan/gm/ast.hpp>
#include <stan/gm/grammars/whitespace_grammar.hpp>
#include <stan/gm/grammars/term_grammar.hpp>
#include <stan/gm/grammars/expression_grammar.hpp>

BOOST_FUSION_ADAPT_STRUCT(stan::gm::index_op,
                          (stan::gm::expression, expr_)
                          (std::vector<std::vector<stan::gm::expression> >, 
                           dimss_) );

BOOST_FUSION_ADAPT_STRUCT(stan::gm::fun,
                          (std::string, name_)
                          (std::vector<stan::gm::expression>, args_) );

BOOST_FUSION_ADAPT_STRUCT(stan::gm::int_literal,
                          (int,val_)
                          (stan::gm::expr_type,type_));

BOOST_FUSION_ADAPT_STRUCT(stan::gm::double_literal,
                          (double,val_)
                          (stan::gm::expr_type,type_) );




namespace stan { 

  namespace gm {


    struct set_fun_type {
      template <typename T1, typename T2>
      struct result { typedef fun type; };

      fun operator()(fun& fun,
                     std::ostream& error_msgs) const {
        std::vector<expr_type> arg_types;
        for (size_t i = 0; i < fun.args_.size(); ++i)
          arg_types.push_back(fun.args_[i].expression_type());
        fun.type_ = function_signatures::instance().get_result_type(fun.name_,
                                                                    arg_types,
                                                                    error_msgs);
        return fun;
      }
    };
    boost::phoenix::function<set_fun_type> set_fun_type_f;


    struct set_fun_type_named {
      template <typename T1, typename T2, typename T3, typename T4, typename T5>
      struct result { typedef void type; };

      void operator()(expression& fun_result,
                      fun& fun,
                      const var_origin& var_origin,
                      bool& pass,
                      std::ostream& error_msgs) const {
        std::vector<expr_type> arg_types;
        for (size_t i = 0; i < fun.args_.size(); ++i)
          arg_types.push_back(fun.args_[i].expression_type());
        fun.type_ = function_signatures::instance().get_result_type(fun.name_,
                                                                    arg_types,
                                                                    error_msgs);


        if (has_rng_suffix(fun.name_)) {  
          if (!( var_origin == derived_origin
                 || var_origin == function_argument_origin_rng )) {
            error_msgs << "random number generators only allowed in"
                       << " generated quantities block or"
                       << " user-defined functions with names ending in _rng"
                       << "; found function=" << fun.name_
                       << " in block=";
          print_var_origin(error_msgs,var_origin);
          error_msgs << std::endl;
          pass = false;
          return;
          }
        }

        if (has_lp_suffix(fun.name_)) {
          if (!( var_origin == parameter_origin
                 || var_origin == transformed_parameter_origin
                 || var_origin == function_argument_origin
                 || var_origin == local_origin )) {
            error_msgs << "lp suffixed functions only allowed in"
                       << " transformed parameter, function argument, or model"
                       << " blocks;  found function=" << fun.name_ 
                       << " in block=";
            print_var_origin(error_msgs,var_origin);
            error_msgs << std::endl;
            pass = false;
            return;
          }
        }

        if (fun.name_ == "abs" 
            && fun.args_.size() > 0 
            && fun.args_[0].expression_type().is_primitive_double()) {
          error_msgs << "Warning: Function abs(real) is deprecated."
                     << std::endl
                     << "         It will be removed in a future release."
                     << std::endl
                     << "         Use fabs(real) instead."
                     << std::endl << std::endl;
        }

        fun_result = fun;
        pass = true;
      }
    };
    boost::phoenix::function<set_fun_type_named> set_fun_type_named_f;

 
    struct exponentiation_expr {
      template <typename T1, typename T2, typename T3, typename T4, typename T5>
      struct result { typedef void type; };

      void operator()(expression& expr1,
                      const expression& expr2,
                      const var_origin& var_origin,
                      bool& pass,
                      std::ostream& error_msgs) const {

        //        if (expr1.expression_type().is_vector() 
        //            && !expr2.expression_type().is_primitive()) {
        // handle raising each member of a vector to a power?
        // }
        if (!expr1.expression_type().is_primitive() 
            || !expr2.expression_type().is_primitive()) {
          error_msgs << "arguments to ^ must be primitive (real or int)"
                     << "; cannot exponentiate "
                     << expr1.expression_type()
{'fraction_non_alphanumeric': 0.11305911440773207, 'fraction_numerical': 0.0034464673709447818, 'mean_word_length': 1.9583333333333333, 'pattern_counts': {'":': 0, '<': 197, '<?xml version=': 0, '>': 103, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3820637', 'n_tokens_mistral': 3967, 'n_tokens_neox': 3780, 'n_words': 714}