#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/lex_lexertl.hpp>
#include <boost/phoenix/operator.hpp>
#include <boost/phoenix/stl/container.hpp>
#include <iostream>
#include <string>
#include "example.hpp"
using namespace boost::spirit;
enum tokenids
{
IDANY = lex::min_token_id + 10
};
template <typename Lexer>
struct strip_comments_tokens : lex::lexer<Lexer>
{
strip_comments_tokens()
: strip_comments_tokens::base_type(lex::match_flags::match_default)
{
cppcomment = "\"//\"[^\n]*"; ccomment = "\"/*\""; endcomment = "\"*/\"";
this->self.add
(cppcomment) (ccomment)
(".", IDANY) ;
this->self("COMMENT").add
(endcomment)
(".", IDANY)
;
}
lex::token_def<> cppcomment, ccomment, endcomment;
};
template <typename Iterator>
struct strip_comments_grammar : qi::grammar<Iterator>
{
template <typename TokenDef>
strip_comments_grammar(TokenDef const& tok)
: strip_comments_grammar::base_type(start)
{
start = *( tok.ccomment
>> qi::in_state("COMMENT")
[
*token(IDANY) >> tok.endcomment
]
| tok.cppcomment
| qi::token(IDANY) [ std::cout << _1 ]
)
;
}
qi::rule<Iterator> start;
};
int main(int argc, char* argv[])
{
typedef std::string::iterator base_iterator_type;
typedef
lex::lexertl::lexer<lex::lexertl::token<base_iterator_type> >
lexer_type;
typedef strip_comments_tokens<lexer_type>::iterator_type iterator_type;
strip_comments_tokens<lexer_type> strip_comments; strip_comments_grammar<iterator_type> g (strip_comments);
std::string str (read_from_file(1 == argc ? "strip_comments.input" : argv[1]));
base_iterator_type first = str.begin();
bool r = lex::tokenize_and_parse(first, str.end(), strip_comments, g);
if (r) {
std::cout << "-------------------------\n";
std::cout << "Parsing succeeded\n";
std::cout << "-------------------------\n";
}
else {
std::string rest(first, str.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;
}