Commit 73f89f10 authored by Pierre Aubert's avatar Pierre Aubert
Browse files

The parsing an dthe basic code generation of basic input parsing is ok

parent 6bac8354
project(Phoenix)
cmake_minimum_required(VERSION 2.8)
add_subdirectory(TEST_BASE_HEADER)
project(Phoenix)
cmake_minimum_required(VERSION 2.8)
//Comment
void print_arch(const char* str);
......@@ -18,31 +18,157 @@ std::string cpp_licenceSaveStr(){
body += "\tLicence : CeCILL-C\n";
body += "****************************************/\n\n\n";
body += "//Warning : this file has been generated automatically by the phoenix_hdf5 program\n";
body += "//You can find it at https://gitlab.in2p3.fr/CTA-LAPP/PHOENIX_LIBS/PhoenixHDF5\n";
body += "//Warning : this file has been generated automatically by the phoenix_hpc_proxy program\n";
body += "//You can find it at https://gitlab.in2p3.fr/CTA-LAPP/PHOENIX_LIBS/PhoenixHPCProxy\n";
body += "//Do NOT modify it\n\n\n";
return body;
}
///Get the handle name of the given function
/** @param functionName : function name
* @return corresponding function name
*/
std::string getHandleFunction(const std::string & functionName){
return "handle_" + functionName;
}
///Get the handle type name of the given function
/** @param functionName : function name
* @return corresponding function name
*/
std::string getHandleFunctionType(const std::string & functionName){
return "FunctionTypehandle_" + functionName;
}
///Save the function prototype as an inline function
/** @param function : PFunction to be used
* @return corresponding string
*/
std::string cpp_backendTableHeaderInline(PFunction & function){
if(function.getOtherCode() != ""){return function.getOtherCode();}
std::string body("");
if(function.getDocString() != ""){body += function.getDocString();}
if(function.getTemplateDef() != ""){body += function.getTemplateDef();}
body += "inline " + function.getOutputType() + " " + function.getName() + "(";
bool isComma(false);
PVecArgument & vecArg = function.getVecArgument();
for(PVecArgument::iterator it(vecArg.begin()); it != vecArg.end(); ++it){
if(isComma){body += ", ";}
body += it->getType() + " ";
if(it->getPtrRef() != ""){body += it->getPtrRef() + " ";}
body += it->getName();
if(it->getDefaultValue() != ""){body += " = " + it->getDefaultValue();}
isComma = true;
}
body += "){\n";
isComma = false;
body += "\t"+getHandleFunction(function.getName())+"(";
for(PVecArgument::iterator it(vecArg.begin()); it != vecArg.end(); ++it){
if(isComma){body += ", ";}
body += it->getName();
isComma = true;
}
body += ");\n";
body += "}\n\n";
return body;
}
///Save the function prototype as an inline function
/** @param function : PFunction to be used
* @return corresponding string
*/
std::string cpp_backendTableHeaderHandleType(PFunction & function){
if(!repr_isFunction(function)){return "";}
std::string body("");
body += "typedef " + function.getOutputType() + "(*" + getHandleFunctionType(function.getName()) + ")(";
bool isComma(false);
PVecArgument & vecArg = function.getVecArgument();
for(PVecArgument::iterator it(vecArg.begin()); it != vecArg.end(); ++it){
if(isComma){body += ", ";}
body += it->getType();
if(it->getPtrRef() != ""){body += " " + it->getPtrRef();}
isComma = true;
}
body += ");\n";
return body;
}
///Save the header of the given PSource
/** @param source : PSource to be used
* @return corresponding string
*/
std::string cpp_backendHeader(PSource & source){
std::string body(cpp_licenceSaveStr());
std::string baseMacro("__" + strToUpper(eraseExtension(source.getName())) + "_H__");
body += "#ifndef " + baseMacro + "\n";
body += "#define " + baseMacro + "\n\n";
body += "#include <iostream>\n\n\n";
PVecFunction & vecFunction = source.getVecFunction();
for(PVecFunction::iterator it(vecFunction.begin()); it != vecFunction.end(); ++it){
body += cpp_backendTableHeaderHandleType(*it);
}
body += "\n\n";
for(PVecFunction::iterator it(vecFunction.begin()); it != vecFunction.end(); ++it){
if(!repr_isFunction(*it)){continue;}
body += "extern " + getHandleFunctionType(it->getName()) + " " + getHandleFunction(it->getName()) + ";\n";
}
body += "\n\n";
for(PVecFunction::iterator it(vecFunction.begin()); it != vecFunction.end(); ++it){
body += cpp_backendTableHeaderInline(*it);
}
body += "#endif\n\n";
return body;
}
///Save the source of the given PSource
/** @param source : PSource to be used
* @return corresponding string
*/
std::string cpp_backendSource(PSource & source){
std::string body(cpp_licenceSaveStr());
body += "#include <stdio.h>\n";
body += "#include <stdlib.h>\n";
body += "#include <dlfcn.h>\n\n";
body += "#include \""+eraseExtension(source.getName())+".h\"\n\n";
PVecFunction & vecFunction = source.getVecFunction();
for(PVecFunction::iterator it(vecFunction.begin()); it != vecFunction.end(); ++it){
if(!repr_isFunction(*it)){continue;}
body += getHandleFunctionType(it->getName()) + " " + getHandleFunction(it->getName()) + ";\n";
}
body += "\n\n";
body += "///Update the all the function handles of the file\n";
body += "/**\t@param handle : pointer to loaded library\n";
body += "*/\n";
body += "void update_function_handle(void * handle){\n";
for(PVecFunction::iterator it(vecFunction.begin()); it != vecFunction.end(); ++it){
if(!repr_isFunction(*it)){continue;}
//TODO : find a way to demangle the function name
body += "\t"+getHandleFunction(it->getName())+" = ("+getHandleFunctionType(it->getName())+")(dlsym(handle, \""+it->getName()+"\"));\n";
}
body += "}\n\n";
return body;
}
///Save a vector of PSource in the output directory
/** @param source : source to be saved
* @param outputDir : output directory where to save PSource
* @return true on success, false otherwise
*/
bool cpp_backend(PSource & source, const std::string & outputDir){
// std::string headerSrc(cpp_backendHeader(source));
// std::string sourceSrc(cpp_backendSource(source));
// std::string baseFileName(eraseExtension(source.getName()));
// std::string outputHeaderFile(outputDir + "/" + baseFileName + ".h");
// std::string outputSourceFile(outputDir + "/" + baseFileName + ".cpp");
// if(!saveFileContent(outputHeaderFile, headerSrc)){
// std::cerr << "cpp_backend : cannot save header file '"<<outputHeaderFile<<"'" << std::endl;
// return false;
// }
// if(!saveFileContent(outputSourceFile, sourceSrc)){
// std::cerr << "cpp_backend : cannot save source file '"<<outputSourceFile<<"'" << std::endl;
// return false;
// }
std::string headerSrc(cpp_backendHeader(source));
std::string sourceSrc(cpp_backendSource(source));
std::string baseFileName(eraseExtension(source.getName()));
std::string outputHeaderFile(outputDir + "/" + baseFileName + ".h");
std::string outputSourceFile(outputDir + "/" + baseFileName + ".cpp");
if(!saveFileContent(outputHeaderFile, headerSrc)){
std::cerr << "cpp_backend : cannot save header file '"<<outputHeaderFile<<"'" << std::endl;
return false;
}
if(!saveFileContent(outputSourceFile, sourceSrc)){
std::cerr << "cpp_backend : cannot save source file '"<<outputSourceFile<<"'" << std::endl;
return false;
}
return true;
}
......
......@@ -130,7 +130,6 @@ bool HeaderParser::parseFunction(PFunction & function){
parseTemplateDef(templateDef);
parseType(returnType);
std::string functionName(p_parser->getNextToken());
if(functionName == ""){return false;} //This is not a function
function.setDocString(p_lastDocString);
......@@ -138,7 +137,7 @@ bool HeaderParser::parseFunction(PFunction & function){
//Let's get the arguments of the function
if(!p_parser->isMatch("(")){return false;} //This is not a function
function.setName(functionName);
function.setOutputType(returnType);
while(!p_parser->isEndOfFile() && !p_parser->isMatch(")") && p_run){
PArgument arg;
if(parseDocString()){}
......@@ -152,6 +151,11 @@ bool HeaderParser::parseFunction(PFunction & function){
}
p_parser->skipWhiteSpace();
}
if(!p_parser->isMatch(";")){
errorAt();
std::cerr << "HeaderParser::parseFunction : expect ';' after function definition '"<<functionName<<"'" << std::endl;
return true;
}
playOtherCode();
return true;
......@@ -228,10 +232,13 @@ bool HeaderParser::parseArgument(PArgument & arg){
/** @param[out] type : type to be parsed
*/
void HeaderParser::parseType(std::string & type){
type = p_parser->getNextToken();
if(type == "unsigned"){
type += " " + p_parser->getNextToken();
type = "";
std::string token(p_parser->getNextToken());
while(token == "const" || token == "unsigned"){
type += token + " ";
token = p_parser->getNextToken();
}
type += token;
}
......
......@@ -6,5 +6,11 @@
#include "representation.h"
///Say if the current function is a function prototype
/** @param fct : PFunction
* @return true if the function is a function prototype, false otherwise
*/
bool repr_isFunction(const PFunction & fct){
return fct.getOtherCode() == "";
}
......@@ -15,4 +15,6 @@ typedef std::vector<PArgument> PVecArgument;
typedef std::vector<PFunction> PVecFunction;
typedef std::vector<PSource> PVecSource;
bool repr_isFunction(const PFunction & fct);
#endif
......@@ -36,17 +36,17 @@ OptionParser createOptionParser(){
* @return true on success, false otherwise
*/
bool createSourceFromConfig(const std::string & inputFile, const std::string & outputDir){
// HeaderParser parser;
// if(!parser.load(inputFile)){
// std::cerr << "createSourceFromConfig : cannot parse the input file '"<<inputFile<<"'" << std::endl;
// return false;
// }
// PVecSource vecSource = parser.getVecSource();
// bool b(ph5_backend(vecSource, outputDir));
// if(!b){
// std::cerr << "createSourceFromConfig : cannot save source/header from input file '"<<inputFile<<"'" << std::endl;
// }
// return b;
HeaderParser parser;
if(!parser.load(inputFile)){
std::cerr << "createSourceFromConfig : cannot parse the input file '"<<inputFile<<"'" << std::endl;
return false;
}
PVecSource vecSource = parser.getVecSource();
bool b(cpp_backend(vecSource, outputDir));
if(!b){
std::cerr << "createSourceFromConfig : cannot save source/header from input file '"<<inputFile<<"'" << std::endl;
}
return b;
return true;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment