Boost.Spirit:将preparsing与关键字解析器和Nabialek技巧相结合

时间:2015-08-11 02:51:22

标签: c++ parsing boost boost-spirit boost-spirit-qi

我有类似json形式的输入

{
  type: "name",
  value: <json_value>,
}

类型名称可以是&#34; int1&#34;,&#34; int2a&#34;,&#34; int2b&#34; (当然,我简化了实际情况以提供最小的相关代码)。

该值始终确认为JSON语法,但也取决于类型名称。可能的情况是:

type: int1 => expected value: <number>
type: int2a => expected value: [ <number>, <number> ]
type: int2b => expected value: [ <number>, <number> ]

我需要将输入解析为以下数据类型:

struct int_1  { int i1; };
struct int_2a { int i1, i2; };
struct int_2b { int i1, i2; };

using any_value = boost::variant<int_1, int_2a, int_2b>;

struct data { std::string type; any_value value; };

我将关键字解析器与Nabialek技巧相结合。我创建符号表并将指向 int_1 int_2a int_2b 解析器的指针存储到其中:

using value_rule_type = qi::rule<It, any_value (), Skipper>;

qi::symbols<char, value_rule_type *> value_selector;

qi::rule<It, int_1  (), Skipper> int1_parser;
qi::rule<It, int_2a (), Skipper> int2a_parser;
qi::rule<It, int_2b (), Skipper> int2b_parser;

value_rule_type int1_rule, int2a_rule, int2b_rule;

int1_parser  =        int_                      ;
int2a_parser = '[' >> int_ >> ',' >> int_ >> ']';
int2b_parser = '[' >> int_ >> ',' >> int_ >> ']';

int1_rule  = int1_parser;
int2a_rule = int2a_parser;
int2b_rule = int2b_parser;

value_selector.add
  ( "\"int1\"",  &int1_rule  )
  ("\"int2a\"",  &int2a_rule )
  ("\"int2b\"",  &int2b_rule )
;

我使用关键字解析器来解析外部数据结构:

data_
  %= eps [ _a = px::val (nullptr) ]
  > '{' > (
      kwd ( lit ("\"type\"" ) ) [ ':' >> parsed_type_ (_a) >> ',' ]
    / kwd ( lit ("\"value\"") ) [ ':' >> value_ (_a)       >> ',' ]
  ) > '}'
;

parsed_type_ rule在这里查看符号表中的类型名称,并将数据规则的局部变量设置为找到的规则指针。

parsed_type_ %=  raw[value_selector [ _r1  = _1 ]];

而且value_规则具有Nabialek技巧的通常形式:

value_ = lazy (*_r1);

这个解析器工作正常(live demo)...除了在类型名称之前传递值的情况:

{
  value: <json_value>,
  type: "name",
}

因为我们在存储的指针指针中有NULL,而#34;类型的解析器&#34;字段尚未运行,程序在解析&#34;值&#34;期间崩溃。字段。

我想解决这个问题。如果值字段在类型名称字段后面,我喜欢应用当前的解析器逻辑(如在现场演示中)。

如果value在类型键之前,我想用json解析器预先解析value字段(只是为了找到该字段的结束边界)并以迭代器范围的形式存储该字段一些局部变量。解析器得到&#34;键入&#34;字段,我想在存储的范围内启动特定的解析器 - int1_rule,int2a_rule或int2b_rule。

所以,表达式

value_ = lazy (*_r1);

应该改为:

if (_r1 == NULL) { 
  <parse json value>
  <store raw range into local variable> 
} else { 
  lazy (*_r1); 
}  

和表达

parsed_type_ %=  raw[value_selector [ _r1  = _1 ]];

应该扩展为:

if (has stored range) parse it with lazy (*_r1);  

不幸的是,我不知道如何实现它,或者根本不可能。

为方便起见,我将stackoverflow上的简化JSON解析器包含到我的现场演示中。

问题:精神上是否有可能?如果是的话,怎么做?

PS。完整的演示源:

#define BOOST_SPIRIT_USE_PHOENIX_V3

#include <boost/optional.hpp>
#include <boost/variant.hpp>
#include <boost/variant/recursive_variant.hpp>

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

