3

(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;
};
Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77
Max
  • 3,128
  • 1
  • 24
  • 24
  • can someone edit the title to remove the qi::as_wstring part ? it is not relevent (I think) – Max Feb 15 '17 at 19:36
  • 4
    I think you meant to write `+(digit)` (one or more digits) instead of `+(int_)` (one or more integers). Note that it won't do the same thing as `int_` (no range check, no +/- sign). If you want to capture the input recognized by 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 – Boris Glick Feb 15 '17 at 21:57
  • (got back on the project) Hey @BorisGlick, can you put that as an answer so I can accept this as an answer ? Thanks. – Max Apr 13 '17 at 17:34

1 Answers1

1

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

Community
  • 1
  • 1
Boris Glick
  • 202
  • 1
  • 4