File: allmain.cc

package info (click to toggle)
javacc 7.0.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,712 kB
  • sloc: java: 27,671; xml: 2,305; cpp: 404; sh: 128; makefile: 24
file content (52 lines) | stat: -rw-r--r-- 1,252 bytes parent folder | download | duplicates (2)
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
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <stdlib.h>

#include "gen/CharStream.h"
#include "gen/JavaParser.h"
#include "gen/JavaParserTokenManager.h"

using namespace java::parser;
using namespace std;

JAVACC_STRING_TYPE ReadFileFully(char *file_name) {
  JAVACC_STRING_TYPE s;
#if WIDE_CHAR
  wifstream fp_in;
#else
  ifstream fp_in;
#endif
  fp_in.open(file_name, ios::in);
  // Very inefficient.
  while (!fp_in.eof()) {
   s += fp_in.get();
  }
  return s;
}

int main(int argc, char **argv) {
  if (argc < 2) {
    cout << "Usage: wjavaparser <java inputfile>" << endl;
    exit(1);
  }
  JAVACC_STRING_TYPE s = ReadFileFully(argv[1]);
  CharStream *stream = new CharStream(s.c_str(), s.size() - 1, 1, 1);
  JavaParserTokenManager *scanner = new JavaParserTokenManager(stream);
  JavaParser parser(scanner);
  parser.setErrorHandler(new MyErrorHandler());
  parser.CompilationUnit();
  SimpleNode *root = (SimpleNode*)parser.jjtree.peekNode();
  if (root) {
    JAVACC_STRING_TYPE buffer;
#if WIDE_CHAR
    //root->dumpToBuffer(L" ", L"\n", &buffer);
    //wcout << buffer << "\n";
    root->dump(L" ");
#else
    root->dumpToBuffer(" ", "\n", &buffer);
    printf("%s\n", buffer.c_str());
#endif
  }
}