指示 Qi 转换属性失败的正确方法是什么?

What's the appropriate way to indicate a Qi transform attribute fail?

boost::spirit::traits::transform_attribute 中指示解析失败的正确方法是什么?我可以抛出任何旧的异常吗,或者是否有特定的事情要我做?

namespace boost
{
    namespace spirit
    {
        namespace traits
        {
            template <>
            struct transform_attribute<TwoNums, std::vector<char>, qi::domain>
            {
                typedef std::vector<char> type;

                static type pre(TwoWords&) { return{}; }

                static void post(TwoWords& val, type const& attr) {
                    std::string stringed(attr.begin(), attr.end());
                    //
                    std::vector<std::string> strs;
                    boost::split(strs, stringed, ",");
                    if(strs.size()!=2) 
                    { 
                        //What do I do here?
                    }
                    val = TwoWords(strs[0],strs[1]);
                }
                static void fail(FDate&) { }
            };
        }
    }
}
  1. 是的,引发异常似乎是唯一的带外方式。

  2. 你可以用qi::on_error诱捕和回应它

  3. 但是,您不太清楚您需要它做什么。在 parser 中使用 split 似乎有点颠倒。拆分基本上是一个糟糕的解析版本。

    为什么没有子解析规则?

1。简单的投掷...

Live On Coliru

#include <boost/algorithm/string.hpp>
#include <boost/spirit/include/qi.hpp>
#include <iomanip>

namespace qi = boost::spirit::qi;

struct Invalid {};

struct TwoWords {
    std::string one, two;
};

namespace boost { namespace spirit { namespace traits {

    template <> struct transform_attribute<TwoWords, std::vector<char>, qi::domain> {
        typedef std::vector<char> type;

        static type pre(TwoWords &) { return {}; }

        static void post(TwoWords &val, type const &attr) {
            std::string stringed(attr.begin(), attr.end());

            std::vector<std::string> strs;
            boost::split(strs, stringed, boost::is_any_of(","));
            if (strs.size() != 2) {
                throw Invalid{};
            }
            val = TwoWords{ strs.at(0), strs.at(1) };
        }

        static void fail(TwoWords &) {}
    };

} } }

template <typename It>
struct Demo1 : qi::grammar<It, TwoWords()> {
    Demo1() : Demo1::base_type(start) {
        start = qi::attr_cast<TwoWords>(+qi::char_);
    }
  private:
    qi::rule<It, TwoWords()> start;
};

int main() {
    Demo1<std::string::const_iterator> parser;

    for (std::string const input : { ",", "a,b", "a,b,c" }) {
        std::cout << "Parsing " << std::quoted(input) << " -> ";

        TwoWords tw;
        try {
            if (parse(input.begin(), input.end(), parser, tw)) {
                std::cout << std::quoted(tw.one) << ", " << std::quoted(tw.two) << "\n";
            } else {
                std::cout << "Failed\n";
            }
        } catch(Invalid) {
            std::cout << "Input invalid\n";
        }
    }
}

版画

Parsing "," -> "", ""
Parsing "a,b" -> "a", "b"
Parsing "a,b,c" -> Input invalid

2。处理解析器内部的错误

这感觉有点老套,因为它需要你抛出一个 expectation_failure

This is not optimal since it assumes you know the iterator the parser is going to be instantiated with.

on_error was designed for use with expectation points

*Live On Coliru

#include <boost/algorithm/string.hpp>
#include <boost/spirit/include/qi.hpp>
#include <iomanip>

namespace qi = boost::spirit::qi;

struct Invalid {};

struct TwoWords {
    std::string one, two;
};

namespace boost { namespace spirit { namespace traits {

    template <> struct transform_attribute<TwoWords, std::vector<char>, qi::domain> {
        typedef std::vector<char> type;

        static type pre(TwoWords &) { return {}; }

        static void post(TwoWords &val, type const &attr) {
            std::string stringed(attr.begin(), attr.end());

            std::vector<std::string> strs;
            boost::split(strs, stringed, boost::is_any_of(","));
            if (strs.size() != 2) {
                throw qi::expectation_failure<std::string::const_iterator>({}, {}, info("test"));
            }
            val = TwoWords{ strs.at(0), strs.at(1) };
        }

        static void fail(TwoWords &) {}
    };

} } }

