XML-pp.cxx

Go to the documentation of this file.
00001 /**************************************************************************
00002  *
00003  * Copyright (C) 2008, Johns Hopkins University.
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or
00007  * without modification, are permitted provided that the following
00008  * conditions are met:
00009  *
00010  *   - Redistributions of source code must contain the above 
00011  *     copyright notice, this list of conditions, and the following
00012  *     disclaimer. 
00013  *
00014  *   - Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions, and the following
00016  *     disclaimer in the documentation and/or other materials 
00017  *     provided with the distribution.
00018  *
00019  *   - Neither the names of the copyright holders nor the names of any
00020  *     of any contributors may be used to endorse or promote products
00021  *     derived from this software without specific prior written
00022  *     permission. 
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00025  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00026  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00027  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00028  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00029  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00030  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00031  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00032  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00033  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  **************************************************************************/
00037 
00038 #include <stdint.h>
00039 #include <stdio.h>
00040 #include <unistd.h>
00041 #include <dirent.h>
00042 #include <string>
00043 #include <iostream>
00044 #include "AST.hxx"
00045 #include "backend.hxx"
00046 
00047 using namespace std;
00048 using namespace boost;
00049 using namespace sherpa;
00050 
00051 static void
00052 XMLp(std::ostream& out, shared_ptr<AST> ast, std::string pad, bool showLoc)
00053 {
00054   out << pad 
00055       << "<"
00056       << AST::tagName(ast->astType)
00057       << " ID=\""
00058       << ast->ID
00059       << "\"";
00060   if (ast->s.size()) {
00061     out << " s=\""
00062         << ast->s
00063         << "\"";
00064   }
00065 
00066   if (showLoc)
00067     out << " loc=\"" + ast->loc.asString() + "\"";
00068 
00069   if (ast->astType == at_ident) {
00070     out << " idType=\""
00071         << identTypeToString(ast->identType)
00072         << "\"";
00073 
00074     out << " Sym=\"";
00075     if (!ast->symbolDef) {
00076       out << "DEF"
00077           << "\"";
00078     }
00079     else {
00080       out << "USE"
00081           << "\"";
00082       out << " symDef=\""
00083           << ast->symbolDef->ID
00084           << "\"";
00085     }
00086   }
00087  
00088   if (ast->children.size()) {
00089     out << ">\n";
00090     for (unsigned i = 0; i < ast->children.size(); i++)
00091       XMLp(out, ast->child(i), pad + "   ", showLoc);
00092     out << pad
00093         << "</" 
00094         << AST::tagName(ast->astType)
00095         << ">\n";
00096   }
00097   else {
00098     out << "/>\n";
00099   }
00100 }
00101 
00102 static void
00103 XMLd(std::ostream& out, shared_ptr<AST> ast, bool showLoc)
00104 {
00105   out << "<"
00106       << AST::tagName(ast->astType)
00107       << " ID=\""
00108       << ast->ID
00109       << "\"";
00110 
00111   if (ast->s.size()) {
00112     out << " s=\""
00113         << ast->s
00114         << "\"";
00115   }
00116   
00117   if (showLoc)
00118     out << " loc=\"" + ast->loc.asString() + "\"";
00119 
00120   if (ast->astType == at_ident) {
00121 
00122     out << " idType=\""
00123         << identTypeToString(ast->identType)
00124         << "\"";
00125 
00126     out << " Sym=\"";
00127     if (!ast->symbolDef) {
00128       out << "DEF"
00129           << "\"";
00130     }
00131     else {
00132       out << "USE"
00133           << "\"";
00134       out << "symDef=\""
00135           << ast->symbolDef->ID
00136           << "\"";
00137     }
00138   }
00139 
00140   if (ast->children.size()) {
00141     out << ">";
00142     for (unsigned i = 0; i < ast->children.size(); i++)
00143       XMLd(out, ast->child(i), showLoc);
00144     out << "</" 
00145         << AST::tagName(ast->astType)
00146         << ">";
00147   }
00148   else {
00149     out << "/>";
00150   }
00151 }
00152 
00153 bool
00154 XMLpp(std::ostream& out, std::ostream& err, shared_ptr<UocInfo> uoc)
00155 {
00156   XMLp(out, uoc->uocAst, "", false);
00157   return true;
00158 }
00159 
00160 bool
00161 XMLdump(std::ostream& out, std::ostream& err, shared_ptr<UocInfo> uoc)
00162 {
00163   XMLd(out, uoc->uocAst, false);
00164   out << std::endl;
00165   return true;
00166 }
00167 
00168 
00169 
00170 std::string
00171 xmlMangle(std::string idName)
00172 {
00173   std::stringstream ss;      
00174   const char *s = idName.c_str();
00175   
00176   while (*s) {
00177     if (*s == '<') {
00178       ss << "&lt;";
00179     }
00180     else if (*s == '>') {
00181       ss << "&gt;";
00182     }
00183     /* need a fix for !--  */
00184     else {
00185       ss << *s;
00186     }
00187 
00188     s++;
00189   }
00190   
00191   return ss.str();
00192 }
00193 
00194 static void
00195 emitXMLType(INOstream &out, std::string name, shared_ptr<TypeScheme> ts,
00196             bool raw=false)
00197 {
00198   shared_ptr<TvPrinter> tvP = GC_NULL;
00199   if (!raw)
00200     tvP = TvPrinter::make();
00201   
00202   out << "<tqExpr>" << endl;
00203   out.more();
00204 
00205   out << "<id name='" << xmlMangle(name) << "'/>" << endl;
00206   
00207   if (!ts)
00208     out <<  "<unknown/>" << endl;
00209   else
00210     ts->asXML(tvP, out);
00211   
00212   out.less();
00213   out << "</tqExpr>" << endl;
00214 }
00215 
00216 static void 
00217 XMLtypes(INOstream &out, shared_ptr<AST> ast, bool raw=false)
00218 {
00219   switch(ast->astType) {
00220   case at_ident:
00221     emitXMLType(out, ast->s, ast->scheme);
00222     break;    
00223 
00224   case at_usesel:
00225     XMLtypes(out, ast->child(1), raw);
00226     break;
00227 
00228   case at_interface:
00229   case at_module:
00230     {
00231       size_t i=0;
00232       out << "<btypes:TYPE>" << endl;
00233       out.more();
00234       out << "<text content='";
00235       if (ast->astType == at_interface) {
00236         i=1;
00237         out << "*** Interface: " << ast->child(0)->s << " ***";
00238       }
00239       else {
00240         out << "*** Source Module ***";
00241       }
00242       out << "'/>" << endl;
00243       out << "<br/>" << endl;
00244       for (; i<ast->children.size(); i++) {
00245         XMLtypes(out, ast->child(i), raw);
00246         out << "<br/>" << endl;
00247       }      
00248       out << "<br/>" << endl;      
00249       out.less();  
00250       out << "</btypes:TYPE>" << endl;  
00251       break;
00252     }
00253         
00254   case at_proclaim:
00255   case at_defexception:
00256   case at_deftypeclass:
00257   case at_defunion:    
00258   case at_declunion:
00259   case at_defstruct:
00260   case at_declstruct:
00261   case at_define:
00262   case at_recdef:
00263   case at_identPattern:
00264     XMLtypes(out, ast->child(0), raw);
00265     break;
00266 
00267   case at_definstance:
00268     emitXMLType(out, "#Instance#", ast->scheme);
00269     break;    
00270     
00271   case at_declare:
00272   case at_importAs:
00273   case at_provide:
00274     break;
00275     
00276   default:
00277     assert(false);
00278     break;
00279   }
00280 }
00281 
00282 void 
00283 XML_types_PP(std::ostream &out, shared_ptr<AST> ast, bool raw=false)
00284 {
00285   INOstream ino(out);
00286   XMLtypes(ino, ast, raw);
00287 }
00288 
00289 
00290 bool
00291 XMLtypesPP(std::ostream& out, std::ostream& err, shared_ptr<UocInfo> uoc)
00292 {
00293   XML_types_PP(out, uoc->uocAst, false);
00294   return true;
00295 }

Generated on Thu May 17 23:59:16 2012 for BitC Compiler by  doxygen 1.4.7