Search code examples
boost-spiritboost-spirit-qi

Can I test a parsed number as part of the rule. int_ <= 120


I'm very new to spirit::qi 2.5.2 and I'm not sure if I can test the parsed value as part of the rule.

I've seen this;

bool c = true; // a flag
test_parser("1234", eps(phx::ref(c) == true) >> int_);
test_phrase_parser("1 2 3 4",
  *(eps(phx::ref(c) == true) >> int_[phx::ref(c) = (_1 == 4)]));

and this Parse time_period expression with Boost::Spirit but that parser would except 50:90:90 as a valid time;

I want to parse the number first and than make sure it is <= the value.

void TestRule()
{
  using boost::phoenix::ref;
  using qi::_1;
  using qi::_val;
  using qi::eps;

  qi::uint_parser<unsigned int, 10, 2, 2> uint2_p;

  std::wstring mTime = L"50:90:90";  // This should fail as it isn't a valid time

  auto f = mTime.begin(), l = mTime.end();

  bool validTime = qi::parse(f, l, uint2_p[_val = _1] >> eps(_val <= 24) >> ":" >> uint2_p >> ":" >> uint2_p);
}

The above code fails.

Can I do this or do I need to use a function?

Thank you.


Solution

  • This correctly validates for input like 23:59:59 and fails for input like 24:00:00.

    bool validTime = qi::parse(f, l, uint2_p[ _pass = _1<24] >> ":" >> uint2_p[ _pass = _1<60] >> ":" >> uint2_p[ _pass = _1<60]);
    

    Thanks for taking the time to look at my question.