
On 6/1/07, Eric Niebler <eric@boost-consulting.com> wrote:
It's a poorly kept secret that I've been adding new features to Xpressive on CVS HEAD for a while now. So here are the features already implemented (documentation forthcoming) for Xpressive 2.0:
<< Semantic Actions >>
Specify code to execute when parts of a regex match, a-la Spirit's semantic actions. Eg.: if you want to parse a string of name/value pairs into a std::map, you might:
std::map<std::string, int> result; std::string str("aaa=>1 bbb=>23 ccc=>456");
// Like "(\\w+)=>(\\d+)": sregex pair = ( (s1= +_w) >> "=>" >> (s2= +_d) ) [ ref(result)[s1] = as<int>(s2) ]; sregex rx = pair >> *(+_s >> pair);
if(regex_match(str, rx)) { assert(result["aaa"] == 1); assert(result["bbb"] == 23); assert(result["ccc"] == 456); }
The actions are placed on a queue and executed in order only when the regex match succeeds.
Neat! Great work Eric! I want to use this with my bimaps (I will add a section Bimap and Boost.Xpressive to my Bimap and Boost.? docs) but I do not know the correct approach. Because bimaps have other constraints than maps, I can not provide operator[] for insertion. I have to use the usual insert. This works as you said: (operator[] is provided for this bimap because list_of collection type does not impose additional constraints) ---------------------------------------------------------------------------- typedef bimap< std::string, list_of< int > > bm_type; bm_type bm; std::string str("aaa=>1 bbb=>23 ccc=>456"); sregex pair = ( (s1= +_w) >> "=>" >> (s2= +_d) ) [ xp::ref(bm.left)[ s1 ] = as<int>(s2) ]; ---------------------------------------------------------------------------- But when operator[] is not present I have tried to use insert but failed. Being able to parse a file is a nice addition to bimap docs. I know I have to wait to your docs but I want to know now :) Can you help me? Regards Matias