Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to parse integer as string (qi::as_wstring)

(disclaimer, I am learning boost spirit)

I am trying to parse an expression like this: F( 1 )

and want to get the 1 as a string ("1" instead of a number (which works with qi::int_)).

I tried something like this (which is wrong, but maybe I am in the right direction), but the resulting string is "\x1" instead of just "1"

Any insight as to what is needed to parse a number into a string?

template <typename Iterator>
struct range_parser : boost::spirit::qi::grammar<Iterator, RangeResult(), iso8859_1::space_type>
{
    range_parser() : range_parser::base_type(start)
    {
        using qi::lexeme;
        using qi::int_;
        using iso8859_1::digit;

        number_as_string %= lexeme[ +(int_) ];

        start %=
            qi::lit("F")
            >> '('
            >> number_as_string
            >> ")"
            ;
    }

    qi::rule<Iterator, std::wstring(), iso8859_1::space_type> number_as_string;
    qi::rule<Iterator, RangeResult(), iso8859_1::space_type> start;
};
like image 494
Max Avatar asked Apr 07 '26 00:04

Max


1 Answers

I think you meant to write +(digit) (one or more digits) instead of +(int_) (one or more integers). Note that +(digit) won't do the same thing as qi::int_ (no range check, no +/- sign, no limit on the number of digits, accepting inputs like "000", etc).

If you want to capture the input recognized by qi::int_, you might try using the raw directive: raw[int_] http://www.boost.org/doc/libs/1_63_0/libs/spirit/doc/html/spirit/qi/reference/directive/raw.html

If necessary, it's possible to save both the parsed value and the raw input of the qi::int_ parser, see here: qi::rule with inherited attribute as inherited attribute

like image 57
Boris Glick Avatar answered Apr 09 '26 13:04

Boris Glick