1

Within a boost::spirit::qi grammar rule, how do you match a string of characters up to and excluding the next whitespace character, as defined by the supplied skipper?

For example, if the grammar is a set of attributes defined as:

attributeList = '(' >> *attribute >> ')';
attribute     = (name : value) | (name : value units);

How do I match any character for name up to and excluding the first skipper character?

For example, for name, I would like to pre-skip, then match all characters except ':' or a skipper character. Do I have to instantiate a skipper within the grammar class, so that I can do something like:

name = +qi::char_ !(skipper | ':');

or can I access the existing supplied skipper object somehow and reference it directly? Also, I don't believe this needs to be wrapped in qi:: lexeme[]...

Thanks in advance for correcting the error of my ways

Steger
  • 887
  • 7
  • 16
  • it does seem you're swimming against the stream here. Could you try to make your question slightly less [XY-dissociated](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)? – sehe Feb 14 '15 at 19:50
  • @sehe -- admonishment duly noted. I may have been a bit lazy in crafting my question. But I did get the answer I was looking for. I have worked with Antlr, lex/yacc, flex/bison and am really liking boost spirit. Tokenizing may be the only thing that's a bit more natural with the old stuff. – Steger Feb 15 '15 at 15:58

1 Answers1

3

In order to do this, you'll need to suppress skipping, so qi::lexeme will have to be involved (or at least qi::no_skip, but you'd only use it to reimplement qi::lexeme), and to do precisely what you write you'll also need the skip parser. Then you could write

qi::lexeme[ +(qi::char_ - ':' - skipper) ]

The requirements seem rather lax, though. It is unusual to allow even non-printable characters such as the bell sign (ASCII 7) in identifiers. I don't know what exactly you're trying to do, so I can't answer such design questions for you, but to me it seems like there's a good chance you'd be happier with a more standard rule such as

qi::lexeme[ qi::alpha >> *qi::alnum ]

(for a very simple example. Your mileage may vary wrt underscores etc.)

Wintermute
  • 42,983
  • 5
  • 77
  • 80
  • Nice answer. OP: some related backgrounds [here](http://stackoverflow.com/questions/17072987/boost-spirit-skipper-issues/17073965#17073965) – sehe Feb 14 '15 at 19:52
  • Thanks for the great answer. I was exploring initially allowing any character in a variable name to later generate richer error messages instead of an early "syntax error" message. Just getting started figuring how to generate meaningful error messages in boost::spirit... – Steger Feb 15 '15 at 16:02