Boost logo

Boost Users :

From: Владимир Красовски (vkrasovsky_at_[hidden])
Date: 2004-02-18 03:15:36


Hello everyone

Is it expected behavior, that code like this:

typedef uint_parser<double> uint_double_parser;

my_rule_t max_59_uint
     = max_limit_d<double>(59.)
             [
                 uint_double_parser()
             ] [max_59_uint.value = arg1]
     ;

causes assertion on input like "" or "aaa"?

Investigating problem, I found that in function

parse(ScannerT const& scan) const

hit.value() used directly, without checking if it valid (see original
code). If it is expected use, then what function
parser_result::has_valid_attribute() for ?

I've modified function

parse(ScannerT const& scan) const

(see my code)
and everything works fine, but I'm not sure if such modification is
"legitimate" and does not break anything else.

original code
(<boost_1_31_0>\boost\spirit\core\composite\directives.hpp):

   ///////////////////////////////////////////////////////////////////////////
   //
   // max_bounded class
   //
   ///////////////////////////////////////////////////////////////////////////
   template <typename BoundsT>
   struct max_bounded_gen;

   template <typename ParserT, typename BoundsT>
   struct max_bounded
   : public unary<ParserT, parser<max_bounded<ParserT, BoundsT> > >
   {
       typedef max_bounded<ParserT, BoundsT> self_t;
       typedef unary_parser_category parser_category_t;
       typedef max_bounded_gen<BoundsT> parser_generator_t;
       typedef unary<ParserT, parser<self_t> > base_t;

       template <typename ScannerT>
       struct result
       {
           typedef typename parser_result<ParserT, ScannerT>::type type;
       };

       max_bounded(ParserT const& p, BoundsT const& max__)
       : base_t(p)
       , max_(max__) {}

       template <typename ScannerT>
       typename parser_result<self_t, ScannerT>::type
       parse(ScannerT const& scan) const
       {
           typedef typename parser_result<self_t, ScannerT>::type result_t;
           result_t hit = this->subject().parse(scan);
           if (hit.value() > max_)
           // ^^^^^^^^^^^ no check for validity
               return scan.no_match();
           return hit;
       }

       BoundsT max_;
   };

my code:
   ///////////////////////////////////////////////////////////////////////////
   //
   // max_bounded class
   //
   ///////////////////////////////////////////////////////////////////////////
   template <typename BoundsT>
   struct max_bounded_gen;

   template <typename ParserT, typename BoundsT>
   struct max_bounded
   : public unary<ParserT, parser<max_bounded<ParserT, BoundsT> > >
   {
       typedef max_bounded<ParserT, BoundsT> self_t;
       typedef unary_parser_category parser_category_t;
       typedef max_bounded_gen<BoundsT> parser_generator_t;
       typedef unary<ParserT, parser<self_t> > base_t;

       template <typename ScannerT>
       struct result
       {
           typedef typename parser_result<ParserT, ScannerT>::type type;
       };

       max_bounded(ParserT const& p, BoundsT const& max__)
       : base_t(p)
       , max_(max__) {}

       template <typename ScannerT>
       typename parser_result<self_t, ScannerT>::type
       parse(ScannerT const& scan) const
       {
           typedef typename parser_result<self_t, ScannerT>::type result_t;
           result_t hit = this->subject().parse(scan);
           if (!hit.has_valid_attribute() || hit.value() > max_)
           // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ is this right ?
               return scan.no_match();
           return hit;
       }

       BoundsT max_;
   };

-- 
Best regards,
 Vladimir                          mailto:vkrasovsky_at_[hidden]

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