template <typename It>
struct Demo2 : qi::grammar<It, TwoWords()> {
    Demo2() : Demo2::base_type(start) {
        start = qi::attr_cast<TwoWords>(+qi::char_);

        qi::on_error(start, [](auto&&...){});
        // more verbose spelling:
        // qi::on_error<qi::error_handler_result::fail> (start, [](auto&&...){[>no-op<]});
    }
  private:
    qi::rule<It, TwoWords()> start;
};

int main() {
    Demo2<std::string::const_iterator> parser;

    for (std::string const input : { ",", "a,b", "a,b,c" }) {
        std::cout << "Parsing " << std::quoted(input) << " -> ";

        TwoWords tw;
        try {
            if (parse(input.begin(), input.end(), parser, tw)) {
                std::cout << std::quoted(tw.one) << ", " << std::quoted(tw.two) << "\n";
            } else {
                std::cout << "Failed\n";
            }
        } catch(Invalid) {
            std::cout << "Input invalid\n";
        }
    }
}

版画

Parsing "," -> "", ""
Parsing "a,b" -> "a", "b"
Parsing "a,b,c" -> Failed

3。最后:子规则规则!

让我们假设一个稍微有趣的语法,其中您有一个 ; 分隔列表 TwoWords:

"foo,bar;a,b"

我们解析成TwoWords的向量:

using Word = std::string;
struct TwoWords { std::string one, two; };
using TwoWordses = std::vector<TwoWords>;

我们只是调整结构并依赖自动属性传播,而不是将特征用于 "coerce" 属性:

BOOST_FUSION_ADAPT_STRUCT(TwoWords, one, two)

解析器模仿数据类型:

template <typename It>
struct Demo3 : qi::grammar<It, TwoWordses()> {
    Demo3() : Demo3::base_type(start) {
        using namespace qi;
        word     = *(graph - ',' - ';');
        twowords = word >> ',' >> word;
        start    = twowords % ';';
    }
  private:
    qi::rule<It, Word()>       word;
    qi::rule<It, TwoWords()>   twowords;
    qi::rule<It, TwoWordses()> start;
};

而完整的测试是Live On Coliru

#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <iomanip>

namespace qi = boost::spirit::qi;

using Word = std::string;
struct TwoWords { std::string one, two; };
using TwoWordses = std::vector<TwoWords>;

BOOST_FUSION_ADAPT_STRUCT(TwoWords, one, two);

template <typename It>
struct Demo3 : qi::grammar<It, TwoWordses()> {
    Demo3() : Demo3::base_type(start) {
        using namespace qi;
        word     = *(graph - ',' - ';');
        twowords = word >> ',' >> word;
        start    = twowords % ';';
    }
  private:
    qi::rule<It, Word()>       word;
    qi::rule<It, TwoWords()>   twowords;
    qi::rule<It, TwoWordses()> start;
};

int main() {
    using It = std::string::const_iterator;
    Demo3<It> parser;

    for (std::string const input : {
            ",", 
            "foo,bar",
            "foo,bar;qux,bax",
            "foo,bar;qux,bax;err,;,ful",

            // failing cases or cases with trailing input:
            "",
            "foo,bar;",
            "foo,bar,qux",
            })
    {
        std::cout << "Parsing " << std::quoted(input) << " ->\n";

        TwoWordses tws;
        It f = input.begin(), l = input.end();
        if (parse(f, l, parser, tws)) {
            for(auto& tw : tws) {
                std::cout << " - " << std::quoted(tw.one) << ", " << std::quoted(tw.two) << "\n";
            }
        } else {
            std::cout << "Failed\n";
        }

        if (f != l) {
            std::cout << "Remaining unparsed input: " << std::quoted(std::string(f,l)) << "\n";
        }

    }
}

版画

Parsing "," ->
 - "", ""
Parsing "foo,bar" ->
 - "foo", "bar"
Parsing "foo,bar;qux,bax" ->
 - "foo", "bar"
 - "qux", "bax"
Parsing "foo,bar;qux,bax;err,;,ful" ->
 - "foo", "bar"
 - "qux", "bax"
 - "err", ""
 - "", "ful"
Parsing "" ->
Failed
Parsing "foo,bar;" ->
 - "foo", "bar"
Remaining unparsed input: ";"
Parsing "foo,bar,qux" ->
 - "foo", "bar"
Remaining unparsed input: ",qux"