Вектор последовательности значений Ци Boost.Spirit - PullRequest
0 голосов
/ 15 октября 2018

Следующий код не компилируется с ошибкой:

/usr/include/boost/spirit/home/qi/detail/assign_to.hpp:153:20: error: no matching conversion for static_cast from 'const char' to 'boost::fusion::vector<char,
      std::vector<double, std::allocator<double> > >'
            attr = static_cast<Attribute>(val);
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~

Я не могу понять, почему, потому что он работает, как ожидается, с изменением на auto grammar = boost::spirit::no_skip[drawto_commands];.

Типы чего moveto и lineto синтаксические разборы одинаковы.

Оператор ци >> имеет правило типа a: A, b: vector<A> --> (a >> b): vector<A>, которое должно делать типы из того, что drawto_commands и moveto_drawto_command_group синтаксический разбор одинаковы.

Чтоя скучаю?

#include <string>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/boost_tuple.hpp>

typedef boost::fusion::vector<char, std::vector<double>> Arc;

template <typename P, typename T>
bool test_phrase_parser_attr(const std::string &string, P const& grammar, T& attr, bool full_match = true)
{
    using boost::spirit::qi::phrase_parse;
    using boost::spirit::qi::ascii::space;
    auto f = string.begin();
    auto l = string.end();
    bool match = phrase_parse(f, l, grammar, space, attr);
    return match && (!full_match || (f == l));
}

