Blame view

src/expressionParser/FunctionsParser.cc 5.31 KB
0d5a1aa5   Benjamin Renard   Parser: add a fun...
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
/*
 * FunctionsParser.cc
 *
 *  Created on: Mar 19, 2019
 *      Author: AKKA
 */

#include "NodeCfg.hh"
#include "AMDA_exception.hh"
#include "FunctionsParser.hh"
#include "ParserLogger.hh"

#include <boost/lexical_cast.hpp>

using namespace AMDA::XMLConfigurator;

namespace AMDA {
namespace parser {

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief info_brief node
 */
class InfoBriefNode : public NodeCfg
{
public:
b65a4679   Benjamin Renard   Fix a warning mes...
27
	void proceed(xmlNodePtr /*pNode*/,const AMDA::Parameters::CfgContext& /*pContext*/) {
0d5a1aa5   Benjamin Renard   Parser: add a fun...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
		//Not used
	}
};

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief prompt node
 */
class PromptNode : public NodeCfg
{
public:
	void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
		if ((pNode->children != NULL) && (pNode->children->content != NULL)) {
			FunctionInfo *pFunctionInfo =  pContext.get<FunctionInfo *>();
66aeb1bf   Benjamin Renard   First step to sup...
42
			pFunctionInfo->setNbPromptedArgs(pFunctionInfo->getNbPromptedArgs()+1);
0d5a1aa5   Benjamin Renard   Parser: add a fun...
43
44
45
46
47
48
		}
	}
};

///////////////////////////////////////////////////////////////////////////////
/*
66aeb1bf   Benjamin Renard   First step to sup...
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
 * @brief prompts node
 */
class PromptsNode : public NodeGrpCfg {
public:

	PromptsNode () : NodeGrpCfg() {
		getChildList()["prompt"] = NodeCfgSPtr(new PromptNode);
	}

	void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) {
		LOG4CXX_INFO(gLogger, "PromptsNode::proceed");

                // Proceed nodes
                NodeGrpCfg::proceed(pNode, pContext);
	}
};

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief prompt node
 */
class PromptParamNode : public NodeCfg
{
public:
	void proceed(xmlNodePtr /*pNode*/,const AMDA::Parameters::CfgContext& /*pContext*/) {
		//Nothing to do
	}
};

///////////////////////////////////////////////////////////////////////////////
/*
ea6be348   Benjamin Renard   Support arguments...
80
81
82
83
84
85
86
87
88
89
90
91
 * @brief default_args node
 */
class DefaultArgsNode : public NodeCfg
{
public:
	void proceed(xmlNodePtr /*pNode*/,const AMDA::Parameters::CfgContext& /*pContext*/) {
		//Nothing to do
	}
};

///////////////////////////////////////////////////////////////////////////////
/*
0d5a1aa5   Benjamin Renard   Parser: add a fun...
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
 * @brief new_kernel node
 */
class NewKernelNode : public NodeCfg
{
public:
	void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
		if ((pNode->children != NULL) && (pNode->children->content != NULL)) {
			FunctionInfo *pFunctionInfo =  pContext.get<FunctionInfo *>();
			pFunctionInfo->setKernelName(std::string((char*)pNode->children->content));
		}
	}
};

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief function node
 */
class FunctionNode : public NodeGrpCfg
{
public:

	FunctionNode () : NodeGrpCfg() {
66aeb1bf   Benjamin Renard   First step to sup...
114
115
		getChildList()["prompts"] = RootNodeCfgSPtr(new PromptsNode);
                getChildList()["prompt_param"] = NodeCfgSPtr(new PromptParamNode);
ea6be348   Benjamin Renard   Support arguments...
116
		getChildList()["default_args"] = NodeCfgSPtr(new DefaultArgsNode);
0d5a1aa5   Benjamin Renard   Parser: add a fun...
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
		getChildList()["info_brief"] = NodeCfgSPtr(new InfoBriefNode);
		getChildList()["new_kernel"] = NodeCfgSPtr(new NewKernelNode);
	}

	void proceed(xmlNodePtr pNode,const AMDA::Parameters::CfgContext& pContext) {
		//Get name
		xmlChar* value = xmlGetProp(pNode, (const xmlChar *) "name");
		std::string name;
		if (value != NULL)
		{
			name = (char *)value;
			xmlFree(value);
			size_t lastindex = name.find_last_of("(");
			name = name.substr(0, lastindex);
		}

		if (!name.empty()) {
			AMDA::Parameters::CfgContext ctx;
			FunctionInfo functionInfo;
			ctx.push<FunctionInfo *>(&functionInfo);

			functionInfo.setIHMName(name);
			//Get args
			value = xmlGetProp(pNode, (const xmlChar *) "args");
			int nbArgs = 1;
			if (value != NULL)
			{
				nbArgs = boost::lexical_cast<int>(std::string((char*)value));
				functionInfo.setNbArgs(nbArgs);
			}

			//kind attribute  not used

			// Proceed nodes
			NodeGrpCfg::proceed(pNode, ctx);

			//Add function in map
			FunctionInfoMap *pFunctionsMap =  pContext.get<FunctionInfoMap *>();
			(*pFunctionsMap)[name] = functionInfo;
		}
	}
};

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief functions node
 */
class FunctionsNode : public NodeGrpCfg {
public:

	FunctionsNode () : NodeGrpCfg() {
		getChildList()["function"] = NodeCfgSPtr(new FunctionNode);
	}

	void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) {
		LOG4CXX_INFO(gLogger, "FunctionsNode::proceed");

		// Proceed nodes
		NodeGrpCfg::proceed(pNode, pContext);
	}
};

///////////////////////////////////////////////////////////////////////////////
/*
 * @brief functions node parser
 */
FunctionsParser::FunctionsParser (const char* pXSDFile) : XMLConfigurator(pXSDFile,true)
{

	// functions root node
	getXmlConfiguratorMap()["functions"] = RootNodeCfgSPtr(new FunctionsNode ());
}

FunctionInfoMap FunctionsParser::parse (const std::string& functionsFile) {
	LOG4CXX_INFO(gLogger, "FunctionsParser::parse parsing " << functionsFile);

	AMDA::Parameters::CfgContext ctx;
	FunctionInfoMap functionsMap;
	ctx.push<FunctionInfoMap *>(&functionsMap);

	// Check schema validity and parse xml file
	try {
		if (!XMLConfigurator::proceed(functionsFile.c_str(), ctx, true)) {
			return functionsMap;
		}
	}
	catch (...) {
		LOG4CXX_INFO(gLogger, "FunctionsParser::parse error while parsing file " << functionsFile);
		return functionsMap;
	}
	return functionsMap;
}

} /* namespace parser */
} /* namespace AMDA */