| 1 |
/* |
| 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 |
* 1. Acknowledgement of the program authors must be made in any |
| 10 |
* publication of scientific results based in part on use of the |
| 11 |
* program. An acceptable form of acknowledgement is citation of |
| 12 |
* the article in which the program was described (Matthew |
| 13 |
* A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher |
| 14 |
* J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented |
| 15 |
* Parallel Simulation Engine for Molecular Dynamics," |
| 16 |
* J. Comput. Chem. 26, pp. 252-271 (2005)) |
| 17 |
* |
| 18 |
* 2. Redistributions of source code must retain the above copyright |
| 19 |
* notice, this list of conditions and the following disclaimer. |
| 20 |
* |
| 21 |
* 3. Redistributions in binary form must reproduce the above copyright |
| 22 |
* notice, this list of conditions and the following disclaimer in the |
| 23 |
* documentation and/or other materials provided with the |
| 24 |
* distribution. |
| 25 |
* |
| 26 |
* This software is provided "AS IS," without a warranty of any |
| 27 |
* kind. All express or implied conditions, representations and |
| 28 |
* warranties, including any implied warranty of merchantability, |
| 29 |
* fitness for a particular purpose or non-infringement, are hereby |
| 30 |
* excluded. The University of Notre Dame and its licensors shall not |
| 31 |
* be liable for any damages suffered by licensee as a result of |
| 32 |
* using, modifying or distributing the software or its |
| 33 |
* derivatives. In no event will the University of Notre Dame or its |
| 34 |
* licensors be liable for any lost revenue, profit or data, or for |
| 35 |
* direct, indirect, special, consequential, incidental or punitive |
| 36 |
* damages, however caused and regardless of the theory of liability, |
| 37 |
* arising out of the use of or inability to use software, even if the |
| 38 |
* University of Notre Dame has been advised of the possibility of |
| 39 |
* such damages. |
| 40 |
*/ |
| 41 |
|
| 42 |
#ifndef MDPARSER_SIMPLEPREPROCESSOR_HPP |
| 43 |
#define MDPARSER_SIMPLEPREPROCESSOR_HPP |
| 44 |
#include <iostream> |
| 45 |
#include <set> |
| 46 |
#include <fstream> |
| 47 |
#include <sstream> |
| 48 |
#include "utils/StringTokenizer.hpp" |
| 49 |
#include "utils/Trim.hpp" |
| 50 |
#include "utils/OOPSEException.hpp" |
| 51 |
#include "utils/simError.h" |
| 52 |
|
| 53 |
|
| 54 |
/** |
| 55 |
* @class SimplePreprocessor |
| 56 |
* @brief A simple preprocessor. |
| 57 |
* @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 |
*/ |
| 60 |
namespace oopse { |
| 61 |
class SimplePreprocessor { |
| 62 |
public: |
| 63 |
bool preprocess(const std::string& filename, ostream& os) { |
| 64 |
std::set<std::string> defineSet; |
| 65 |
std::stack<bool> ifStates; |
| 66 |
|
| 67 |
ifStates.push(true); |
| 68 |
return doPreprocess(filename, os, defineSet, ifStates); |
| 69 |
} |
| 70 |
|
| 71 |
private: |
| 72 |
bool doPreprocess(const std::string& filename, 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 |
throw OOPSEException(ss.str()); |
| 86 |
} |
| 87 |
int lineNo =1; |
| 88 |
os << "#line " << lineNo << " \"" << filename << "\"\n"; |
| 89 |
while(input.getline(buffer, bufferSize)) { |
| 90 |
++lineNo; |
| 91 |
std::string line = trimLeftCopy(buffer); |
| 92 |
if (!line.empty() && line[0] == '#') { |
| 93 |
StringTokenizer tokenizer(line.substr(1, line.length())); |
| 94 |
std::vector<std::string> tokens = tokenizer.getAllTokens(); |
| 95 |
if (tokens.size() < 1 ) { |
| 96 |
return false; |
| 97 |
} |
| 98 |
std::string keyword = tokens[0]; |
| 99 |
if (tokens[0] == "endif") { |
| 100 |
ifStates.pop(); |
| 101 |
if (ifStates.empty()) { |
| 102 |
std::cout << "Error in preprocessing: endif \n"; |
| 103 |
return false; |
| 104 |
} |
| 105 |
os << std::endl; |
| 106 |
} else if (tokens.size() == 2) { |
| 107 |
if (tokens[0] == "include") { |
| 108 |
SimplePreprocessor subPreprocessor; |
| 109 |
std::string includeFilename = tokens[1]; |
| 110 |
includeFilename = includeFilename.substr(1, includeFilename.length() -2); |
| 111 |
bool ret = subPreprocessor.doPreprocess(includeFilename, os, defineSet, ifStates); |
| 112 |
if (!ret) { |
| 113 |
std::cout << "Error in preprocessing\n"; |
| 114 |
return false; |
| 115 |
} |
| 116 |
os << "#line " << lineNo << " \"" << filename << "\"\n"; |
| 117 |
} else if (tokens[0] == "define") { |
| 118 |
defineSet.insert(tokens[1]); |
| 119 |
os << std::endl; |
| 120 |
} else if (tokens[0] == "undef") { |
| 121 |
defineSet.erase(tokens[1]); |
| 122 |
os << std::endl; |
| 123 |
} else if (tokens[0] == "ifdef") { |
| 124 |
if (defineSet.find(tokens[1]) != defineSet.end() ) { |
| 125 |
ifStates.push(true); |
| 126 |
} else { |
| 127 |
ifStates.push(false); |
| 128 |
} |
| 129 |
os << std::endl; |
| 130 |
} else if (tokens[0] == "ifndef") { |
| 131 |
if (defineSet.find(tokens[1]) == defineSet.end() ) { |
| 132 |
ifStates.push(true); |
| 133 |
} else { |
| 134 |
ifStates.push(false); |
| 135 |
} |
| 136 |
os << std::endl; |
| 137 |
} else { |
| 138 |
std::cout << tokens[0] << " is not supported (yet)." << std::endl; |
| 139 |
return false; |
| 140 |
} |
| 141 |
}else { |
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
}else if (ifStates.top()){ |
| 146 |
os << buffer << std::endl; |
| 147 |
} |
| 148 |
|
| 149 |
} |
| 150 |
|
| 151 |
return true; |
| 152 |
} |
| 153 |
private: |
| 154 |
|
| 155 |
const static int bufferSize = 1024; |
| 156 |
char buffer[bufferSize]; |
| 157 |
}; |
| 158 |
|
| 159 |
} |
| 160 |
#endif |