
#include "Parsers.h"
#include <string>
#include <iostream>
#include <cassert>
#include <vector>
#include <map>

#include <boost/spirit/core.hpp>
#include <boost/spirit/utility/confix.hpp>
#include <boost/spirit/utility/lists.hpp>
#include <boost/spirit/utility/escape_char.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/algorithm/string.hpp>

using namespace std;
using namespace boost;
using namespace boost::spirit;

namespace Parsers {
bool parse_val( const std::string &input,int &val)
{
  rule<> int_rule= int_p[assign_a(val)];
  parse_info<> result = parse (input.c_str(), int_rule);
  if (!result.hit) {
    return 0;
  }
  return 1;
}

bool parse_val( const std::string &input,double &val)
{
  rule<> double_rule= real_p[assign_a(val)];
  parse_info<> result = parse (input.c_str(), double_rule);
  if (!result.hit) {
    return 0;
  }
  return 1;
}

bool parse_val( const std::string &input,std::string &val)
{ 
  rule<> string_rule= *(ch_p('\"') | '\'')
    >> (*(anychar_p-'\"' -'\''))[assign_a(val)] 
    >> *('\"' | ch_p('\''));
  
  parse_info<> result = parse (input.c_str(), string_rule);
  if (!result.hit) {
    return 0;
  }
  return 1;
}
bool parse_val( const std::string &input,bool &val)
{
  string vv;
  rule<> bool_false=  str_p("False") || str_p("0") || str_p("false");
  rule<> bool_true=  str_p("True") || str_p("1") || str_p("true");
  parse_info<> result = parse (input.c_str(), bool_true);
  if (result.hit) {val=1;return 1;}
  result = parse (input.c_str(), bool_false);
  if (result.hit) {val=0;return 1;}
  return 0;

}
bool parse_val( const std::string &input,std::vector<double> &val)
{
  val.clear();
  rule<> vector_double= 
    (ch_p('{') || '[' )
    >> list_p.direct(real_p[push_back_a(val)], ',')
    >>  (ch_p('}') || ']')
    ;
  parse_info<> result = parse (input.c_str(), vector_double);

  if (!result.hit) {
    val.clear();
    return 0;
  }
  return 1;
}

bool parse_val( const std::string &input,std::vector<int> &val)
{
  val.clear();
  rule<> vector_int= 
    (ch_p('{') || '[' )
    >> list_p.direct(int_p[push_back_a(val)], ',')
    >>  (ch_p('}') || ']')
    ;
  parse_info<> result = parse (input.c_str(), vector_int);

  if (!result.hit) {
    val.clear();
    return 0;
  }
  return 1;
}
bool parse_val( const std::string &input,std::vector<std::string> &val)
{
  val.clear();
  rule<> string_rule= (ch_p('\"') | '\'')
                      >> (*(anychar_p-'\"' -'\''))[push_back_a(val)] 
		      >> ('\"' | ch_p('\''));
  
  rule<> vec_string= ch_p('{') >> list_p.direct(string_rule, 
						(*space_p>>','>>*space_p) )
			       >>'}';
  parse_info<> result = parse (input.c_str(), vec_string);
  if (!result.hit) {
    val.clear();
    return 0;
  }
  return 1;
}


}
