From 1513ea3ac714b9d0f23aa12563d6643359af81b7 Mon Sep 17 00:00:00 2001 From: Sverre Rabbelier Date: Mon, 14 Apr 2008 12:07:04 +0200 Subject: [PATCH] Only allow this language: ((n|s)!(w|e)!(u|d)!;)+ for directions. Example: good: nwu bad: wus bad: wws bad: nsu --- src/Core/Editors/DirectionParser.cpp | 64 ++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/src/Core/Editors/DirectionParser.cpp b/src/Core/Editors/DirectionParser.cpp index d14eea5..eb5f162 100644 --- a/src/Core/Editors/DirectionParser.cpp +++ b/src/Core/Editors/DirectionParser.cpp @@ -26,27 +26,21 @@ using namespace boost::spirit; -struct dirs : symbols -{ - dirs() - { - add - ("n", Coordinate(1, 0, 0)) - ("s", Coordinate(-1, 0, 0)) - ("e", Coordinate(0, 1, 0)) - ("w", Coordinate(0, -1, 0)) - ("u", Coordinate(0, 0, 1)) - ("d", Coordinate(0, 0, -1)) - ; - } - -} dirs_p; - struct add_dir { - add_dir(Coordinate& coordinate) : m_coordinate(coordinate) {} - void operator()(Coordinate& rhs) const { m_coordinate += rhs; } + add_dir(Coordinate& coordinate, Coordinate rhs) : + m_coordinate(coordinate), + m_rhs(rhs) + { + + } + + void operator()(const char&) const + { + m_coordinate += m_rhs; + } Coordinate& m_coordinate; + Coordinate m_rhs; }; struct add_and_reset @@ -55,12 +49,13 @@ struct add_and_reset m_coordinates(coordinates), m_coordinate(coordinate) { - + } void operator()(char const*, char const*) const { - m_coordinates.push_back(m_coordinate); - m_coordinate = m_coordinate - m_coordinate; + Coordinate copy = m_coordinate; + m_coordinates.push_back(copy); + m_coordinate = m_coordinate - copy; } std::vector& m_coordinates; @@ -74,18 +69,29 @@ struct direction : public grammar struct definition { definition(direction const& self) - { + { // Create the rule that will match a directional component - first = list_p( - // Allow arbitrary many direction components - (dirs_p[ add_dir(self.m_coordinate) ]) >> - !(dirs_p[ add_dir(self.m_coordinate) ]) >> - !(dirs_p[ add_dir(self.m_coordinate) ]) >> - eps_p[add_and_reset(self.m_coordinates, self.m_coordinate)], + !( + ch_p('n')[add_dir(self.m_coordinate, Coordinate(1, 0, 0))] | + ch_p('s')[add_dir(self.m_coordinate, Coordinate(-1, 0, 0))] + ) >> + + !( + ch_p('w')[add_dir(self.m_coordinate, Coordinate(0, 1, 0))] | + ch_p('e')[add_dir(self.m_coordinate, Coordinate(0, -1, 0))] + ) >> + + !( + ch_p('u')[add_dir(self.m_coordinate, Coordinate(0, 0, 1 ))] | + ch_p('d')[add_dir(self.m_coordinate, Coordinate(0, 0, -1))] + ) >> + + eps_p[add_and_reset(self.m_coordinates, self.m_coordinate)], + ch_p(';') - ); + ); } rule first; -- 2.11.4.GIT