Created
March 19, 2011 19:40
-
-
Save sergey-miryanov/877743 to your computer and use it in GitHub Desktop.
Example for SO question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* \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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment