Parser.cc
4.14 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* 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 {
Parser::Parser() : _expression("") {
}
Parser::~Parser() {
}
void Parser::process(std::string expression) {
_expression=expression;
string::iterator it = _expression.begin();
while(it != _expression.end()) {
switch(*it) {
case '$':
processParameterName(++it) ;
break;
case '#':
processProcessName(++it);
break;
default:
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();
_constructorCC += "(static_cast<_T_" + boost::to_upper_copy(pParamName) + "_Element_Type>(_T_" + boost::to_upper_copy(pParamName) + "_Element_Type()))";
}
void Parser::processProcessName(std::string::iterator &it) {
string pProcessName = "";
while(it != _expression.end() && (isalnum(*it) || *it == '_')) {
pProcessName.append(1, *it);
++it;
}
AttributList attribut;
std::string expression = processProcessExpression(it, attribut);
boost::hash<std::string> string_hash;
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());
_listProcessName[key.str()] = ProcessInfo(pProcessName, expression, attribut);
_formulaCC += key.str() + ParamData::getAccessor();
_constructorCC += "(static_cast<_T_" + boost::to_upper_copy(key.str()) + "_Element_Type>(_T_" + boost::to_upper_copy(key.str()) + "_Element_Type()))";
}
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 */