PhoenixHPCProxy  0.8.0
Lightweight HPC proxy
Loading...
Searching...
No Matches
HeaderParser.cpp
Go to the documentation of this file.
1/***************************************
2 Auteur : Pierre Aubert
3 Mail : pierre.aubert@lapp.in2p3.fr
4 Licence : CeCILL-C
5****************************************/
6
7#include "HeaderParser.h"
8
13
15
20
25
27
31 copyHeaderParser(other);
32 return *this;
33}
34
36
39 return p_vecSource;
40}
41
43
48
50
53 if(!p_run) return false;
54 p_lastDocString = "";
55 p_otherCode = "";
56 p_currentSource.setName(p_parser->getFileName().getFileName());
57 p_parser->skipWhiteSpace();
58 //To parse the file we need to read char by char until we get something we know
59 while(!p_parser->isEndOfFile() && p_run){ //If we are not at the end of the file
60 PFunction function;
61 if(parseDocString()){}
62 else if(p_parser->isMatch("//")){p_parser->getUntilKeyWithoutPatern("\n");} //Skip comment
63 else if(p_parser->isMatch("/*")){p_parser->getUntilKeyWithoutPatern("*/");} //Skip comment
64 else if(parseMacro()){}
65 else if(parseFunction(function)){
66 p_currentSource.getVecFunction().push_back(function);
67 }else{
69 }
70 p_parser->skipWhiteSpace();
71 }
73 return true;
74}
75
78 //Save the current source
79 p_parser->setWhiteSpace(" \t\n");
80 p_parser->setSeparator(",;{}()");
81}
82
87
92
94
97 //If nothing is known I need to save the current char in the other code
98 char ch = p_parser->getCurrentCh();
99 if(ch == '~'){ch = ' ';}
100 p_otherCode += ch;
101 p_parser->getNextChar();
102}
103
106 if(p_otherCode != ""){
107 PFunction other;
109 p_currentSource.getVecFunction().push_back(other);
110 p_otherCode = "";
111 }
112}
113
115
118 if(!p_parser->isMatch("#")){return false;}
119 PString macro("#" + p_parser->getUntilKeyWithoutPatern("\n") + "\n");
120 PFunction func;
121 func.setOtherCode(macro);
122 p_currentSource.getVecFunction().push_back(func);
123 return true;
124}
125
127
130 if(!p_parser->isMatch("///")){return false;}
131 p_lastDocString = p_parser->getUntilKeyWithoutPatern("\n");
132
133 return true;
134}
135
137
141 p_parser->pushPosition();
142 PString templateDef(""), returnType("");
143 parseTemplateDef(templateDef);
144 parseType(returnType);
145 std::cerr << "HeaderParser::parseFunction : returnType = '"<<returnType<<"'" << std::endl;
146 PString functionName(p_parser->getNextToken());
147 if(functionName == ""){return false;} //This is not a function
149 p_lastDocString = "";
150 //Let's get the arguments of the function
151 if(!p_parser->isMatch("(")){return false;} //This is not a function
152 function.setName(functionName);
153 function.setOutputType(returnType);
154 while(!p_parser->isEndOfFile() && !p_parser->isMatch(")") && p_run){
155 PArgument arg;
156 if(parseDocString()){}
157 else if(p_parser->isMatch("//")){p_parser->getUntilKeyWithoutPatern("\n");} //Skip comment
158 else if(p_parser->isMatch("/*")){p_parser->getUntilKeyWithoutPatern("*/");} //Skip comment
159 else if(parseArgument(arg)){
160 function.getVecArgument().push_back(arg);
161 if(!p_parser->isMatch(",") && !isMatchRewind(")")){
162 errorAt();
163 std::cerr << "HeaderParser::parseFunction : expect ',' or ')' after function definition '"<<functionName<<"'" << std::endl;
164 return true;
165 }
166 }else{
167 errorAt();
168 std::cerr << "HeaderParser::parseFunction : cannot parse attribute" << std::endl;
169 return true;
170 }
171 p_parser->skipWhiteSpace();
172
173 }
174 if(!p_parser->isMatch(";")){
175 errorAt();
176 std::cerr << "HeaderParser::parseFunction : expect ';' after function definition '"<<functionName<<"'" << std::endl;
177 return true;
178 }
180 return true;
181
182}
183
185
187void HeaderParser::parseTemplateDef(PString & templateDef){
188 if(!p_parser->isMatchToken("template")){return;}
189
190 if(!p_parser->isMatch("<")){
191 errorAt();
192 std::cerr << "HeaderParser::parseTemplateDef : expect '<' after 'template' token" << std::endl;
193 return;
194 }
195 templateDef += "template<";
196 bool isTemplate(true);
197 while(!p_parser->isEndOfFile() && isTemplate && p_run){
198 templateDef += p_parser->getNextToken(); //Some typename
199 templateDef += " ";
200 templateDef += p_parser->getNextToken(); //Some T
201 if(p_parser->isMatch(">")){isTemplate = false;}
202 else if(!p_parser->isMatch(",")){
203 errorAt();
204 std::cerr << "HeaderParser::parseTemplateDef : expect '>' or ',' after template definition '"<<templateDef<<"'" << std::endl;
205 return;
206 }
207 }
208 templateDef += ">";
209}
210
212
215bool isPtrRef(const PString & str){
216 return str.find("&*") || str == "__restrict__";
217}
218
220
224 if(p_parser->isMatchRewind(")")){return true;}
225 PString type("");
226 parseType(type);
227
228 PString varName(""), ptrRef(""), tmpPtrRef("");
229
230 tmpPtrRef = p_parser->getNextToken();
231
232 if(isPtrRef(tmpPtrRef)){
233 do{
234 ptrRef += tmpPtrRef;
235 tmpPtrRef = p_parser->getNextToken();
236// if(tmpPtrRef == "__restrict__"){ptrRef += " ";}
237
238 }while(isPtrRef(tmpPtrRef));
239
240 varName = tmpPtrRef;
241 }else{
242 ptrRef = tmpPtrRef;
243 varName = ptrRef;
244 ptrRef = "";
245 }
246 std::cerr << "HeaderParser::parseArgument : type = '"<<type<<", ptrRef = '"<<ptrRef<<"', varName = '"<<varName<<"'" << std::endl;
247 PString defaultValue("");
248 if(p_parser->isMatch("=")){ //Some default value
249 defaultValue = p_parser->getNextToken();
250 //If double quote are not token, even default strings are single token
251 p_parser->pushPosition();
252 PString nexToken(p_parser->getNextToken());
253 if(nexToken == "("){ //We call a function/constructor
254 defaultValue += p_parser->getUntilKeyWithoutPaternRecurse(")", "(");
255 }else{
256 p_parser->popPosition(); //It was not a function/constructor call
257 }
258 }
259 arg.setName(varName);
260 arg.setPtrRef(ptrRef);
261 arg.setType(type);
262 arg.setDefaultValue(defaultValue);
263 return true;
264}
265
267
269void HeaderParser::parseType(PString & type){
270 type = "";
271 PString token(p_parser->getNextToken());
272 while(token == "const" || token == "unsigned" || token == "long"){
273 type += token + " ";
274 token = p_parser->getNextToken();
275 }
276 type += token;
277}
278
279
280
bool isPtrRef(const PString &str)
Say if the given string is a pointer or a reference.
HeaderParser & operator=(const HeaderParser &other)
Definition of equal operator of HeaderParser.
virtual ~HeaderParser()
Destructor of HeaderParser.
virtual bool parseFile()
Parse the input file.
void playOtherCode()
Add the other code parsed into the current PSource.
virtual void preLoadFile()
Initialisation to be done just before loading a file.
virtual void postLoadFile()
Initialisation to be done just after loading a file.
PString p_otherCode
Other code which is not a function prototype.
PString p_lastDocString
Last documentation string.
HeaderParser()
Default constructor of HeaderParser.
PSource p_currentSource
Current source to be parsed.
bool parseMacro()
Parse a macro.
void parseTemplateDef(PString &templateDef)
Parse a template definition.
bool parseArgument(PArgument &argument)
Parse a PAttribute.
PVecSource p_vecSource
Vector of parsed PSource.
bool parseDocString()
Parse a doc string.
void parseType(PString &type)
Parse a data type.
void incrementCurrentChar()
Increment current char position.
const PVecSource & getVecSource() const
Get the parsed vector of PSource.
bool parseFunction(PFunction &function)
Parse a PFunction.
void initialisationHeaderParser()
Initialisation function of the class HeaderParser.
void copyHeaderParser(const HeaderParser &other)
Copy function of HeaderParser.
Parameter of a function.
Definition PRepr.h:14
void setPtrRef(const PString &ptrRef)
Sets the ptrRef of the PArgument.
Definition PRepr.cpp:60
void setType(const PString &type)
Sets the type of the PArgument.
Definition PRepr.cpp:46
void setName(const PString &name)
Sets the name of the PArgument.
Definition PRepr.cpp:39
void setDefaultValue(const PString &defaultValue)
Sets the defaultValue of the PArgument.
Definition PRepr.cpp:53
Function prototype.
Definition PRepr.h:46
void setName(const PString &name)
Sets the name of the PFunction.
Definition PRepr.cpp:159
void setOutputType(const PString &outputType)
Sets the outputType of the PFunction.
Definition PRepr.cpp:180
void setDocString(const PString &docString)
Sets the docString of the PFunction.
Definition PRepr.cpp:166
void setOtherCode(const PString &otherCode)
Sets the otherCode of the PFunction.
Definition PRepr.cpp:194
const std::vector< PArgument > & getVecArgument() const
Gets the vecArgument of the PFunction.
Definition PRepr.cpp:229
std::vector< PSource > PVecSource