使用Boost.Qi实现递归语法

时间:2019-06-14 21:38:51

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

我正在使用Boost.Spirit Qi从一些文本数据构建相当复杂的结构。数据结构可能是递归定义的,因此我需要两个语法互相引用,这就是问题所在。

例如,我有一个语法:

element = line | text | circle | box | composite_element
composite_element = 'C', int, int, '[', +element, ']'

显然,我需要这样的东西:

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <tuple>
#include <boost/fusion/adapted/std_pair.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <boost/spirit/include/qi_eol.hpp>
#include <boost/phoenix.hpp>
#include <vector>
#include <string>

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

struct line {
    int x1;
    int y1;
    int x2;
    int y2;
    int color;
    int width;
    int capstyle;
    int dashstyle;
    int dashlength;
    int dashspace;
};

struct box {
    int x;
    int y;
    int width;
    int height;
    int color;
    int line_width;
    int capstyle;
    int dashstyle;
    int dashlength;
    int dashspace;
    int filltype;
    int fillwidth;
    int angle1;
    int pitch1;
    int angle2;
    int pitch2;
};

struct circle {
    int x;
    int y;
    int radius;
    int color;
    int line_width;
    int capstyle;
    int dashstyle;
    int dashlength;
};

struct text {
    int x;
    int y;
    int color;
    int size;
    int visibility;
    int show_name_value;
    int angle;
    int alignment;
    int num_lines;
    std::vector<std::string> lines;
};

struct composite_component;
using element_t = boost::variant<line, box, circle, text, boost::recursive_wrapper<composite_component>>;

struct composite_component {
    int x;
    int y;
    std::string basename;
    // only used if component is embedded
    // i. e. stores its definition within the schematic file
    std::vector<element_t> elements;
};

struct element {
    // some other fields
    // ...
    element_t element;
};

struct document {
    std::vector<element> elements;
};

BOOST_FUSION_ADAPT_STRUCT(line, x1, y1, x2, y2, color, width, capstyle, dashstyle, dashlength, dashspace)
BOOST_FUSION_ADAPT_STRUCT(box, x, y, width, height, color, line_width, capstyle, dashstyle, dashlength, dashspace, filltype, fillwidth, angle1, pitch1, angle2, pitch2)
BOOST_FUSION_ADAPT_STRUCT(circle, x, y, radius, color, line_width, capstyle, dashstyle, dashlength)
BOOST_FUSION_ADAPT_STRUCT(text, x, y, color, size, visibility, show_name_value, angle, alignment, num_lines, lines)
BOOST_FUSION_ADAPT_STRUCT(composite_component, x, y, basename, elements)
BOOST_FUSION_ADAPT_STRUCT(element, element)
BOOST_FUSION_ADAPT_STRUCT(document, elements)

template <typename Iterator, typename Attribute>
using rule = qi::rule<Iterator, Attribute, qi::blank_type>;

template <typename Iterator>
class composite_element_parser;

template <typename Iterator>
class element_parser : public qi::grammar<Iterator, element(), qi::blank_type> {
public:
    element_parser(): element_parser::base_type{start_rule_}
    {
        using qi::int_;
        using qi::repeat;
        using phoenix::val;
        using phoenix::construct;

        /* other definitions except of the 'line' is omitted in sake of simplicity */
        line_ = 'L' >> int_ >> int_ >> int_ >> int_ >> int_ >>
                    int_ >> int_ >> int_ >> int_ >> int_ >> qi::eol;
        // box = ...
        // circle = ...
        // text = ...
        start_rule_ = (line_ /* || embedded_component_ */) >> qi::eoi;
    }
private:
    rule<Iterator, element()> start_rule_;
    rule<Iterator, line()> line_;
    // here comes the problem - CIRCULAR REFERENCE to incompletely defined template
    // composite_element_parser<Iterator> embedded_component_;
};

template <typename Iterator>
class composite_element_parser : public qi::grammar<Iterator, composite_component(), qi::blank_type> {
    public:
    composite_element_parser() : composite_element_parser::base_type{start_rule_}
    {
        using phoenix::at_c;
        using qi::int_;
        using phoenix::push_back;

        start_rule_ = "C" >> int_ >> int_ >>  qi::lexeme[(qi::char_)[at_c<2>(qi::_val) += qi::_1]]
                                                        >> -(
                                                           "[" >>
                                                           *(element_) [push_back(at_c<3>(qi::_val), qi::_1)] >>
                                                           "]"
                                                           );
    }
    private:
    rule<Iterator, composite_component()> start_rule_;
    element_parser<Iterator> element_;
};

