Skip to content

Instantly share code, notes, and snippets.

@AntonNik0laev
Created November 2, 2015 01:11
Show Gist options
  • Save AntonNik0laev/12d68c40adca9de12075 to your computer and use it in GitHub Desktop.
Save AntonNik0laev/12d68c40adca9de12075 to your computer and use it in GitHub Desktop.
typedef std::map<std::string, std::string> TQuery;
struct Uri
{
std::string scheme;
std::string host;
std::string path;
TQuery query;
};
BOOST_FUSION_ADAPT_STRUCT(
Uri,
(std::string, scheme)
(std::string, host)
(std::string, path)
(TQuery, query)
)
template<class Iterator>
struct UriGrammar : public boost::spirit::qi::grammar<Iterator, Uri()> {
UriGrammar() :UriGrammar::base_type(uri)
{
namespace qi = boost::spirit::qi;
typedef qi::rule<Iterator, std::string()> StringArgRule;
typedef qi::rule<Iterator, std::map<std::string, std::string>()> MapArgRule;
typedef qi::rule<Iterator, std::pair<std::string, std::string>()> PairArgRule;
StringArgRule scheme = *qi::alnum >> "://";
StringArgRule host = qi::alnum >> *(qi::alnum | qi::char_("."));
StringArgRule path = qi::char_('/') >> *(qi::alnum | qi::char_("/."));
StringArgRule queryParamName = *qi::alnum;
StringArgRule queryParamValue = *(qi::alnum |qi::char_('%') );
PairArgRule queryPair = queryParamName >> '=' >> queryParamValue;
MapArgRule query = qi::lit('?') >> (queryPair % '&');
uri = scheme >> host >> path >> query;
}
boost::spirit::qi::rule<Iterator, Uri()> uri;
};
int main()
{
Uri uriStruct;
bool parsed = qi::parse(uri.begin(), uri.end(), UriGrammar<std::string::iterator>(), uriStruct);
std::cout << uriStruct.host << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment