test.cc

Go to the documentation of this file.
00001 /*
00002    The lestes compiler suite
00003    Copyright (C) 2002, 2003, 2004, 2005 Miroslav Tichy
00004    Copyright (C) 2002, 2003, 2004, 2005 Petr Zika
00005    Copyright (C) 2002, 2003, 2004, 2005 Vojtech Hala
00006    Copyright (C) 2002, 2003, 2004, 2005 Jiri Kosina
00007    Copyright (C) 2002, 2003, 2004, 2005 Pavel Sanda
00008    Copyright (C) 2002, 2003, 2004, 2005 Jan Zouhar
00009    Copyright (C) 2002, 2003, 2004, 2005 Rudolf Thomas
00010 
00011    This program is free software; you can redistribute it and/or modify
00012    it under the terms of the GNU General Public License as published by
00013    the Free Software Foundation; version 2 of the License.
00014 
00015    This program is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018    GNU General Public License for more details.
00019 
00020    See the full text of the GNU General Public License version 2, and
00021    the limitations in the file doc/LICENSE.
00022 
00023    By accepting the license the licensee waives any and all claims
00024    against the copyright holder(s) related in whole or in part to the
00025    work, its use, and/or the inability to use it.
00026  
00027  */
00028 #include <lestes/lang/cplus/lex/file_system.hh>
00029 #include <lestes/lang/cplus/lex/preprocessor.hh>
00030 #include <lestes/lang/cplus/sem/sa_statements.g.hh>
00031 #include <lestes/lang/cplus/sem/ss_misc.g.hh>
00032 #include <lestes/lang/cplus/syn/dump_at_lassert.g.hh>
00033 #include <lestes/lang/cplus/syn/hinter.hh>
00034 #include <lestes/lang/cplus/syn/manager.hh>
00035 #include <lestes/lang/cplus/syn/parse_result.hh>
00036 #include <lestes/lang/cplus/syn/parser.hh>
00037 #include <lestes/lang/cplus/syn/prefixer.hh>
00038 #include <lestes/msg/logger.hh>
00039 #include <lestes/package.hh>
00040 #include <lestes/std/action.g.hh>
00041 #include <lestes/std/data_types.hh>
00042 #include <lestes/std/dumper.hh>
00043 #include <vector>
00044 #include <iostream>
00045 #include <fstream>
00046 #include <cstdlib>
00047 
00048 package(lestes);
00049 package(lang);
00050 package(cplus);
00051 package(syn);
00052 
00053 using ::lestes::lang::cplus::sem::ss_decl_seq;
00054 using ::lestes::lang::cplus::sem::sa_context_manager;
00055 
00056 int do_it( bool flat_dump, lstring filename, lstring ss_filename, lstring context_filename, bool hinter_on )
00057 {
00058         /* lex init */
00059         ptr<prefixer::preprocessor> pp =
00060                 prefixer::preprocessor::create(
00061                         ::lestes::lang::cplus::lex::file_system::create(), "" );
00062 
00063         /* syn init */
00064         prefixer::init( prefixer::PREFIX_ON, pp );
00065         manager::init();
00066         hinter::init( hinter_on ? hinter::HINTER_CHECK : hinter::HINTER_OFF );
00067         parser::init();
00068 	::lestes::lang::cplus::sem::sa_statements::instance();
00069         lassert_event->attach(dump_at_lassert::create(ss_filename));
00070         lassert_event->attach(dump_context_at_lassert::create(context_filename));
00071 
00072 //F//   //FIXME: this should be done elsewhere
00073 //F//   hinter::forced_scope_set( ss_decl_seq::root_instance() );
00074 //F//   // end
00075 
00076         ptr<parse_result_type> parse_result = parser::parse();
00077         bool success = parse_result->success_get();
00078         ::std::cout << "top level parser " <<
00079                 (success ? "succeeded" : "failed") << ::std::endl;
00080 
00081         ptr< ::lestes::intercode::as_base > as_result = parse_result->as_result_get();
00082         if (filename != "" && as_result) {
00083                 ::std::ofstream f(filename.c_str());
00084                 if (flat_dump)
00085 			::lestes::std::dumper::dump( f, as_result );
00086                 else
00087 			::lestes::std::readable_dumper::dump( f, as_result );
00088                 f.close();
00089         }
00090 
00091         if (ss_filename != "") {
00092                 ::std::ofstream f(ss_filename.c_str());
00093                 if (flat_dump)
00094 			::lestes::std::dumper::dump(f, sem::ss_decl_seq::root_instance());
00095                 else
00096 			::lestes::std::readable_dumper::dump(f, sem::ss_decl_seq::root_instance());
00097                 f.close();
00098         }
00099 
00100         if (context_filename != "") {
00101                 ::std::ofstream f(context_filename.c_str());
00102                 if (flat_dump)
00103 			::lestes::std::dumper::dump(f, sa_context_manager::instance());
00104                 else
00105 			::lestes::std::readable_dumper::dump(f, sa_context_manager::instance());
00106                 f.close();
00107         }
00108 
00109         return success ? 0 : 1;
00110 }
00111 
00112 end_package(syn);
00113 end_package(cplus);
00114 end_package(lang);
00115 end_package(lestes);
00116 
00117 using ::std::vector;
00118 using ::std::cout;
00119 using ::std::endl;
00120 using ::lestes::std::lstring;
00121 
00122 struct usage_and_exit {
00123 };
00124 
00125 void operator << ( ::std::ostream & os, const usage_and_exit & )
00126 {
00127         os << "\n\n"
00128                 "Usage: parser_test [-h] [-l llc_file] [-s skel_file] [-o output_file] [-f filename] [-c context_filename] [dump_type]\n"
00129                 "\n"
00130                 "Logger configuration is read from llc_file, if specified.\n"
00131                 "Logger configuration skeleton is dumped to skel_file, if specified.\n"
00132                 "AS structures will be dumped into file specified by the -o option.\n"
00133                 "  When the option is missing, they will not be dumped at all.\n"
00134                 "SS structures will be dumped into file specified by the -f option.\n"
00135                 "  When the option is missing, they will not be dumped at all.\n"
00136                 "SA context will be dumped into file specified by the -c option.\n"
00137                 "  When the option is missing, they will not be dumped at all.\n"
00138                 "dump_type is either 'flat' or 'deep', the default is 'deep'.\n"
00139                 "Hinter is turned on by the -h option.\n"
00140                 << endl;
00141         return exit(1);
00142 }
00143 
00144 int main( int argc, char * argv[] )
00145 {
00146         vector<lstring> args( argv, argv+argc );
00147         vector<lstring>::iterator it = args.begin();
00148         ++it;
00149         lstring filename = "";
00150         lstring ss_filename = "";
00151         lstring context_filename = "";
00152         bool log_finish = false;
00153         bool o_seen = false;
00154         bool f_seen = false;
00155         bool c_seen = false;
00156         bool l_seen = false;
00157         bool h_seen = false;
00158         while (it != args.end() && (*it)[0] == '-') {
00159                 // option found
00160                 if (*it == "-o") {
00161                         if (o_seen)
00162                                 cout << "Error: -o option used multiple times." << usage_and_exit();
00163                         o_seen = true;
00164                         ++it;
00165                         if (it == args.end())
00166                                 cout << "Argument to -o option is missing." << usage_and_exit();
00167                         filename = *it++;
00168                 } else if (*it == "-f") {
00169                         if (f_seen)
00170                                 cout << "Error: -f option used multiple times." << usage_and_exit();
00171                         f_seen = true;
00172                         ++it;
00173                         if (it == args.end())
00174                                 cout << "Argument to -f option is missing." << usage_and_exit();
00175                         ss_filename = *it++;
00176                 } else if (*it == "-c") {
00177                         if (c_seen)
00178                                 cout << "Error: -c option used multiple times." << usage_and_exit();
00179                         c_seen = true;
00180                         ++it;
00181                         if (it == args.end())
00182                                 cout << "Argument to -c option is missing." << usage_and_exit();
00183                         context_filename = *it++;
00184                 } else if (*it == "-l") {
00185                         if (l_seen)
00186                                 cout << "Error: -l option used multiple times." << usage_and_exit();
00187                         l_seen = true;
00188                         ++it;
00189                         if (it == args.end())
00190                                 cout << "Argument to -l option is missing." << usage_and_exit();
00191                         log_finish = ::lestes::msg::logger::init(*it++);
00192                         if (!log_finish)
00193                                 cout << "Error, loggers will be off." << endl;
00194                 } else if (*it == "-s") {
00195                         // can be used multiple times
00196                         ++it;
00197                         if (it == args.end())
00198                                 cout << "Argument to -s option is missing." << usage_and_exit();
00199                         ::std::ofstream of( it->c_str() );
00200                         ++it;
00201 			::lestes::msg::logger::dump_skeleton(of);
00202                         of.close();
00203                 } else if (*it == "-h") {
00204                         ++it;
00205                         h_seen = true;
00206                 } else
00207                         cout << "Unknown option '" << *it << "'." << usage_and_exit();
00208         }
00209         bool flat_dump = false;
00210         if (it != args.end()) {
00211                 if (*it == "flat")
00212                         flat_dump = true;
00213                 else if (*it != "deep")
00214                         cout << "Unknown dump type '" << *it << "'." << usage_and_exit();
00215                 ++it;
00216                 if (it != args.end())
00217                         cout << "Too many arguments." << usage_and_exit();
00218         }
00219         int result = ::lestes::lang::cplus::syn::do_it( flat_dump, filename, ss_filename, context_filename, h_seen );
00220         if (log_finish)
00221 		::lestes::msg::logger::finish();
00222         return result;
00223 }

Generated on Mon Feb 12 18:23:36 2007 for lestes by doxygen 1.5.1-20070107