12 #ifndef PARSING_INTERNALS_H
13 #define PARSING_INTERNALS_H
19 #ifndef BOOST_SPIRIT_USE_PHOENIX_V3
20 #define BOOST_SPIRIT_USE_PHOENIX_V3
23 #include <boost/spirit/include/qi.hpp>
24 #include <boost/variant/recursive_variant.hpp>
25 #include <boost/variant/apply_visitor.hpp>
26 #include <boost/fusion/include/adapt_struct.hpp>
27 #include <boost/phoenix/function.hpp>
45 typedef boost::variant<
51 , boost::recursive_wrapper<unaryexpr_>
52 , boost::recursive_wrapper<expression>
78 BOOST_FUSION_ADAPT_STRUCT(
80 (std::string, operator_)
84 BOOST_FUSION_ADAPT_STRUCT(
86 (std::string, operator_)
90 BOOST_FUSION_ADAPT_STRUCT(
93 (std::list<ExpressionParsing::ast::operation>, rest)
149 VirtualMachine(
unsigned stackSize = 4096)
150 : m_stackSize(stackSize)
155 unsigned m_stackSize;
162 typedef void result_type;
164 std::vector<StackElement>&
code;
165 Compiler(std::vector<StackElement>&
code, IProxyLoader *proxyLoader,
166 IUnitInterpreter *unitInterpreter)
167 :
code(
code), m_proxyLoader(proxyLoader), m_unitInterpreter(unitInterpreter) { }
169 void operator()(ast::nil) { BOOST_ASSERT(0); }
170 void operator()(
unsigned int n);
171 void operator()(
bool n);
172 void operator()(
double n);
173 void operator()(
const std::string &
n);
175 void operator()(ast::operation
const&
x);
176 void operator()(ast::unaryexpr_
const&
x);
180 IProxyLoader *m_proxyLoader;
181 IUnitInterpreter *m_unitInterpreter;
184 namespace qi = boost::spirit::qi;
187 struct error_handler_
192 template <
typename Iterator>
195 , Iterator err_pos, Iterator last)
const
198 <<
"Error! Expecting " <<
what
199 <<
" here: \"" << std::string(err_pos, last) <<
"\""
204 boost::phoenix::function<error_handler_>
const error_handler = error_handler_();
206 template <
typename Iterator>
207 struct Grammar :
qi::grammar<Iterator, ast::expression(), ascii::space_type>
231 logical_expr = equality_expr >> *(
232 (qi::string(
"&&") > equality_expr)
233 | (qi::string(
"||") > equality_expr)
236 equality_expr = relational_expr >> *(
237 (qi::string(
"==") > relational_expr)
238 | (qi::string(
"!=") > relational_expr)
241 relational_expr = additive_expr >> *(
242 (qi::string(
">=") > additive_expr)
243 | (qi::string(
"<=") > additive_expr)
244 | (qi::string(
">") > additive_expr)
245 | (qi::string(
"<") > additive_expr)
248 additive_expr = multiplicative_expr >> *(
249 (char_(
'+') > multiplicative_expr)
250 | (char_(
'-') > multiplicative_expr)
253 multiplicative_expr = power_expr >> *(
254 (char_(
'*') > power_expr)
255 | (char_(
'/') > power_expr)
258 power_expr = unary_expr >> *(
259 (qi::string(
"**") > unary_expr)
262 unary_expr = unaryfunc_expr
263 | (qi::string(
"-") > unaryfunc_expr)
264 | (qi::string(
"+") > unaryfunc_expr)
265 | (qi::string(
"!") > unaryfunc_expr)
270 ((qi::string(
"sum") >> &char_(
'(')) > primary_expr)
271 | ((qi::string(
"count") >> &char_(
'(')) > primary_expr)
272 | ((qi::string(
"abs") >> &char_(
'(')) > primary_expr)
273 | ((qi::string(
"sqrt") >> &char_(
'(')) > primary_expr)
274 | ((qi::string(
"cbrt") >> &char_(
'(')) > primary_expr)
275 | ((qi::string(
"asinh") >> &char_(
'(')) > primary_expr)
276 | ((qi::string(
"acosh") >> &char_(
'(')) > primary_expr)
277 | ((qi::string(
"atanh") >> &char_(
'(')) > primary_expr)
278 | ((qi::string(
"asin") >> &char_(
'(')) > primary_expr)
279 | ((qi::string(
"acos") >> &char_(
'(')) > primary_expr)
280 | ((qi::string(
"atan") >> &char_(
'(')) > primary_expr)
281 | ((qi::string(
"sinh") >> &char_(
'(')) > primary_expr)
282 | ((qi::string(
"cosh") >> &char_(
'(')) > primary_expr)
283 | ((qi::string(
"tanh") >> &char_(
'(')) > primary_expr)
284 | ((qi::string(
"sin") >> &char_(
'(')) > primary_expr)
285 | ((qi::string(
"cos") >> &char_(
'(')) > primary_expr)
286 | ((qi::string(
"tan") >> &char_(
'(')) > primary_expr)
287 | ((qi::string(
"log") >> &char_(
'(')) > primary_expr)
288 | ((qi::string(
"exp") >> &char_(
'(')) > primary_expr)
295 | (uint_ >> !char_(
'.')) | double_ | bool_ |
identifier
298 identifier = lexeme[((alpha | char_(
'_') | char_(
'$') | char_(
'[') | char_(
']') | char_(
'"') | char_(
'\''))
299 >> *(alnum | char_(
'.') | char_(
'_') | char_(
'$') | char_(
'[') | char_(
']') | char_(
'"') | char_(
'\'')))];
302 BOOST_SPIRIT_DEBUG_NODES(
304 (logical_expr)(additive_expr)(multiplicative_expr)
305 (unary_expr)(unaryfunc_expr)(primary_expr)
309 on_error<fail>(
expression, error_handler(_4, _3, _2));
315 logical_expr, additive_expr, multiplicative_expr, power_expr;
318 unary_expr, unaryfunc_expr, primary_expr;
320 qi::rule<Iterator, std::string(), ascii::space_type>
identifier;
327 #endif // PARSING_INTERNALS_H