Geant4 Cross Reference |
1 // 2 // ******************************************************************** 3 // * License and Disclaimer * 4 // * * 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. * 10 // * * 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitation of liability. * 17 // * * 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************************************** 25 // 26 /* 27 * ============================================================================= 28 * 29 * Filename: CexmcCustomFilter.hh 30 * 31 * Description: custom filter grammar and compiler 32 * 33 * Version: 1.0 34 * Created: 17.07.2010 15:31:43 35 * Revision: none 36 * Compiler: gcc 37 * 38 * Author: Alexey Radkov (), 39 * Company: PNPI 40 * 41 * ============================================================================= 42 */ 43 44 #ifndef CEXMC_CUSTOM_FILTER_HH 45 #define CEXMC_CUSTOM_FILTER_HH 46 47 #ifdef CEXMC_USE_CUSTOM_FILTER 48 49 #include <string> 50 #include <boost/spirit/include/qi.hpp> 51 #include <boost/spirit/include/phoenix_core.hpp> 52 #include <boost/spirit/include/phoenix_operator.hpp> 53 #include <boost/spirit/include/phoenix_function.hpp> 54 #include "CexmcAST.hh" 55 56 57 namespace CexmcCustomFilter 58 { 59 using namespace boost::spirit; 60 using namespace boost::spirit::qi; 61 using namespace boost::spirit::ascii; 62 using namespace boost::phoenix; 63 using namespace CexmcAST; 64 using boost::spirit::qi::rule; 65 using boost::spirit::ascii::space; 66 using boost::spirit::ascii::space_type; 67 using boost::spirit::ascii::alpha; 68 using boost::spirit::ascii::alnum; 69 using boost::spirit::unused_type; 70 71 72 enum Action 73 { 74 KeepTPT, 75 KeepEDT, 76 DeleteTPT, 77 DeleteEDT 78 }; 79 80 81 struct ParseResult 82 { 83 ParseResult() : action( KeepTPT ) 84 {} 85 86 void Initialize( void ) 87 { 88 action = KeepTPT; 89 expression.children.clear(); 90 expression.type = Operator( Uninitialized ); 91 } 92 93 Action action; 94 95 Subtree expression; 96 }; 97 98 99 struct Compiler 100 { 101 template < typename A, typename B = unused_type, 102 typename C = unused_type, typename D = unused_type > 103 struct result { typedef void type; }; 104 105 void operator()( ParseResult & parseResult, Action value ) const; 106 107 void operator()( ParseResult & parseResult, Subtree & value ) const; 108 109 void operator()( Subtree & ast, Node & node ) const; 110 111 void operator()( Node & self, Node & left, Node & right, 112 Operator value ) const; 113 114 void operator()( Node & self, Node & child, Operator value ) const; 115 116 void operator()( Node & self, Node & primary ) const; 117 118 void operator()( Node & self, Node & child, std::string & value ) 119 const; 120 121 void operator()( Leaf & self, std::string & name ) const; 122 123 void operator()( Leaf & self, int value, size_t index ) const; 124 }; 125 126 127 template < typename Iterator > 128 struct Grammar : grammar< Iterator, ParseResult(), space_type > 129 { 130 Grammar(); 131 132 rule< Iterator, ParseResult(), space_type > statement; 133 134 rule< Iterator, Action(), space_type > action; 135 136 rule< Iterator, Subtree(), space_type > condition; 137 138 rule< Iterator, Node(), space_type > expression; 139 140 rule< Iterator, Node(), space_type > primary_expr; 141 142 rule< Iterator, Node(), space_type > function1; 143 144 rule< Iterator, std::string(), space_type > identifier; 145 146 rule< Iterator, Leaf(), space_type > leaf_operand; 147 148 rule< Iterator, Leaf(), space_type > constant; 149 150 rule< Iterator, Leaf(), space_type > variable; 151 152 rule< Iterator, Node(), space_type > or_expr; 153 154 rule< Iterator, Node(), space_type > and_expr; 155 156 rule< Iterator, Node(), space_type > relation; 157 158 rule< Iterator, Node(), space_type > addition; 159 160 rule< Iterator, Node(), space_type > multiplication; 161 162 rule< Iterator, Node(), space_type > unary_expr; 163 164 rule< Iterator, Operator(), space_type > unary_op; 165 166 rule< Iterator, Operator(), space_type > mult_op; 167 168 rule< Iterator, Operator(), space_type > add_op; 169 170 rule< Iterator, Operator(), space_type > rel_op; 171 172 real_parser< double, strict_real_policies< double > > strict_double; 173 174 function< Compiler > op; 175 }; 176 177 178 template < typename Iterator > 179 Grammar< Iterator >::Grammar() : Grammar::base_type( statement ) 180 { 181 statement = action[ op( _val, _1 ) ] >> 182 *( condition[ op( _val, _1 ) ] ); 183 184 action = lit( "keep" ) >> 185 ( lit( "tpt" )[ _val = KeepTPT ] | 186 lit( "edt" )[ _val = KeepEDT ] ) | 187 lit( "delete" ) >> 188 ( lit( "tpt" )[ _val = DeleteTPT ] | 189 lit( "edt" )[ _val = DeleteEDT ] ); 190 191 condition = lit( "if" ) >> expression[ op( _val, _1 ) ]; 192 193 expression %= or_expr; 194 195 identifier %= raw[ lexeme[ alpha >> *( alnum | '_' ) ] ]; 196 197 primary_expr = function1[ _val = _1 ] | 198 lit( '(' ) >> expression[ op( _val, _1 ) ] >> lit( ')' ) | 199 leaf_operand[ _val = _1 ]; 200 201 leaf_operand %= constant | variable; 202 203 constant %= strict_double | int_; 204 205 variable = identifier[ op( _val, _1 ) ] >> 206 -( lit( '[' ) >> ( uint_[ op( _val, _1, 0 ) ] - lit( '0' ) ) >> 207 -( lit( ',' ) >> ( uint_[ op( _val, _1, 1 ) ] - 208 lit( '0' ) ) ) >> lit( ']' ) ); 209 210 function1 = ( identifier >> lit( '(' ) >> expression >> lit( ')' ) ) 211 [ op( _val, _2, _1 ) ]; 212 213 or_expr = ( and_expr >> lit( '|' ) >> or_expr ) 214 [ op( _val, _1, _2, Operator( Or, 1 ) ) ] | 215 and_expr[ _val = _1 ]; 216 217 and_expr = ( relation >> lit( '&' ) >> and_expr ) 218 [ op( _val, _1, _2, Operator( And, 2 ) ) ] | 219 relation[ _val = _1 ]; 220 221 relation = ( addition >> rel_op >> addition ) 222 [ op( _val, _1, _3, _2 ) ] | 223 addition[ _val = _1 ]; 224 225 addition = ( multiplication >> add_op >> addition ) 226 [ op( _val, _1, _3, _2 ) ] | 227 multiplication[ _val = _1 ]; 228 229 multiplication = ( unary_expr >> mult_op >> multiplication ) 230 [ op( _val, _1, _3, _2 ) ] | 231 unary_expr[ _val = _1 ]; 232 233 unary_expr = ( unary_op >> primary_expr )[ op( _val, _2, _1 ) ] | 234 primary_expr[ _val = _1 ]; 235 236 unary_op = lit( '-' )[ _val = Operator( UMinus, 6, true ) ] | 237 lit( '!' )[ _val = Operator( Not, 6, true ) ]; 238 239 mult_op = lit( '*' )[ _val = Operator( Mult, 5 ) ] | 240 lit( '/' )[ _val = Operator( Div, 5 ) ]; 241 242 add_op = lit( '+' )[ _val = Operator( Plus, 4 ) ] | 243 lit( '-' )[ _val = Operator( Minus, 4 ) ]; 244 245 rel_op = lit( "<=" )[ _val = Operator( LessEq, 3 ) ] | 246 lit( ">=" )[ _val = Operator( MoreEq, 3 ) ] | 247 lit( "!=" )[ _val = Operator( NotEq, 3 ) ] | 248 lit( '<' )[ _val = Operator( Less, 3 ) ] | 249 lit( '>' )[ _val = Operator( More, 3 ) ] | 250 lit( '=' )[ _val = Operator( Eq, 3 ) ]; 251 } 252 } 253 254 #endif 255 256 #endif 257 258