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

repr_cpp_token_operator.cpp 7.43 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
/***************************************
	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());
Pierre Aubert's avatar
Pierre Aubert committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
	body += "\t//repr_cpp_token_operator : Let's parse an Operator with 1 token\n";
	
	body += "\t//repr_cpp_token_operator : the first operand is already parsed and given as parameter\n";
	body += "\t//repr_cpp_token_operator : it would be great to add a while\n\n";
	
	body += "\tif(!isMatch(\""+op.getVecToken().front()+"\")){return false;}\n";
	
	body += "\t//repr_cpp_token_operator : Let's call the graph token of the second operand\n";
	
	body += "\tPStmt rightOp = "+op.getExprClass()+"_default();\n";
	std::string rightOperandGraph(op.getVecExprGraph().back());
	body += "\tif(!" + rightOperandGraph + "(rightOp)){return false;}\n";
	
	body += "\t//repr_cpp_token_operator : here the operator is well parsed\n";
	
	body += "\tPStmt op = "+op.getExprClass()+"_default();\n";
// 	body += "\top.setType("+op.getValueEnum()+");\n";
	body += "\t"+op.getExprClass()+"_set"+firstToUpper(op.getAttrEnum())+"(op, "+op.getValueEnum()+");\n";
	
	body += "\tPStmt & vecOperand = PExpr_getVecOperand(op);\n";
	body += "\tvecOperand.push_back("+varName+");\n";
	body += "\tvecOperand.push_back(rightOp);\n";
	body += "\t//repr_cpp_token_operator : Finally, put the operator in the in/out parameter\n";
	body += "\t"+varName+" = op;\n";
Pierre Aubert's avatar
Pierre Aubert committed
61 62 63 64 65 66 67 68 69 70 71
	
	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());
Pierre Aubert's avatar
Pierre Aubert committed
72
	body += "\t//repr_cpp_token_operator : Let's parse an Operator with 2 tokens\n";
Pierre Aubert's avatar
Pierre Aubert committed
73 74 75 76 77 78 79 80 81 82 83
	
	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());
Pierre Aubert's avatar
Pierre Aubert committed
84
	body += "\t//repr_cpp_token_operator : Let's parse an Operator with 3 tokens\n";
Pierre Aubert's avatar
Pierre Aubert committed
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
	
	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";
Pierre Aubert's avatar
Pierre Aubert committed
117
	body += "\t//repr_cpp_token_operator : If there is no operand\n";
Pierre Aubert's avatar
Pierre Aubert committed
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
	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;
}