public
Created

Example for SO question

  • Download Gist
so.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
/**
* \author Sergey Miryanov (sergey.miryanov@gmail.com)
* */
#define BOOST_SPIRIT_QI_DEBUG
 
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/spirit/include/support_multi_pass.hpp>
#include <boost/bind.hpp>
 
#include <iostream>
 
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phoenix = boost::phoenix;
namespace spirit = boost::spirit;
 
template <typename Iterator>
struct Grammar : qi::grammar <Iterator>
{
Grammar ()
: Grammar::base_type (expression)
{
using qi::char_;
using qi::string;
using qi::symbols;
using qi::no_case;
using qi::alnum;
using qi::lexeme;
using qi::_val;
using ascii::space;
 
symbols <char, char const*> control, limit;
control.add
("prod", "PROD")
;
limit.add
("oil", "OIL")
;
 
qi::rule <Iterator, std::string ()> str = +alnum;
 
param = "WELL" >> space >> str >> ((space >> no_case[control]) || (space >> no_case[limit])) >> space >> qi::eol
;
 
BOOST_SPIRIT_DEBUG_NODE (param);
expression = qi::no_skip[param];
}
 
qi::rule <Iterator> param, expression;
qi::rule <Iterator> well;
};
 
void
parse (char *text, size_t len)
{
std::cout << text << std::endl;
char *begin = text;
char *end = &text[len];
 
Grammar <char *> g;
 
bool r = phrase_parse (begin, end, g, ascii::space);
if (begin != end)
{
std::cout << r << ": " << end - begin << std::endl;
std::cout << r << ": " << end << std::endl;
}
}
 
int
main ()
{
char test[] = "WELL name3 PROD OIL \n";
parse (test, sizeof (test) - 1);
 
return 0;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.