1 // Copyright (C) 2006-2022 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "typeParsers.hxx"
21 #include "dataParsers.hxx"
22 #include "Exception.hxx"
24 #include "Runtime.hxx"
29 #include "YacsTrace.hxx"
31 extern YACS::ENGINE::Proc* currentProc;
32 extern YACS::ENGINE::Runtime* theRuntime;
36 typetypeParser typetypeParser::typeParser;
37 seqtypeParser seqtypeParser::seqParser;
38 objtypeParser objtypeParser::objParser;
39 structtypeParser structtypeParser::structParser;
40 membertypeParser membertypeParser::memberParser;
42 void typetypeParser::buildAttr(const XML_Char** attr)
46 required("name",attr);
47 required("kind",attr);
48 for (int i = 0; attr[i]; i += 2)
50 if(std::string(attr[i]) == "name")name(attr[i+1]);
51 if(std::string(attr[i]) == "kind")kind(attr[i+1]);
54 void typetypeParser::pre (){}
55 void typetypeParser::name(const std::string& name)
57 DEBTRACE( "type_name: " << name )
60 void typetypeParser::kind(const std::string& name)
62 DEBTRACE( "type_kind: " << name )
65 mytype typetypeParser::post()
67 DEBTRACE( "type_post" )
74 void seqtypeParser::onStart(const XML_Char* el, const XML_Char** attr)
76 DEBTRACE( "seqtypeParser::onStart: " << el )
77 parser* pp=&parser::main_parser;
78 SetUserDataAndPush(pp);
83 void seqtypeParser::onEnd(const char *el,parser* child)
85 DEBTRACE( "seqtypeParser::onEnd: " << el )
87 void seqtypeParser::buildAttr(const XML_Char** attr)
91 required("name",attr);
92 required("content",attr);
93 for (int i = 0; attr[i]; i += 2)
95 if(std::string(attr[i]) == "name")name(attr[i+1]);
96 if(std::string(attr[i]) == "content")content(attr[i+1]);
99 void seqtypeParser::name(const std::string& name)
101 DEBTRACE( "seqtype_name: " << name )
104 void seqtypeParser::content(const std::string& name)
106 DEBTRACE( "seqtype_content: " << name )
107 if(currentProc->typeMap.count(name)==0)
109 //Check if the typecode is defined in the runtime
110 YACS::ENGINE::TypeCode* t=theRuntime->getTypeCode(name);
113 std::stringstream msg;
114 msg << "Type " << name << " does not exist" << " (" <<__FILE__ << ":" << __LINE__ << ")";
115 throw Exception(msg.str());
119 currentProc->typeMap[name]=t;
123 _contentType=currentProc->typeMap[name];
126 ENGINE::TypeCode* seqtypeParser::post()
128 DEBTRACE( "seqtype_post" )
129 ENGINE::TypeCode *t = currentProc->createSequenceTc(_name,_name,_contentType);
133 void objtypeParser::onStart(const XML_Char* el, const XML_Char** attr)
135 DEBTRACE( "objtypeParser::onStart: " << el )
136 std::string element(el);
137 parser* pp=&parser::main_parser;
138 if(element == "base")pp=&stringtypeParser::stringParser;
139 SetUserDataAndPush(pp);
144 void objtypeParser::onEnd(const char *el,parser* child)
146 DEBTRACE( "objtypeParser::onEnd: " << el )
147 std::string element(el);
148 if(element == "base")base(((stringtypeParser*)child)->post());
150 void objtypeParser::buildAttr(const XML_Char** attr)
154 required("name",attr);
155 for (int i = 0; attr[i]; i += 2)
157 if(std::string(attr[i]) == "name")name(attr[i+1]);
158 if(std::string(attr[i]) == "id")id(attr[i+1]);
161 void objtypeParser::pre ()
166 void objtypeParser::name(const std::string& name)
168 DEBTRACE( "objtype_name: " << name )
171 void objtypeParser::id(const std::string& name)
173 DEBTRACE( "objtype_id: " << name )
176 void objtypeParser::base(const std::string& name)
178 DEBTRACE( "base_name: " << name )
179 if(currentProc->typeMap.count(name)==0)
181 //Check if the typecode is defined in the runtime
182 YACS::ENGINE::TypeCode* t=theRuntime->getTypeCode(name);
185 std::stringstream msg;
186 msg << "Type " << name << " does not exist" << " (" <<__FILE__ << ":" << __LINE__ << ")";
187 throw Exception(msg.str());
191 currentProc->typeMap[name]=t;
195 if(currentProc->typeMap[name]->kind() != ENGINE::Objref)
197 std::stringstream msg;
198 msg << "Type " << name << " is not an objref" ;
199 msg << " (" <<__FILE__ << ":" << __LINE__ << ")";
200 throw Exception(msg.str());
202 _ltc.push_back((ENGINE::TypeCodeObjref *)currentProc->typeMap[name]);
204 ENGINE::TypeCode * objtypeParser::post()
206 DEBTRACE( "objtype_post" )
207 ENGINE::TypeCode *t = currentProc->createInterfaceTc(_id,_name,_ltc);
211 void membertypeParser::buildAttr(const XML_Char** attr)
215 required("name",attr);
216 required("type",attr);
217 for (int i = 0; attr[i]; i += 2)
219 if(std::string(attr[i]) == "name")name(attr[i+1]);
220 if(std::string(attr[i]) == "type")type(attr[i+1]);
223 void membertypeParser::name(const std::string& name){ _prop._name=name; }
224 void membertypeParser::type(const std::string& name){ _prop._value=name; }
225 myprop membertypeParser::post(){return _prop;}
227 void structtypeParser::onStart(const XML_Char* el, const XML_Char** attr)
229 DEBTRACE( "structtypeParser::onStart: " << el )
230 std::string element(el);
231 parser* pp=&parser::main_parser;
232 if(element == "member")pp=&membertypeParser::memberParser;
233 SetUserDataAndPush(pp);
238 void structtypeParser::onEnd(const char *el,parser* child)
240 DEBTRACE( "structtypeParser::onEnd: " << el )
241 std::string element(el);
242 if(element == "member")member(((membertypeParser*)child)->post());
244 void structtypeParser::buildAttr(const XML_Char** attr)
248 required("name",attr);
249 for (int i = 0; attr[i]; i += 2)
251 if(std::string(attr[i]) == "name")name(attr[i+1]);
252 if(std::string(attr[i]) == "id")id(attr[i+1]);
255 void structtypeParser::pre ()
260 void structtypeParser::name(const std::string& name)
262 DEBTRACE( "structtype_name: " << name );
265 void structtypeParser::id(const std::string& name)
267 DEBTRACE( "structtype_id: " << name );
270 void structtypeParser::member (const myprop& prop)
272 DEBTRACE( "structtype_member: " << prop._name << prop._value );
273 if(currentProc->typeMap.count(prop._value)==0)
275 //Check if the typecode is defined in the runtime
276 YACS::ENGINE::TypeCode* t=theRuntime->getTypeCode(prop._value);
279 std::string msg="Unknown type " + prop._value + " for member " + prop._name + " in struct " + _name;
280 throw Exception(msg);
284 currentProc->typeMap[prop._value]=t;
289 _members.push_back(prop);
291 ENGINE::TypeCode * structtypeParser::post()
293 DEBTRACE( "structtype_post" );
294 ENGINE::TypeCodeStruct *t;
295 if(currentProc->typeMap.count(_name)!=0)
297 //reuse a forward declaration
298 ENGINE::TypeCode* tt=currentProc->typeMap[_name];
299 if(tt->kind()==YACS::ENGINE::Struct)
301 t=(ENGINE::TypeCodeStruct*)tt;
305 std::string msg="Forward declaration must be a struct type but " + std::string(tt->name()) + " is not one" ;
306 throw Exception(msg);
311 t = (ENGINE::TypeCodeStruct*)currentProc->createStructTc(_id,_name);
313 std::vector<myprop>::const_iterator iter;
314 for(iter=_members.begin();iter!=_members.end();iter++)
316 DEBTRACE("member: " << iter->_name << " " <<iter->_value);
317 t->addMember(iter->_name,currentProc->typeMap[iter->_value]);