1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
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. *
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. *
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
;
31 add_dir(Coordinate
& coordinate
, Coordinate rhs
) :
32 m_coordinate(coordinate
),
38 void operator()(const char&) const
40 m_coordinate
+= m_rhs
;
42 Coordinate
& m_coordinate
;
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
>
71 definition(direction
const& self
)
73 // Create the rule that will match a directional component
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))]
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))]
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))]
91 eps_p
[add_and_reset(self
.m_coordinates
, self
.m_coordinate
)],
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
)
125 DirectionParser::~DirectionParser()