Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "Parsers.h"
00011 #include "ParserExceptions.h"
00012 #include <sstream>
00013
00014 using namespace std;
00015
00016 #define CREATE_PARSERS \
00017 BTParser* btparser = 0;\
00018 FSMParser* fsmparser = 0;\
00019 TAOParser* taoparser = 0;\
00020 struct GC{\
00021 BTParser*& btparser;\
00022 FSMParser*& fsmparser;\
00023 TAOParser*& taoparser;\
00024 std::string file;\
00025 GC(BTParser*& btparser, FSMParser*& fsmparser, TAOParser*& taoparser, std::string file):\
00026 btparser(btparser), fsmparser(fsmparser), taoparser(taoparser), file(file)\
00027 {\
00028 btparser = createBT(file);\
00029 fsmparser = createFSM(file);\
00030 taoparser = createTAO(file);\
00031 }\
00032 ~GC(){\
00033 del(btparser);\
00034 del(fsmparser);\
00035 del(taoparser);\
00036 }\
00037 } gc(btparser, fsmparser, taoparser, file);
00038
00039 bool parseToXml(std::ostream& parsing_result, std::ostream& errors, std::string file){
00040
00041 CREATE_PARSERS
00042
00043
00044 try{
00045
00046 fsm_constructor::FSMConstructor& fsm = parseFSM(fsmparser);
00047 bt_constructor::BTConstructor& bt = parseBT(btparser);
00048 tao_constructor::TAOConstructor& tao = parseTAO(taoparser);
00049
00050
00051 fsm.trees = &bt;
00052 bt.fsms = &fsm;
00053
00054 xml_version(parsing_result, ""); parsing_result<<std::endl;
00055 fsm_constructor::saveXml(parsing_result, fsm)<<std::endl;
00056 bt_constructor::saveXml(parsing_result, bt)<<std::endl;
00057 tao_constructor::saveXml(parsing_result, tao)<<std::endl;
00058
00059 errors << fsm.errors.str();
00060 errors << bt.errors.str();
00061 errors << tao.errors.str();
00062
00063 }catch(const PEFileNotFound& err){
00064 errors << err.what() << std::endl;
00065 return false;
00066 }
00067
00068 return true;
00069 }
00070 bool parseToXml(std::string result_prefix, std::ostream& errors, std::string file){
00071
00072 CREATE_PARSERS
00073
00074
00075 try{
00076
00077 fsm_constructor::FSMConstructor& fsm = parseFSM(fsmparser);
00078 bt_constructor::BTConstructor& bt = parseBT(btparser);
00079 tao_constructor::TAOConstructor& tao = parseTAO(taoparser);
00080
00081
00082 fsm.trees = &bt;
00083 bt.fsms = &fsm;
00084
00085 fsm_constructor::saveXml(result_prefix, fsm);
00086 bt_constructor::saveXml(result_prefix, bt);
00087 tao_constructor::saveXml(result_prefix, tao);
00088
00089 errors << fsm.errors.str();
00090 errors << bt.errors.str();
00091 errors << tao.errors.str();
00092
00093 }catch(const PEFileNotFound& err){
00094 errors << err.what() << std::endl;
00095 return false;
00096 }
00097
00098 return true;
00099 }
00100
00101 bool parseToDot(std::ostream& parsing_result, std::ostream& errors, std::string file){
00102
00103 CREATE_PARSERS
00104
00105
00106 try{
00107
00108 fsm_constructor::FSMConstructor& fsm = parseFSM(fsmparser);
00109 bt_constructor::BTConstructor& bt = parseBT(btparser);
00110
00111
00112 fsm.trees = &bt;
00113 bt.fsms = &fsm;
00114
00115 fsm_constructor::saveDot(parsing_result, fsm)<<std::endl;
00116 bt_constructor::saveDot(parsing_result, bt)<<std::endl;
00117
00118 errors << fsm.errors.str();
00119 errors << bt.errors.str();
00120
00121 }catch(const PEFileNotFound& err){
00122 errors << err.what() << std::endl;
00123 return false;
00124 }
00125
00126 return true;
00127 }
00128 bool parseToDot(std::string result_prefix, std::ostream& errors, std::string file){
00129
00130 CREATE_PARSERS
00131
00132
00133 try{
00134
00135 fsm_constructor::FSMConstructor& fsm = parseFSM(fsmparser);
00136 bt_constructor::BTConstructor& bt = parseBT(btparser);
00137
00138
00139 fsm.trees = &bt;
00140 bt.fsms = &fsm;
00141
00142 fsm_constructor::saveDot(result_prefix, fsm);
00143 bt_constructor::saveDot(result_prefix, bt);
00144
00145 errors << fsm.errors.str();
00146 errors << bt.errors.str();
00147
00148 }catch(const PEFileNotFound& err){
00149 errors << err.what() << std::endl;
00150 return false;
00151 }
00152
00153 return true;
00154 }
00155
00156
00157 vector<string> split(string text, char del=':'){
00158 stringstream in(text);
00159 vector<string> res;
00160 char ch;
00161 while(in.eof()==false){
00162 in >> ch;
00163 stringstream word;
00164 while(in.eof()==false and ch!=del){
00165 word << ch;
00166 in >> ch;
00167 }
00168 if(word.str().size()>0)
00169 res.push_back(word.str());
00170 }
00171 return res;
00172 }
00173
00174 #define SearchFlag(NAME) searchFlag(argc, argv, NAME)
00175 int searchFlag(int argc, char** argv, string name){
00176 for(int i=0;i<argc;i++){
00177 string v(argv[i]);
00178 if( v == name ) return i;
00179 }
00180 return -1;
00181 }
00182 #define SearchValue(NAME) searchValue(argc, argv, NAME)
00183 int searchValue(int argc, char** argv, string name){
00184 int i = searchFlag(argc, argv, name);
00185 if(i<0 or i==argc-1) return -1;
00186 return i+1;
00187 }
00188
00189 int main(int argc, char** argv){
00190
00191 cout<<"-- Start decision making parsing"<<endl;
00192 string exe ( argv[0] );
00193 string print_mode = "PRINT_ERRORS";
00194 if(SearchFlag("-pe")>0) print_mode = "PRINT_ERRORS";
00195 if(SearchFlag("-pa")>0) print_mode = "PRINT_ALL";
00196 bool isParseDots = false;
00197 bool isParseXmls = false;
00198 if(SearchFlag("-dot")>0) isParseDots=true;
00199 if(SearchFlag("-xml")>0) isParseXmls=true;
00200 string project="";
00201 if(SearchValue("-src")>0){
00202 project = string( argv[SearchValue("-src")] );
00203 }
00204 string shared_folder="";
00205 if(SearchValue("-dst")>0){
00206 shared_folder = string( argv[SearchValue("-dst")] );
00207 if(shared_folder[shared_folder.size()-1]!='/'){
00208 shared_folder+="/";
00209 }
00210 }else{
00211 std::cerr << "Error: destination folder does not found in parameters. (use -dst flag)" << endl;
00212 return 1;
00213 }
00214 vector<string> files;
00215 if(SearchValue("-f")>0){
00216 files = split(string(argv[SearchValue("-f")]));
00217 }else{
00218 std::cerr << "Error: list of files does not found in parameters. (use -f flag and : as delimiter)" << endl;
00219 return 2;
00220 }
00221
00222
00223 cout<<" -- parser path: "<<exe<<endl;
00224 cout<<" -- project folder: "<<project<<endl;
00225 cout<<" -- share folder: "<<shared_folder<<endl;
00226 if(print_mode == "PRINT_ALL")
00227 cout<<"files"<<endl;
00228 if(isParseXmls)
00229 cout<<" -- parse XMLs"<<endl;
00230 if(isParseDots)
00231 cout<<" -- parse DOTs"<<endl;
00232
00233 for(size_t i=0;i<files.size();i++){
00234 std::string input_file = files[i];
00235 if(print_mode == "PRINT_ALL"){
00236 cout<<" "<<i<<") parse "<<input_file<<endl;
00237 cout<<" save to "<<shared_folder<<endl;
00238 }
00239 if(isParseXmls){
00240
00241 std::stringstream errors;
00242 parseToXml(shared_folder, errors, input_file);
00243 if(errors.str().size()>0){
00244 std::cerr << errors.str() <<std::endl;
00245 }
00246 }
00247 if(isParseDots){
00248
00249 std::stringstream errors;
00250 parseToDot(shared_folder, errors, input_file);
00251 if(not isParseXmls){
00252 if(errors.str().size()>0){
00253 std::cerr << errors.str() <<std::endl;
00254 }
00255 }
00256 }
00257 }
00258
00259 cout<<"-- End decision making parsing"<<endl;
00260 return 0;
00261 }