Boost: Spirit parses string quotes and other symmetric symbols

Source: Internet
Author: User

----------------------- Spirit \ Mexico pression2.h ------------------------------

# Pragma once
# Include <string>
# Include <iostream>
# Include <cassert>

# Include <boost/spirit/include/classic_core.hpp>
# Include <boost/spirit/include/classic_confix.hpp>
# Include <boost/spirit/include/classic_chset.hpp>

Using namespace STD;
Using namespace boost_spirit_classic_ns;

Namespace mexpr {
//////////////////////////////////////// ///////////////////////////////////////
// Actor called after successfully matching a single character
Class actor_string
{
Public:
Actor_string (STD: string & rstr ):
Matched (rstr)
{
}

Void operator () (const char * pbegin, const char * pend) const
{
Matched + = STD: string (pbegin, pend-pbegin );
}

PRIVATE:
STD: string & matched;
};

Void ev_3 (const char * expr );

}

Bytes -------------------------------------------------------------------------------------------

------------------------ Spirit \ mexpression2.cpp ----------------------------------

# Include "mexpression2.h"

Void mexpr: ev_3 (const char * expr ){
String body;
Rule <> open_tag, html_tag, close_tag, body_text;
Open_tag = str_p ("<B> ");
Body_text = anychar_p;
Close_tag = str_p ("</B> ");

Html_tag = confix_p (open_tag, (* body_text) [actor_string (body)], close_tag );

Char const * ptag = "<B> body text </B> ";

parse_info <> result = parse (ptag, html_tag );
If (result. hit)
{< br> cout <"parsed HTML snippet \" body text \ "successfully"
"(with re-attached actor)! " cout <" found body ("<(INT) body. size () <"characters):";
cout <"\" "}< br> else
{< br> cout <"failed to parse HTML snippet (with re-attached actor )! "
}

String STR;
Rule <> strrule;
Strrule = confix_p (str_p ("\" "), (* anychar_p) [actor_string (STR)], str_p ("\""));
String S = "\" abcdefg \"";
Cout <S <Endl;
If (PARSE (S. c_str (), strrule). Hit)
{
Cout <STR <Endl;
}
Else
{
Cout <"not a string" <Endl;
}

}

Bytes -------------------------------------------------------------------------------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.