File: cparser.cc

package info (click to toggle)
aspectc%2B%2B 1%3A1.1%2Bsvn20120529-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 222,560 kB
  • sloc: cpp: 3,935,531; ansic: 18,166; pascal: 14,783; sh: 2,188; makefile: 1,110; python: 340
file content (133 lines) | stat: -rw-r--r-- 4,811 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "Puma/CParser.h"
#include "Puma/ErrorStream.h"
#include "Puma/CSemVisitor.h"
#include "Puma/CPrintVisitor.h"
#include "Puma/CTranslationUnit.h"
#include <iostream>
using namespace std;

/*DEBUG*/#include <stdlib.h>

using namespace Puma;

namespace Puma {
/*DEBUG*/extern int TRACE_TYPE;     // created types
/*DEBUG*/extern int TRACE_UNDO;     // deleted syntax trees
/*DEBUG*/extern int TRACE_OBJS;     // inserted/deleted sem objects
/*DEBUG*/extern int TRACE_FCT;      // current function
/*DEBUG*/extern int TRACE_SCOPES;   // entered/leaved scopes
/*DEBUG*/extern int SEM_DECL_SPECS; // created - deleted CSemDeclSpecs
}

static bool TRACE_STDOUT = false;
static bool TRACE_FILE = false;
static bool TRACE_TOKENS = false;
static bool DO_SEMANTICS = true;
static bool PRINT_TREE = true;
static bool PRINT_DB = true;
static int  ONLY_PRE = 0;

void evalOptions (int argc, char **argv) {
  if (argc < 2) {
    cout << "usage: cparser [options] <filename>" << endl;
    cout << "options:" << endl;
    cout << "  -tr|--trace           trace parse process, write to stdout" << endl;
    cout << "  -tf|--trace-file      trace parse process, write to debug/trace.file" << endl;
    cout << "  -tt|--trace-tokens    trace parsing tokens too" << endl;
    cout << "  -ts|--trace-scopes    trace entered/leaved scopes" << endl;
    cout << "  -to|--trace-objs      trace inserted/destroyed semantic objects" << endl;
    cout << "  -tu|--trace-undo      trace deleted syntax tree nodes" << endl;
    cout << "  -tc|--trace-types     trace created types" << endl;
    cout << "  -nd|--no-print-db     do not print the class database" << endl;
    cout << "  -nt|--no-print-tree   do not print the syntax tree" << endl;
    cout << "  -ns|--no-semantics    do not start the semantic process" << endl;
    cout << "  -E                    write preprocessor output to stdout" << endl;
    exit (1);
  }
  for (int i = 1; i < argc-1; i++) {
    const char *arg = argv[i];
    if (! strcmp (arg, "-tr") || ! strcmp (arg, "--trace"))
      TRACE_STDOUT = (TRACE_FILE = false, true);
    else if (! strcmp (arg, "-tf") || ! strcmp (arg, "--trace-file"))
      TRACE_FILE = (TRACE_STDOUT = false, true);
    else if (! strcmp (arg, "-tt") || ! strcmp (arg, "--trace-tokens"))
      TRACE_TOKENS = ((! TRACE_FILE && (TRACE_STDOUT = true)), true);
    else if (! strcmp (arg, "-ts") || ! strcmp (arg, "--trace-scopes"))
      TRACE_SCOPES = 1;
    else if (! strcmp (arg, "-to") || ! strcmp (arg, "--trace-objs"))
      TRACE_OBJS = 1;
    else if (! strcmp (arg, "-tu") || ! strcmp (arg, "--trace-undo"))
      TRACE_UNDO = 1;
    else if (! strcmp (arg, "-tc") || ! strcmp (arg, "--trace-types"))
      TRACE_TYPE = 1;
    else if (! strcmp (arg, "-nd") || ! strcmp (arg, "--no-print-db"))
      PRINT_DB = false;
    else if (! strcmp (arg, "-nt") || ! strcmp (arg, "--no-print-tree"))
      PRINT_TREE = false;
    else if (! strcmp (arg, "-ns") || ! strcmp (arg, "--no-semantics"))
      DO_SEMANTICS = false;
    else if (! strcmp (arg, "-E"))
      ONLY_PRE = 1;
  }
}

int main (int argc, char **argv) {
  ErrorStream err;
  CProject project (err, argc, argv);
  evalOptions (argc, argv);

  // language C mode
  project.config ().Add ("--lang-c");  
  project.configure (project.config ());

  Unit *unit = project.scanFile (argv[argc-1]);
  if (! unit) {
    cout << "Aborted: Unable to scan input file" << endl;
    exit (1);
  }

  CParser parser;
  ofstream *fout = 0; 
#ifdef __PUMA_TRACING__
  if (TRACE_STDOUT)
    parser.trace (cout, TRACE_TOKENS);         // trace the parse process
  else if (TRACE_FILE) {
    fout = new ofstream ("debug/trace.file");
    parser.trace (*fout, TRACE_TOKENS);        // trace the parse process
  }
#endif

  cout << "Starting parse process..." << endl;
  CTranslationUnit *tu = parser.parse (*unit, project, ONLY_PRE);

  if (tu->tree ()) {
    if (DO_SEMANTICS) {
      CSemVisitor semantics (err);
      cout << "Starting semantic analysis..." << endl;
      semantics.run (tu->tree ());
    }
    if (PRINT_TREE) {
      CPrintVisitor printer;
      cout << "Printing syntax tree..." << endl;
      printer.print (tu->tree (), cout);
    }
    if (PRINT_DB) {
      cout << "Printing semantic database..." << endl;
      tu->db ().Dump (cout, 2);
    }
  } else 
    cout << "Note: Empty program" << endl;
  
  if (err.severity () > sev_warning)
    cout << "Errors!" << endl;
  else if (err.severity () > sev_none)
    cout << "Warnings!" << endl;
  else
    cout << "No errors" << endl;

  delete tu;
  if (fout) delete fout;
  /*DEBUG*/cout << "Statistics: +" << CTree::alloc << " -" << CTree::release << endl;
  /*DEBUG*/if (SEM_DECL_SPECS) cerr<<"WARNING: "<<SEM_DECL_SPECS<<" CSemDeclSpec(s) not deleted"<<endl;
  return err.severity () > sev_warning;
}