summaryrefslogtreecommitdiffstats
path: root/buildtools/lib/parsers/qmake/tests/runner.cpp
blob: b01284f1f9560a577f6d8f388177c1d7c50c554c (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/***************************************************************************
 *   Copyright (C) 2005 by Alexander Dymo                                  *
 *   [email protected]                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include "qmakedriver.h"
#include "qmakeastvisitor.h"

#include <qstring.h>

#include <kdebug.h>
#include <kcmdlineargs.h>
#include <kurl.h>

static const KCmdLineOptions options[] =
{
    {"silent", "Enable Parser debug output", 0},
    {"!debug", "Disable output of the generated AST", 0},
    {"!+files", "QMake project files", 0}
};


class PrintAST : QMake::ASTVisitor
{
public:
  PrintAST() : QMake::ASTVisitor()
  {
      indent = 0;
  }

  virtual void processProject( QMake::ProjectAST* p )
  {
      QMake::ASTVisitor::processProject(p);
  }
private:
  virtual void enterRealProject( QMake::ProjectAST* p )
  {
      kdDebug(9024) << getIndent() << "--------- Entering Project: " << replaceWs(p->fileName()) << "| LineEnding:" << p->lineEnding() << " --------------" << endl;
      indent += 4;
      QMake::ASTVisitor::enterRealProject(p);
  }

  virtual void leaveRealProject( QMake::ProjectAST* p )
  {
      indent -= 4;
      kdDebug(9024) << getIndent() << "--------- Leaving Project: " << replaceWs(p->fileName()) << " --------------" << endl;
      QMake::ASTVisitor::leaveRealProject(p);
  }

  virtual void enterScope( QMake::ProjectAST* p )
  {
      kdDebug(9024) << getIndent() << "--------- Entering Scope: " << replaceWs(p->scopedID) << " --------------" << endl;
      indent += 4;
      QMake::ASTVisitor::enterScope(p);
  }

  virtual void leaveScope( QMake::ProjectAST* p )
  {
      indent -= 4;
      kdDebug(9024) << getIndent() << "--------- Leaving Scope: " << replaceWs(p->scopedID) << " --------------" << endl;
      QMake::ASTVisitor::leaveScope(p);
  }

  virtual void enterFunctionScope( QMake::ProjectAST* p )
  {
      kdDebug(9024) << getIndent() << "--------- Entering FunctionScope: " << replaceWs(p->scopedID) << "(" << replaceWs(p->args) << ")"<< " --------------" << endl;
      indent += 4;
      QMake::ASTVisitor::enterFunctionScope(p);
  }

  virtual void leaveFunctionScope( QMake::ProjectAST* p )
  {
      indent -= 4;
      kdDebug(9024) << getIndent() << "--------- Leaving FunctionScope: " << replaceWs(p->scopedID) << "(" << replaceWs(p->args) << ")"<< " --------------" << endl;
      QMake::ASTVisitor::leaveFunctionScope(p);
  }

  QString replaceWs(QString s)
  {
      return s.replace("\n", "%nl").replace("\t", "%tab").replace(" ", "%spc");
  }

  virtual void processAssignment( QMake::AssignmentAST* a)
  {
      kdDebug(9024) << getIndent() << "Assignment(" << replaceWs(a->indent) << "):" << replaceWs(a->scopedID) << " " << replaceWs(a->op) << " " << replaceWs(a->values.join("|")) << endl;
      QMake::ASTVisitor::processAssignment(a);
  }

  virtual void processNewLine( QMake::NewLineAST* n)
  {
      kdDebug(9024) << getIndent() << "Newline " << endl;
      QMake::ASTVisitor::processNewLine(n);
  }

  virtual void processComment( QMake::CommentAST* a)
  {
      kdDebug(9024) << getIndent() << "Comment: " << replaceWs(a->comment) << endl;
      QMake::ASTVisitor::processComment(a);
  }

  virtual void processInclude( QMake::IncludeAST* a)
  {
      kdDebug(9024) << getIndent() << "Include: " << replaceWs(a->projectName) << endl;
      QMake::ASTVisitor::processInclude(a);
  }

  QString getIndent()
  {
      QString ind;
      for( int i = 0 ; i < indent ; i++)
          ind += " ";
      return ind;
  }
  int indent;
};
int main(int argc, char *argv[])
{
  KCmdLineArgs::init( argc, argv, "QMake Parser", "qmake-parser", "Parse QMake project files", "1.0.0");
    KCmdLineArgs::addCmdLineOptions(options);

    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();

    if( args->count() < 1 )
    {
        KCmdLineArgs::usage(0);
    }

    int debug = 0;
    bool silent = false;

    if( args->isSet("silent") )
        silent = true;
    if( args->isSet("debug") )
        debug = 1;
    for( int i = 0 ; i < args->count() ; i++ )
    {
        QMake::ProjectAST *projectAST;
        int ret = QMake::Driver::parseFile(args->url(i).path(), &projectAST, debug);
        PrintAST pa;
        if ( ret == 0 )
            if ( !silent )
            {
                pa.processProject(projectAST);
                QString profile;
                projectAST->writeBack(profile);
                kdDebug(9024) << "QMake file written back:\n" << profile << endl;
            }
        return ret;
    }
    return 0;
}