2008-08-08 00:01:22 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <boost/spirit.hpp>
|
|
|
|
#include <boost/spirit/phoenix.hpp>
|
|
|
|
#include <boost/spirit/phoenix/composite.hpp>
|
|
|
|
#include <boost/spirit/phoenix/functions.hpp>
|
|
|
|
#include <boost/spirit/phoenix/operators.hpp>
|
|
|
|
#include <boost/function.hpp>
|
|
|
|
#include <boost/noncopyable.hpp>
|
2008-08-08 03:23:34 +00:00
|
|
|
#include <boost/format.hpp>
|
2008-08-08 00:01:22 +00:00
|
|
|
|
|
|
|
#include "aftypes.h"
|
|
|
|
#include "parser.h"
|
|
|
|
|
|
|
|
using namespace boost::spirit;
|
|
|
|
using namespace phoenix;
|
|
|
|
|
2008-08-08 10:36:17 +00:00
|
|
|
/*
|
|
|
|
* ABUSEFILTER EXPRESSION PARSER
|
|
|
|
* =============================
|
|
|
|
*
|
|
|
|
* This is the basic expression parser. It doesn't contain any AF logic
|
|
|
|
* itself, but rather presents an interface for the user to add custom
|
|
|
|
* functions and variables.
|
|
|
|
*
|
|
|
|
* The interface to the parser is the 'expressor' class. Use it like this:
|
|
|
|
*
|
|
|
|
* expressor e;
|
|
|
|
* e.add_variable("ONE", 1);
|
|
|
|
* e.evaluate("ONE + 2"); -- returns 3
|
|
|
|
*
|
|
|
|
* Custom functions should have the following prototype:
|
|
|
|
*
|
|
|
|
* datum (std::vector<afp::datum) const &args);
|
|
|
|
*
|
|
|
|
* Functions must return a value; they cannot be void. The arguments passed to
|
|
|
|
* the function are stored in the 'args' array in left-to-right order.
|
|
|
|
*
|
|
|
|
* The parser implements a C-like grammar with some differences. The following
|
|
|
|
* operators are available:
|
|
|
|
*
|
|
|
|
* a & b true if a and b are both true
|
|
|
|
* a | b true if either a or b is true
|
|
|
|
* a ^ b true if either a or b is true, but not if both are true
|
|
|
|
* a + b arithmetic
|
|
|
|
* a - b
|
|
|
|
* a * b
|
|
|
|
* a / b
|
|
|
|
* a % b
|
|
|
|
* a ** b power-of (a^b)
|
|
|
|
* a in b true if the string "b" contains the substring "a"
|
|
|
|
* !a true if a is false
|
|
|
|
* (a) same value as a
|
|
|
|
* a ? b : c if a is true, returns the value of b, otherwise c
|
|
|
|
* a == b comparison operators
|
|
|
|
* a != b
|
|
|
|
* a < b
|
|
|
|
* a <= b
|
|
|
|
* a > b
|
|
|
|
* a >= b
|
|
|
|
* a === b returns true if a==b and both are the same type
|
|
|
|
* a !== b return true if a != b or they are different types
|
|
|
|
*
|
|
|
|
* The parser uses afp::datum for its variables. This means it supports
|
|
|
|
* strings, ints and floats, with automatic conversion between types.
|
|
|
|
*/
|
|
|
|
|
2008-08-08 00:01:22 +00:00
|
|
|
namespace px = phoenix;
|
|
|
|
|
2008-08-08 03:23:34 +00:00
|
|
|
namespace afp {
|
|
|
|
|
2008-08-08 00:01:22 +00:00
|
|
|
struct parse_error : std::runtime_error {
|
|
|
|
parse_error(char const *what) : std::runtime_error(what) {}
|
|
|
|
};
|
|
|
|
|
2008-08-08 10:36:17 +00:00
|
|
|
/*
|
|
|
|
* The parser stores the result of each grammar rule in a closure. Most rules
|
|
|
|
* use the parser_closure, which simply stores a single value.
|
|
|
|
*/
|
2008-08-08 03:23:34 +00:00
|
|
|
struct parser_closure : boost::spirit::closure<parser_closure, datum>
|
2008-08-08 00:01:22 +00:00
|
|
|
{
|
|
|
|
member1 val;
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2008-08-08 03:23:34 +00:00
|
|
|
datum f_in(datum const &a, datum const &b)
|
2008-08-08 00:01:22 +00:00
|
|
|
{
|
|
|
|
std::string sa = a, sb = b;
|
2008-08-08 03:23:34 +00:00
|
|
|
return datum(std::search(sb.begin(), sb.end(), sa.begin(), sa.end()) != sb.end());
|
2008-08-08 00:01:22 +00:00
|
|
|
}
|
|
|
|
|
2008-08-08 05:39:51 +00:00
|
|
|
datum
|
|
|
|
f_ternary(datum const &v, datum const &iftrue, datum const &iffalse)
|
|
|
|
{
|
|
|
|
return (bool)v ? iftrue : iffalse;
|
|
|
|
}
|
|
|
|
|
2008-08-08 00:01:22 +00:00
|
|
|
}
|
|
|
|
|
2008-08-08 10:36:17 +00:00
|
|
|
/*
|
|
|
|
* This is the closure types for functions. 'val' stores the final result of
|
|
|
|
* the function call; func and args store the function object and the parsed
|
|
|
|
* arguments.
|
|
|
|
*/
|
2008-08-08 00:01:22 +00:00
|
|
|
struct function_closure : boost::spirit::closure<
|
|
|
|
function_closure,
|
2008-08-08 03:23:34 +00:00
|
|
|
datum,
|
|
|
|
boost::function<datum (std::vector<datum>)>,
|
|
|
|
std::vector<datum> >
|
2008-08-08 00:01:22 +00:00
|
|
|
{
|
|
|
|
member1 val;
|
|
|
|
member2 func;
|
|
|
|
member3 args;
|
|
|
|
};
|
|
|
|
|
2008-08-08 10:36:17 +00:00
|
|
|
/*
|
|
|
|
* The closure for the ?: operator. Parsed as expr ? iftrue : iffalse.
|
|
|
|
*/
|
2008-08-08 05:39:51 +00:00
|
|
|
struct ternary_closure : boost::spirit::closure<
|
|
|
|
ternary_closure,
|
|
|
|
datum,
|
|
|
|
datum,
|
|
|
|
datum>
|
|
|
|
{
|
|
|
|
member1 val;
|
|
|
|
member2 iftrue;
|
|
|
|
member3 iffalse;
|
|
|
|
};
|
|
|
|
|
2008-08-08 10:36:17 +00:00
|
|
|
/*
|
|
|
|
* The grammar itself.
|
|
|
|
*/
|
2008-08-08 00:01:22 +00:00
|
|
|
struct parser_grammar : public grammar<parser_grammar, parser_closure::context_t>
|
|
|
|
{
|
2008-08-08 10:36:17 +00:00
|
|
|
/* User-defined variables. */
|
2008-08-08 03:23:34 +00:00
|
|
|
symbols<datum> variables;
|
2008-08-08 00:01:22 +00:00
|
|
|
|
2008-08-08 03:23:34 +00:00
|
|
|
void add_variable(std::string const &name, datum const &value) {
|
2008-08-08 00:01:22 +00:00
|
|
|
variables.add(name.c_str(), value);
|
|
|
|
}
|
|
|
|
|
2008-08-08 10:36:17 +00:00
|
|
|
/* User-defined functions. */
|
|
|
|
symbols<boost::function<datum (std::vector<datum>)> > functions;
|
|
|
|
|
2008-08-08 03:23:34 +00:00
|
|
|
void add_function(std::string const &name, boost::function<datum (std::vector<datum>)> func) {
|
2008-08-08 00:01:22 +00:00
|
|
|
functions.add(name.c_str(), func);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename ScannerT>
|
|
|
|
struct definition
|
|
|
|
{
|
|
|
|
typedef rule<ScannerT, parser_closure::context_t> rule_t;
|
|
|
|
|
|
|
|
parser_grammar const &self_;
|
|
|
|
|
2008-08-08 10:36:17 +00:00
|
|
|
/*
|
|
|
|
* A phoenix actor to append its argument to a container.
|
|
|
|
*/
|
2008-08-08 00:01:22 +00:00
|
|
|
struct push_back_impl {
|
|
|
|
template<typename C, typename I>
|
|
|
|
struct result {
|
|
|
|
typedef void type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename C, typename I>
|
|
|
|
void operator() (C &c, I const &i) const {
|
|
|
|
c.push_back(i);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
phoenix::function<push_back_impl> const push_back;
|
|
|
|
|
2008-08-08 10:36:17 +00:00
|
|
|
/*
|
|
|
|
* A phoenix actor to call a user-defined function given the
|
|
|
|
* function object and arguments.
|
|
|
|
*/
|
2008-08-08 00:01:22 +00:00
|
|
|
struct call_function_impl {
|
|
|
|
template<typename F, typename A>
|
|
|
|
struct result {
|
2008-08-08 03:23:34 +00:00
|
|
|
typedef datum type;
|
2008-08-08 00:01:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename F, typename A>
|
2008-08-08 03:23:34 +00:00
|
|
|
datum operator() (F const &func, A const &args) const {
|
2008-08-08 00:01:22 +00:00
|
|
|
return func(args);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
phoenix::function<call_function_impl> const call_function;
|
|
|
|
|
|
|
|
definition(parser_grammar const &self)
|
|
|
|
: self_(self)
|
|
|
|
, push_back(push_back_impl())
|
|
|
|
, call_function(call_function_impl())
|
|
|
|
{
|
2008-08-08 10:36:17 +00:00
|
|
|
/*
|
|
|
|
* A literal value. Either a string, a floating
|
|
|
|
* pointer number or an integer.
|
|
|
|
*/
|
2008-08-08 00:01:22 +00:00
|
|
|
value =
|
2008-08-08 10:36:17 +00:00
|
|
|
strict_real_p[value.val = arg1]
|
2008-08-08 00:01:22 +00:00
|
|
|
| int_p[value.val = arg1]
|
|
|
|
| confix_p('"', *c_escape_ch_p, '"')[
|
|
|
|
value.val = construct_<std::string>(arg1 + 1, arg2 - 1)
|
|
|
|
]
|
|
|
|
;
|
|
|
|
|
2008-08-08 10:36:17 +00:00
|
|
|
/*
|
|
|
|
* A variable. If the variable is found in the
|
|
|
|
* user-supplied variable list, we use that.
|
|
|
|
* Otherwise, unknown variables (containing uppercase
|
|
|
|
* letters and underscore only) are returned as the
|
|
|
|
* empty string.
|
|
|
|
*/
|
2008-08-08 00:01:22 +00:00
|
|
|
variable =
|
|
|
|
self.variables[variable.val = arg1]
|
2008-08-08 10:36:17 +00:00
|
|
|
| (+ (upper_p | '_') )[variable.val = ""]
|
2008-08-08 00:01:22 +00:00
|
|
|
;
|
|
|
|
|
2008-08-08 10:36:17 +00:00
|
|
|
/*
|
|
|
|
* A function call: func([arg[, arg...]]).
|
|
|
|
*/
|
2008-08-08 00:01:22 +00:00
|
|
|
function =
|
|
|
|
(
|
|
|
|
self.functions[function.func = arg1]
|
|
|
|
>> '('
|
2008-08-08 05:39:51 +00:00
|
|
|
>> ( tern_expr[push_back(function.args, arg1)] % ',' )
|
2008-08-08 00:01:22 +00:00
|
|
|
>> ')'
|
|
|
|
) [function.val = call_function(function.func, function.args)]
|
|
|
|
;
|
|
|
|
|
2008-08-08 10:36:17 +00:00
|
|
|
/*
|
|
|
|
* A basic atomic value. Either a variable, function
|
|
|
|
* or literal, or a negated expression !a, or a
|
|
|
|
* parenthesised expression (a).
|
|
|
|
*/
|
2008-08-08 00:01:22 +00:00
|
|
|
basic =
|
2008-08-08 05:39:51 +00:00
|
|
|
( '(' >> tern_expr[basic.val = arg1] >> ')' )
|
|
|
|
| ch_p('!') >> tern_expr[basic.val = !arg1]
|
2008-08-08 00:01:22 +00:00
|
|
|
| variable[basic.val = arg1]
|
|
|
|
| function[basic.val = arg1]
|
|
|
|
| value[basic.val = arg1]
|
|
|
|
;
|
|
|
|
|
2008-08-08 10:36:17 +00:00
|
|
|
/*
|
|
|
|
* "a in b" operator
|
|
|
|
*/
|
2008-08-08 00:01:22 +00:00
|
|
|
in_expr =
|
|
|
|
basic[in_expr.val = arg1]
|
|
|
|
>> *(
|
|
|
|
"in" >> basic[in_expr.val = bind(&f_in)(in_expr.val, arg1)]
|
|
|
|
)
|
|
|
|
;
|
|
|
|
|
2008-08-08 10:36:17 +00:00
|
|
|
/*
|
|
|
|
* power-of. This is right-associative.
|
|
|
|
*/
|
|
|
|
pow_expr =
|
|
|
|
in_expr[pow_expr.val = arg1]
|
|
|
|
>> !(
|
|
|
|
"**" >> pow_expr[pow_expr.val = bind(&afp::pow)(pow_expr.val, arg1)]
|
|
|
|
)
|
|
|
|
;
|
2008-08-08 00:01:22 +00:00
|
|
|
|
2008-08-08 10:36:17 +00:00
|
|
|
/*
|
|
|
|
* Multiplication and operators with the same
|
|
|
|
* precedence.
|
|
|
|
*/
|
2008-08-08 00:01:22 +00:00
|
|
|
mult_expr =
|
2008-08-08 10:36:17 +00:00
|
|
|
pow_expr[mult_expr.val = arg1]
|
2008-08-08 00:01:22 +00:00
|
|
|
>> *(
|
2008-08-08 10:36:17 +00:00
|
|
|
'*' >> pow_expr[mult_expr.val *= arg1]
|
|
|
|
| '/' >> pow_expr[mult_expr.val /= arg1]
|
|
|
|
| '%' >> pow_expr[mult_expr.val %= arg1]
|
2008-08-08 00:01:22 +00:00
|
|
|
)
|
|
|
|
;
|
|
|
|
|
2008-08-08 10:36:17 +00:00
|
|
|
/*
|
|
|
|
* Additional and operators with the same precedence.
|
|
|
|
*/
|
2008-08-08 00:01:22 +00:00
|
|
|
plus_expr =
|
|
|
|
mult_expr[plus_expr.val = arg1]
|
|
|
|
>> *(
|
|
|
|
'+' >> mult_expr[plus_expr.val += arg1]
|
|
|
|
| '-' >> mult_expr[plus_expr.val -= arg1]
|
|
|
|
)
|
|
|
|
;
|
|
|
|
|
2008-08-08 10:36:17 +00:00
|
|
|
/*
|
|
|
|
* Ordinal comparisons and operators with the same
|
|
|
|
* precedence.
|
|
|
|
*/
|
|
|
|
ord_expr =
|
|
|
|
plus_expr[ord_expr.val = arg1]
|
2008-08-08 00:01:22 +00:00
|
|
|
>> *(
|
2008-08-08 10:36:17 +00:00
|
|
|
"<" >> plus_expr[ord_expr.val = ord_expr.val < arg1]
|
|
|
|
| ">" >> plus_expr[ord_expr.val = ord_expr.val > arg1]
|
|
|
|
| "<=" >> plus_expr[ord_expr.val = ord_expr.val <= arg1]
|
|
|
|
| ">=" >> plus_expr[ord_expr.val = ord_expr.val >= arg1]
|
2008-08-08 00:01:22 +00:00
|
|
|
)
|
|
|
|
;
|
|
|
|
|
2008-08-08 10:36:17 +00:00
|
|
|
/*
|
|
|
|
* Equality comparisons.
|
|
|
|
*/
|
|
|
|
eq_expr =
|
|
|
|
ord_expr[eq_expr.val = arg1]
|
2008-08-08 00:01:22 +00:00
|
|
|
>> *(
|
2008-08-08 10:36:17 +00:00
|
|
|
"==" >> eq_expr[eq_expr.val = eq_expr.val == arg1]
|
|
|
|
| "!=" >> eq_expr[eq_expr.val = eq_expr.val != arg1]
|
|
|
|
| "===" >> eq_expr[eq_expr.val =
|
|
|
|
bind(&datum::compare_with_type)(eq_expr.val, arg1)]
|
|
|
|
| "!==" >> eq_expr[eq_expr.val =
|
|
|
|
!bind(&datum::compare_with_type)(eq_expr.val, arg1)]
|
2008-08-08 00:01:22 +00:00
|
|
|
)
|
|
|
|
;
|
|
|
|
|
2008-08-08 10:36:17 +00:00
|
|
|
/*
|
|
|
|
* Boolean expressions.
|
|
|
|
*/
|
2008-08-08 00:01:22 +00:00
|
|
|
bool_expr =
|
2008-08-08 10:36:17 +00:00
|
|
|
eq_expr[bool_expr.val = arg1]
|
2008-08-08 00:01:22 +00:00
|
|
|
>> *(
|
2008-08-08 10:36:17 +00:00
|
|
|
'&' >> eq_expr[bool_expr.val = bool_expr.val && arg1]
|
|
|
|
| '|' >> eq_expr[bool_expr.val = bool_expr.val || arg1]
|
|
|
|
| '^' >> eq_expr[bool_expr.val =
|
2008-08-08 00:01:22 +00:00
|
|
|
((bool_expr.val || arg1)
|
|
|
|
&& !(bool_expr.val && arg1)) ]
|
|
|
|
)
|
|
|
|
;
|
|
|
|
|
2008-08-08 10:36:17 +00:00
|
|
|
/*
|
|
|
|
* The ternary operator. Notice this is
|
|
|
|
* right-associative: a ? b ? c : d : e
|
|
|
|
* is supported.
|
|
|
|
*/
|
2008-08-08 05:39:51 +00:00
|
|
|
tern_expr =
|
|
|
|
bool_expr[tern_expr.val = arg1]
|
|
|
|
>> !(
|
|
|
|
(
|
2008-08-08 05:52:44 +00:00
|
|
|
"?" >> tern_expr[tern_expr.iftrue = arg1]
|
|
|
|
>> ":" >> tern_expr[tern_expr.iffalse = arg1]
|
2008-08-08 05:39:51 +00:00
|
|
|
)[tern_expr.val =
|
|
|
|
bind(f_ternary)(tern_expr.val, tern_expr.iftrue, tern_expr.iffalse)]
|
|
|
|
)
|
|
|
|
;
|
|
|
|
|
2008-08-08 10:36:17 +00:00
|
|
|
/*
|
|
|
|
* The root expression type.
|
|
|
|
*/
|
2008-08-08 05:39:51 +00:00
|
|
|
expr = tern_expr[self.val = arg1];
|
2008-08-08 00:01:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rule_t const &start() const {
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
rule_t value, variable, basic, bool_expr,
|
2008-08-08 10:36:17 +00:00
|
|
|
ord_expr, eq_expr, pow_expr, mult_expr, plus_expr, in_expr, expr;
|
2008-08-08 00:01:22 +00:00
|
|
|
rule<ScannerT, function_closure::context_t> function;
|
2008-08-08 05:39:51 +00:00
|
|
|
rule<ScannerT, ternary_closure::context_t> tern_expr;
|
2008-08-08 00:01:22 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
expressor::expressor()
|
|
|
|
: grammar_(new parser_grammar)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
expressor::~expressor()
|
|
|
|
{
|
|
|
|
delete grammar_;
|
|
|
|
}
|
|
|
|
|
2008-08-08 10:36:17 +00:00
|
|
|
/*
|
|
|
|
* The user interface to evaluate an expression. It returns the result, or
|
|
|
|
* throws an exception if an error occurs.
|
|
|
|
*/
|
2008-08-08 03:23:34 +00:00
|
|
|
datum
|
2008-08-08 00:01:22 +00:00
|
|
|
expressor::evaluate(std::string const &filter) const
|
|
|
|
{
|
2008-08-08 03:23:34 +00:00
|
|
|
datum ret;
|
2008-08-08 00:01:22 +00:00
|
|
|
parse_info<std::string::const_iterator> info =
|
2008-08-08 03:23:34 +00:00
|
|
|
parse(filter.begin(), filter.end(), (*grammar_)[var(ret) = arg1],
|
|
|
|
comment_p("/*", "*/") | chset<>("\n\t "));
|
2008-08-08 00:01:22 +00:00
|
|
|
if (info.full) {
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
std::cerr << "stopped at: [" << std::string(info.stop, filter.end()) << "]\n";
|
|
|
|
throw parse_error("parsing failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-08-08 03:23:34 +00:00
|
|
|
expressor::add_variable(std::string const &name, datum value)
|
2008-08-08 00:01:22 +00:00
|
|
|
{
|
|
|
|
grammar_->add_variable(name, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
expressor::add_function(std::string const &name, func_t value)
|
|
|
|
{
|
|
|
|
grammar_->add_function(name, value);
|
|
|
|
}
|
|
|
|
|
2008-08-08 03:23:34 +00:00
|
|
|
} // namespace afp
|
|
|
|
|
2008-08-08 00:01:22 +00:00
|
|
|
#ifdef TEST_PARSER
|
2008-08-08 03:23:34 +00:00
|
|
|
afp::datum
|
|
|
|
f_add(std::vector<afp::datum> const &args)
|
2008-08-08 00:01:22 +00:00
|
|
|
{
|
|
|
|
return args[0] + args[1];
|
|
|
|
}
|
|
|
|
|
2008-08-08 03:23:34 +00:00
|
|
|
afp::datum
|
|
|
|
f_norm(std::vector<afp::datum> const &args)
|
2008-08-08 00:01:22 +00:00
|
|
|
{
|
|
|
|
return args[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2008-08-08 03:23:34 +00:00
|
|
|
if (argc != 2) {
|
|
|
|
std::cerr << boost::format("usage: %s <expr>\n")
|
|
|
|
% argv[0];
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
afp::expressor e;
|
2008-08-08 00:01:22 +00:00
|
|
|
e.add_variable("ONE", 1);
|
|
|
|
e.add_variable("TWO", 2);
|
|
|
|
e.add_variable("THREE", 3);
|
|
|
|
e.add_function("add", f_add);
|
|
|
|
e.add_function("norm", f_norm);
|
|
|
|
|
|
|
|
try {
|
|
|
|
std::cout << e.evaluate(argv[1]) << '\n';
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
std::cout << "parsing failed: " << e.what() << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|