Docker-in-Docker (DinD) capabilities of public runners deactivated. More info

repr_cpp_token_operator.cpp 6.29 KB
Newer Older
Pierre Aubert's avatar
Pierre Aubert committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
/***************************************
	Auteur : Pierre Aubert
	Mail : aubertp7@gmail.com
	Licence : CeCILL-C
****************************************/

#include "string_utils.h"
#include "repr_cpp_operator.h"
#include "repr_cpp_token_operator.h"

///Save the headers of the PToken functions for Operator
/**	@param tabGraphToken : PTabGraphToken of all Operator
 * 	@param graphToken : graph of the current token
 * 	@return C++ header
*/
std::string repr_cpp_tokenOperatorHeader(const PTabGraphToken & tabGraphToken, const PGraphToken & graphToken){
	std::string body(""), name(graphToken.getName()), varName(graphToken.getVarName()), varType(graphToken.getVarType());
	PVecOperator vecOp;
	search_operatorByGraphUse(vecOp, tabGraphToken.getVecOperator(), graphToken.getName());
	if(vecOp.size() != 0lu){
		for(PVecOperator::iterator it(vecOp.begin()); it != vecOp.end(); ++it){
			std::string baseFctName("Operator" + repr_cpp_operator_getEnumValue(it->getValueEnum()));
			body += "\t\tbool " + name + baseFctName + "Do(" + varType + " & " + varName + ");\n";
			body += "\t\tvoid " + name + baseFctName + "Undo(" + varType + " & " + varName + ");\n\n";
		}
	}
	return body;
}

///Save the implementation of a single token Operator parsing function for the given graphToken
/**	@param op : operator to be used
 * 	@param graphToken : graph token related to the current operator
 * 	@return C++
*/
std::string repr_cpp_singleTokenOperator(const POperator & op, const PGraphToken & graphToken){
	std::string body(""), name(graphToken.getName()), varName(graphToken.getVarName()), varType(graphToken.getVarType());
	body += "\t//Let's parse an Operator with 1 token\n";
	body += "\tif(isMatch(\""+op.getVecToken().front()+"\")){return false;}\n";
	
	return body;
}

///Save the implementation of a double tokens Operator parsing function for the given graphToken
/**	@param op : operator to be used
 * 	@param graphToken : graph token related to the current operator
 * 	@return C++
*/
std::string repr_cpp_doubleTokenOperator(const POperator & op, const PGraphToken & graphToken){
	std::string body(""), name(graphToken.getName()), varName(graphToken.getVarName()), varType(graphToken.getVarType());
	body += "\t//Let's parse an Operator with 2 tokens\n";
	
	return body;
}

///Save the implementation of a triple tokens Operator parsing function for the given graphToken
/**	@param op : operator to be used
 * 	@param graphToken : graph token related to the current operator
 * 	@return C++
*/
std::string repr_cpp_tripleTokenOperator(const POperator & op, const PGraphToken & graphToken){
	std::string body(""), name(graphToken.getName()), varName(graphToken.getVarName()), varType(graphToken.getVarType());
	body += "\t//Let's parse an Operator with 3 tokens\n";
	
	return body;
}

///Save the implementation of the Operator parsing function for the given graphToken
/**	@param parserName : name of the generated parser class
 * 	@param op : operator to be used
 * 	@param graphToken : graph token related to the current operator
 * 	@return C++
*/
std::string repr_cpp_tokenOperatorSourceOperatorDo(const std::string & parserName, const POperator & op, const PGraphToken & graphToken){
	std::string body(""), name(graphToken.getName()), varName(graphToken.getVarName()), varType(graphToken.getVarType());
	std::string opValue(repr_cpp_operator_getEnumValue(op.getValueEnum()));
	std::string baseFctName("Operator" + opValue);
	size_t nbToken(op.getVecToken().size());
	body += "///Parse the Operator " + opValue + " related to the GraphToken "+varName+"\n";
	body += "/**\t@param " + varName + " : " + varType + " to be parsed by the operator\n";
	body += " * \t@return true on success, false otherwise\n";
	body += "*/\n";
	body += "bool " + parserName + "::" + name + baseFctName + "Do(" + varType + " & " + varName + "){\n";
	body += "\tp_parser->pushPosition();\n";
	if(nbToken == 1lu){
		body += repr_cpp_singleTokenOperator(op, graphToken);
	}else if(nbToken == 2lu){
		body += repr_cpp_doubleTokenOperator(op, graphToken);
	}else if(nbToken == 3lu){
		body += repr_cpp_tripleTokenOperator(op, graphToken);
	}else{
		body += "\t#error \"repr_cpp_tokenOperatorSourceOperatorDo : no rules to parse operator with "+convertToString(nbToken)+" token\"\n";
	}
	
	body += "\tPStmt & _vecOp = " + varType + "_get"+firstToUpper(op.getVecOp())+"(" + varName + ");\n";
	body += "\t//If there is no operand\n";
	body += "\tif(_vecOp.size() == 0lu){return false;}\n";
	
	body +="\treturn false;\n";
	body +="}\n\n";
	return body;
}


///Save the implementation of the Operator parsing function for the given graphToken
/**	@param parserName : name of the generated parser class
 * 	@param op : operator to be used
 * 	@param graphToken : graph token related to the current operator
*/
std::string repr_cpp_tokenOperatorSourceOperatorUndo(const std::string & parserName, const POperator & op, const PGraphToken & graphToken){
	std::string body(""), name(graphToken.getName()), varName(graphToken.getVarName()), varType(graphToken.getVarType());
	std::string opValue(repr_cpp_operator_getEnumValue(op.getValueEnum()));
	std::string baseFctName("Operator" + repr_cpp_operator_getEnumValue(op.getValueEnum()));
	body += "///Undo the Parsing of the Operator " + opValue + " related to the GraphToken "+varName+"\n";
	body += "/**\t@param " + varName + " : " + varType + " to be parsed by the operator\n";
	body += "*/\n";
	body += "void " + parserName + "::" + name + baseFctName + "Undo(" + varType + " & " + varName + "){\n";
	body += "\tp_parser->popPosition();\n";
	body +="}\n\n";
	return body;
}


///Save the source of the PToken functions
/**	@param parserName : name of the generated parser class
 * 	@param tabGraphToken : contains all the graph and internal representation
 * 	@param graph : graph of the current token
 * 	@param token : PToken to deal with
 * 	@return C++ source
*/
std::string repr_cpp_tokenOperatorSource(const std::string & parserName, const PTabGraphToken & tabGraphToken, const PGraphToken & graph)
{
	std::string body("");
	PVecOperator vecOp;
	search_operatorByGraphUse(vecOp, tabGraphToken.getVecOperator(), graph.getName());
	if(vecOp.size() != 0lu){
		for(PVecOperator::iterator it(vecOp.begin()); it != vecOp.end(); ++it){
			body += repr_cpp_tokenOperatorSourceOperatorDo(parserName, *it, graph);
			body += repr_cpp_tokenOperatorSourceOperatorUndo(parserName, *it, graph);
		}
	}
	return body;
}