Only allow this language: ((n|s)!(w|e)!(u|d)!;)+ for directions.
[UnsignedByte.git] / src / Core / Editors / DirectionParser.cpp
blobeb5f16209d98c6aeee27dd46c36180f025aaa3bd
1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "DirectionParser.h"
23 #include <boost/spirit/core.hpp>
24 #include <boost/spirit/symbols/symbols.hpp>
25 #include <boost/spirit/utility/lists.hpp>
27 using namespace boost::spirit;
29 struct add_dir
31 add_dir(Coordinate& coordinate, Coordinate rhs) :
32 m_coordinate(coordinate),
33 m_rhs(rhs)
38 void operator()(const char&) const
40 m_coordinate += m_rhs;
42 Coordinate& m_coordinate;
43 Coordinate m_rhs;
46 struct add_and_reset
48 add_and_reset(std::vector<Coordinate>& coordinates, Coordinate& coordinate) :
49 m_coordinates(coordinates),
50 m_coordinate(coordinate)
55 void operator()(char const*, char const*) const {
56 Coordinate copy = m_coordinate;
57 m_coordinates.push_back(copy);
58 m_coordinate = m_coordinate - copy;
61 std::vector<Coordinate>& m_coordinates;
62 Coordinate& m_coordinate;
66 struct direction : public grammar<direction>
68 template <typename ScannerT>
69 struct definition
71 definition(direction const& self)
73 // Create the rule that will match a directional component
74 first =
75 list_p(
77 ch_p('n')[add_dir(self.m_coordinate, Coordinate(1, 0, 0))] |
78 ch_p('s')[add_dir(self.m_coordinate, Coordinate(-1, 0, 0))]
79 ) >>
82 ch_p('w')[add_dir(self.m_coordinate, Coordinate(0, 1, 0))] |
83 ch_p('e')[add_dir(self.m_coordinate, Coordinate(0, -1, 0))]
84 ) >>
87 ch_p('u')[add_dir(self.m_coordinate, Coordinate(0, 0, 1 ))] |
88 ch_p('d')[add_dir(self.m_coordinate, Coordinate(0, 0, -1))]
89 ) >>
91 eps_p[add_and_reset(self.m_coordinates, self.m_coordinate)],
93 ch_p(';')
97 rule<ScannerT> first;
98 rule<ScannerT> const& start() const { return first; }
101 direction(std::vector<Coordinate>& coordinates, Coordinate& coordinate) :
102 m_coordinate(coordinate),
103 m_coordinates(coordinates)
107 Coordinate& m_coordinate;
108 std::vector<Coordinate>& m_coordinates;
111 void DirectionParser::parseDirections()
113 Coordinate coordinate(0,0,0);
114 direction direction_p(m_result, coordinate);
116 parse(m_directions.c_str(), direction_p).full;
119 DirectionParser::DirectionParser(cstring directions) :
120 m_directions(directions)
122 parseDirections();
125 DirectionParser::~DirectionParser()