#include <boost/spirit/repository/include/qi_kwd.hpp>
#include <boost/spirit/repository/include/qi_keywords.hpp>

#include <boost/fusion/adapted.hpp>
#include <boost/fusion/adapted/std_pair.hpp>

namespace spirit = ::boost::spirit;
namespace qi = spirit::qi;
namespace px = ::boost::phoenix;

namespace json {

struct null {
  constexpr bool operator== (null) const { return true; }
};

template<typename Ch, typename Tr>
std::basic_ostream<Ch, Tr>&
operator<< (std::basic_ostream<Ch, Tr>& os, null) { return os << "null"; }

using text = std::string;

using value = boost::make_recursive_variant<
    null
  , text                                        // string
  , double                                      // number
  , std::map<text, boost::recursive_variant_>   // object
  , std::vector<boost::recursive_variant_>      // array
  , bool
>::type;

using member = std::pair<text, value>;
using object = std::map<text, value>;
using array = std::vector<value>;

static auto const null_ = "null" >> qi::attr (null {});

static auto const bool_ =
  "true" >> qi::attr (true) | "false" >> qi::attr (false);

#if 0
static auto const text_   =
  '"' >> qi::raw [*('\\' >> qi::char_ | ~qi::char_('"'))] >> '"';
#endif

template <typename It, typename Skipper = qi::space_type>
struct grammar : qi::grammar<It, value (), Skipper>
{
  grammar () : grammar::base_type (value_)
  {
    using namespace qi;

    text_   = '"' >> qi::raw [*('\\' >> qi::char_ | ~qi::char_('"'))] >> '"';

    value_  = null_ | bool_ | text_ | double_ | object_ | array_;
    member_ = text_ >> ':' >> value_;
    object_ = '{' >> -(member_ % ',') >> '}';
    array_  = '[' >> -(value_ % ',') >> ']';

    BOOST_SPIRIT_DEBUG_NODES((value_)(member_)(object_)(array_))
  }

private:
  qi::rule<It, std::string ()> text_;
  qi::rule<It, json:: value (), Skipper> value_;
  qi::rule<It, json::member (), Skipper> member_;
  qi::rule<It, json::object (), Skipper> object_;
  qi::rule<It, json:: array (), Skipper> array_;
};

template <typename Range,
  typename It = typename boost::range_iterator<Range const>::type>
value parse(Range const& input)
{
  grammar<It> g;

  It first(boost::begin(input)), last(boost::end(input));
  value parsed;
  bool ok = qi::phrase_parse(first, last, g, qi::space, parsed);

  if (ok && (first == last))
    return parsed;

  throw std::runtime_error("Remaining unparsed: '" +
                           std::string(first, last) + "'");
}

} // namespace json

namespace mine {

struct int_1  { int_1 (int i) : i1 (i) {} int_1 () : i1 () {} int i1; };
struct int_2a { int i1, i2; };
struct int_2b { int i1, i2; };

using any_value = boost::variant<int_1, int_2a, int_2b>;

struct data { std::string type; any_value value; };

template <class C, class T> std::basic_ostream<C,T>&
operator<< (std::basic_ostream<C,T>& os, int_1 const& i)
{
  return os << "{int1:" << i.i1 << '}';
}

template <class C, class T> std::basic_ostream<C,T>&
operator<< (std::basic_ostream<C,T>& os, int_2a const& i)
{
  return os << "{int2a:" << i.i1 << ',' << i.i2 << '}';
}

template <class C, class T> std::basic_ostream<C,T>&
operator<< (std::basic_ostream<C,T>& os, int_2b const& i)
{
  return os << "{int2b:" << i.i1 << ',' << i.i2 << '}';
}

template <class C, class T> std::basic_ostream<C,T>&
operator<< (std::basic_ostream<C,T>& os, data const& d)
{
  return os << "{type=" << d.type << ",value=" << d.value << '}';
}

}

BOOST_FUSION_ADAPT_STRUCT(mine::int_1,  (int, i1) )
BOOST_FUSION_ADAPT_STRUCT(mine::int_2a, (int, i1) (int, i2) )
BOOST_FUSION_ADAPT_STRUCT(mine::int_2b, (int, i1) (int, i2) )
BOOST_FUSION_ADAPT_STRUCT(mine::data,(std::string,type)(mine::any_value,value))

