File indexing completed on 2025-01-31 09:21:51
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 #ifdef CEXMC_USE_CUSTOM_FILTER
0045
0046 #include <fstream>
0047 #include <string>
0048 #include "CexmcCustomFilterEval.hh"
0049 #include "CexmcException.hh"
0050
0051
0052 CexmcCustomFilterEval::CexmcCustomFilterEval( const G4String & sourceFileName,
0053 const CexmcEventFastSObject * evFastSObject,
0054 const CexmcEventSObject * evSObject ) :
0055 astEval( evFastSObject, evSObject )
0056 {
0057 std::string command;
0058 std::ifstream sourceFile( sourceFileName );
0059
0060 if ( ! sourceFile )
0061 throw CexmcException( CexmcCFBadSource );
0062
0063 bool commandIsPending( false );
0064
0065 while ( ! sourceFile.eof() )
0066 {
0067 std::string line;
0068 std::getline( sourceFile, line );
0069
0070 size_t commentStartPos( line.find_first_of( '#' ) );
0071 if ( commentStartPos != std::string::npos )
0072 line.erase( commentStartPos );
0073
0074 if ( line.empty() ||
0075 line.find_first_not_of( " \t" ) == std::string::npos )
0076 {
0077 if ( commandIsPending )
0078 {
0079 sourceFile.close();
0080 throw CexmcException( CexmcCFParseError );
0081 }
0082 continue;
0083 }
0084
0085 command += line;
0086
0087 size_t length( command.length() );
0088
0089 if ( command[ length - 1 ] == '\\' )
0090 {
0091 command.erase( length - 1 );
0092 commandIsPending = true;
0093 continue;
0094 }
0095
0096 CexmcCustomFilter::ParseResult curParseResult;
0097
0098 std::string::const_iterator begin( command.begin() );
0099 std::string::const_iterator end( command.end() );
0100
0101 try
0102 {
0103 if ( ! CexmcCustomFilter::phrase_parse( begin, end, grammar,
0104 CexmcCustomFilter::space, curParseResult ) ||
0105 begin != end )
0106 {
0107 throw CexmcException( CexmcCFParseError );
0108 }
0109 }
0110 catch ( ... )
0111 {
0112 sourceFile.close();
0113 throw;
0114 }
0115
0116 #ifdef CEXMC_DEBUG_CF
0117 G4cout << "Parsed expression AST:" << G4endl;
0118 curParseResult.expression.Print();
0119 #endif
0120
0121 switch ( curParseResult.action )
0122 {
0123 case CexmcCustomFilter::KeepTPT :
0124 case CexmcCustomFilter::DeleteTPT :
0125 parseResultTPT.push_back( curParseResult );
0126 break;
0127 case CexmcCustomFilter::KeepEDT :
0128 case CexmcCustomFilter::DeleteEDT :
0129 parseResultEDT.push_back( curParseResult );
0130 break;
0131 default :
0132 break;
0133 }
0134
0135 command = "";
0136 commandIsPending = false;
0137 }
0138
0139 sourceFile.close();
0140
0141 if ( commandIsPending )
0142 throw CexmcException( CexmcCFParseError );
0143 }
0144
0145
0146 void CexmcCustomFilterEval::SetAddressedData(
0147 const CexmcEventFastSObject * evFastSObject,
0148 const CexmcEventSObject * evSObject )
0149 {
0150 astEval.SetAddressedData( evFastSObject, evSObject );
0151
0152 for ( ParseResultVector::iterator k( parseResultTPT.begin() );
0153 k != parseResultTPT.end(); ++k )
0154 {
0155 if ( evFastSObject == NULL || evSObject == NULL )
0156 astEval.ResetAddressBinding( k->expression );
0157 else
0158 astEval.BindAddresses( k->expression );
0159 }
0160 for ( ParseResultVector::iterator k( parseResultEDT.begin() );
0161 k != parseResultEDT.end(); ++k )
0162 {
0163 if ( evFastSObject == NULL || evSObject == NULL )
0164 astEval.ResetAddressBinding( k->expression );
0165 else
0166 astEval.BindAddresses( k->expression );
0167 }
0168 }
0169
0170
0171 bool CexmcCustomFilterEval::EvalTPT( void ) const
0172 {
0173 for ( ParseResultVector::const_iterator k( parseResultTPT.begin() );
0174 k != parseResultTPT.end(); ++k )
0175 {
0176 if ( astEval( k->expression ) )
0177 return k->action == CexmcCustomFilter::KeepTPT;
0178 }
0179
0180 return true;
0181 }
0182
0183
0184 bool CexmcCustomFilterEval::EvalEDT( void ) const
0185 {
0186 for ( ParseResultVector::const_iterator k( parseResultEDT.begin() );
0187 k != parseResultEDT.end(); ++k )
0188 {
0189 if ( astEval( k->expression ) )
0190 return k->action == CexmcCustomFilter::KeepEDT;
0191 }
0192
0193 return true;
0194 }
0195
0196
0197 #endif
0198