#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/lex_lexertl.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include "example.hpp"
using namespace boost::spirit;
template <typename Lexer>
struct example1_tokens : lex::lexer<Lexer>
{
example1_tokens()
{
identifier = "[a-zA-Z_][a-zA-Z0-9_]*";
this->self = lex::char_(',') | '{' | '}' | identifier;
this->white_space = "[ \\t\\n]+";
this->self("WS") = white_space;
}
lex::token_def<> identifier, white_space;
};
template <typename Iterator>
struct example1_grammar
: qi::grammar<Iterator, qi::in_state_skipper<lex::token_def<> > >
{
template <typename TokenDef>
example1_grammar(TokenDef const& tok)
: example1_grammar::base_type(start)
{
start = '{' >> *(tok.identifier >> -ascii::char_(',')) >> '}';
}
qi::rule<Iterator, qi::in_state_skipper<lex::token_def<> > > start;
};
int main()
{
typedef std::string::iterator base_iterator_type;
typedef lex::lexertl::token<base_iterator_type> token_type;
typedef lex::lexertl::lexer<token_type> lexer_type;
typedef example1_tokens<lexer_type> example1_lex;
typedef example1_lex::iterator_type iterator_type;
typedef example1_grammar<iterator_type> example1_grammar;
example1_lex lex; example1_grammar calc(lex);
std::string str (read_from_file("example1.input"));
std::string::iterator it = str.begin();
iterator_type iter = lex.begin(it, str.end());
iterator_type end = lex.end();
bool r = qi::phrase_parse(iter, end, calc, qi::in_state("WS")[lex.white_space]);
if (r && iter == end)
{
std::cout << "-------------------------\n";
std::cout << "Parsing succeeded\n";
std::cout << "-------------------------\n";
}
else
{
std::string rest(iter, end);
std::cout << "-------------------------\n";
std::cout << "Parsing failed\n";
std::cout << "stopped at: \"" << rest << "\"\n";
std::cout << "-------------------------\n";
}
std::cout << "Bye... :-) \n\n";
return 0;
}