namespace mine {

template <typename It, typename Skipper = qi::space_type>
struct grammar: qi::grammar<It, data (), Skipper>
{
  grammar () : grammar::base_type (start)
  {
    using namespace qi;
    using spirit::repository::qi::kwd;

    int1_parser  =        int_                      ;
    int2a_parser = '[' >> int_ >> ',' >> int_ >> ']';
    int2b_parser = '[' >> int_ >> ',' >> int_ >> ']';

    int1_rule  = int1_parser;
    int2a_rule = int2a_parser;
    int2b_rule = int2b_parser;

    value_selector.add
      ( "\"int1\"",  &int1_rule  )
      ("\"int2a\"",  &int2a_rule )
      ("\"int2b\"",  &int2b_rule )
    ;

    start = data_.alias ();

    parsed_type_ %=  raw[value_selector [ _r1  = _1 ]];
    value_ = lazy (*_r1);

    data_
      %= eps [ _a = px::val (nullptr) ]
      > '{' > (
          kwd ( lit ("\"type\"" ) ) [ ':' >> parsed_type_ (_a) >> ',' ]
        / kwd ( lit ("\"value\"") ) [ ':' >> value_ (_a)       >> ',' ]
      ) > '}'
     ;

    on_error<fail>(start,
                   px::ref(std::cout)
                   << "Error! Expecting "
                   << qi::_4
                   << " here: '"
                   << px::construct<std::string>(qi::_3, qi::_2)
                   << "'\n"
                   );

  }

private:
  using value_rule_type = qi::rule<It, any_value (), Skipper>;
  qi::rule<It, data (), Skipper> start;

  qi::rule<It, data (), qi::locals<value_rule_type *>, Skipper> data_;

  qi::symbols<char, value_rule_type *> value_selector;

  qi::rule<It, int_1  (), Skipper> int1_parser;
  qi::rule<It, int_2a (), Skipper> int2a_parser;
  qi::rule<It, int_2b (), Skipper> int2b_parser;

  value_rule_type int1_rule, int2a_rule, int2b_rule;

  qi::rule<It, std::string (value_rule_type *&)         > parsed_type_;
  qi::rule<It, any_value   (value_rule_type *&), Skipper> value_;
};

template <typename Range,
typename It = typename boost::range_iterator<Range const>::type>
data parse(Range const& input)
{
  grammar<It> g;

  It first(boost::begin(input)), last(boost::end(input));
  data parsed;
  bool ok = qi::phrase_parse(first, last, g, qi::space, parsed);

  if (ok && (first == last))
    return parsed;

  throw std::runtime_error("Remaining unparsed: '" +
                           std::string(first, last) + "'");
}

}

static std::string const sample1 = R"(
{
  "type": "int1",
  "value": 111,
})";

static std::string const sample2 = R"(
{
  "type": "int2a",
  "value": [ 111, 222 ],
})";

static std::string const sample3 = R"(
{
  "type": "int2b",
  "value": [ 111, 333 ],
})";

static std::string const sample4 = R"(
{
  "value": 111,
  "type": "int1",
})";

int main()
{
  auto mine = mine::parse(sample1); std::cout << mine << '\n';
       mine = mine::parse(sample2); std::cout << mine << '\n';
       mine = mine::parse(sample3); std::cout << mine << '\n';
       // mine = mine::parse(sample4); std::cout << mine << '\n';
  return 0;
}

1 个答案:

答案 0 :(得分:1)

精神无法展望未来。

Nabialek技巧与变体替代解析器的目标不同。

因此,从概念上讲,您所描述的内容无法完成:您无法根据将来的类型切换解析器。

Nabialek技巧根本不适合这种情况。您需要的是一般化的JSON数据类型和后处理函数,以便在知道所有必要数据后创建实际的AST鼻子。

我过去发布了全面的JSON语法,并在我自己的项目中亲自使用它们。

您描述的解决方法可能被解释为与此方法类似,但我得到的印象是您努力坚持当前的解析器结构。我说你需要重构解析器(概念上),只有然后你可能会发现你仍然可以重用旧解析器的位。

如果你关心并且有耐心,当我靠近有互联网的电脑时,我可以尝试一下手。