| 1 | #define BOOST_SPIRIT_DEBUG
|
|---|
| 2 |
|
|---|
| 3 | #include <boost/spirit/include/qi.hpp>
|
|---|
| 4 | #include <boost/multi_array.hpp>
|
|---|
| 5 |
|
|---|
| 6 | #include <vector>
|
|---|
| 7 |
|
|---|
| 8 | namespace myproject {
|
|---|
| 9 | namespace parser {
|
|---|
| 10 |
|
|---|
| 11 | //---------------------------------------------------------------------------
|
|---|
| 12 | // NAMESPACES
|
|---|
| 13 | //---------------------------------------------------------------------------
|
|---|
| 14 | namespace qi = boost::spirit::qi;
|
|---|
| 15 |
|
|---|
| 16 | //---------------------------------------------------------------------------
|
|---|
| 17 | // TYPEDEFS
|
|---|
| 18 | //---------------------------------------------------------------------------
|
|---|
| 19 | typedef boost::multi_array<int, 2> ReturnT;
|
|---|
| 20 | typedef std::vector<int> ElevationT;
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 | //---------------------------------------------------------------------------
|
|---|
| 24 | // VARIABLES
|
|---|
| 25 | //---------------------------------------------------------------------------
|
|---|
| 26 |
|
|---|
| 27 | //---------------------------------------------------------------------------
|
|---|
| 28 | // PARSER
|
|---|
| 29 | //---------------------------------------------------------------------------
|
|---|
| 30 |
|
|---|
| 31 | /*!
|
|---|
| 32 | * \brief DTED Elevation parsing grammar
|
|---|
| 33 | */
|
|---|
| 34 | template <typename Iterator>
|
|---|
| 35 | struct dted_elevation_parser : public qi::grammar<Iterator, ElevationT()>
|
|---|
| 36 | {
|
|---|
| 37 |
|
|---|
| 38 | /*!
|
|---|
| 39 | * \brief Constructor where the Qi grammar is defined.
|
|---|
| 40 | */
|
|---|
| 41 | dted_elevation_parser ()
|
|---|
| 42 | : dted_elevation_parser::base_type ( elevation )
|
|---|
| 43 | {
|
|---|
| 44 | using qi::big_word;
|
|---|
| 45 |
|
|---|
| 46 | //---------------------------------------------------
|
|---|
| 47 | // RULE: DTED Elevation
|
|---|
| 48 | //---------------------------------------------------
|
|---|
| 49 | elevation = +big_word;
|
|---|
| 50 |
|
|---|
| 51 | //---------------------------------------------------
|
|---|
| 52 | // Boost Spirit Debugg Information. Only active if
|
|---|
| 53 | // DEBUG_TLE_PARSER is enabled via CMake
|
|---|
| 54 | //---------------------------------------------------
|
|---|
| 55 | BOOST_SPIRIT_DEBUG_NODE(elevation);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | //---------------------------------------------------------------------------
|
|---|
| 59 | // VARIABLES
|
|---|
| 60 | //---------------------------------------------------------------------------
|
|---|
| 61 |
|
|---|
| 62 | /*! \brief Top-level grammar rule */
|
|---|
| 63 | qi::rule<Iterator, ElevationT()> elevation;
|
|---|
| 64 | };
|
|---|
| 65 |
|
|---|
| 66 | } // namespace parser
|
|---|
| 67 | } // namespace myproject
|
|---|
| 68 |
|
|---|
| 69 | int main ( int, char** )
|
|---|
| 70 | {
|
|---|
| 71 | std::string input = "\x81\x02\x03\x04";
|
|---|
| 72 |
|
|---|
| 73 | typedef std::string::const_iterator iterator_type;
|
|---|
| 74 | typedef myproject::parser::dted_elevation_parser<iterator_type> elevation_parser;
|
|---|
| 75 |
|
|---|
| 76 | elevation_parser parser_obj;
|
|---|
| 77 |
|
|---|
| 78 | iterator_type iter = input.begin();
|
|---|
| 79 | iterator_type end = input.end();
|
|---|
| 80 | myproject::parser::ElevationT results;
|
|---|
| 81 |
|
|---|
| 82 | if ( boost::spirit::qi::parse ( iter, end, parser_obj, results ) )
|
|---|
| 83 | {
|
|---|
| 84 | std::cout << "successfully parsed" << std::endl;
|
|---|
| 85 | myproject::parser::ReturnT data ( boost::extents[2][2] );
|
|---|
| 86 | std::copy ( results.begin(), results.end(), data.data() );
|
|---|
| 87 | std::cout << data[0][0] << std::endl;
|
|---|
| 88 | std::cout << data[0][1] << std::endl;
|
|---|
| 89 | }
|
|---|
| 90 | else
|
|---|
| 91 | {
|
|---|
| 92 | std::cout << "failed to parse" << std::endl;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | return 0;
|
|---|
| 96 | }
|
|---|