Search code examples
c++boostgrammarboost-spirit-qi

spirit::qi::grammar-name with different char size


I am making a parser with spirit::qi and aim for making it compatible with different char sizes.

template < typename Iterator >
struct grammar : qi::grammar < Iterator >
{
    grammar () : grammar::base_type ( file, alter_string_size_to < Iterator::value_type > ( _szFile ) )
    {

    }

    qi::rule < Iterator > file;
};

Given that

alter_string_size_to < Iterator::value_type > ( _szFile )

successfully returns a pointer to a string containing _szFile in the required char size.

When I compile with

Iterator = std::wstring::const_iterator

I get following error message

cannot convert parameter 2 from 'unsigned short *' to 'const std::string &'

So .. grammar::base_type is only expecting an const std::string & as the second parameter. My question is, how can I tell qi::grammar to expect a string with its Iterators value_type size ?

Thanks in advance !


Solution

  • It's defined as follows in the source:

        grammar(
            start_type const& start
          , std::string const& name_ = "unnamed-grammar")
        : proto::extends<terminal, base_type>(terminal::make(reference_(start)))
        , name_(name_)
        {}
    
        std::string name_;
    

    So you can't. As it's stated in the doc:

    name is an optional string that gives the grammar its name, useful for debugging and error handling.

    So, you'll have to make your own lookup based on it.