template <typename Iterator>
class document_parser : public qi::grammar<Iterator, document(), qi::blank_type> {
public:
    document_parser() : document_parser::base_type{start_rule_}
    {

        using phoenix::at_c;
        using phoenix::push_back;
        using qi::_val;
        using qi::_0;
        using qi::_1;

        start_rule_ = +(element_)[push_back(at_c<0>(_val), _1)] >> qi::eoi;
    }
    private:
    rule<Iterator, document()> start_rule_;
    element_parser<Iterator> element_;
};

int main(int , char **) {
    document_parser<std::string::const_iterator> parser;
    document doc;
    const std::string text = "v 20180904 2\n"
                             "L 1 2 3 4 5 6 7 8 9 10\n"
                             "C 10 10 FOO\n"
                             "[ "
                             "L 1 2 3 4 5 6 7 8 9 10\n"
                             "]\n";
    bool r = qi::phrase_parse(text.cbegin(), text.cend(), parser, qi::blank, doc);
    std::cout << (r ? "OK" : "FAIL") << std::endl;

     return 0;
}

虽然省略了“文本”,“圆圈”和“框”的规则定义。请注意element_parser定义的私有部分中的注释-编译器将无法实例化不完整的类模板composite_element_parser<Iterator>。我该怎么办?显然,我不能将element_parsercomposite_element_parser作为顶级语法的成员(在我的情况下为document_parser),并且不能在构造函数的初始值设定项列表中相互传递引用/指针,因为它们目前尚未初始化。

更新:该线程可能被识别为Deeply-recursive qi grammars (parsers) with synthesized and inherited attributes的重复项,但我确实无法理解批准的答案。

3 个答案:

答案 0 :(得分:3)

通常,您只是不以这种方式拆分语法。但是,如果您真的想要,有多种方法:

  1. 分别创建语法并将语法从外部分配给rule占位符:

    #include <boost/spirit/include/qi.hpp>
    
    namespace qi = boost::spirit::qi;
    
    template <typename Iterator>
    struct grammar1 : qi::grammar<Iterator, int()>
    {
        grammar1() : grammar1::base_type{start_}
        {
            start_ = '[' >> outer >> ']';
        }
    
        qi::rule<Iterator, int()> outer;
    
    private:
        qi::rule<Iterator, int()> start_;
    };
    
    template <typename Iterator>
    struct grammar2 : qi::grammar<Iterator, int()>
    {
        grammar2() : grammar2::base_type{start_}
        {
            start_ = outer | qi::int_;
        }
    
        qi::rule<Iterator, int()> outer;
    
    private:
        qi::rule<Iterator, int()> start_;
    };
    
    int main()
    {
        char const* s = "[[123]]", * e = s + std::strlen(s);
        grammar2<char const*> g2;
        grammar1<char const*> g1;
        g2.outer = g1;
        g1.outer = g2;
        int value = 0;
        if (qi::parse(s, e, g1, value))
            std::cout << value << '\n';
        else
            std::cout << "failed\n";
    }
    

    https://wandbox.org/permlink/QhA18pIZwVlQ2osi

  2. 动态创建另一个语法,并将其传递给前一个语法:

    #include <boost/spirit/include/qi.hpp>
    
    namespace qi = boost::spirit::qi;
    
    template <typename Iterator>
    struct grammar2;
    
    template <typename Iterator>
    struct grammar1 : qi::grammar<Iterator, int()>
    {
        grammar1()
            : grammar1::base_type{start_}
        {
            outer_ = std::make_unique<grammar2<Iterator>>(start_);
            start_ = '[' >> *outer_ >> ']';  // NOTE: it is not a kleen star!
        }
    
    private:
        std::unique_ptr<grammar2<Iterator>> outer_;
        qi::rule<Iterator, int()> start_;
    };
    
    template <typename Iterator>
    struct grammar2 : qi::grammar<Iterator, int()>
    {
        explicit grammar2(qi::rule<Iterator, int()> const& outer)
            : grammar2::base_type{start_}
        {
            start_ = outer | qi::int_;
        }
    
    private:
        qi::rule<Iterator, int()> start_;
    };
    
    int main()
    {
        char const* s = "[[123]]", * e = s + std::strlen(s);
        grammar1<char const*> const g1;
        int value = 0;
        if (qi::parse(s, e, g1, value))
            std::cout << value << '\n';
        else
            std::cout << "failed\n";
    }
    

    https://wandbox.org/permlink/hJz3v1ApK8GCkquS

