// Copyright (c) 2001-2010 Hartmut Kaiser // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // The purpose of this example is to demonstrate a simple use case for the // flush_multi_pass parser. #include #include #include //[qi_flush_multi_pass_includes #include #include //] //[qi_flush_multi_pass_namespace namespace spirit = boost::spirit; using boost::spirit::repository::flush_multi_pass; //] namespace client { //[qi_flush_multi_pass_clear_buffer template struct preprocessor : spirit::qi::grammar { // This is a simplified preprocessor grammar recognizing // // #define MACRONAME something // #undef MACRONAME // // Its sole purpose is to show an example how to use the // flush_multi_pass parser. At the end of each line no backtracking can // occur anymore so that it's safe to clear all internal buffers in the // multi_pass. preprocessor() : preprocessor::base_type(file) { using spirit::ascii::char_; using spirit::qi::eol; using spirit::qi::lit; file = *line ; line = ( command | *(char_ - eol) ) >> eol >> flush_multi_pass ; command = "#define" >> *lit(' ') >> *(char_ - ' ') >> *lit(' ') >> *(char_ - eol) | "#undef" >> *lit(' ') >> *(char_ - eol) ; } spirit::qi::rule file, line, command; }; //] } template bool parse(Iterator& first, Iterator end, Skipper const& skipper) { client::preprocessor g; return boost::spirit::qi::phrase_parse(first, end, g, skipper); } int main() { namespace spirit = boost::spirit; using spirit::ascii::char_; using spirit::qi::eol; std::ifstream in("flush_multi_pass.txt"); // we get our input from this file if (!in.is_open()) { std::cout << "Could not open input file: 'flush_multi_pass.txt'" << std::endl; return -1; } typedef std::istreambuf_iterator base_iterator_type; spirit::multi_pass first = spirit::make_default_multi_pass(base_iterator_type(in)); spirit::multi_pass end = spirit::make_default_multi_pass(base_iterator_type()); bool result = parse(first, end, '#' >> *(char_ - eol) >> eol); if (!result) { std::cout << "Failed parsing input file!" << std::endl; return -2; } std::cout << "Successfully parsed input file!" << std::endl; return 0; }