Checking in miniswig: It's a flex/bison based parser that is able to parse
[supertux.git] / tools / miniswig / main.cpp
1 #include <iostream>
2 #include <fstream>
3 #include <vector>
4 #include <string>
5 #include "tree.h"
6 #include "globals.h"
7 #include "create_wrapper.h"
8
9 extern int yyparse();
10 extern int yylex();
11
12 CompilationUnit* unit = 0;
13 std::istream* input = 0;
14 std::string inputfile;
15 std::string modulename = "wrapper";
16
17 void usage()
18 {
19     std::cout << "Usage: miniswig --input FILE --output-cpp FILE --output-hpp FILE [--module NAME]\n";
20     std::cout << "\n";
21 }
22
23 int main(int argc, char** argv)
24 {
25     std::string outputcpp;
26     std::string outputhpp;
27     for(int i = 0; i < argc; ++i) {
28         if(strcmp(argv[i], "--module") == 0) {
29             if(i+1 >= argc) {
30                 std::cerr << "Need to specify a module name.\n";
31                 usage();
32                 return 1;
33             }
34             modulename = argv[++i];
35         } else if(strcmp(argv[i], "--input") == 0) {
36             if(i+1 >= argc) {
37                 std::cerr << "Need to specify input file name.\n";
38                 usage();
39                 return 1;
40             }
41             inputfile = argv[++i];
42         } else if(strcmp(argv[i], "--output-cpp") == 0) {
43             if(i+1 >= argc) {
44                 std::cerr << "Need to specifiy output cpp name.\n";
45                 usage();
46                 return 1;
47             }
48             outputcpp = argv[++i];
49         } else if(strcmp(argv[i], "--output-hpp") == 0) {
50             if(i+1 >= argc) {
51                 std::cerr << "Need to specify output hpp name.\n";
52                 usage();
53                 return 1;
54             }
55             outputhpp = argv[++i];
56         } else if(argv[i][0] == '-') {
57             std::cerr << "Unknown option '" << argv[i] << "'.\n";
58             usage();
59             return 1;
60         } else {
61         }
62     }
63     if(inputfile == "" || outputcpp == "" || outputhpp == "") {
64         std::cerr << "Not all options specified.\n";
65         usage();
66         return 1;
67     }
68     
69     try {
70         input = new std::ifstream(inputfile.c_str());
71         if(!input->good()) {
72             std::cerr << "Couldn't open file '" << input << "' for reading.\n";
73             return 1;
74         }
75         unit = new CompilationUnit();
76         unit->types.push_back(new StringType());
77         Namespace* std_namespace = new Namespace();
78         std_namespace->name = "std";
79         unit->namespaces.push_back(std_namespace);
80         yyparse();
81         std::ofstream cppout(outputcpp.c_str());
82         if(!cppout.good()) {
83             std::cerr << "Couldn't open file '" << outputcpp << "' for writing.\n";
84             return 1;
85         }
86         std::ofstream hppout(outputhpp.c_str());
87         if(!hppout.good()) {
88             std::cerr << "Couldn't open file '" << outputhpp << "' for writing.\n";
89             return 1;
90         }
91         WrapperCreator creator(cppout, hppout);
92         creator.create_wrapper(unit);
93     } catch(std::exception& e) {
94         std::cerr << e.what() << "\n";
95         return 1;
96     }
97
98     return 0;
99 }
100