[Spirit] Creating a simple text format parser

Hi, I'm trying to create a simple parser to format a text input, Spirit seems to fit the bill nicely for my needs, but I'm having a few problems. First of all while the documentation is rather large, it would be sweet with some more small examples for a few small but as different as possible grammars. There's some already hidden in the docs at various places, it would be sweet if there was a section in the manual with all the code examples gathered. Anyway, on to my little problem. I'm trying to create a grammar which is fairly close to HTML in one regard, consider the following: Some default text <font=Arial><color=FFFFFF>some white arial text</font> some white text with default font</color> some text with default settings So basically I need to create a rule which when encountering a tag pushes it onto a stack, and later on pops it when the matching closing tag is found, clearing its states. I have got a bit into that part, even if I would love some suggestions on it still if anyone have done something similar in Spirit. But the real problem actually is with the text which isn't a tag. I use anychar_p for that at the moment to capture it with a simple semantic action. The problem is that anychar_p just retrieves a single character, when I'd like to actually get it in the form of a string instead. To render it 1 char at the time is highly inefficient. I could use some global string to which I accumulate the chars and then let the tags check it and render its contents, but that seems hackish at best. For a parser such as this, is there a better way perhaps? //Ylisen

On Fri, Nov 21, 2008 at 09:55, Kisso Bajslovski <ylisaren@gmail.com> wrote:
But the real problem actually is with the text which isn't a tag. I use anychar_p for that at the moment to capture it with a simple semantic action. The problem is that anychar_p just retrieves a single character, when I'd like to actually get it in the form of a string instead. To render it 1 char at the time is highly inefficient. I could use some global string to which I accumulate the chars and then let the tags check it and render its contents, but that seems hackish at best. For a parser such as this, is there a better way perhaps?
I'm not a spirit guru, but I suspect you want something like this: ( *~ch_p('<') )[ push_the_iterator_range_as_a_string() ] So you get everything up to the next "tag" at once. ~ Scott

I'm not a spirit guru, but I suspect you want something like this:
( *~ch_p('<') )[ push_the_iterator_range_as_a_string() ]
So you get everything up to the next "tag" at once.
~ Scott
Ah, ~ch_p('<') seems a lot better, thanks :) Will take a look at the spirit mailing list, thanks Joel.

Kisso Bajslovski wrote:
Hi, I'm trying to create a simple parser to format a text input, Spirit seems to fit the bill nicely for my needs, but I'm having a few problems. First of all while the documentation is rather large, it would be sweet with some more small examples for a few small but as different as possible grammars. There's some already hidden in the docs at various places, it would be sweet if there was a section in the manual with all the code examples gathered.
Anyway, on to my little problem. I'm trying to create a grammar which is fairly close to HTML in one regard, consider the following:
Some default text <font=Arial><color=FFFFFF>some white arial text</font> some white text with default font</color> some text with default settings
So basically I need to create a rule which when encountering a tag pushes it onto a stack, and later on pops it when the matching closing tag is found, clearing its states. I have got a bit into that part, even if I would love some suggestions on it still if anyone have done something similar in Spirit. But the real problem actually is with the text which isn't a tag. I use anychar_p for that at the moment to capture it with a simple semantic action. The problem is that anychar_p just retrieves a single character, when I'd like to actually get it in the form of a string instead. To render it 1 char at the time is highly inefficient. I could use some global string to which I accumulate the chars and then let the tags check it and render its contents, but that seems hackish at best. For a parser such as this, is there a better way perhaps?
You might want to post to Spirit's list: https://lists.sourceforge.net/lists/listinfo/spirit-general Regards, -- Joel de Guzman http://www.boostpro.com http://spirit.sf.net
participants (3)
-
Joel de Guzman
-
Kisso Bajslovski
-
Scott McMurray