AJM*_*AJM 2 parsing boost boost-spirit boost-phoenix boost-spirit-qi
这很长,有很多代码,所以我希望Stack Overflow能够应对它.:P
我正在尝试用Boost Spirit编写一个SVG解析器.我有一个语法,用"Contours"填充一个矢量,它是"BezierPoints"的矢量,可以用bezier控件表示常规点或点.
到目前为止我有这个(还没有处理相对绘图命令):
#ifndef SVG_PARSER_HPP
#define SVG_PARSER_HPP
#include <vector>
#include "boost/spirit/include/qi.hpp"
#include "boost/spirit/include/phoenix.hpp"
#include "boost/fusion/include/adapt_struct.hpp"
#include "boost/fusion/include/std_pair.hpp"
namespace qi = boost::spirit::qi;
namespace phoenix = boost::phoenix;
namespace ascii = boost::spirit::ascii;
struct Point
{
Point(const double nx = 0.0, const double ny = 0.0) : x(nx), y(ny)
{}
double x;
double y;
};
BOOST_FUSION_ADAPT_STRUCT(
Point,
(double, x)
(double, y)
)
struct BezierPoint
{
BezierPoint(const double x = 0.0, const double y = 0.0) :
point(x, y), control1(0.0, 0.0), control2(0.0, 0.0) {}
BezierPoint(const Point &p) : point(p), control1(0.0, 0.0),
control2(0.0, 0.0) {}
Point point; // End point. Start point is in the BezierPoint that
// came before it.
// Todo: Set these to be coincident with point for non-curve points.
Point control1;
Point control2;
};
BOOST_FUSION_ADAPT_STRUCT(
BezierPoint,
(Point, control1)
(Point, control2)
(Point, point)
)
typedef std::vector<BezierPoint> BezierVec;
typedef std::vector<BezierVec> Contours;
template <typename Iterator>
struct PathGrammar : qi::grammar<Iterator, Contours()>
{
///////////////////////////
// SVG is a damn monster //
///////////////////////////
PathGrammar() : PathGrammar::base_type(path_data)
{
using qi::char_;
using qi::double_;
using qi::_val;
using qi::_1;
using phoenix::push_back;
using phoenix::insert;
using phoenix::begin;
using phoenix::end;
using phoenix::construct;
using phoenix::val;
using ascii::space;
path_data = *space >> -moveto_drawto_command_groups
>> *space;
moveto_drawto_command_groups = moveto_drawto_command_group
% *space;
moveto_drawto_command_group = moveto[
insert(_val, end(_val), begin(_1), end(_1))
] >> *space
>> -drawto_commands[
insert(_val, end(_val), begin(_1), end(_1))
];
// Draw commands are (optionally) followed by a closepath
// command.
drawto_commands = (drawto_command[
insert(_val, end(_val), begin(_1), end(_1))
] % *space) >> *space >> -closepath;
drawto_command = lineto | horizontal_lineto
| vertical_lineto | curveto | smooth_curveto;
moveto = ( char_('M') | char_('m') ) >> *space
>> lineto_argument_sequence;
closepath = (char_('Z') | char_('z'));
lineto = ( char_('L') | char_('l') ) >> *space
>> lineto_argument_sequence;
lineto_argument_sequence = coordinate_pair[
push_back(_val, construct<BezierPoint>(_1))
] % -comma_space;
horizontal_lineto = ( char_('H') | char_('h') ) >> *space
>> horizontal_lineto_argument_sequence;
horizontal_lineto_argument_sequence = coordinate[
push_back(_val, construct<BezierPoint>(_1, val(0.0)))
] % -comma_space;
vertical_lineto = ( char_('V') | char_('v') ) >> *space
>> vertical_lineto_argument_sequence;
vertical_lineto_argument_sequence = coordinate[
push_back(_val, construct<BezierPoint>(val(0.0), _1))
] % -comma_space;
curveto = ( char_('C') | char_('c') ) >> *space
>> curveto_argument_sequence;
curveto_argument_sequence = curveto_argument % -comma_space;
curveto_argument = coordinate_pair >> -comma_space
>> coordinate_pair >> -comma_space >> coordinate_pair;
smooth_curveto = ( char_('S') | char_('s') ) >> *space
>> smooth_curveto_argument_sequence;
smooth_curveto_argument_sequence = smooth_curveto_argument
% -comma_space;
smooth_curveto_argument = coordinate_pair >> -comma_space
>> coordinate_pair;
coordinate_pair = (double_ >> -comma_space >> double_);
coordinate = double_;
comma_space = (+space >> -char_(',') >> *space)
| (char_(',') >> *space);
}
// Quadratic curves are not supported
qi::rule<Iterator, Contours()> path_data;
qi::rule<Iterator, Contours()> moveto_drawto_command_groups;
qi::rule<Iterator, BezierVec()> moveto_drawto_command_group;
qi::rule<Iterator, BezierVec()> drawto_commands;
qi::rule<Iterator, BezierVec()> drawto_command;
qi::rule<Iterator, BezierVec()> moveto;
qi::rule<Iterator, BezierVec()> moveto_argument_sequence;
qi::rule<Iterator> closepath;
qi::rule<Iterator, BezierVec()> lineto;
qi::rule<Iterator, BezierVec()> lineto_argument_sequence;
qi::rule<Iterator, BezierVec()> horizontal_lineto;
qi::rule<Iterator, BezierVec()>
horizontal_lineto_argument_sequence;
qi::rule<Iterator, BezierVec()> vertical_lineto;
qi::rule<Iterator, BezierVec()> vertical_lineto_argument_sequence;
qi::rule<Iterator, BezierVec()> curveto;
qi::rule<Iterator, BezierVec()> curveto_argument_sequence;
qi::rule<Iterator, BezierPoint()> curveto_argument;
qi::rule<Iterator, BezierVec()> smooth_curveto;
qi::rule<Iterator, BezierVec()> smooth_curveto_argument_sequence;
qi::rule<Iterator, BezierPoint()> smooth_curveto_argument;
qi::rule<Iterator, Point()> coordinate_pair;
qi::rule<Iterator, double()> coordinate;
qi::rule<Iterator> comma_space;
};
#endif
Run Code Online (Sandbox Code Playgroud)
语法被调用如下:
typedef string::const_iterator StrItr;
PathGrammar<StrItr> grammar;
Contours paths;
StrItr startIt = pathData.begin();
StrItr endIt = pathData.end();
qi::parse(startIt, endIt, grammar, paths);
BOOST_FOREACH(BezierVec v, paths)
{
cout << "Path:" << endl;
BOOST_FOREACH(BezierPoint p, v)
{
cout << '\t' << p.point.x << ", " << p.point.y << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我目前的测试字符串:
M26.591,0L0,22.348l25.46,23.479L12.306,100l36.067-23.619L85.008,28.43L26.591,0z M30.553,34.23
l-8.487-10.467l9.052-5.234l25.601,8.77l-3.109,12.729L30.553,34.23z
Run Code Online (Sandbox Code Playgroud)
重新格式化的字符串使其更易于阅读:
M 26.591, 0
L 0 , 22.348
l 25.46 , 23.479
L 12.306, 100
l 36.067, -23.619
L 85.008, 28.43
L 26.591, 0
z
M 30.553, 34.23
l -8.487, -10.467
l 9.052, -5.234
l 25.601, 8.77
l -3.109, 12.729
L 30.553, 34.23
z
Run Code Online (Sandbox Code Playgroud)
这是输出:
Path:
77, 0
26.591, 0
76, 0
0, 22.348
108, 0
25.46, 23.479
76, 0
12.306, 100
108, 0
36.067, -23.619
76, 0
85.008, 28.43
76, 0
26.591, 0
Path:
77, 0
30.553, 34.23
108, 0
-8.487, -10.467
108, 0
9.052, -5.234
108, 0
25.601, 8.77
108, 0
-3.109, 12.729
76, 0
30.553, 34.23
Run Code Online (Sandbox Code Playgroud)
语法是看到了积分,但它不断加入所有这些额外点,我不知道它们来自哪里.
我也想知道我的几条规则.首先是这个规则:
qi::rule<Iterator, BezierVec()> drawto_commands;
qi::rule<Iterator, BezierVec()> drawto_command;
...
drawto_commands = (drawto_command[
insert(_val, end(_val), begin(_1), end(_1))
] % *space) >> *space >> -closepath;
Run Code Online (Sandbox Code Playgroud)
我希望将结果(drawto_command % *space)作为单个向量而不是向量向量.据我所知,我必须与凤凰手动完成此操作.是这样的吗?
我对moveto规则有类似的看法:
qi::rule<Iterator, BezierVec()> moveto_drawto_command_group;
qi::rule<Iterator, BezierVec()> moveto;
qi::rule<Iterator, BezierVec()> moveto_argument_sequence;
...
moveto_drawto_command_group = moveto[
insert(_val, end(_val), begin(_1), end(_1))
] >> *space
>> -drawto_commands[
insert(_val, end(_val), begin(_1), end(_1))
];
Run Code Online (Sandbox Code Playgroud)
我有两条规则给出了BezierVec,我想将它组合成一个BezierVec用于第三条规则.到目前为止,唯一的方法是使用Phoenix手动插入.有没有更简单的方法?
在输出中的附加值是从ASCII字符产生'M' == 77,'L' == 76,'l' == 108等,这发生,因为你匹配使用那些char_('M'),等等,这暴露了匹配的值作为char属性.编译器很乐意将其分配给double输出数组中的值.为了避免这种情况,请使用lit('M')或者只使用'M'.这些都没有暴露任何属性,使得表达式在生成的值方面是中性的.
可以改进的第二件事是*space从所有地方删除构造并切换到短语解析(请参阅此处phrase_parse API函数的文档).如果您提供解析器组件作为skipper参数并向所有规则添加一个skipper模板类型参数,那么您将获得与在语法中散布构造的效果相同的效果.例如:spacespace_type*space
qi::rule<Iterator, Contours(), space_type> path_data;
Run Code Online (Sandbox Code Playgroud)
如果输入的某些部分不允许包含空格,那么这些部分仍然可以嵌入到lexeme[]指令中.有关更多信息,请参见此处
你的PS:
为了组合从drawto_command你返回的所有向量可以使用一个技巧强制Spirit.Qi附加到提供的(左侧属性)向量:
drawto_commands = +drawto_command >> -closepath;
Run Code Online (Sandbox Code Playgroud)
这已经假设您已切换到短语解析,因此我删除了*space构造.为什么这样做?好吧,Spirit.Qi为序列提供了一个特殊的属性传播规则,如果此序列的所有元素都公开此属性类型或这些属性类型的容器,则强制它将其元素公开的所有属性附加到提供的容器中.这里不需要语义操作.注意,这仅适用于序列,而不适用于单个元素右侧构造.
您的第二个相关问题可以通过类似的方式解决:
moveto_drawto_command_group = moveto >> -drawto_commands;
Run Code Online (Sandbox Code Playgroud)
再次,不需要语义动作.