| 1 |
tim |
770 |
/* |
| 2 |
|
|
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
| 3 |
|
|
* |
| 4 |
|
|
* The University of Notre Dame grants you ("Licensee") a |
| 5 |
|
|
* non-exclusive, royalty free, license to use, modify and |
| 6 |
|
|
* redistribute this software in source and binary code form, provided |
| 7 |
|
|
* that the following conditions are met: |
| 8 |
|
|
* |
| 9 |
gezelter |
1390 |
* 1. Redistributions of source code must retain the above copyright |
| 10 |
tim |
770 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
|
|
* |
| 12 |
gezelter |
1390 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
tim |
770 |
* notice, this list of conditions and the following disclaimer in the |
| 14 |
|
|
* documentation and/or other materials provided with the |
| 15 |
|
|
* distribution. |
| 16 |
|
|
* |
| 17 |
|
|
* This software is provided "AS IS," without a warranty of any |
| 18 |
|
|
* kind. All express or implied conditions, representations and |
| 19 |
|
|
* warranties, including any implied warranty of merchantability, |
| 20 |
|
|
* fitness for a particular purpose or non-infringement, are hereby |
| 21 |
|
|
* excluded. The University of Notre Dame and its licensors shall not |
| 22 |
|
|
* be liable for any damages suffered by licensee as a result of |
| 23 |
|
|
* using, modifying or distributing the software or its |
| 24 |
|
|
* derivatives. In no event will the University of Notre Dame or its |
| 25 |
|
|
* licensors be liable for any lost revenue, profit or data, or for |
| 26 |
|
|
* direct, indirect, special, consequential, incidental or punitive |
| 27 |
|
|
* damages, however caused and regardless of the theory of liability, |
| 28 |
|
|
* arising out of the use of or inability to use software, even if the |
| 29 |
|
|
* University of Notre Dame has been advised of the possibility of |
| 30 |
|
|
* such damages. |
| 31 |
gezelter |
1390 |
* |
| 32 |
|
|
* SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your |
| 33 |
|
|
* research, please cite the appropriate papers when you publish your |
| 34 |
|
|
* work. Good starting points are: |
| 35 |
|
|
* |
| 36 |
|
|
* [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). |
| 37 |
|
|
* [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). |
| 38 |
|
|
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). |
| 39 |
|
|
* [4] Vardeman & Gezelter, in progress (2009). |
| 40 |
tim |
770 |
*/ |
| 41 |
|
|
|
| 42 |
|
|
#ifndef MDPARSER_SIMPLEPREPROCESSOR_HPP |
| 43 |
|
|
#define MDPARSER_SIMPLEPREPROCESSOR_HPP |
| 44 |
|
|
#include <iostream> |
| 45 |
|
|
#include <set> |
| 46 |
|
|
#include <fstream> |
| 47 |
tim |
912 |
#include <sstream> |
| 48 |
tim |
770 |
#include "utils/StringTokenizer.hpp" |
| 49 |
|
|
#include "utils/Trim.hpp" |
| 50 |
gezelter |
1390 |
#include "utils/OpenMDException.hpp" |
| 51 |
chuckv |
924 |
#include "utils/simError.h" |
| 52 |
tim |
912 |
|
| 53 |
chuckv |
924 |
|
| 54 |
tim |
770 |
/** |
| 55 |
|
|
* @class SimplePreprocessor |
| 56 |
|
|
* @brief A simple preprocessor. |
| 57 |
gezelter |
938 |
* @note only supports #include #ifdef, #ifndef, #endif, #define and #undef, c-like multiple line |
| 58 |
|
|
* comment is not supported, macro substitute is not supported. |
| 59 |
tim |
770 |
*/ |
| 60 |
gezelter |
1390 |
namespace OpenMD { |
| 61 |
tim |
770 |
class SimplePreprocessor { |
| 62 |
|
|
public: |
| 63 |
tim |
1024 |
bool preprocess(std::istream& myStream, const std::string& filename, int startingLine, ostream& os) { |
| 64 |
tim |
770 |
std::set<std::string> defineSet; |
| 65 |
|
|
std::stack<bool> ifStates; |
| 66 |
|
|
|
| 67 |
|
|
ifStates.push(true); |
| 68 |
tim |
1024 |
return doPreprocess(myStream, filename, startingLine, os, defineSet, ifStates); |
| 69 |
tim |
770 |
} |
| 70 |
|
|
|
| 71 |
|
|
private: |
| 72 |
tim |
1024 |
bool doPreprocess(std::istream& myStream, const std::string& filename, int startingLine, ostream& os, std::set<std::string>& defineSet, std::stack<bool>& ifStates) { |
| 73 |
|
|
//std::ifstream input(filename.c_str()); |
| 74 |
|
|
//if (!input.is_open()) { |
| 75 |
|
|
// std::stringstream ss; |
| 76 |
|
|
// ss << "Can not open " << filename << " for preprocessing\n"; |
| 77 |
|
|
// |
| 78 |
|
|
// sprintf(painCave.errMsg, |
| 79 |
|
|
// "Can not open (%s) for processing. \n" |
| 80 |
|
|
// "\tPlease check md file name syntax.\n", filename.c_str()); |
| 81 |
|
|
// |
| 82 |
|
|
// painCave.isFatal = 1; |
| 83 |
|
|
// simError(); |
| 84 |
|
|
// |
| 85 |
gezelter |
1390 |
// throw OpenMDException(ss.str()); |
| 86 |
tim |
1024 |
//} |
| 87 |
|
|
int lineNo = startingLine; |
| 88 |
tim |
770 |
os << "#line " << lineNo << " \"" << filename << "\"\n"; |
| 89 |
tim |
1024 |
const int bufferSize = 1024; |
| 90 |
|
|
char buffer[bufferSize]; |
| 91 |
|
|
while(myStream.getline(buffer, bufferSize)) { |
| 92 |
tim |
770 |
++lineNo; |
| 93 |
|
|
std::string line = trimLeftCopy(buffer); |
| 94 |
|
|
if (!line.empty() && line[0] == '#') { |
| 95 |
|
|
StringTokenizer tokenizer(line.substr(1, line.length())); |
| 96 |
|
|
std::vector<std::string> tokens = tokenizer.getAllTokens(); |
| 97 |
|
|
if (tokens.size() < 1 ) { |
| 98 |
|
|
return false; |
| 99 |
|
|
} |
| 100 |
|
|
std::string keyword = tokens[0]; |
| 101 |
|
|
if (tokens[0] == "endif") { |
| 102 |
|
|
ifStates.pop(); |
| 103 |
|
|
if (ifStates.empty()) { |
| 104 |
|
|
std::cout << "Error in preprocessing: endif \n"; |
| 105 |
|
|
return false; |
| 106 |
|
|
} |
| 107 |
|
|
os << std::endl; |
| 108 |
|
|
} else if (tokens.size() == 2) { |
| 109 |
|
|
if (tokens[0] == "include") { |
| 110 |
|
|
SimplePreprocessor subPreprocessor; |
| 111 |
|
|
std::string includeFilename = tokens[1]; |
| 112 |
|
|
includeFilename = includeFilename.substr(1, includeFilename.length() -2); |
| 113 |
tim |
1024 |
std::ifstream includeStream(includeFilename.c_str()); |
| 114 |
|
|
if (!includeStream.is_open()) { |
| 115 |
|
|
std::stringstream ss; |
| 116 |
chuckv |
1245 |
ss << "Can not open " << includeFilename << " for preprocessing\n"; |
| 117 |
gezelter |
1390 |
throw OpenMDException(ss.str()); |
| 118 |
tim |
1024 |
} |
| 119 |
|
|
|
| 120 |
|
|
bool ret = subPreprocessor.doPreprocess(includeStream, includeFilename, 1, os, defineSet, ifStates); |
| 121 |
tim |
770 |
if (!ret) { |
| 122 |
|
|
std::cout << "Error in preprocessing\n"; |
| 123 |
|
|
return false; |
| 124 |
|
|
} |
| 125 |
|
|
os << "#line " << lineNo << " \"" << filename << "\"\n"; |
| 126 |
|
|
} else if (tokens[0] == "define") { |
| 127 |
|
|
defineSet.insert(tokens[1]); |
| 128 |
|
|
os << std::endl; |
| 129 |
|
|
} else if (tokens[0] == "undef") { |
| 130 |
|
|
defineSet.erase(tokens[1]); |
| 131 |
|
|
os << std::endl; |
| 132 |
|
|
} else if (tokens[0] == "ifdef") { |
| 133 |
|
|
if (defineSet.find(tokens[1]) != defineSet.end() ) { |
| 134 |
|
|
ifStates.push(true); |
| 135 |
|
|
} else { |
| 136 |
|
|
ifStates.push(false); |
| 137 |
|
|
} |
| 138 |
|
|
os << std::endl; |
| 139 |
|
|
} else if (tokens[0] == "ifndef") { |
| 140 |
|
|
if (defineSet.find(tokens[1]) == defineSet.end() ) { |
| 141 |
|
|
ifStates.push(true); |
| 142 |
|
|
} else { |
| 143 |
|
|
ifStates.push(false); |
| 144 |
|
|
} |
| 145 |
|
|
os << std::endl; |
| 146 |
|
|
} else { |
| 147 |
gezelter |
938 |
std::cout << tokens[0] << " is not supported (yet)." << std::endl; |
| 148 |
tim |
770 |
return false; |
| 149 |
|
|
} |
| 150 |
|
|
}else { |
| 151 |
|
|
return false; |
| 152 |
|
|
} |
| 153 |
|
|
|
| 154 |
|
|
}else if (ifStates.top()){ |
| 155 |
|
|
os << buffer << std::endl; |
| 156 |
|
|
} |
| 157 |
|
|
|
| 158 |
|
|
} |
| 159 |
|
|
|
| 160 |
|
|
return true; |
| 161 |
|
|
} |
| 162 |
|
|
private: |
| 163 |
|
|
|
| 164 |
|
|
}; |
| 165 |
|
|
|
| 166 |
|
|
} |
| 167 |
|
|
#endif |