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>
47 typedef boost::variant<
53 , boost::recursive_wrapper<unaryexpr_>
54 , boost::recursive_wrapper<expression>
76 inline std::ostream&
operator<<(std::ostream& out,
nil) { out <<
"nil";
return out; }
80BOOST_FUSION_ADAPT_STRUCT(
82 (std::string, operator_)
86BOOST_FUSION_ADAPT_STRUCT(
88 (std::string, operator_)
92BOOST_FUSION_ADAPT_STRUCT(
95 (std::list<ExpressionParsing::ast::operation>, rest)
151 VirtualMachine(
unsigned stackSize = 4096)
152 : m_stackSize(stackSize)
155 StackElement
execute(
const EventContext& ctx, std::vector<StackElement>
const& code)
const;
157 unsigned m_stackSize;
164 typedef void result_type;
166 std::vector<StackElement>&
code;
167 Compiler(std::vector<StackElement>& code, IProxyLoader *proxyLoader,
168 IUnitInterpreter *unitInterpreter)
169 :
code(
code), m_proxyLoader(proxyLoader), m_unitInterpreter(unitInterpreter) { }
171 void operator()(ast::nil) { BOOST_ASSERT(0); }
172 void operator()(
unsigned int n);
173 void operator()(
bool n);
174 void operator()(
double n);
175 void operator()(
const std::string &n);
177 void operator()(ast::operation
const&
x);
178 void operator()(ast::unaryexpr_
const&
x);
179 void operator()(ast::expression
const&
x);
182 IProxyLoader *m_proxyLoader;
183 IUnitInterpreter *m_unitInterpreter;
186 namespace qi = boost::spirit::qi;
187 namespace ascii = boost::spirit::ascii;
189 struct error_handler_
194 template <
typename Iterator>
197 , Iterator err_pos, Iterator last)
const
200 <<
"Error! Expecting " <<
what
201 <<
" here: \"" << std::string(err_pos, last) <<
"\""
206 boost::phoenix::function<error_handler_>
const error_handler = error_handler_();
208 template <
typename Iterator>
209 struct Grammar : qi::grammar<Iterator, ast::expression(), ascii::space_type>
233 logical_expr = equality_expr >> *(
234 (qi::string(
"&&") > equality_expr)
235 | (qi::string(
"||") > equality_expr)
238 equality_expr = relational_expr >> *(
239 (qi::string(
"==") > relational_expr)
240 | (qi::string(
"!=") > relational_expr)
243 relational_expr = additive_expr >> *(
244 (qi::string(
">=") > additive_expr)
245 | (qi::string(
"<=") > additive_expr)
246 | (qi::string(
">") > additive_expr)
247 | (qi::string(
"<") > additive_expr)
250 additive_expr = multiplicative_expr >> *(
251 (char_(
'+') > multiplicative_expr)
252 | (char_(
'-') > multiplicative_expr)
255 multiplicative_expr = power_expr >> *(
256 (char_(
'*') > power_expr)
257 | (char_(
'/') > power_expr)
260 power_expr = unary_expr >> *(
261 (qi::string(
"**") > unary_expr)
264 unary_expr = unaryfunc_expr
265 | (qi::string(
"-") > unaryfunc_expr)
266 | (qi::string(
"+") > unaryfunc_expr)
267 | (qi::string(
"!") > unaryfunc_expr)
272 ((qi::string(
"sum") >> &char_(
'(')) > primary_expr)
273 | ((qi::string(
"count") >> &char_(
'(')) > primary_expr)
274 | ((qi::string(
"abs") >> &char_(
'(')) > primary_expr)
275 | ((qi::string(
"sqrt") >> &char_(
'(')) > primary_expr)
276 | ((qi::string(
"cbrt") >> &char_(
'(')) > primary_expr)
277 | ((qi::string(
"asinh") >> &char_(
'(')) > primary_expr)
278 | ((qi::string(
"acosh") >> &char_(
'(')) > primary_expr)
279 | ((qi::string(
"atanh") >> &char_(
'(')) > primary_expr)
280 | ((qi::string(
"asin") >> &char_(
'(')) > primary_expr)
281 | ((qi::string(
"acos") >> &char_(
'(')) > primary_expr)
282 | ((qi::string(
"atan") >> &char_(
'(')) > primary_expr)
283 | ((qi::string(
"sinh") >> &char_(
'(')) > primary_expr)
284 | ((qi::string(
"cosh") >> &char_(
'(')) > primary_expr)
285 | ((qi::string(
"tanh") >> &char_(
'(')) > primary_expr)
286 | ((qi::string(
"sin") >> &char_(
'(')) > primary_expr)
287 | ((qi::string(
"cos") >> &char_(
'(')) > primary_expr)
288 | ((qi::string(
"tan") >> &char_(
'(')) > primary_expr)
289 | ((qi::string(
"log") >> &char_(
'(')) > primary_expr)
290 | ((qi::string(
"exp") >> &char_(
'(')) > primary_expr)
296 | (uint_ >> !char_(
'.')) | double_ | bool_ |
identifier
299 identifier = lexeme[((
alpha | char_(
'_') | char_(
'$') | char_(
'[') | char_(
']') | char_(
'"') | char_(
'\''))
300 >> *(alnum | char_(
'.') | char_(
'_') | char_(
'$') | char_(
'[') | char_(
']') | char_(
'"') | char_(
'\'')))];
303 BOOST_SPIRIT_DEBUG_NODES(
304 (expression)(equality_expr)(relational_expr)
305 (logical_expr)(additive_expr)(multiplicative_expr)
306 (unary_expr)(unaryfunc_expr)(primary_expr)
310 on_error<fail>(expression, error_handler(_4, _3, _2));
314 qi::rule<Iterator, ast::expression(), ascii::space_type>
316 logical_expr, additive_expr, multiplicative_expr, power_expr;
318 qi::rule<Iterator, ast::operand(), ascii::space_type>
319 unary_expr, unaryfunc_expr, primary_expr;
321 qi::rule<Iterator, std::string(), ascii::space_type>
identifier;
boost::variant< nil, double, unsigned int, bool, std::string, boost::recursive_wrapper< unaryexpr_ >, boost::recursive_wrapper< expression > > operand
std::ostream & operator<<(std::ostream &out, nil)
Namespace holding all the expression evaluation code.
std::list< operation > rest