Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CexmcCustomFilter.hh
Go to the documentation of this file.
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::ascii::space;
65  using boost::spirit::ascii::space_type;
66  using boost::spirit::ascii::alpha;
67  using boost::spirit::ascii::alnum;
68  using boost::spirit::unused_type;
69 
70 
71  enum Action
72  {
73  KeepTPT,
74  KeepEDT,
75  DeleteTPT,
76  DeleteEDT
77  };
78 
79 
80  struct ParseResult
81  {
82  ParseResult() : action( KeepTPT )
83  {}
84 
85  void Initialize( void )
86  {
87  action = KeepTPT;
88  expression.children.clear();
89  expression.type = Operator( Uninitialized );
90  }
91 
92  Action action;
93 
94  Subtree expression;
95  };
96 
97 
98  struct Compiler
99  {
100  template < typename A, typename B = unused_type,
101  typename C = unused_type, typename D = unused_type >
102  struct result { typedef void type; };
103 
104  void operator()( ParseResult & parseResult, Action value ) const;
105 
106  void operator()( ParseResult & parseResult, Subtree & value ) const;
107 
108  void operator()( Subtree & ast, Node & node ) const;
109 
110  void operator()( Node & self, Node & left, Node & right,
111  Operator value ) const;
112 
113  void operator()( Node & self, Node & child, Operator value ) const;
114 
115  void operator()( Node & self, Node & primary ) const;
116 
117  void operator()( Node & self, Node & child, std::string & value )
118  const;
119 
120  void operator()( Leaf & self, std::string & name ) const;
121 
122  void operator()( Leaf & self, int value, size_t index ) const;
123  };
124 
125 
126  template < typename Iterator >
127  struct Grammar : grammar< Iterator, ParseResult(), space_type >
128  {
129  Grammar();
130 
131  rule< Iterator, ParseResult(), space_type > statement;
132 
133  rule< Iterator, Action(), space_type > action;
134 
135  rule< Iterator, Subtree(), space_type > condition;
136 
137  rule< Iterator, Node(), space_type > expression;
138 
139  rule< Iterator, Node(), space_type > primary_expr;
140 
141  rule< Iterator, Node(), space_type > function1;
142 
143  rule< Iterator, std::string(), space_type > identifier;
144 
145  rule< Iterator, Leaf(), space_type > leaf_operand;
146 
147  rule< Iterator, Leaf(), space_type > constant;
148 
149  rule< Iterator, Leaf(), space_type > variable;
150 
151  rule< Iterator, Node(), space_type > or_expr;
152 
153  rule< Iterator, Node(), space_type > and_expr;
154 
155  rule< Iterator, Node(), space_type > relation;
156 
157  rule< Iterator, Node(), space_type > addition;
158 
159  rule< Iterator, Node(), space_type > multiplication;
160 
161  rule< Iterator, Node(), space_type > unary_expr;
162 
163  rule< Iterator, Operator(), space_type > unary_op;
164 
165  rule< Iterator, Operator(), space_type > mult_op;
166 
167  rule< Iterator, Operator(), space_type > add_op;
168 
169  rule< Iterator, Operator(), space_type > rel_op;
170 
171  real_parser< double, strict_real_policies< double > > strict_double;
172 
173  function< Compiler > op;
174  };
175 
176 
177  template < typename Iterator >
178  Grammar< Iterator >::Grammar() : Grammar::base_type( statement )
179  {
180  statement = action[ op( _val, _1 ) ] >>
181  *( condition[ op( _val, _1 ) ] );
182 
183  action = lit( "keep" ) >>
184  ( lit( "tpt" )[ _val = KeepTPT ] |
185  lit( "edt" )[ _val = KeepEDT ] ) |
186  lit( "delete" ) >>
187  ( lit( "tpt" )[ _val = DeleteTPT ] |
188  lit( "edt" )[ _val = DeleteEDT ] );
189 
190  condition = lit( "if" ) >> expression[ op( _val, _1 ) ];
191 
192  expression %= or_expr;
193 
194  identifier %= raw[ lexeme[ alpha >> *( alnum | '_' ) ] ];
195 
196  primary_expr = function1[ _val = _1 ] |
197  lit( '(' ) >> expression[ op( _val, _1 ) ] >> lit( ')' ) |
198  leaf_operand[ _val = _1 ];
199 
200  leaf_operand %= constant | variable;
201 
202  constant %= strict_double | int_;
203 
204  variable = identifier[ op( _val, _1 ) ] >>
205  -( lit( '[' ) >> ( uint_[ op( _val, _1, 0 ) ] - lit( '0' ) ) >>
206  -( lit( ',' ) >> ( uint_[ op( _val, _1, 1 ) ] -
207  lit( '0' ) ) ) >> lit( ']' ) );
208 
209  function1 = ( identifier >> lit( '(' ) >> expression >> lit( ')' ) )
210  [ op( _val, _2, _1 ) ];
211 
212  or_expr = ( and_expr >> lit( '|' ) >> or_expr )
213  [ op( _val, _1, _2, Operator( Or, 1 ) ) ] |
214  and_expr[ _val = _1 ];
215 
216  and_expr = ( relation >> lit( '&' ) >> and_expr )
217  [ op( _val, _1, _2, Operator( And, 2 ) ) ] |
218  relation[ _val = _1 ];
219 
220  relation = ( addition >> rel_op >> addition )
221  [ op( _val, _1, _3, _2 ) ] |
222  addition[ _val = _1 ];
223 
224  addition = ( multiplication >> add_op >> addition )
225  [ op( _val, _1, _3, _2 ) ] |
226  multiplication[ _val = _1 ];
227 
228  multiplication = ( unary_expr >> mult_op >> multiplication )
229  [ op( _val, _1, _3, _2 ) ] |
230  unary_expr[ _val = _1 ];
231 
232  unary_expr = ( unary_op >> primary_expr )[ op( _val, _2, _1 ) ] |
233  primary_expr[ _val = _1 ];
234 
235  unary_op = lit( '-' )[ _val = Operator( UMinus, 6, true ) ] |
236  lit( '!' )[ _val = Operator( Not, 6, true ) ];
237 
238  mult_op = lit( '*' )[ _val = Operator( Mult, 5 ) ] |
239  lit( '/' )[ _val = Operator( Div, 5 ) ];
240 
241  add_op = lit( '+' )[ _val = Operator( Plus, 4 ) ] |
242  lit( '-' )[ _val = Operator( Minus, 4 ) ];
243 
244  rel_op = lit( "<=" )[ _val = Operator( LessEq, 3 ) ] |
245  lit( ">=" )[ _val = Operator( MoreEq, 3 ) ] |
246  lit( "!=" )[ _val = Operator( NotEq, 3 ) ] |
247  lit( '<' )[ _val = Operator( Less, 3 ) ] |
248  lit( '>' )[ _val = Operator( More, 3 ) ] |
249  lit( '=' )[ _val = Operator( Eq, 3 ) ];
250  }
251 }
252 
253 #endif
254 
255 #endif
256 
G4double condition(const G4ErrorSymMatrix &m)
void Initialize()
Definition: errprop.cc:96
const XML_Char * name
const XML_Char int const XML_Char * value