答案 1 :(得分:3)

我认为在您的示例中,无需使用语法实例。

规则可以递归/循环地相互引用(因为它们通过引用相互引用)。我会利用这一点。

  

可以仍然分离出语法类(例如,将实现分离为编译单元),但您只想将它​​们粘合在一个中心位置,在那里您可以拥有实例然后相互引用(换句话说:经典所有权管理:如果所有类都不能拥有对象,则让另一个实体同时拥有它们)。

或者,您可以简单地将引用传递给构造函数中的补充语法,并保留这些引用而不是语法实例。

演示

为了方便起见,我采用了第二种方法¹。

我做了几件事:

  • composite_element::elements的类型从std::vector<element_t>固定为std::vector<element>
  • 解决了跳过空格的一些问题:

    • '[' ... ']'块解析不允许输入显示的换行符(请参见skip(qi::space)
    • eol仅在line_之后是必需的,但您的输入将在其他元素之后显示
    • 元素解析器错误地要求eoi-导致解析在第一个元素之后停止(如果不是EOI,则失败)。
    • 船长不可为最终用户维修。我的信条是对呼叫者隐藏它,除非您希望呼叫者实际上能够更改船长
  • 增加了方便性start_rule_,可为船长编码并确保在调试输出中有用地显示每个语法的顶级规则(如果所有内容都称为start_rule_,则几乎没有信息左)

  • 删除了所有内容phoenix(请参见Boost Spirit: "Semantic actions are evil"?):

    • 此示例:

      start_rule_ = +(element_)[push_back(at_c<0>(_val), _1)] >> qi::eoi;
      

      这就是自动属性传播已经完成的工作,因此就足够了:

      start_rule_ = +elements_ >> qi::eoi;
      
    • 此:

      qi::lexeme[(qi::char_)[at_c<2>(qi::_val) += qi::_1]]
      

      有很多问题:它缺少重复(仅解析1个字符),没有指定接受哪些字符,如果重复,sp将读取直到EOI。我怀疑这就是您想要的:

      qi::lexeme[+qi::graph]
      

      另请参阅Boost spirit skipper issues

  • ||更改为|(请参阅Alternative ParserSequential-Or Parser)。

  • 也许更多,但我忘了?哦,是的,我评论了v这一行。

Live On Coliru

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

namespace qi = boost::spirit::qi;

struct line { int x1, y1, x2, y2, color, width, capstyle, dashstyle, dashlength, dashspace; };
struct box { int x, y, width, height, color, line_width, capstyle, dashstyle, dashlength, dashspace, filltype, fillwidth, angle1, pitch1, angle2, pitch2; };
struct circle { int x, y, radius, color, line_width, capstyle, dashstyle, dashlength; };
struct text { int x, y, color, size, visibility, show_name_value, angle, alignment, num_lines;
    std::vector<std::string> lines;
};

struct composite_component;
using element_t = boost::variant<line, box, circle, text, boost::recursive_wrapper<composite_component>>;

struct element {
    // ...
    element_t element;
};

struct composite_component {
    int x;
    int y;
    std::string basename;
    std::vector<element> elements;
};

struct document { std::vector<element> elements; };

BOOST_FUSION_ADAPT_STRUCT(line, x1, y1, x2, y2, color, width, capstyle, dashstyle, dashlength, dashspace)
BOOST_FUSION_ADAPT_STRUCT(box, x, y, width, height, color, line_width, capstyle, dashstyle, dashlength, dashspace, filltype, fillwidth, angle1, pitch1, angle2, pitch2)
BOOST_FUSION_ADAPT_STRUCT(circle, x, y, radius, color, line_width, capstyle, dashstyle, dashlength)
BOOST_FUSION_ADAPT_STRUCT(text, x, y, color, size, visibility, show_name_value, angle, alignment, num_lines, lines)
BOOST_FUSION_ADAPT_STRUCT(composite_component, x, y, basename, elements)
BOOST_FUSION_ADAPT_STRUCT(element, element)
BOOST_FUSION_ADAPT_STRUCT(document, elements)

template <typename Iterator, typename Attribute>
using blank_rule = qi::rule<Iterator, Attribute, qi::blank_type>;

template <typename Iterator>
struct composite_element_parser;

template <typename Iterator>
struct element_parser : qi::grammar<Iterator, element()> {
    element_parser(): element_parser::base_type{start_rule_},
        embedded_component_(*this)
    {
        using qi::int_;

        /* other definitions except of the 'line' is omitted in sake of simplicity */
        line_ = 'L' >> int_ >> int_ >> int_ >> int_ >> int_ >>
                    int_ >> int_ >> int_ >> int_ >> int_;
        // box = ...
        // circle = ...
        // text = ...
        element_rule_ = (line_ | embedded_component_) >> qi::eol;
        start_rule_ = qi::skip(qi::blank) [ element_rule_ ];
        BOOST_SPIRIT_DEBUG_NODES((element_rule_)(line_));
    }
  private:
    qi::rule<Iterator, element()> start_rule_;
    blank_rule<Iterator, element()> element_rule_;
    blank_rule<Iterator, line()> line_;
    composite_element_parser<Iterator> embedded_component_;
};

template <typename Iterator>
struct composite_element_parser : qi::grammar<Iterator, composite_component()> {
    composite_element_parser(element_parser<Iterator> const& ep)
        : composite_element_parser::base_type{start_rule_},
          element_(ep)
    {
        using qi::int_;
        elements_ = -qi::skip(qi::space) [ '[' >> *element_ >> ']' ];
        composite_element_rule_ = 'C' >> int_ >> int_ >> qi::lexeme[+qi::graph] >> elements_;
        start_rule_ = qi::skip(qi::blank) [ composite_element_rule_ ];
        BOOST_SPIRIT_DEBUG_NODES((composite_element_rule_)(elements_));
    }
  private:
    qi::rule<Iterator, composite_component()> start_rule_;
    blank_rule<Iterator, composite_component()> composite_element_rule_;
    blank_rule<Iterator, std::vector<element>()> elements_;
    element_parser<Iterator> const& element_;
};

template <typename Iterator>
struct document_parser : qi::grammar<Iterator, document()> {
    document_parser() : document_parser::base_type{start_rule_}
    {
        document_rule_ = +element_ >> qi::eoi;
        start_rule_ = qi::skip(qi::blank) [ document_rule_ ];
        BOOST_SPIRIT_DEBUG_NODES((document_rule_));
    }
  private:
    qi::rule<Iterator, document()> start_rule_;
    blank_rule<Iterator, document()> document_rule_;
    element_parser<Iterator> element_;
};

int main(int , char **) {
    document_parser<std::string::const_iterator> parser;

    const std::string text = // "v 20180904 2\n"
         "L 1 2 3 4 5 6 7 8 9 10\n"
         "C 10 10 FOO\n"
         "[ "
         "    L 10 20 30 40 50 60 70 80 90 100\n"
         "]\n";

    document doc;
    bool r = qi::parse(text.cbegin(), text.cend(), parser, doc);
    std::cout << (r ? "OK" : "FAIL") << std::endl;
}

打印

OK

调试输出:

<document_rule_>
  <try>L 1 2 3 4 5 6 7 8 9 </try>
  <element_rule_>
    <try>L 1 2 3 4 5 6 7 8 9 </try>
    <line_>
      <try>L 1 2 3 4 5 6 7 8 9 </try>
      <success>\nC 10 10 FOO\n[     L</success>
      <attributes>[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]</attributes>
    </line_>
    <success>C 10 10 FOO\n[     L </success>
    <attributes>[[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]]</attributes>
  </element_rule_>
  <element_rule_>
    <try>C 10 10 FOO\n[     L </try>
    <line_>
      <try>C 10 10 FOO\n[     L </try>
      <fail/>
    </line_>
    <composite_element_rule_>
      <try>C 10 10 FOO\n[     L </try>
      <elements_>
        <try>\n[     L 10 20 30 40</try>
        <element_rule_>
          <try>L 10 20 30 40 50 60 </try>
          <line_>
            <try>L 10 20 30 40 50 60 </try>
            <success>\n]\n</success>
            <attributes>[[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]]</attributes>
          </line_>
          <success>]\n</success>
          <attributes>[[[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]]]</attributes>
        </element_rule_>
        <element_rule_>
          <try>]\n</try>
          <line_>
            <try>]\n</try>
            <fail/>
          </line_>
          <composite_element_rule_>
            <try>]\n</try>
            <fail/>
          </composite_element_rule_>
          <fail/>
        </element_rule_>
        <success>\n</success>
        <attributes>[[[[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]]]]</attributes>
      </elements_>
      <success>\n</success>
      <attributes>[[10, 10, [F, O, O], [[[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]]]]]</attributes>
    </composite_element_rule_>
    <success></success>
    <attributes>[[[10, 10, [F, O, O], [[[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]]]]]]</attributes>
  </element_rule_>
  <element_rule_>
    <try></try>
    <line_>
      <try></try>
      <fail/>
    </line_>
    <composite_element_rule_>
      <try></try>
      <fail/>
    </composite_element_rule_>
    <fail/>
  </element_rule_>
  <success></success>
  <attributes>[[[[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]], [[10, 10, [F, O, O], [[[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]]]]]]]]</attributes>
</document_rule_>

¹我通常不这样做,因为它会引起生命周期问题,并且我认为引用成员是代码臭味,除非对于短寿命的仿函数对象

答案 2 :(得分:3)

基于我的earlier answer(其中展示了传递引用的方法),我简化了对此的回答:

template <typename Iterator>
struct document_parser : qi::grammar<Iterator, document()> {
    document_parser() : document_parser::base_type{start_}
    {
        using namespace qi;

        line_              = 'L' >> auto_;
        box_               = 'B' >> auto_;
        circle_            = 'S' >> auto_;
        // text            = 'T' >> ...;
        element_           = (line_ | box_ | circle_ | composite_element_) >> eol;
        elements_          = -skip(space) [ '[' >> skip(blank) [*element_] >> ']' ];
        composite_element_ = 'C' >> int_ >> int_ >> lexeme[+graph] >> elements_;

        document_          = +element_ >> eoi;

        start_ = skip(blank) [ document_ ];
        BOOST_SPIRIT_DEBUG_NODES((document_)(element_)(composite_element_)(elements_)(line_));
    }
  private:
    qi::rule<Iterator, document()> start_;
    qi::rule<Iterator, document(),             qi::blank_type> document_;
    qi::rule<Iterator, element(),              qi::blank_type> element_;
    qi::rule<Iterator, line(),                 qi::blank_type> line_;
    qi::rule<Iterator, box(),                  qi::blank_type> box_;
    qi::rule<Iterator, circle(),               qi::blank_type> circle_;
    qi::rule<Iterator, composite_component(),  qi::blank_type> composite_element_;
    qi::rule<Iterator, std::vector<element>(), qi::blank_type> elements_;
};

请注意,它现在也解析方框和圆圈。您可能需要阅读有关Auto Parser的魔法。

查看 Live On Wandbox

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

namespace qi = boost::spirit::qi;

struct line { int x1, y1, x2, y2, color, width, capstyle, dashstyle, dashlength, dashspace; };
struct box { int x, y, width, height, color, line_width, capstyle, dashstyle, dashlength, dashspace, filltype, fillwidth, angle1, pitch1, angle2, pitch2; };
struct circle { int x, y, radius, color, line_width, capstyle, dashstyle, dashlength; };
struct text { int x, y, color, size, visibility, show_name_value, angle, alignment, num_lines;
    std::vector<std::string> lines;
};

struct composite_component;
using element_t = boost::variant<line, box, circle, text, boost::recursive_wrapper<composite_component>>;

struct element {
    // ...
    element_t element;
};

struct composite_component {
    int x;
    int y;
    std::string basename;
    std::vector<element> elements;
};

struct document { std::vector<element> elements; };

BOOST_FUSION_ADAPT_STRUCT(line, x1, y1, x2, y2, color, width, capstyle, dashstyle, dashlength, dashspace)
BOOST_FUSION_ADAPT_STRUCT(box, x, y, width, height, color, line_width, capstyle, dashstyle, dashlength, dashspace, filltype, fillwidth, angle1, pitch1, angle2, pitch2)
BOOST_FUSION_ADAPT_STRUCT(circle, x, y, radius, color, line_width, capstyle, dashstyle, dashlength)
BOOST_FUSION_ADAPT_STRUCT(text, x, y, color, size, visibility, show_name_value, angle, alignment, num_lines, lines)
BOOST_FUSION_ADAPT_STRUCT(composite_component, x, y, basename, elements)
BOOST_FUSION_ADAPT_STRUCT(element, element)
BOOST_FUSION_ADAPT_STRUCT(document, elements)

template <typename Iterator>
struct document_parser : qi::grammar<Iterator, document()> {
    document_parser() : document_parser::base_type{start_}
    {
        using namespace qi;

        line_              = 'L' >> auto_;
        box_               = 'B' >> auto_;
        circle_            = 'S' >> auto_;
        // text            = 'T' >> ...;
        element_           = (line_ | box_ | circle_ | composite_element_) >> eol;
        elements_          = -skip(space) [ '[' >> skip(blank) [*element_] >> ']' ];
        composite_element_ = 'C' >> int_ >> int_ >> lexeme[+graph] >> elements_;

        document_          = +element_ >> eoi;

        start_ = skip(blank) [ document_ ];
        BOOST_SPIRIT_DEBUG_NODES((document_)(element_)(composite_element_)(elements_)(line_)(box_)(circle_));
    }
  private:
    qi::rule<Iterator, document()> start_;
    qi::rule<Iterator, document(),             qi::blank_type> document_;
    qi::rule<Iterator, element(),              qi::blank_type> element_;
    qi::rule<Iterator, line(),                 qi::blank_type> line_;
    qi::rule<Iterator, box(),                  qi::blank_type> box_;
    qi::rule<Iterator, circle(),               qi::blank_type> circle_;
    qi::rule<Iterator, composite_component(),  qi::blank_type> composite_element_;
    qi::rule<Iterator, std::vector<element>(), qi::blank_type> elements_;
};

int main(int , char **) {
    document_parser<std::string::const_iterator> parser;

    const std::string text = // "v 20180904 2\n"
         "L 1 2 3 4 5 6 7 8 9 10\n"
         "C 10 10 FOO\n"
         "[ "
         "    L 10 20 30 40 50 60 70 80 90 100\n"
         "]\n";

    document doc;
    bool r = qi::parse(text.cbegin(), text.cend(), parser, doc);
    std::cout << (r ? "OK" : "FAIL") << std::endl;
}

打印

OK

并调试输出:

<document_>
  <try>L 1 2 3 4 5 6 7 8 9 </try>
  <element_>
    <try>L 1 2 3 4 5 6 7 8 9 </try>
    <line_>
      <try>L 1 2 3 4 5 6 7 8 9 </try>
      <success>\nC 10 10 FOO\n[     L</success>
      <attributes>[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]</attributes>
    </line_>
    <success>C 10 10 FOO\n[     L </success>
    <attributes>[[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]]</attributes>
  </element_>
  <element_>
    <try>C 10 10 FOO\n[     L </try>
    <line_>
      <try>C 10 10 FOO\n[     L </try>
      <fail/>
    </line_>
    <box_>
      <try>C 10 10 FOO\n[     L </try>
      <fail/>
    </box_>
    <circle_>
      <try>C 10 10 FOO\n[     L </try>
      <fail/>
    </circle_>
    <composite_element_>
      <try>C 10 10 FOO\n[     L </try>
      <elements_>
        <try>\n[     L 10 20 30 40</try>
        <element_>
          <try>     L 10 20 30 40 5</try>
          <line_>
            <try>     L 10 20 30 40 5</try>
            <success>\n]\n</success>
            <attributes>[[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]]</attributes>
          </line_>
          <success>]\n</success>
          <attributes>[[[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]]]</attributes>
        </element_>
        <element_>
          <try>]\n</try>
          <line_>
            <try>]\n</try>
            <fail/>
          </line_>
          <box_>
            <try>]\n</try>
            <fail/>
          </box_>
          <circle_>
            <try>]\n</try>
            <fail/>
          </circle_>
          <composite_element_>
            <try>]\n</try>
            <fail/>
          </composite_element_>
          <fail/>
        </element_>
        <success>\n</success>
        <attributes>[[[[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]]]]</attributes>
      </elements_>
      <success>\n</success>
      <attributes>[[10, 10, [F, O, O], [[[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]]]]]</attributes>
    </composite_element_>
    <success></success>
    <attributes>[[[10, 10, [F, O, O], [[[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]]]]]]</attributes>
  </element_>
  <element_>
    <try></try>
    <line_>
      <try></try>
      <fail/>
    </line_>
    <box_>
      <try></try>
      <fail/>
    </box_>
    <circle_>
      <try></try>
      <fail/>
    </circle_>
    <composite_element_>
      <try></try>
      <fail/>
    </composite_element_>
    <fail/>
  </element_>
  <success></success>
  <attributes>[[[[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]], [[10, 10, [F, O, O], [[[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]]]]]]]]</attributes>
</document_>