| 1 |
|
|---|
| 2 | #include <boost/optional.hpp>
|
|---|
| 3 | #include <boost/spirit/include/qi.hpp>
|
|---|
| 4 | #include <boost/spirit/include/phoenix_core.hpp>
|
|---|
| 5 | #include <boost/spirit/include/phoenix_operator.hpp>
|
|---|
| 6 | #include <boost/spirit/include/phoenix_object.hpp>
|
|---|
| 7 | #include <boost/spirit/include/phoenix_fusion.hpp>
|
|---|
| 8 | #include <boost/spirit/include/support_multi_pass.hpp>
|
|---|
| 9 | #include <boost/algorithm/string/trim.hpp>
|
|---|
| 10 |
|
|---|
| 11 | #include <sstream>
|
|---|
| 12 | #include <iostream>
|
|---|
| 13 |
|
|---|
| 14 | namespace spirit = boost::spirit;
|
|---|
| 15 | namespace qi = boost::spirit::qi;
|
|---|
| 16 | namespace ascii = boost::spirit::ascii;
|
|---|
| 17 | namespace fusion = boost::fusion;
|
|---|
| 18 | namespace phoenix = boost::phoenix;
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 | class DLVresultParserDriver
|
|---|
| 22 | {
|
|---|
| 23 | public:
|
|---|
| 24 | DLVresultParserDriver();
|
|---|
| 25 |
|
|---|
| 26 | void
|
|---|
| 27 | parse(std::istream& is);
|
|---|
| 28 | };
|
|---|
| 29 |
|
|---|
| 30 | // "The Grammar"
|
|---|
| 31 | template<typename Iterator>
|
|---|
| 32 | struct DLVResultGrammar:
|
|---|
| 33 | public qi::grammar<Iterator, ascii::space_type>
|
|---|
| 34 | {
|
|---|
| 35 | DLVResultGrammar():
|
|---|
| 36 | DLVResultGrammar::base_type(answersets)
|
|---|
| 37 | {
|
|---|
| 38 | using spirit::int_;
|
|---|
| 39 | using spirit::_val;
|
|---|
| 40 | using spirit::_1;
|
|---|
| 41 | using qi::lexeme;
|
|---|
| 42 | using qi::char_;
|
|---|
| 43 | using qi::omit;
|
|---|
| 44 | using qi::lit;
|
|---|
| 45 |
|
|---|
| 46 | answersets
|
|---|
| 47 | // end_p enforces a "full" match (in case of success) even with trailing newlines
|
|---|
| 48 | = *(
|
|---|
| 49 | (-lit("Best model:") >> answerset)
|
|---|
| 50 | |
|
|---|
| 51 | costline
|
|---|
| 52 | ) > (qi::eol | qi::eoi);
|
|---|
| 53 |
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | qi::rule<Iterator, ascii::space_type> answersets, answerset, costline;
|
|---|
| 57 | };
|
|---|
| 58 |
|
|---|
| 59 | void
|
|---|
| 60 | DLVresultParserDriver::parse(std::istream& is)
|
|---|
| 61 | {
|
|---|
| 62 |
|
|---|
| 63 | std::ostringstream buf;
|
|---|
| 64 | buf << is.rdbuf();
|
|---|
| 65 | const std::string& input = buf.str();
|
|---|
| 66 |
|
|---|
| 67 | typedef std::string::const_iterator forward_iterator_type;
|
|---|
| 68 | // convert input iterator to forward iterator, usable by spirit parser
|
|---|
| 69 | forward_iterator_type fwd_begin = input.begin();
|
|---|
| 70 | forward_iterator_type fwd_end = input.end();
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 | DLVResultGrammar<forward_iterator_type> grammar;
|
|---|
| 74 | qi::phrase_parse(fwd_begin, fwd_end, grammar, ascii::space);
|
|---|
| 75 |
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | // Local Variables:
|
|---|
| 79 | // mode: C++
|
|---|
| 80 | // End:
|
|---|