1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the qt3to4 porting application of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "rpptreewalker.h"
43 
44 QT_BEGIN_NAMESPACE
45 
46 namespace Rpp {
47 
evaluateItem(const Item * item)48 void RppTreeWalker::evaluateItem(const Item *item)
49 {
50     if(!item)
51         return;
52     if (Source *source = item->toSource())
53         evaluateSource(source);
54     else if (Directive *directive = item->toDirective())
55         evaluateDirective(directive);
56     else if (IfSection *ifSection = item->toIfSection())
57         evaluateIfSection(ifSection);
58     else if (Text *text = item->toText())
59         evaluateText(text);
60 }
61 
evaluateItemComposite(const ItemComposite * itemComposite)62 void RppTreeWalker::evaluateItemComposite(const ItemComposite *itemComposite)
63 {
64     if (!itemComposite)
65         return;
66     for (int i = 0; i < itemComposite->count(); ++i) {
67         evaluateItem(itemComposite->item(i));
68     }
69 }
70 
evaluateSource(const Source * source)71 void RppTreeWalker::evaluateSource(const Source *source)
72 {
73     evaluateItemComposite(source->toItemComposite());
74 }
75 
evaluateDirective(const Directive * directive)76 void RppTreeWalker::evaluateDirective(const Directive *directive)
77 {
78     if (!directive)
79         return;
80     if (EmptyDirective *dir = directive->toEmptyDirective())
81         evaluateEmptyDirective(dir);
82     else if (ErrorDirective *dir = directive->toErrorDirective())
83         evaluateErrorDirective(dir);
84     else if (PragmaDirective *dir = directive->toPragmaDirective())
85         evaluatePragmaDirective(dir);
86     else if (IncludeDirective *dir = directive->toIncludeDirective())
87         evaluateIncludeDirective(dir);
88     else if (DefineDirective *dir = directive->toDefineDirective())
89         evaluateDefineDirective(dir);
90     else if (UndefDirective *dir = directive->toUndefDirective())
91         evaluateUndefDirective(dir);
92     else if (LineDirective *dir = directive->toLineDirective())
93         evaluateLineDirective(dir);
94     else if (NonDirective *dir = directive->toNonDirective())
95         evaluateNonDirective(dir);
96     else if (NonDirective *dir = directive->toNonDirective())
97         evaluateNonDirective(dir);
98     else if (ConditionalDirective *dir = directive->toConditionalDirective())
99         evaluateConditionalDirective(dir);
100 }
101 
102 /*
103     This function evaluates all the branches of an IfSection. You should
104     override it if you want to only evaluate the "correct" branch.
105 */
evaluateIfSection(const IfSection * ifSection)106 void RppTreeWalker::evaluateIfSection(const IfSection *ifSection)
107 {
108     if (!ifSection)
109         return;
110     evaluateItemComposite(ifSection->toItemComposite());
111 }
112 
evaluateConditionalDirective(const ConditionalDirective * conditionalDirective)113 void RppTreeWalker::evaluateConditionalDirective(const ConditionalDirective *conditionalDirective)
114 {
115     if (!conditionalDirective)
116         return;
117     if (IfdefDirective *dir = conditionalDirective->toIfdefDirective())
118          evaluateIfdefDirective(dir);
119     else if (IfndefDirective *dir = conditionalDirective->toIfndefDirective())
120          evaluateIfndefDirective(dir);
121     else if (IfDirective *dir = conditionalDirective->toIfDirective())
122          evaluateIfDirective(dir);
123     else if (ElifDirective *dir = conditionalDirective->toElifDirective())
124          evaluateElifDirective(dir);
125     else if (ElseDirective *dir = conditionalDirective->toElseDirective())
126          evaluateElseDirective(dir);
127 }
128 
evaluateIfdefDirective(const IfdefDirective * directive)129 void RppTreeWalker::evaluateIfdefDirective(const IfdefDirective *directive)
130 {
131    if (!directive)
132        return;
133    evaluateItemComposite(directive->toItemComposite());
134 }
135 
evaluateIfndefDirective(const IfndefDirective * directive)136 void RppTreeWalker::evaluateIfndefDirective(const IfndefDirective *directive)
137 {
138    if (!directive)
139        return;
140    evaluateItemComposite(directive->toItemComposite());
141 }
142 
evaluateIfDirective(const IfDirective * directive)143 void RppTreeWalker::evaluateIfDirective(const IfDirective *directive)
144 {
145    if (!directive)
146        return;
147    evaluateItemComposite(directive->toItemComposite());
148 }
149 
evaluateElifDirective(const ElifDirective * directive)150 void RppTreeWalker::evaluateElifDirective(const ElifDirective *directive)
151 {
152    if (!directive)
153        return;
154    evaluateItemComposite(directive->toItemComposite());
155 }
156 
evaluateElseDirective(const ElseDirective * directive)157 void RppTreeWalker::evaluateElseDirective(const ElseDirective *directive)
158 {
159    if (!directive)
160        return;
161    evaluateItemComposite(directive->toItemComposite());
162 }
163 
164 }
165 
166 QT_END_NAMESPACE
167