File indexing completed on 2025-01-31 09:21:49
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044 #ifndef CEXMC_CUSTOM_FILTER_HH
0045 #define CEXMC_CUSTOM_FILTER_HH
0046
0047 #ifdef CEXMC_USE_CUSTOM_FILTER
0048
0049 #include <string>
0050 #include <boost/spirit/include/qi.hpp>
0051 #include <boost/spirit/include/phoenix_core.hpp>
0052 #include <boost/spirit/include/phoenix_operator.hpp>
0053 #include <boost/spirit/include/phoenix_function.hpp>
0054 #include "CexmcAST.hh"
0055
0056
0057 namespace CexmcCustomFilter
0058 {
0059 using namespace boost::spirit;
0060 using namespace boost::spirit::qi;
0061 using namespace boost::spirit::ascii;
0062 using namespace boost::phoenix;
0063 using namespace CexmcAST;
0064 using boost::spirit::qi::rule;
0065 using boost::spirit::ascii::space;
0066 using boost::spirit::ascii::space_type;
0067 using boost::spirit::ascii::alpha;
0068 using boost::spirit::ascii::alnum;
0069 using boost::spirit::unused_type;
0070
0071
0072 enum Action
0073 {
0074 KeepTPT,
0075 KeepEDT,
0076 DeleteTPT,
0077 DeleteEDT
0078 };
0079
0080
0081 struct ParseResult
0082 {
0083 ParseResult() : action( KeepTPT )
0084 {}
0085
0086 void Initialize( void )
0087 {
0088 action = KeepTPT;
0089 expression.children.clear();
0090 expression.type = Operator( Uninitialized );
0091 }
0092
0093 Action action;
0094
0095 Subtree expression;
0096 };
0097
0098
0099 struct Compiler
0100 {
0101 template < typename A, typename B = unused_type,
0102 typename C = unused_type, typename D = unused_type >
0103 struct result { typedef void type; };
0104
0105 void operator()( ParseResult & parseResult, Action value ) const;
0106
0107 void operator()( ParseResult & parseResult, Subtree & value ) const;
0108
0109 void operator()( Subtree & ast, Node & node ) const;
0110
0111 void operator()( Node & self, Node & left, Node & right,
0112 Operator value ) const;
0113
0114 void operator()( Node & self, Node & child, Operator value ) const;
0115
0116 void operator()( Node & self, Node & primary ) const;
0117
0118 void operator()( Node & self, Node & child, std::string & value )
0119 const;
0120
0121 void operator()( Leaf & self, std::string & name ) const;
0122
0123 void operator()( Leaf & self, int value, size_t index ) const;
0124 };
0125
0126
0127 template < typename Iterator >
0128 struct Grammar : grammar< Iterator, ParseResult(), space_type >
0129 {
0130 Grammar();
0131
0132 rule< Iterator, ParseResult(), space_type > statement;
0133
0134 rule< Iterator, Action(), space_type > action;
0135
0136 rule< Iterator, Subtree(), space_type > condition;
0137
0138 rule< Iterator, Node(), space_type > expression;
0139
0140 rule< Iterator, Node(), space_type > primary_expr;
0141
0142 rule< Iterator, Node(), space_type > function1;
0143
0144 rule< Iterator, std::string(), space_type > identifier;
0145
0146 rule< Iterator, Leaf(), space_type > leaf_operand;
0147
0148 rule< Iterator, Leaf(), space_type > constant;
0149
0150 rule< Iterator, Leaf(), space_type > variable;
0151
0152 rule< Iterator, Node(), space_type > or_expr;
0153
0154 rule< Iterator, Node(), space_type > and_expr;
0155
0156 rule< Iterator, Node(), space_type > relation;
0157
0158 rule< Iterator, Node(), space_type > addition;
0159
0160 rule< Iterator, Node(), space_type > multiplication;
0161
0162 rule< Iterator, Node(), space_type > unary_expr;
0163
0164 rule< Iterator, Operator(), space_type > unary_op;
0165
0166 rule< Iterator, Operator(), space_type > mult_op;
0167
0168 rule< Iterator, Operator(), space_type > add_op;
0169
0170 rule< Iterator, Operator(), space_type > rel_op;
0171
0172 real_parser< double, strict_real_policies< double > > strict_double;
0173
0174 function< Compiler > op;
0175 };
0176
0177
0178 template < typename Iterator >
0179 Grammar< Iterator >::Grammar() : Grammar::base_type( statement )
0180 {
0181 statement = action[ op( _val, _1 ) ] >>
0182 *( condition[ op( _val, _1 ) ] );
0183
0184 action = lit( "keep" ) >>
0185 ( lit( "tpt" )[ _val = KeepTPT ] |
0186 lit( "edt" )[ _val = KeepEDT ] ) |
0187 lit( "delete" ) >>
0188 ( lit( "tpt" )[ _val = DeleteTPT ] |
0189 lit( "edt" )[ _val = DeleteEDT ] );
0190
0191 condition = lit( "if" ) >> expression[ op( _val, _1 ) ];
0192
0193 expression %= or_expr;
0194
0195 identifier %= raw[ lexeme[ alpha >> *( alnum | '_' ) ] ];
0196
0197 primary_expr = function1[ _val = _1 ] |
0198 lit( '(' ) >> expression[ op( _val, _1 ) ] >> lit( ')' ) |
0199 leaf_operand[ _val = _1 ];
0200
0201 leaf_operand %= constant | variable;
0202
0203 constant %= strict_double | int_;
0204
0205 variable = identifier[ op( _val, _1 ) ] >>
0206 -( lit( '[' ) >> ( uint_[ op( _val, _1, 0 ) ] - lit( '0' ) ) >>
0207 -( lit( ',' ) >> ( uint_[ op( _val, _1, 1 ) ] -
0208 lit( '0' ) ) ) >> lit( ']' ) );
0209
0210 function1 = ( identifier >> lit( '(' ) >> expression >> lit( ')' ) )
0211 [ op( _val, _2, _1 ) ];
0212
0213 or_expr = ( and_expr >> lit( '|' ) >> or_expr )
0214 [ op( _val, _1, _2, Operator( Or, 1 ) ) ] |
0215 and_expr[ _val = _1 ];
0216
0217 and_expr = ( relation >> lit( '&' ) >> and_expr )
0218 [ op( _val, _1, _2, Operator( And, 2 ) ) ] |
0219 relation[ _val = _1 ];
0220
0221 relation = ( addition >> rel_op >> addition )
0222 [ op( _val, _1, _3, _2 ) ] |
0223 addition[ _val = _1 ];
0224
0225 addition = ( multiplication >> add_op >> addition )
0226 [ op( _val, _1, _3, _2 ) ] |
0227 multiplication[ _val = _1 ];
0228
0229 multiplication = ( unary_expr >> mult_op >> multiplication )
0230 [ op( _val, _1, _3, _2 ) ] |
0231 unary_expr[ _val = _1 ];
0232
0233 unary_expr = ( unary_op >> primary_expr )[ op( _val, _2, _1 ) ] |
0234 primary_expr[ _val = _1 ];
0235
0236 unary_op = lit( '-' )[ _val = Operator( UMinus, 6, true ) ] |
0237 lit( '!' )[ _val = Operator( Not, 6, true ) ];
0238
0239 mult_op = lit( '*' )[ _val = Operator( Mult, 5 ) ] |
0240 lit( '/' )[ _val = Operator( Div, 5 ) ];
0241
0242 add_op = lit( '+' )[ _val = Operator( Plus, 4 ) ] |
0243 lit( '-' )[ _val = Operator( Minus, 4 ) ];
0244
0245 rel_op = lit( "<=" )[ _val = Operator( LessEq, 3 ) ] |
0246 lit( ">=" )[ _val = Operator( MoreEq, 3 ) ] |
0247 lit( "!=" )[ _val = Operator( NotEq, 3 ) ] |
0248 lit( '<' )[ _val = Operator( Less, 3 ) ] |
0249 lit( '>' )[ _val = Operator( More, 3 ) ] |
0250 lit( '=' )[ _val = Operator( Eq, 3 ) ];
0251 }
0252 }
0253
0254 #endif
0255
0256 #endif
0257