2

boost::spirit::qi次のような「テンプレート」形式を解析するために使用しています。

/path/to/:somewhere:/nifty.json

ここで:somewhere:、名前で識別される任意の文字列を表しますsomewhere(名前は、2 つの文字の間の任意の一連の文字にすることができます:)。私はこれに対応するパーサーを持っていますが、もう 1 つ改善したいと思います。

:somewhere:プレースホルダーの後に続く文字 (この場合は a )を知りたい/です。/しかし、私のパーサーの残りの部分は、これを認識し、次のセクションの一部としてそれを使用する必要があります。

パーサーの残りの部分がそれを見て消費できるように、実際に消費せずに/アフターを「読み取る」にはどうすればよいですか。:somewhere:

4

2 に答える 2

8

あなたは探している

例:

 myrule = lexeme [ *~char_(":") ] >> ":" >>
       (  (&lit('/') >> absolute_path)
        | (relative_path)
       )
于 2013-02-13T15:18:49.897 に答える
2

seheが述べたように、これは先読みパーサー演算子&を使用して実行できますが、キャラクターを出力する場合は、 boost.phoenixqi :: locals、およびqi::attrも必要です。

例えば:

#include <boost/fusion/include/std_pair.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/qi.hpp>

#include <iostream>
#include <string>

namespace qi = boost::spirit::qi;

int main(int argc, char** argv)
{
    std::string input("foo:/bar");
    std::pair<char, std::string> output;

    std::string::const_iterator begin = input.begin(),
                                end = input.end();

    qi::rule<std::string::const_iterator, qi::locals<char>, std::pair<char, std::string>()> duplicate =
          "foo"
       >> qi::omit[
             &(":" >> qi::char_[qi::_a = qi::_1])
          ]
       >> qi::attr(qi::_a)
       >> ":"
       >> *qi::char_;

    bool r = qi::parse(begin,
                       end,
                       duplicate,
                       output);

    std::cout << std::boolalpha
              << r << " "
              << (begin == end) << " '"
              << output.first << "' \""
              << output.second << "\""
              << std::endl;

    return 0;
}

これは以下を出力します:

true true '/' "/bar"
于 2013-02-13T16:04:43.663 に答える