spirit qi no_case 应用于语法的指令

spirit qi no_case directive applied to grammar

我有一个可以正常工作的语法,现在我想用它来解析不区分大小写的字符串。

我是 运行 Boost 1.46 Ubuntu 12.04 Linux。

我尝试了以下代码:

bool parseSuccess = qi::phrase_parse(begin, end,
                    qi::no_case[grammar], ascii::space, result);

但是 no_case 指令无效。

我是不是做错了什么,或者该指令不能用于非终端?

两件事:

  • no_case[] 要求包含以小写形式指定的解析器表达式
  • 它似乎没有在任何地方记录,但 no_case 指令 不会 传播到 Non-Terminal parsers

In that sense no_case is similar to e.g. locals<>, or the Skipper in that they are part of the parser context; Only in this case, case-sensitive happens to not be controlled from the rule's template arguments.

我似乎记得 Spirit X3 将在这里获得更通用的设施,可能会消除这些限制。

显示操作限制的示例程序:

住在 Coliru

#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>

namespace qi = boost::spirit::qi;

template <typename It, typename Skipper> 
struct Parser : qi::grammar<It, std::string(), Skipper> {
    Parser() : Parser::base_type(start)  {
        using namespace qi;

        a_symbol      += "aap", "noot", "mies";
        start          = raw [ no_case [ a_symbol >> "literal" >> char_("qwerty") >> -subrule_lexeme ] ];
        subrule_lexeme = "also works";

        BOOST_SPIRIT_DEBUG_NODES((start)(subrule_lexeme))
    }
  private:
    qi::symbols<char, qi::unused_type> a_symbol;
    qi::rule<It, std::string(), Skipper> start;
    qi::rule<It, std::string()> subrule_lexeme;
};

// test
using It = std::string::const_iterator;

template <typename S = qi::space_type> void test(S const& s = S()) {
    Parser<It, S> g;

    for (std::string const& input : { 
            "aap\t literal r",
            "aAp\t liTeral R",
            // hitting subrule_lexeme:
            "aap\t literal r\talso works",
            "aAp\t liTeral R\tALSO WoRkS",
        })
    {
        It f = input.begin(), l = input.end();

        std::string parsed;
        bool ok = qi::phrase_parse(f, l, g, s, parsed);

        if (ok)
            std::cout << "Parsed successfully: '" << parsed << "'\n";
        else
            std::cout << "Not matched ('" << input << "')\n";

        if (f!=l)
            std::cout << " -- remaining unparsed input: '" << std::string(f,l) << "'\n";
    }
}

int main()
{
    test(qi::space);
}

打印:

Parsed successfully: 'aap    literal r'
Parsed successfully: 'aAp    liTeral R'
Parsed successfully: 'aap    literal r  also works'
Parsed successfully: 'aAp    liTeral R  '
 -- remaining unparsed input: 'ALSO WoRkS'

并且具有完整的调试跟踪:

<start>
  <try>aap\t literal r</try>
  <subrule_lexeme>
    <try></try>
    <fail/>
  </subrule_lexeme>
  <success></success>
  <attributes>[[a, a, p,    ,  , l, i, t, e, r, a, l,  , r]]</attributes>
</start>
Parsed successfully: 'aap    literal r'
<start>
  <try>aAp\t liTeral R</try>
  <subrule_lexeme>
    <try></try>
    <fail/>
  </subrule_lexeme>
  <success></success>
  <attributes>[[a, A, p,    ,  , l, i, T, e, r, a, l,  , R]]</attributes>
</start>
Parsed successfully: 'aAp    liTeral R'
<start>
  <try>aap\t literal r\talso </try>
  <subrule_lexeme>
    <try>also works</try>
    <success></success>
    <attributes>[[a, l, s, o,  , w, o, r, k, s]]</attributes>
  </subrule_lexeme>
  <success></success>
  <attributes>[[a, a, p,    ,  , l, i, t, e, r, a, l,  , r,     , a, l, s, o,  , w, o, r, k, s]]</attributes>
</start>
Parsed successfully: 'aap    literal r  also works'
<start>
  <try>aAp\t liTeral R\tALSO </try>
  <subrule_lexeme>
    <try>ALSO WoRkS</try>
    <fail/>
  </subrule_lexeme>
  <success>ALSO WoRkS</success>
  <attributes>[[a, A, p,    ,  , l, i, T, e, r, a, l,  , R,     ]]</attributes>
</start>
Parsed successfully: 'aAp    liTeral R  '
 -- remaining unparsed input: 'ALSO WoRkS'