1 /*
2     (C) Copyright 2009 Jonathan Schmidt-Dominé <devel@the-user.org>
3 
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public
6    License version 2 as published by the Free Software Foundation.
7 
8    This library is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11    Library General Public License for more details.
12 
13    You should have received a copy of the GNU Library General Public License
14    along with this library; see the file COPYING.LIB.  If not, write to
15    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16    Boston, MA 02110-1301, USA.
17 */
18 
19 #define QT_NO_STL
20 #include <iostream>
21 #include <fstream>
22 #include <climits>
23 #include <QDebug>
24 #include "dumptree.h"
25 #include "ccparser.h"
26 #include "lexer.h"
27 
28 using namespace cc;
29 using namespace std;
30 
main(int argc,char ** argv)31 int main(int argc, char** argv)
32 {
33 	if(argc == 1)
34 	{
35 		cerr << "Simply use some preprocessed C-code-files (output of gcc -E) as arguments" << endl;
36 		return -1;
37 	}
38 	for(int i = 1; i != argc; ++i)
39 	{
40 		ifstream filestr(argv[i]);
41 		char* contents;
42 		if(filestr.is_open())
43 		{
44 			long size;
45 			filestr.ignore(10000);
46 			size = filestr.tellg();
47 			filestr.close();
48 			contents = new char[size+2];
49 			filestr.open(argv[i]);
50 			filestr.read(contents, size);
51 			contents[size] = '\n';
52 			contents[size+1] = '\0';
53 			filestr.close();
54 		}
55 		else
56 		{
57 			cerr << "File not found: " << argv[i] << endl;
58 			return -1;
59 		}
60 		KDevPG::TokenStream token_stream;
61 		Parser::memoryPoolType memory_pool;
62 		Parser parser;
63 		parser.setTokenStream(&token_stream);
64 		parser.setMemoryPool(&memory_pool);
65 		Lexer lexer(&parser, contents);
66 	        int kind = Parser::Token_EOF;
67 		do
68 		{
69 			kind = lexer.yylex();
70 			if ( !kind ) // when the lexer returns 0, the end of file is reached
71 				kind = Parser::Token_EOF;
72                         qDebug() << kind;
73 			Parser::Token &t = token_stream.push();
74 			t.kind = kind;
75 			t.begin = lexer.tokenBegin();
76 			t.end = lexer.tokenEnd();
77 		}
78 		while ( kind != Parser::Token_EOF );
79 		token_stream.rewind(0);
80 		parser.yylex();
81 		DocumentAst *ast = 0;
82 		bool matched = parser.parseDocument(&ast);
83 		if(matched)
84 		{
85 			DumpTree dt;
86 			dt.dump(ast);
87 		}
88 		else
89 		{
90 			qDebug() << "Parsing failed";
91 		}
92 		delete[] contents;
93 	}
94 	return 0;
95 }
96 
97