Blame view

src/InternLib/Parser.cc 4.41 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
 * Parser.cc
 *
 *  Created on: Dec 11, 2012
 *      Author: AKKA IS
 */
#include <ctype.h>
#include <sstream>

#include "DicError.hh"
#include "AMDA_exception.hh"

#include "Parser.hh"
#include "ParamData.hh"
#include <boost/functional/hash.hpp>
#include <boost/algorithm/string.hpp>

using namespace std;
namespace AMDA {
namespace Parameters {

1ddc0f12   Benjamin Renard   Fix expression pa...
22
Parser::Parser() : _expression(""), _paramOnly(false), _processOnly(false) {
fbe3c2bb   Benjamin Renard   First commit
23
24
25
26
27
28
29
30
31
32

}

Parser::~Parser() {

}

void Parser::process(std::string expression) {
	_expression=expression;
	string::iterator it = _expression.begin();
1ddc0f12   Benjamin Renard   Fix expression pa...
33
34
	_paramOnly = false;
	_processOnly = false;
fbe3c2bb   Benjamin Renard   First commit
35
36
37
	while(it != _expression.end()) {
		switch(*it) {
		case '$':
1ddc0f12   Benjamin Renard   Fix expression pa...
38
39
			_paramOnly = (it == _expression.begin());
			_processOnly = false;
fbe3c2bb   Benjamin Renard   First commit
40
			processParameterName(++it) ;
fbe3c2bb   Benjamin Renard   First commit
41
42
			break;
		case '#':
1ddc0f12   Benjamin Renard   Fix expression pa...
43
44
			_paramOnly = false;
			_processOnly = (it == _expression.begin());
fbe3c2bb   Benjamin Renard   First commit
45
46
47
			processProcessName(++it);
			break;
		default:
1ddc0f12   Benjamin Renard   Fix expression pa...
48
49
			_paramOnly = false;
			_processOnly = false;
fbe3c2bb   Benjamin Renard   First commit
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
			if(isalpha(*it)) {
				processFctName(it);
			}
			else {
				_formulaCC.append(1, *it);
				_constructorCC.append(1, *it);
				++it;
			}
			break;
		}
	}

}

std::string Parser::findAlphaNum_(std::string::iterator &it) {

	string pParamName = "";
	while(it != _expression.end() &&  (isalnum(*it) || *it == '_')) {
		pParamName.append(1, *it);
		++it;
	}
	return pParamName;
}

void Parser::processParameterName(std::string::iterator &it) {
	string pParamName = findAlphaNum_(it);
	_listParameterName.insert(pParamName);
	_formulaCC += pParamName + ParamData::getAccessor();
22bc7b60   Benjamin Renard   Fix a bug with ex...
78
	_constructorCC += "(static_cast<_T_" + boost::to_upper_copy(pParamName) + "_Element_Type>(_T_" + boost::to_upper_copy(pParamName) + "_Element_Type()))";
fbe3c2bb   Benjamin Renard   First commit
79
80
81
82
83
84
85
86
87
}

void Parser::processProcessName(std::string::iterator &it) {
	string pProcessName = "";
	while(it != _expression.end() &&  (isalnum(*it) || *it == '_')) {
		pProcessName.append(1, *it);
		++it;
	}

137764cf   Benjamin Renard   Fix key definitio...
88
	AttributList attribut;
fbe3c2bb   Benjamin Renard   First commit
89
90
	std::string expression = processProcessExpression(it, attribut);

137764cf   Benjamin Renard   Fix key definitio...
91
	boost::hash<std::string> string_hash;
fbe3c2bb   Benjamin Renard   First commit
92

137764cf   Benjamin Renard   Fix key definitio...
93
94
95
96
97
98
99
	std::stringstream buffer;
	buffer << expression;
	for (AttributList::iterator it = attribut.begin(); it != attribut.end(); ++it) {
		buffer << "_" << *it;
	}
	std::stringstream key;
	key << pProcessName << "_" << string_hash(buffer.str());
fbe3c2bb   Benjamin Renard   First commit
100
101
	_listProcessName[key.str()] = ProcessInfo(pProcessName, expression, attribut);
	_formulaCC += key.str() + ParamData::getAccessor();
22bc7b60   Benjamin Renard   Fix a bug with ex...
102
	_constructorCC += "(static_cast<_T_" + boost::to_upper_copy(key.str()) + "_Element_Type>(_T_" + boost::to_upper_copy(key.str()) + "_Element_Type()))";
fbe3c2bb   Benjamin Renard   First commit
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
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

}

size_t Parser::searchSemiColon(const char* expression) {
	size_t result = string::npos;
	int nbBraket = 0;
	unsigned int length = strlen(expression);
	for ( unsigned int i = 0; i < length; ++i) {
		switch(expression[i]) {
		case '(': ++nbBraket; break;
		case ')': --nbBraket; break;
		case ';': if ( nbBraket == 0 ) { return i; } break;
		default:
			break;
		}
	}
 return result;
}


std::string  Parser::processProcessExpression(std::string::iterator &it, AttributList &attribut) {

	string lProcessExpression = "";
	while(*it==' ') ++it;

	if(*it == '(') {
		int nbBraket = 1;
		++it;
		while(it!=_expression.end() && nbBraket > 0) {
			if(*it == '(') ++nbBraket;
			if(*it == ')') --nbBraket;
			if(nbBraket > 0) lProcessExpression.append(1, *it);
			++it;
		}
	}
	else {
	    BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_PROCESS_ERR) << AMDA::ex_msg(std::string("Bad format")));
	}

	size_t indexOfFirstSemiColon  = 0;
	size_t indexOfSecondSemiColon = searchSemiColon(lProcessExpression.c_str()+indexOfFirstSemiColon);

	string lSubExpression = "";

	if (indexOfSecondSemiColon == string::npos) {
		lSubExpression=lProcessExpression;
	} else {
		lSubExpression=lProcessExpression.substr(indexOfFirstSemiColon, indexOfSecondSemiColon);
		indexOfFirstSemiColon+=indexOfSecondSemiColon+1;
		while((indexOfSecondSemiColon=searchSemiColon(lProcessExpression.c_str()+indexOfFirstSemiColon))!=string::npos) {
			attribut.push_back(lProcessExpression.substr(indexOfFirstSemiColon, indexOfSecondSemiColon));
			indexOfFirstSemiColon+=indexOfSecondSemiColon+1;
		}
		attribut.push_back(lProcessExpression.c_str()+indexOfFirstSemiColon);
	}

	return lSubExpression;
}

void Parser::processFctName(std::string::iterator& it) {
	string pFctName = findAlphaNum_(it);
	if(*it == '(') {
	_listFctName.insert(pFctName);
	}
	_formulaCC += pFctName;
	_constructorCC += pFctName;
}

} /* namespace Parameters */
} /* namespace AMDA */