ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-2.0/src/mdParser/SimplePreprocessor.hpp
Revision: 2469
Committed: Fri Dec 2 15:38:03 2005 UTC (18 years, 7 months ago) by tim
File size: 6311 byte(s)
Log Message:
End of the Link --> List
Return of the Oject-Oriented
replace yacc/lex parser with antlr parser

File Contents

# Content
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 "utils/StringTokenizer.hpp"
48 #include "utils/Trim.hpp"
49 /**
50 * @class SimplePreprocessor
51 * @brief A simple preprocessor.
52 * @note only support #include #ifdef, #ifndef, #endif, #define and #undef, c-like multiple line
53 * comment is not support, macro substitude is not support.
54 */
55 namespace oopse {
56 class SimplePreprocessor {
57 public:
58 bool preprocess(const std::string& filename, ostream& os) {
59 std::set<std::string> defineSet;
60 std::stack<bool> ifStates;
61
62 ifStates.push(true);
63 return doPreprocess(filename, os, defineSet, ifStates);
64 }
65
66 private:
67 bool doPreprocess(const std::string& filename, ostream& os, std::set<std::string>& defineSet, std::stack<bool>& ifStates) {
68 std::ifstream input(filename.c_str());
69
70 int lineNo =1;
71 os << "#line " << lineNo << " \"" << filename << "\"\n";
72 while(input.getline(buffer, bufferSize)) {
73 ++lineNo;
74 std::string line = trimLeftCopy(buffer);
75 if (!line.empty() && line[0] == '#') {
76 StringTokenizer tokenizer(line.substr(1, line.length()));
77 std::vector<std::string> tokens = tokenizer.getAllTokens();
78 if (tokens.size() < 1 ) {
79 return false;
80 }
81 std::string keyword = tokens[0];
82 if (tokens[0] == "endif") {
83 ifStates.pop();
84 if (ifStates.empty()) {
85 std::cout << "Error in preprocessing: endif \n";
86 return false;
87 }
88 os << std::endl;
89 } else if (tokens.size() == 2) {
90 if (tokens[0] == "include") {
91 SimplePreprocessor subPreprocessor;
92 std::string includeFilename = tokens[1];
93 includeFilename = includeFilename.substr(1, includeFilename.length() -2);
94 bool ret = subPreprocessor.doPreprocess(includeFilename, os, defineSet, ifStates);
95 if (!ret) {
96 std::cout << "Error in preprocessing\n";
97 return false;
98 }
99 os << "#line " << lineNo << " \"" << filename << "\"\n";
100 } else if (tokens[0] == "define") {
101 defineSet.insert(tokens[1]);
102 os << std::endl;
103 } else if (tokens[0] == "undef") {
104 defineSet.erase(tokens[1]);
105 os << std::endl;
106 } else if (tokens[0] == "ifdef") {
107 if (defineSet.find(tokens[1]) != defineSet.end() ) {
108 ifStates.push(true);
109 } else {
110 ifStates.push(false);
111 }
112 os << std::endl;
113 } else if (tokens[0] == "ifndef") {
114 if (defineSet.find(tokens[1]) == defineSet.end() ) {
115 ifStates.push(true);
116 } else {
117 ifStates.push(false);
118 }
119 os << std::endl;
120 } else {
121 std::cout << tokens[0] << " is not support" << std::endl;
122 return false;
123 }
124 }else {
125 return false;
126 }
127
128 }else if (ifStates.top()){
129 os << buffer << std::endl;
130 }
131
132 }
133
134 return true;
135 }
136 private:
137
138 const static int bufferSize = 1024;
139 char buffer[bufferSize];
140 };
141
142 }
143 #endif