int main()
{
    using boost::spirit::omit;
    using boost::spirit::qi::ascii::char_;
    using boost::spirit::qi::ascii::space;
    using boost::spirit::qi::attr;
    using boost::spirit::qi::double_;
    using boost::spirit::qi::copy;

    auto wsp = copy(omit[boost::spirit::ascii::space]);
    auto comma_wsp = copy(omit[(char_(',') >> *wsp) | (+wsp >> -char_(',') >> *wsp)]);
    auto coordinate = copy(double_);
    auto coordinate_pair = copy(coordinate >> -comma_wsp >> coordinate);
    auto closepath = copy(char_("Zz") >> attr(std::vector<double>()));
    auto vertical_lineto = copy(char_("Vv") >> *wsp >> (coordinate % -comma_wsp));
    auto lineto = copy(char_("Ll") >> *wsp >> (coordinate_pair % -comma_wsp));
    auto moveto = copy(char_("Mm") >> *wsp >> (coordinate_pair % -comma_wsp));
    auto drawto_command = copy(closepath | vertical_lineto | lineto);
    auto drawto_commands = copy(*(*wsp >> drawto_command >> *wsp));
    auto moveto_drawto_command_group = copy(moveto >> drawto_commands);

    auto grammar = boost::spirit::no_skip[moveto_drawto_command_group];
    std::vector<Arc> attribute;
    std::string str;
    std::cout << "*\n";
    while (getline(std::cin, str))
    {
        if (str.empty())
            break;
        attribute = {};
        bool r = test_phrase_parser_attr(str, grammar, attribute, true);
        if (r)
        {
            std::cout << "Parsing succeeded, got: " << std::endl;
            for (auto &command: attribute){
                char line_type = boost::fusion::at_c<0>(command);
                std::cout << line_type;
                const std::vector<double> arguments = boost::fusion::at_c<1>(command);
                for (size_t i = 0; i < arguments.size(); ++i)
                {
                    std::cout << ' ' << arguments[i];
                }
                std::cout << std::endl;
            }
        }
        else
        {
            std::cout << "Parsing failed\n";
        }
    }
}
`

1 Ответ

0 голосов
/ 16 октября 2018

Ладно, как это обычно бывает, я смотрел на это и на спецификации SVG и просто почувствовал, что стоит поделиться некоторыми идеями

  1. стиля
  2. конвенции Ци
  3. продвинутые идеи

, которые могут вас заинтересовать. Справедливое предупреждение: я не пытался ответить на ваш вопрос в поставленном виде.

Будьте конкретны с вашими типами

Вы, кажется, "всегда автоматический" в уже основанной на эвристике структуре синтаксического анализатора.Я не удивлен, что иногда вещи "не волшебны правильно".Предполагая, что вы хотите продолжать использовать Ци, давайте разберем анализатор Ци:

Live On Coliru

#define BOOST_SPIRIT_DEBUG
#include <string>
#include <iostream>
#include <boost/spirit/home/qi.hpp>
#include <boost/fusion/include/adapt_struct.hpp>

namespace AST {
    using Coordinates = std::vector<double>;

    struct Arc {
        char command;
        Coordinates coordinates;
    };

    using PathData = std::vector<Arc>;
}

BOOST_FUSION_ADAPT_STRUCT(AST::Arc, command, coordinates)

namespace qi = boost::spirit::qi;
namespace Parsers {

    template <typename It>
    struct PathData : qi::grammar<It, AST::PathData()> {
        PathData() : PathData::base_type(start) {
            using namespace qi;

            opt_comma       = -lit(',');
            coordinate      = double_;
            coordinate_pair = coordinate >> opt_comma >> coordinate;

            moveto          = char_("Mm") >> (coordinate_pair % opt_comma);
            closepath       = char_("Zz") >> attr(AST::Coordinates{});
            vertical_lineto = char_("Vv") >> (coordinate % opt_comma);
            lineto          = char_("Ll") >> (coordinate_pair % opt_comma);
            drawto_command  = closepath | vertical_lineto | lineto;

            drawto_commands = *drawto_command;
            start           = skip(space) [ moveto >> drawto_commands ];


            BOOST_SPIRIT_DEBUG_NODES((opt_comma)(coordinate)(coordinate_pair)
                    (moveto)(closepath)(vertical_lineto)(lineto)(drawto_command)
                    (drawto_commands))
        }
      private:
        using Skipper = qi::space_type;
        qi::rule<It> opt_comma;
        qi::rule<It, double()> coordinate;
        qi::rule<It, AST::Coordinates(), Skipper> coordinate_pair;

        qi::rule<It, AST::Arc(), Skipper> moveto, closepath, vertical_lineto, lineto, drawto_command;
        qi::rule<It, AST::PathData(), Skipper> drawto_commands;

        qi::rule<It, AST::PathData()> start;
    };
}

template <typename P, typename T>
bool test_parse_attr(const std::string &text, P const& grammar, T& attr, bool full_match = true) {
    return parse(text.cbegin(), text.cend(),
            grammar >> (qi::eps(!full_match) | qi::eoi),
            attr);
}

int main() {
    const Parsers::PathData<std::string::const_iterator> grammar;

    for (std::string const str : { "M 100 100 L 300 100 L 200 300 z" }) {
        AST::PathData attribute;
        if (test_parse_attr(str, grammar, attribute, true)) {
            std::cout << "Parsing succeeded, got: " << std::endl;

            for (auto &command: attribute) {
                std::cout << command.command;
                for (auto const& arg : command.coordinates) {
                    std::cout << ' ' << arg;
                }
                std::cout << std::endl;
            }
        } else {
            std::cout << "Parsing failed\n";
        }
    }
}

Отпечатки

Parsing succeeded, got: 
M 100 100
L 300 100
L 200 300
z

Примечания:

  • Шкипер является обязанностью синтаксического анализатора, а не абонента
  • не вмешивайтесь в fusion::vector(или даже tuple), поэтому сохраняйте свой код обслуживаемым:

    namespace AST {
        using Coordinates = std::vector<double>;
    
        struct Arc {
            char command;
            Coordinates coordinates;
        };
    
        using PathData = std::vector<Arc>;
    }
    

    и позже:

     for (auto &command: attribute) {
          std::cout << command.command;
          for (auto const& arg : command.coordinates) { std::cout << ' ' << arg; }
          std::cout << std::endl;
      }
    
  • Откладывает все необязательные пробелы, соответствующие Skipper.Я знаю, что это меняет поведение (мы будем анализировать «L100,200», в то время как «L100,200» потребуется).Если вы настаиваете на диагностике этого случая, укажите это:

        command_letter  = no_case [ char_(_r1) ] >> &(space|eoi);
        moveto          = command_letter('m') >> (coordinate_pair % opt_comma);
        closepath       = command_letter('z') >> attr(AST::Coordinates{});
        vertical_lineto = command_letter('v') >> (coordinate % opt_comma);
        lineto          = command_letter('l') >> (coordinate_pair % opt_comma);
    

    Где command_letter - это правило, которое принимает унаследованный атрибут:

    qi::rule<It, char(char)> command_letter;
    

BeКонкретный с большим количеством типов

Может быть, вы хотите быть конкретным и о ваших типах AST.В зависимости от логики вашего домена, вам не следует рассматривать все аргументы как просто вектор, вероятно.

namespace AST {
    using Coordinate = double;
    using Coordinates = std::vector<Coordinate>;

    struct Point { Coordinate x, y; };
    using Points = std::vector<Point>;

    namespace Cmds {
        struct MoveTo         { Points points; } ;
        struct ClosePath      {                } ;
        struct VerticalLineTo { Coordinates x; } ;
        struct LineTo         { Points points; } ;
    }

    using Cmd = boost::variant<
            Cmds::MoveTo,
            Cmds::ClosePath,
            Cmds::VerticalLineTo,
            Cmds::LineTo
        >;

    using PathData = std::vector<Cmd>;
}

Адаптировать их все:

BOOST_FUSION_ADAPT_STRUCT(AST::Point, x, y)
BOOST_FUSION_ADAPT_STRUCT(AST::Cmds::MoveTo, points)
BOOST_FUSION_ADAPT_STRUCT(AST::Cmds::LineTo, points)

Вы можете рассмотреть NabialekХитрость , чтобы разобрать их.Смотрите здесь пример: Анализ языка команд с помощью Boost Spirit

Дополнительные расширенные идеи

Возможно, использование X3 более эмулирует исходную организацию кода:

Live On Coliru

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

namespace AST {
    using Coordinates = std::vector<double>;

    struct Arc {
        char command;
        Coordinates coordinates;
    };

    using PathData = std::vector<Arc>;
}

BOOST_FUSION_ADAPT_STRUCT(AST::Arc, command, coordinates)

namespace x3 = boost::spirit::x3;
namespace Parsers {
    using namespace x3;

    auto const opt_comma       = -lit(',');
    auto const coordinate      = double_;
    auto const coordinate_pair = coordinate >> opt_comma >> coordinate;

    template <typename T> auto as = [](auto p) { return rule<struct _, T>{} = p; };

    auto const command_letter  = [](auto p) { return lexeme [ no_case [ char_(p) ] >> &(space|eoi) ]; };
    auto const moveto          = command_letter('m') >> as<AST::Coordinates>(coordinate_pair % opt_comma);
    auto const lineto          = command_letter('l') >> as<AST::Coordinates>(coordinate_pair % opt_comma);
    auto const vertical_lineto = command_letter('v') >> as<AST::Coordinates>(coordinate % opt_comma);
    auto const closepath       = command_letter('z') >> attr(AST::Coordinates{});
    auto const drawto_command  = as<AST::Arc>(closepath | vertical_lineto | lineto);

    auto const drawto_commands = as<AST::PathData>(*drawto_command);
    auto const path_data       = as<AST::PathData>(skip(space) [ moveto >> drawto_commands ]);
}

template <typename P, typename T>
bool test_parse_attr(const std::string &text, P const& grammar, T& attr, bool full_match = true) {
    return parse(
            text.cbegin(), text.cend(),
            grammar >> (x3::eps(!full_match) | x3::eoi),
            attr
        );
}

int main() {
    for (std::string const str : { "M 100 100 L 300 100 L 200 300 z" }) {
        AST::PathData attribute;
        if (test_parse_attr(str, Parsers::path_data, attribute, true)) {
            std::cout << "Parsing succeeded, got: " << std::endl;

            for (auto &command: attribute) {
                std::cout << command.command;
                for (auto const& arg : command.coordinates) {
                    std::cout << ' ' << arg;
                }
                std::cout << std::endl;
            }
        } else {
            std::cout << "Parsing failed\n";
        }
    }
}

Также печатается:

Parsing succeeded, got: 
M 100 100
L 300 100
L 200 300
z
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...