[xpressive] first try: error handling, nested refs

[I am using Boost 1.35, on Linux, running gcc 4.1.2]
I want to parse a string like the following
---
ResA { opt1=>val1, opt2 => val2}, ResB
ResC {opt3=> valllll }
ResD
---
I thought that maybe it is easier to to this using xpressive rather than
Spirit (which I have used for more complicated stuff so far).
I am happy to say, that with the very helpful docs I was able to create a
sregex that parses the above. Also, I found it the solution is quite short,
so I am basically happy with xpressive.
As this was my first try, there is certainly (lots of) room for improvement:
I have attached my program, and would welcome comments (and suggestions for
enhancements.)
#include

Christoph Duelli wrote:
[I am using Boost 1.35, on Linux, running gcc 4.1.2]
I want to parse a string like the following --- ResA { opt1=>val1, opt2 => val2}, ResB ResC {opt3=> valllll }
ResD ---
I thought that maybe it is easier to to this using xpressive rather than Spirit (which I have used for more complicated stuff so far).
I am happy to say, that with the very helpful docs I was able to create a sregex that parses the above. Also, I found it the solution is quite short, so I am basically happy with xpressive.
As this was my first try, there is certainly (lots of) room for improvement:
I have attached my program, and would welcome comments (and suggestions for enhancements.)
<snip code> Wow, for a first try, I'd say this is pretty darn good. You'll be pleased to know that in the next version of xpressive, there will be a skip() modifier which will eliminate all those nasty *_s from your patterns. For instance, you'll be able to simply say skip(_s)(rx_res >> *(',' >> rx_res)). To improve runtime performance, you might consider replacing *_s with keep(*_s), which will turn off the kleene star's backtracking behavior. (Google "regex independent subexpression" to read about it.) That's really the only suggestion I can make. The rest looks very nice. -- Eric Niebler Boost Consulting www.boost-consulting.com

In particular, I'd like to know: 1) Is it possible to avoid my (ugly) use of "*optsref"? I would have liked to write something like sregex rx_opt = ( (s1= +_w) >> *_s >> "=>" >> *_s >> (s2= +_w) ) [ (ref(result[ref(res_name)])) [s1] = s2 ]; ie nest the maps directly. The way I tried, it would not compile.
It works if you change the parenthesis: [ (ref(result) [ref(res_name)]) [s1] = s2 ]; Regards, Dave Jenkins
participants (4)
-
Christoph
-
Christoph Duelli
-
Dave Jenkins
-
Eric Niebler