0

I want to parse something like "1m2s3ms" where each part is optional e.g. "1m3ms"

qi::int_parser<sint64, 10> dec64;
const qi::rule<const char *, sint64()> nsRule = dec64 >> qi::lit("ns");
const qi::rule<const char *, sint64()> usRule = dec64 >> qi::lit("us");
const qi::rule<const char *, sint64()> msRule = dec64 >> qi::lit("ms");
const qi::rule<const char *, sint64()> sRule = dec64 >> qi::lit("s");
const qi::rule<const char *, sint64()> mRule = dec64 >> qi::lit("m");
const qi::rule<const char *, sint64()> hRule = dec64 >> qi::lit("h");
auto result = qi::parse(f, f + length, 
  -hRule >>
  -mRule >>
  -sRule >>
  -msRule >>
  -usRule >>
  -nsRule, h, m, s, ms, us, ns);

The problem is, that the mRule and msRule are very similar and mRule wins while parsing the example above. Is there any directive I can use?

VAr
  • 2,551
  • 1
  • 27
  • 40
SRoeber
  • 61
  • 1
  • 1
    You could use a [not predicate](http://www.boost.org/doc/libs/1_63_0/libs/spirit/doc/html/spirit/qi/reference/operator/not_predicate.html) in the `mRule` to specify that the `m` character must not be immediately followed by an `s`. – Dan Mašek Feb 20 '17 at 18:25
  • Great. That works. Thank you! – SRoeber Feb 21 '17 at 06:30
  • 1
    Not exactly the same, but related: https://stackoverflow.com/a/24943150/85371 – sehe Feb 21 '17 at 17:17
  • Is there a way, in general, to have some optional parsers like above but ensure that at least one has parsed a valid value? – SRoeber Feb 24 '17 at 06:45

0 Answers0