summaryrefslogtreecommitdiffstats
path: root/buildtools/lib/parsers/qmake/tests/runner.cpp
blob: 489dd754cd5b4b9c84010d2fab81a0026394edb7 (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                                  *
 *   adymo@tdevelop.org                                                    *
 *                                                                         *
 *   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.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include "tqmakedriver.h"
#include "tqmakeastvisitor.h"

#include <tqstring.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", "TQMake project files", 0}
};


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

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

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

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

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

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

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

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

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

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

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

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

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

    TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();

    if( args->count() < 1 )
    {
        TDECmdLineArgs::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++ )
    {
        TQMake::ProjectAST *projectAST;
        int ret = TQMake::Driver::parseFile(args->url(i).path(), &projectAST, debug);
        PrintAST pa;
        if ( ret == 0 )
            if ( !silent )
            {
                pa.processProject(projectAST);
                TQString profile;
                projectAST->writeBack(profile);
                kdDebug(9024) << "TQMake file written back:\n" << profile << endl;
            }
        return ret;
    }
    return 0;
}