File: main.cpp

package info (click to toggle)
kdevelop-pg-qt 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,284 kB
  • sloc: cpp: 22,743; lex: 928; ansic: 716; yacc: 615; ruby: 68; sh: 14; lisp: 10; fortran: 6; makefile: 2
file content (83 lines) | stat: -rw-r--r-- 1,760 bytes parent folder | download
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
/*
    SPDX-FileCopyrightText: 2009 Jonathan Schmidt-Dominé <devel@the-user.org>

    SPDX-License-Identifier: LGPL-2.0-only
*/

#include <iostream>
#include <fstream>
#include <climits>
#include <QDebug>
#include "dumptree.h"
#include "ccparser.h"
#include "lexer.h"

using namespace cc;
using namespace std;

int main(int argc, char** argv)
{
	if(argc == 1)
	{
		cerr << "Simply use some preprocessed C-code-files (output of gcc -E) as arguments" << endl;
		return -1;
	}
	for(int i = 1; i != argc; ++i)
	{
		ifstream filestr(argv[i]);
		char* contents;
		if(filestr.is_open())
		{
			long size;
			filestr.ignore(10000);
			size = filestr.tellg();
			filestr.close();
			contents = new char[size+2];
			filestr.open(argv[i]);
			filestr.read(contents, size);
			contents[size] = '\n';
			contents[size+1] = '\0';
			filestr.close();
		}
		else
		{
			cerr << "File not found: " << argv[i] << endl;
			return -1;
		}
		KDevPG::TokenStream token_stream;
		Parser::memoryPoolType memory_pool;
		Parser parser;
		parser.setTokenStream(&token_stream);
		parser.setMemoryPool(&memory_pool);
		Lexer lexer(&parser, contents);
	        int kind = Parser::Token_EOF;
		do
		{
			kind = lexer.yylex();
			if ( !kind ) // when the lexer returns 0, the end of file is reached
				kind = Parser::Token_EOF;
                        qDebug() << kind;
			Parser::Token &t = token_stream.push();
			t.kind = kind;
			t.begin = lexer.tokenBegin();
			t.end = lexer.tokenEnd();
		}
		while ( kind != Parser::Token_EOF );
		token_stream.rewind(0);
		parser.yylex();
		DocumentAst *ast = 0;
		bool matched = parser.parseDocument(&ast);
		if(matched)
		{
			DumpTree dt;
			dt.dump(ast);
		}
		else
		{
			qDebug() << "Parsing failed";
		}
		delete[] contents;
	}
	return 0;
}