-
Notifications
You must be signed in to change notification settings - Fork 150
/
LSystemParser.cpp
184 lines (156 loc) · 5.96 KB
/
LSystemParser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "lsys/LSystemParser.hpp"
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/bind.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
using namespace utymap::lsys;
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phx = boost::phoenix;
typedef std::map<LSystem::RuleType, LSystem::Productions, RuleComparator> ProductionMap;
BOOST_FUSION_ADAPT_STRUCT(
LSystem,
(int, generations)
(double, angle)
(double, scale)
(LSystem::Rules, axiom)
(ProductionMap, productions)
)
namespace {
const std::shared_ptr<MoveForwardRule> forward = std::make_shared<MoveForwardRule>();
const std::shared_ptr<JumpForwardRule> jump = std::make_shared<JumpForwardRule>();
const std::shared_ptr<TurnLeftRule> turnLeft = std::make_shared<TurnLeftRule>();
const std::shared_ptr<TurnRightRule> turnRight = std::make_shared<TurnRightRule>();
const std::shared_ptr<TurnAroundRule> turnAround = std::make_shared<TurnAroundRule>();
const std::shared_ptr<PitchUpRule> pitchUp = std::make_shared<PitchUpRule>();
const std::shared_ptr<PitchDownRule> pitchDown = std::make_shared<PitchDownRule>();
const std::shared_ptr<RollLeftRule> rollLeft = std::make_shared<RollLeftRule>();
const std::shared_ptr<RollRightRule> rollRight = std::make_shared<RollRightRule>();
const std::shared_ptr<IncrementRule> increment = std::make_shared<IncrementRule>();
const std::shared_ptr<DecrementRule> decrement = std::make_shared<DecrementRule>();
const std::shared_ptr<ScaleUpRule> scaleUp = std::make_shared<ScaleUpRule>();
const std::shared_ptr<ScaleDownRule> scaleDown = std::make_shared<ScaleDownRule>();
const std::shared_ptr<SwitchStyleRule> switchStyle = std::make_shared<SwitchStyleRule>();
const std::shared_ptr<SaveRule> save = std::make_shared<SaveRule>();
const std::shared_ptr<RestoreRule> restore = std::make_shared<RestoreRule>();
struct RuleTable : qi::symbols<char, LSystem::RuleType> {
RuleTable() {
add
("F", forward)
("G", jump)
("+", turnLeft)
("-", turnRight)
("|", turnAround)
("^", pitchUp)
("&", pitchDown)
("\\", rollLeft)
("/", rollRight)
("$", increment)
("!", decrement)
(">", scaleUp)
("<", scaleDown)
("@", switchStyle)
("[", save)
("]", restore);
}
};
struct WordRuleFactory {
template<typename Arg>
struct result { typedef LSystem::RuleType type; };
template<typename Arg>
LSystem::RuleType operator()(const Arg &c) const {
return std::make_shared<WordRule>(std::string(c.begin(), c.end()));
}
};
struct ProductionInserter {
template<typename Arg1, typename Arg2, typename Arg3, typename Arg4>
struct result { typedef void type; };
template<typename Arg1, typename Arg2, typename Arg3, typename Arg4>
void operator()(Arg1 &dict, Arg2 &predcessor, Arg3 &probability, Arg4 &successor) const {
dict[predcessor].push_back(std::make_pair(probability, successor));
}
};
template<typename Iterator>
struct CommentSkipper : qi::grammar<Iterator> {
CommentSkipper() : CommentSkipper::base_type(start, "comment") {
start =
' ' | '#' >> *(qi::char_ - '\n') >> '\n';
start.name("comment");
}
qi::rule<Iterator> start;
};
template<typename Iterator>
struct RuleGrammar : qi::grammar<Iterator, LSystem::RuleType(), CommentSkipper<Iterator>> {
RuleGrammar() : RuleGrammar::base_type(start, "rule") {
word =
qi::lexeme[+ascii::alpha][qi::_val = wordRuleFactory(qi::_1)];
start = ruleTable | word;
word.name("word");
start.name("rule");
}
RuleTable ruleTable;
boost::phoenix::function<WordRuleFactory> wordRuleFactory;
qi::rule<Iterator, LSystem::RuleType(), CommentSkipper<Iterator>> start;
qi::rule<Iterator, LSystem::RuleType(), CommentSkipper<Iterator>> word;
};
template<typename Iterator>
struct ProductionGrammar : qi::grammar<Iterator, ProductionMap(), CommentSkipper<Iterator>> {
ProductionGrammar() : ProductionGrammar::base_type(start, "production") {
probability =
('(' > qi::double_ > ')') | qi::attr(1);
start =
(rule > probability > "->" >
+rule)[inserter(qi::_val, qi::_1, qi::_2, qi::_3)]%'\n';
}
boost::phoenix::function<ProductionInserter> inserter;
RuleGrammar<Iterator> rule;
qi::rule<Iterator, double(), CommentSkipper<Iterator>> probability;
qi::rule<Iterator, ProductionMap(), CommentSkipper<Iterator>> start;
};
template<typename Iterator>
struct LSystemGrammar : qi::grammar<Iterator, LSystem(), CommentSkipper<Iterator>> {
LSystemGrammar() : LSystemGrammar::base_type(start, "lsystem") {
start =
"generations:" > qi::int_ >> +ascii::space >
"angle:" > qi::double_ >> +ascii::space >
"scale:" > qi::double_ >> +ascii::space >
"axiom:" > +rule > '\n' >
production;
start.name("lsystem");
rule.name("rule");
production.name("production");
qi::on_error<qi::fail>
(
start,
error
<< phx::val("Error! Expecting ")
<< qi::_4
<< phx::val(" here: \"")
<< phx::construct<std::string>(qi::_3, qi::_2)
<< phx::val("\"")
<< std::endl
);
}
std::stringstream error;
RuleGrammar<Iterator> rule;
ProductionGrammar<Iterator> production;
qi::rule<Iterator, LSystem(), CommentSkipper<Iterator>> start;
};
template<typename Iterator>
void parse(Iterator begin, Iterator end, LSystem &lsystem) {
LSystemGrammar<Iterator> grammar;
CommentSkipper<Iterator> skipper;
if (!phrase_parse(begin, end, grammar, skipper, lsystem))
throw std::domain_error(std::string("Cannot parse lsystem:") + grammar.error.str());
}
}
LSystem LSystemParser::parse(const std::string &str) const {
LSystem lsystem;
::parse(str.begin(), str.end(), lsystem);
return lsystem;
}
LSystem LSystemParser::parse(std::istream &istream) const {
std::string content((std::istreambuf_iterator<char>(istream)), std::istreambuf_iterator<char>());
return parse(content);
}