Boost logo

Boost Users :

Subject: [Boost-users] How to write a config file parser with boost::spirit that supports line comments?
From: Lars Rohwedder (l.rohwedder_at_[hidden])
Date: 2010-03-17 09:15:36


Hi there,

I want to write a parser for config files having many entries with this
format:

<name> "{" <token> [ <token> ... ] "}"

The format is "free-form" so there can be line breaks and other
whitespaces within each entry between the tokens etc.

<name> is chset_p("a-zA-Z_"),
<token> is chset_p("a-zA-Z0-9_.-").

This parser works fine so far.

I extended the format to allow even whitespaces etc. in the tokens. So
<token> is changed to (I omit the assign_a actions):

  ( +chset_p("a-zA-Z0-9_.-") |
  ('"' >> +(!ch_p('"')) >> '"') |
  ('\'' >> +(!ch_p('\'')) >> '\'';

which also works fine as I tested. (I know, newlines would be allowed in
these tokens but I hope noone use that.)

But now I want to extend the format to also allow line comments,
starting with '#', ending with '\r' or '\n', and I failed:

Just scanning for '#' also recognized '#' within a quoted token. But
that is not the start of a comment. :-( But first searching for quotes
also catched unbalanced quote chars in comments and confused the parser.

What would be a working solution for this - IMHO still simple - format?

(I should mention that I am stuck with boost 1.35 so I cannot use
spirit2 or all the other new features in the current release. :-(( )

Lars R.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net