// spirit-test.cpp : Defines the entry point for the console application.
//

#include <string>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include <boost/spirit/include/support_ascii.hpp>


namespace qi = boost::spirit::qi;
namespace fusion = boost::fusion;
namespace phoenix = boost::phoenix;

void getstr(std::string& str) 
{
    std::cout << str;
    return ;
}

template <typename Iterator >
bool parse (Iterator begin, Iterator end)
{
    using qi::lexeme;
    using qi::char_;
    using qi::lit;
    using qi::rule;
    using qi::space;
    using boost::spirit::no_skip;
    using boost::spirit::ascii::space_type;
    
    
    
    rule<Iterator> Sequence;
    rule<Iterator, std::string()> KeyName, Value, ValueExpression;
        
    
    KeyName
        %= no_skip[+char_("a-zA-Z0-9_") >> lit("=")]
        ;

    Value
        %=  +(char_ - char_("{=")) 
        ;

    ValueExpression 
        %= ( 
        Value  
        >> *space 
        >> &(KeyName | lit("{"))
        )
        ;

    Sequence
        =	
        +(  KeyName[boost::bind(&getstr, _1)]
            >> ValueExpression[boost::bind(&getstr, _1)]
        )  
        ;

    bool r =  qi::phrase_parse(begin, end, Sequence, space);
    if (!r || begin != end) { return false; }
    else return true;
}

int main ()
{
    std::string test(" ARG=Test still in first ARG ARG2=Zombie cat EXP2=FunctionCall(A, B C) {" );
    bool status = parse<std::string::const_iterator> (test.begin(), test.end());
    return 0;
}