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

0001 /* 
0002  * Copyright 2005, 2006 Jakob Petsovits <jpetso@gmx.at>
0003  * Copyright 2008 Niko Sams <niko.sams@gmail.com>
0004  *
0005  * This program is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU General Public License
0007  * as published by the Free Software Foundation; either version 2
0008  * of the License, or (at your option) any later version.
0009  *
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU General Public License
0016  * along with this program; if not, write to the Free Software
0017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0018  * 02110-1301, USA.
0019  */
0020 
0021 #include "factparser.h"
0022 #include "factdefaultvisitor.h"
0023 #include "factdebugvisitor.h"
0024 
0025 #include <QDebug>
0026 #include <QFile>
0027 
0028 using namespace fact;
0029 
0030 static void usage(char const* argv0);
0031 static bool parseFile(const QString& filename, bool debug);
0032 
0033 int main(int argc, char *argv[])
0034 {
0035     if (argc == 1)
0036     {
0037         usage(argv[0]);
0038         exit(EXIT_FAILURE);
0039     }
0040     bool debug = false;
0041     while (char const *arg = *++argv)
0042     {
0043         if (!strncmp(arg, "--debug", 7))
0044         {
0045             debug = true;
0046         }
0047         else if (!strncmp(arg, "--", 2))
0048         {
0049             qDebug() << "Unknown option: " << arg;
0050             usage(argv[0]);
0051             exit(EXIT_FAILURE);
0052         }
0053         else if(!parseFile(arg, debug))
0054         {
0055             exit(EXIT_FAILURE);
0056         }
0057     }
0058 
0059   return EXIT_SUCCESS;
0060 }
0061 
0062 bool parseFile(const QString& filename, bool debug)
0063 {
0064     QString contents;
0065     QFile file(filename);
0066     if (file.open(QIODevice::ReadOnly | QIODevice::Text))
0067     {
0068         contents = file.readAll();
0069         file.close();
0070     }
0071     else
0072     {
0073         qDebug() << filename << ": file not found";
0074         return false;
0075     }
0076 
0077     KDevPG::TokenStream tokenStream;
0078     Parser::memoryPoolType memoryPool;
0079 
0080     // 0) setup
0081     Parser factParser;
0082     factParser.setTokenStream(&tokenStream);
0083     factParser.setMemoryPool(&memoryPool);
0084     factParser.setDebug( debug );
0085 
0086     // 1) tokenize
0087     factParser.tokenize(contents);
0088 
0089     // 2) parse
0090     ProgramAst *ast = 0;
0091     bool matched = factParser.parseProgram(&ast);
0092     if (matched)
0093     {
0094         if (debug) {
0095             DebugVisitor d(&tokenStream);
0096             d.visitNode(ast);
0097         }
0098     }
0099     else
0100     {
0101         factParser.expectedSymbol(AstNode::ProgramKind, "program");
0102     }
0103 
0104     return matched;
0105 }
0106 
0107 static void usage(char const* argv0)
0108 {
0109     qDebug() << "usage: " << argv0 << "--debug file1.f [file2.f...]";
0110 }
0111 
0112 // kate: space-indent on; indent-width 4; tab-width 4; replace-tabs on