使用布尔属性而不是可选的精神 x3

using a boolean attribute instead optional in spirit x3

在我想要实现的语法中有一些关键字,其中枚举很重要(将具体关键字的枚举 ID 存储在 ast 的节点内),或者甚至只是存在相同的特定关键字 - 因此在布尔上下文中是可选的。我喜欢有一个自我表达的解析器表达式和 ast 节点,所以我想出了以下(可编译的)解决方案:

#include <iostream>
//#define BOOST_SPIRIT_X3_DEBUG
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_struct.hpp>

namespace x3 = boost::spirit::x3;

namespace ast {
    enum class keyword_token {
        UNSPECIFIED, FOO, BAR
    };
    struct buz {
        bool            foo;
        int             dummy;
    };
}

BOOST_FUSION_ADAPT_STRUCT( ast::buz, foo, dummy )

namespace boost { namespace spirit { namespace x3 { namespace traits {
    template <>
    inline void
    move_to(ast::keyword_token&& src, bool& dest) {
        dest = static_cast<bool>(src);
    }
} } } } // boost.spirit.x3.traits


namespace parser {
    auto const FOO = x3::rule<struct _, ast::keyword_token> { "FOO" } = 
        x3::lexeme[ x3::no_case[ "foo" ] >> !(x3::alnum | '_') ]
        >> x3::attr(ast::keyword_token::FOO);
    auto const buz = x3::rule<struct _, ast::buz> { "buz" } = 
        -FOO >> x3::int_;
}

int main() {
    for(std::string const str: {
           "FOO 42",
           "42"
    }) {
      auto iter = str.begin(), end = str.end();
      ast::buz attr;
      bool r = x3::phrase_parse(iter, end, parser::buz, x3::space, attr);

      std::cout << "parse '" << str << "': ";
      if (r && iter == end) {
        std::cout << "succeeded:\n";
        std::cout << (attr.foo ? "FOO " : "") << attr.dummy << "\n";
        std::cout << "\n";
      } else {
        std::cout << "*** failed ***\n";
      }
    }
    return 0;
}

这里 ast 的 buz 节点有一个布尔属性,解析器有 'optional' 语法。背后的想法是 bool 是默认可构造的,并且标准保证由预期的 0 aka false 初始化。此外,我有一个 keyword::UNSPECIFIED 的后备解决方案,它(应该,因为我不是 100% 确定枚举 类)等于 0 - imo 永远不应该被触发 - 也在评估在最坏的情况下,x3 的 move_to(...) 特征内为 false。

运行 这两个测试用例的解析阶段都按预期成功,但第二个测试用例的属性不是预期的;显然 'boolean as optional' 方法没有按预期工作:

<buz>
  <try>FOO 42</try>
  <FOO>
    <try>FOO 42</try>
    <success> 42</success>
    <attributes>1</attributes>
  </FOO>
  <success></success>
  <attributes>[1, 42]</attributes>
</buz>
parse 'FOO 42': succeeded:
FOO 42

<buz>
  <try>42</try>
  <FOO>
    <try>42</try>
    <fail/>
  </FOO>
  <success></success>
  <attributes>[1, 42]</attributes>
</buz>
parse '42': succeeded:
FOO 42

调试模式显示综合属性[1, 42]。那么,我的考虑是否合理,是否可行,如果是,如何修复它以按预期工作? 可能还有另一个问题:没有定义 BOOST_SPIRIT_X3_DEBUG 我收到警告:

warning: 'attr' may be used uninitialized in this function
...
warning: 'attr.ast::buz::dummy' may be used uninitialized in this function

在 cout 行。可能我不理解正确的警告,因为 ast::buz 是默认构造的,我不想将其作为默认值 (false, 0)。

蛮力解决方案是这样写:

auto bool_attr = [](auto p) {
    return x3::omit[ p ] >> x3::attr(true) | x3::attr(false);
};

并在规则中使用它,但我更喜欢 'optional' 语法而不是写 bool_attr(FOO) >> x3::int_

来源也在Coliru

llonesmiz 得到了 does POD class object initialization require constructor? 的观点;我必须显式编写 ast 节点的构造函数:

struct buz {
    bool            foo;
    int             dummy;

    buz() : foo{}, dummy{}
    { }
};

比预期的属性要好:

parse 'FOO 42': succeeded:
FOO 42

parse '42': succeeded:
42

有了这个,上面提到的没有定义的警告 BOOST_SPIRIT_X3_DEBUG 也消失了,这个警告对我来说更有意义 ...