home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_11 / splash / xcl.cpp < prev   
Text File  |  1993-01-15  |  4KB  |  174 lines

  1. #include <iostream.h>
  2. #include <fstream.h>
  3. #include "splash.h"
  4. #include "tracer.h"
  5.  
  6. #ifdef    _Windows
  7. #include "../win/debugwin/debugstr.h"
  8. #define OUTSTREAM dout
  9. #else
  10. #define OUTSTREAM cout
  11. #endif
  12.  
  13. ifstream fin;
  14.  
  15. // Globals
  16. SPString curln, curclass;
  17. int bcnt;   // current brace count
  18.  
  19. /*
  20.  * Reads in a line, and eliminates anything in between comments
  21.  * or quotes. Also keeps count of the brace level
  22.  */
  23. int getline()
  24. {
  25. LTRACER("getline", 4)
  26.  
  27.     if(!(fin >> curln)) return 0;
  28.     cout << curln << endl;
  29.     LTRACE(2, curln)
  30.     while(curln.m("\".*\"")){ // remove anything in quoted strings
  31.     int o, c;
  32.     SPString tl= curln.substr(0, o= curln.index("\""));
  33.     do{ // make sure the quote is not backslashed
  34.             c= curln.index("\"", o+1);
  35.         o= c;
  36.     }while(c > 1 && curln[c-1] == '\\');
  37.     tl += curln.substr(c+1); // rest of line
  38.     curln= tl;
  39. //    cout << "quote stripped curln= " << curln <<endl;
  40.     }
  41.  
  42.     if(curln.m("//")){ // found a comment in line
  43.     curln.substr(curln.index("//"))= ""; // chop it off
  44.     }
  45.  
  46.     if(curln.m("/\\*")){ // in c comment
  47.     SPString tl= curln.substr(0, curln.index("/*"));
  48.     if(curln.m("\\*/")){ // closing comment on same line
  49.         tl += curln.substr(curln.index("*/")+2); // rest of line
  50.         curln= tl;
  51. //        cout << "comment stripped curln= " << curln <<endl;
  52.     }else{ // find close of comment
  53.         while(fin >> curln){
  54.         cout << curln << endl;
  55.         if(curln.m("\\*/")){
  56.             tl += curln.substr(curln.index("*/")+2); // get rest of line
  57.             break;        
  58.         }
  59.         }
  60.         curln= tl;
  61.     }
  62.     }
  63.     LTRACE(4, curln)
  64.     int ob= curln.tr("{", "");
  65.     int cb= curln.tr("}", "");
  66.     int cnt= ob - cb;
  67.     bcnt += cnt;
  68.     LTRACE(4, "Brace count = " << bcnt);
  69.     if(bcnt < 0){
  70.     cout.flush();
  71.     cerr << "Brace count dropped below zero,  oops" << endl;
  72.     exit(1);
  73.     }
  74.     return 1;
  75. }
  76.  
  77. void dofunction(const SPStringList &subm, int bc= 0)
  78. {
  79. TRACER("dofunction")
  80. SPString fnname= subm[1], fnparams= subm[2];
  81.  
  82. LTRACE(1, "In Function " << fnname << "(" << fnparams << ")")
  83.  
  84.     if(curln.m("{.*}")){
  85. //    cerr << "Can't insert TRACER here" << endl;
  86.     return;
  87.     }else if(curln.m("{")) bc--;
  88.  
  89.     while(bcnt == bc && getline()); // find opening brace
  90.     LTRACE(1, "Inside Function " << fnname)
  91.  
  92.     if(fnname.m("^main$")){ // special treatment for main()
  93.     cout << "FTRACER(\"main()\", 0)" << endl;
  94.     }else{
  95.     cout << "TRACER(\"";
  96.     if(curclass.length()) cout << curclass << "::";
  97.     cout << fnname << "(" << fnparams << ")\")" << endl;
  98.     }
  99.  
  100.     while(getline()){
  101.     if(bcnt == bc){ // no longer in function
  102.         LTRACE(1, "End of function " << fnname)
  103.         break;
  104.     }
  105.     
  106.     }
  107.  
  108. }
  109.  
  110. void doclass(const SPStringList &subm)
  111. {
  112. TRACER("doclass")
  113. SPStringList sl;
  114.  
  115.     if(curln.m(";")) return; // if a ';' on line then probably a forward reference
  116.     LTRACE(1, "Seen Class " << subm[1])
  117.     curclass= subm[1];    // remember class name
  118.     
  119.     while(bcnt == 0 && getline()); // find opening brace
  120.     LTRACE(1, "Inside Class " << curclass)
  121.     
  122.     while(getline()){
  123.     if(bcnt == 0){ // no longer in class
  124.         LTRACE(1, "End of class " << curclass)
  125.         break;
  126.     }
  127.  
  128.     if(curln.m("[ \t]*([^ \t]+)[ \t]*\\(([^)]*)\\)", sl)){ // function def
  129.         if(curln.m(";[ \t]*$")) continue; // declaration
  130.         dofunction(sl, bcnt);
  131.     }
  132.     
  133.     }
  134.     curclass= "";
  135. }
  136.  
  137. int main(int argc, char **argv)
  138. {
  139. int debug= 0;
  140.  
  141.     if(argc > 2 && *argv[1] == '-'){
  142.     debug= atoi(&argv[1][1]);
  143.     argc--; argv++;
  144.     }
  145.  
  146.  
  147. FTRACER("xcl", debug, OUTSTREAM)
  148. SPStringList sl;
  149.  
  150.     if(argc < 2){
  151.     cerr << "Usage: xcl [-n] fn" << endl;
  152.     exit(1);
  153.     }
  154.  
  155.     fin.open(argv[1], ios::in);
  156.     if(!fin){
  157.     cerr << "Couldn't open file: " << argv[1] << endl;
  158.     exit(1);
  159.     }
  160.     
  161.     while(getline()){
  162.     if(curln.m("^[ \t]*class[ \t]+([a-zA-Z_]+[a-zA-Z_0-9]*)", sl)){
  163.         doclass(sl);
  164.         continue;
  165.     }
  166.  
  167.         if(curln.m("[ \t]*([^ \t]+)[ \t]*\\(([^)]*)\\)", sl)){ // function def
  168.         if(curln.m(";[ \t]*$")) continue; // declaration
  169.         dofunction(sl, bcnt);
  170.     }
  171.     }
  172. }
  173.  
  174.