Boost logo

Boost Users :

From: Jens Theisen (jth02_at_[hidden])
Date: 2006-09-16 07:38:57


Joel de Guzman <joel_at_[hidden]> writes:

> I think Jens got it incorrectly. The var will *not* be created at
> all on an unsuccessful match.

Which is cool, but misses my point. Take the example again, but this
time I have made a mistake:

        factor
            = ureal_p[factor.val = arg1]
            | '(' >> expression[factor.val = arg1] >> ')'
            | ('-' >> factor)
            | ('+' >> factor[factor.val = arg1])
            ;

In the third branch, I forgot to assign something. It will compile,
though it better should not - the parsed return value is bogus.

How do I prevent such bugs?

Hand-written parsers can be written in such a safe way:

optional< value_t > factor(tokens_t tokens)
{
  // value_t is not default constructible

       if(optional< value_t > temp = ureal_p(tokens)) return temp;
  else if(optional< value_t > temp = expression(tokens)) return temp;
  else ...

  return none;
}

How to do it with spirit?

Regards,

Jens


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