File indexing completed on 2024-04-28 04:36:06

0001 /*
0002     (C) Copyright 2009 Jonathan Schmidt-Dominé <devel@the-user.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License version 2 as published by the Free Software Foundation.
0007 
0008    This library is distributed in the hope that it will be useful,
0009    but WITHOUT ANY WARRANTY; without even the implied warranty of
0010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0011    Library General Public License for more details.
0012 
0013    You should have received a copy of the GNU Library General Public License
0014    along with this library; see the file COPYING.LIB.  If not, write to
0015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0016    Boston, MA 02110-1301, USA.
0017 */
0018 
0019 #define QT_NO_STL
0020 #include <iostream>
0021 #include <fstream>
0022 #include <climits>
0023 #include <QDebug>
0024 #include "dumptree.h"
0025 #include "ccparser.h"
0026 #include "lexer.h"
0027 
0028 using namespace cc;
0029 using namespace std;
0030 
0031 int main(int argc, char** argv)
0032 {
0033     if(argc == 1)
0034     {
0035         cerr << "Simply use some preprocessed C-code-files (output of gcc -E) as arguments" << endl;
0036         return -1;
0037     }
0038     for(int i = 1; i != argc; ++i)
0039     {
0040         ifstream filestr(argv[i]);
0041         char* contents;
0042         if(filestr.is_open())
0043         {
0044             long size;
0045             filestr.ignore(10000);
0046             size = filestr.tellg();
0047             filestr.close();
0048             contents = new char[size+2];
0049             filestr.open(argv[i]);
0050             filestr.read(contents, size);
0051             contents[size] = '\n';
0052             contents[size+1] = '\0';
0053             filestr.close();
0054         }
0055         else
0056         {
0057             cerr << "File not found: " << argv[i] << endl;
0058             return -1;
0059         }
0060         KDevPG::TokenStream token_stream;
0061         Parser::memoryPoolType memory_pool;
0062         Parser parser;
0063         parser.setTokenStream(&token_stream);
0064         parser.setMemoryPool(&memory_pool);
0065         Lexer lexer(&parser, contents);
0066             int kind = Parser::Token_EOF;
0067         do
0068         {
0069             kind = lexer.yylex();
0070             if ( !kind ) // when the lexer returns 0, the end of file is reached
0071                 kind = Parser::Token_EOF;
0072                         qDebug() << kind;
0073             Parser::Token &t = token_stream.push();
0074             t.kind = kind;
0075             t.begin = lexer.tokenBegin();
0076             t.end = lexer.tokenEnd();
0077         }
0078         while ( kind != Parser::Token_EOF );
0079         token_stream.rewind(0);
0080         parser.yylex();
0081         DocumentAst *ast = 0;
0082         bool matched = parser.parseDocument(&ast);
0083         if(matched)
0084         {
0085             DumpTree dt;
0086             dt.dump(ast);
0087         }
0088         else
0089         {
0090             qDebug() << "Parsing failed";
0091         }
0092         delete[] contents;
0093     }
0094     return 0;
0095 }
0096