277 lines
11 KiB
C++
Executable File
277 lines
11 KiB
C++
Executable File
#ifndef BOOLEANGRAMMAR_H_
|
|
#define BOOLEANGRAMMAR_H_
|
|
|
|
#include "lib/utils/bts_no_copy.h"
|
|
|
|
#include <boost/config/warning_disable.hpp>
|
|
#include <boost/spirit/include/qi.hpp>
|
|
#include <boost/spirit/include/support_utree.hpp>
|
|
#include <boost/spirit/include/phoenix_operator.hpp>
|
|
#include <boost/spirit/include/phoenix_function.hpp>
|
|
|
|
#include <string>
|
|
|
|
/*
|
|
* Using and modifying this grammar:
|
|
*
|
|
* Boost::spirit is a fantastically complex bit of technology which could take years to master.
|
|
* I'll try to describe the conventsions followed in this grammar which form a more comprehensible
|
|
* sub-set.
|
|
*
|
|
* spirit::utree is a generic syntax type which can act as the result (aka "property") of an expression or grammar.
|
|
* boost::spirit can act on any arbitrary type using template based duck typing and a set of rules to transform the result
|
|
* of your expressions to those types. However, this is incredibly tricky to get right and the error messages aren't very
|
|
* useful. Whenever possible try to use utree as the result type of your grammar elements. It should support any type needed
|
|
* for a typical parse grammar. To learn more about utree see the slides and talk
|
|
* here: http://boost-spirit.com/home/2011/06/12/ast-construction-with-the-universal-tree/
|
|
* and docs here http://www.boost.org/doc/libs/1_47_0/libs/spirit/doc/html/spirit/support/utree.html
|
|
*
|
|
* Note:
|
|
* In utree there are 2 string type nodes. The standard std::string represents a quoted string value and utf8_symbol_range_type represents
|
|
* an operator or key word in your grammar.
|
|
*
|
|
* Note 2:
|
|
* When you modify your grammar keep an eye on the raw build output. There is a lot of debug information that gets printed but not included in the Visual Studio
|
|
* error log.
|
|
*
|
|
* This is not meant ot be a complete tutorial so you should also read though the spirit docs.
|
|
*
|
|
* Basics:
|
|
*
|
|
* ** Example 0
|
|
* Most spirit parse expressions will look something like this:
|
|
* a = b [X] >> ";"
|
|
* this means a will be a parser which matches b followed by a literal semicolon. The statement inside the [ ] is called a "semantic action"
|
|
* When b is matched it will evaluate the semantic action X passing in the result (or property) of b as _1. The final result is always available
|
|
* as _val.
|
|
* If X takes the form _val = _1 it assigns the result of b to a assuming their type is compatible (nearly all types are compatible with utree)
|
|
* If X takes the form [myoperator(_val, _1)] then _val and _1 are passed in to your user defined operator and you can modify the utree AST directly.
|
|
*
|
|
* ** Example 1
|
|
* rule<Iterator, sp::utree()> term =
|
|
* bool_ [_val = _1]
|
|
* | '!' >> bool_ [not_expr(_val, _1)]
|
|
*
|
|
* If this encounters the phrase "true" the first part matches and we evaluate [_val = _1] the boolean is automatically converted to a utree (which contains a bool)
|
|
* If we have "!true" the second part matches and we evaluate [not_expr(_val, _1)] which calls our unary_operator class passing in expr which is the result property
|
|
* on term and rhs which is the bool value automatically converted to a utree. The operator is then free to modify the result (expr) directly. In this
|
|
* case it pushes 2 child nodes into expr a not operator and the original utree bool node.
|
|
*
|
|
*
|
|
* ** Example 2
|
|
* rule<Iterator, std::string()> quoted_string =
|
|
* char_("'\"")
|
|
* >> (lexeme[*(alnum [_val += _1] | space [_val += _1])])
|
|
* >> char_("'\"");
|
|
*
|
|
* This is currently the only rule in this grammar which does not use a utree. Here we use a regular std::string rule because in each semantic action we want to append
|
|
* the new character to the result. If this semantic action was not there at all the resulting property would be a vector<char> which included the quotes. This would
|
|
* be OK beause boost::spirit can automatically convert a vector<char> to a std::string but we prefer to remove the quotes now.
|
|
* lexeme[] - is a spirit transform which tells the parser to ignore space skipping for the expression. In this case [ ] do not indicate a semantic action.
|
|
* *( ) - a kleene star indicateing 0 or more matches of the contained grammar.
|
|
* char_('") - this syntax matches either of the contained string literals.
|
|
*
|
|
*
|
|
* ** Example 3
|
|
* This is an excerpt from the term expression that might help you understand how the types flow though boost::spirit (and why utree is so useful).
|
|
*
|
|
* ("in" >> ((list | property) [in_expr(_val, _1)]
|
|
* | _internal_string [in_expr(_val, _1)]))
|
|
*
|
|
* Q: Why did we separate quoted_string from (list | property) even though they both appear to have the same semantic action?
|
|
* In this case list and property both have the result type utree but quoted_string has the result type std::string. Since in_expr() has overloads for both
|
|
* a right hand utree and a right hand std::string so shoudln't it do the right thing?
|
|
* A: If we wrote (list | property | quoted_string) [in_expr(_val, _1] the type of _1 would not be either utree or string but a union of both types. Specifically
|
|
* it would be a boost::variant<utree, string>; Since we don't provide an in_expr::operator() overload for that compilation would fail.
|
|
*
|
|
*/
|
|
|
|
#ifdef __GNUC__
|
|
#pragma GCC diagnostic push
|
|
// Disble effc++ for this file because a lot of the spirit base classes don't include a virtual destructor
|
|
#pragma GCC diagnostic ignored "-Weffc++"
|
|
#endif
|
|
|
|
|
|
namespace bts {
|
|
namespace expression {
|
|
|
|
namespace qi = boost::spirit::qi;
|
|
namespace ph = boost::phoenix;
|
|
namespace sp = boost::spirit;
|
|
|
|
// Base operator holds the result type
|
|
struct base_operator
|
|
{
|
|
template <typename T1, typename T2>
|
|
struct result { typedef void type; };
|
|
|
|
base_operator(const std::string& name)
|
|
: _name(name)
|
|
{}
|
|
|
|
protected:
|
|
std::string _name;
|
|
};
|
|
|
|
// Class of operator that acts on a left and right side
|
|
struct binary_operator : public base_operator
|
|
{
|
|
binary_operator(const std::string& name)
|
|
: base_operator(name)
|
|
{}
|
|
|
|
void operator()(sp::utree& expr, const sp::utree& rhs) const
|
|
{
|
|
sp::utree lhs;
|
|
lhs.swap(expr);
|
|
expr.push_back(sp::utf8_symbol_type(_name));
|
|
expr.push_back(lhs);
|
|
expr.push_back(rhs);
|
|
}
|
|
|
|
};
|
|
extern ph::function<binary_operator> const and_expr;
|
|
extern ph::function<binary_operator> const or_expr;
|
|
extern ph::function<binary_operator> const in_expr;
|
|
extern ph::function<binary_operator> const gt_expr;
|
|
extern ph::function<binary_operator> const gte_expr;
|
|
extern ph::function<binary_operator> const lt_expr;
|
|
extern ph::function<binary_operator> const lte_expr;
|
|
extern ph::function<binary_operator> const eq_expr;
|
|
extern ph::function<binary_operator> const neq_expr;
|
|
extern ph::function<binary_operator> const match_expr;
|
|
|
|
// "!"
|
|
struct unary_operator : public base_operator
|
|
{
|
|
unary_operator(const std::string& name)
|
|
: base_operator(name)
|
|
{}
|
|
|
|
void operator()(sp::utree& expr, const sp::utree & rhs) const
|
|
{
|
|
expr.push_back(sp::utf8_symbol_type(_name));
|
|
expr.push_back(rhs);
|
|
}
|
|
};
|
|
extern ph::function<unary_operator> not_expr;
|
|
|
|
//
|
|
// The property expression needs to accept a raw vector of characters.
|
|
//
|
|
struct property_expr_type
|
|
{
|
|
template <typename T1, typename T2>
|
|
struct result { typedef void type; };
|
|
|
|
void operator() (sp::utree& expr, const std::vector<char>& rhs) const
|
|
{
|
|
std::string prop_name(rhs.begin (), rhs.end ());
|
|
expr.clear();
|
|
expr.push_back(sp::utf8_symbol_type("property"));
|
|
expr.push_back(sp::utf8_symbol_type(prop_name));
|
|
}
|
|
};
|
|
extern ph::function<property_expr_type> const property_expr;
|
|
|
|
//
|
|
// The list expression simply concatenates values in to one utree node
|
|
//
|
|
struct list_expr_type
|
|
{
|
|
template <typename T1, typename T2>
|
|
struct result { typedef void type; };
|
|
|
|
void operator()(sp::utree& expr, const sp::utree& rhs) const
|
|
{
|
|
if (expr.empty())
|
|
{
|
|
expr.push_back(sp::utf8_symbol_type("list"));
|
|
}
|
|
expr.push_back(rhs);
|
|
}
|
|
};
|
|
extern ph::function<list_expr_type> list_expr;
|
|
|
|
template <typename Iterator, typename space_type = qi::ascii::space_type>
|
|
class BooleanGrammar : public qi::grammar<Iterator, boost::spirit::utree(), space_type>
|
|
{
|
|
public:
|
|
BooleanGrammar()
|
|
: BooleanGrammar::base_type(_expression)
|
|
{
|
|
using namespace qi;
|
|
using qi::_1;
|
|
|
|
//
|
|
// Basic types
|
|
//
|
|
|
|
// quoted string's property is std::string instead of utree b/c we need to keep the type compatible with the += operator to build up the string contents
|
|
_internal_string =
|
|
char_("'\"")
|
|
>> (lexeme[*(alnum | space | (punct - char_("'\""))) [_val += _1]])
|
|
>> char_("'\"");
|
|
_quoted_string = _internal_string;
|
|
|
|
// [1,2,3,4] - sorry no lists of lists
|
|
_list = '[' >> (_primitive [list_expr(_val, _1)] % ',') >> ']';
|
|
|
|
// A property is an unquoted token which will be passed to an IPropertyEvaluator
|
|
// to be turned into a primitive value or a list.
|
|
_property = lexeme[(+(alnum | char_("_.$")))] [property_expr(_val, _1)];
|
|
|
|
_primitive =
|
|
qi::no_case[qi::bool_] [_val = _1]
|
|
| qi::double_ [_val = _1]
|
|
| _quoted_string [_val = _1]
|
|
| _property [_val = _1]
|
|
| _list [_val = _1]
|
|
| '!' >> _primitive [not_expr(_val, _1)]
|
|
| '(' >> _expression [_val = _1] >> ')'
|
|
;
|
|
|
|
//
|
|
// operators
|
|
//
|
|
|
|
_expression =
|
|
_term [_val = _1]
|
|
>> *(
|
|
("and" >> _term [and_expr(_val, _1)])
|
|
| ("&&" >> _term [and_expr(_val, _1)])
|
|
| ("or" >> _term [or_expr(_val, _1)])
|
|
| ("||" >> _term [or_expr(_val, _1)])
|
|
);
|
|
|
|
_term =
|
|
_primitive [_val = _1]
|
|
>> *( ("in" >> ((_list | _property | _quoted_string) [in_expr(_val, _1)]))
|
|
| ("~=" >> (( _property | _quoted_string) [match_expr(_val, _1)]))
|
|
| (">" >> _primitive [gt_expr(_val, _1)])
|
|
| (">=" >> _primitive [gte_expr(_val, _1)])
|
|
| ("<" >> _primitive [lt_expr(_val, _1)])
|
|
| ("<=" >> _primitive [lte_expr(_val, _1)])
|
|
| ("==" >> _primitive [eq_expr(_val, _1)])
|
|
| ("=" >> _primitive [eq_expr(_val, _1)])
|
|
| ("!=" >> _primitive [neq_expr(_val, _1)])
|
|
);
|
|
|
|
}
|
|
|
|
private:
|
|
BTS_NO_COPY(BooleanGrammar);
|
|
qi::rule<Iterator, std::string()> _internal_string;
|
|
qi::rule<Iterator, sp::utree(), space_type> _expression, _primitive, _term, _list, _property, _quoted_string;
|
|
};
|
|
|
|
extern BooleanGrammar<std::string::const_iterator> boolean_string_grammar;
|
|
|
|
#ifdef __GNUC__
|
|
#pragma GCC diagnostic pop
|
|
#endif
|
|
|
|
}}//namespace
|
|
#endif
|