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 tools applications 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 /*
43   codeparser.cpp
44 */
45 
46 #include "codeparser.h"
47 #include "node.h"
48 #include "tree.h"
49 #include "config.h"
50 
51 QT_BEGIN_NAMESPACE
52 
53 #define COMMAND_COMPAT                  Doc::alias(QLatin1String("compat"))
54 #define COMMAND_DEPENDS                 Doc::alias(QLatin1String("depends"))
55 #define COMMAND_DEPRECATED              Doc::alias(QLatin1String("deprecated")) // ### don't document
56 #define COMMAND_INGROUP                 Doc::alias(QLatin1String("ingroup"))
57 #define COMMAND_INMODULE                Doc::alias(QLatin1String("inmodule"))  // ### don't document
58 #define COMMAND_INTERNAL                Doc::alias(QLatin1String("internal"))
59 #define COMMAND_MAINCLASS               Doc::alias(QLatin1String("mainclass"))
60 #define COMMAND_NONREENTRANT            Doc::alias(QLatin1String("nonreentrant"))
61 #define COMMAND_OBSOLETE                Doc::alias(QLatin1String("obsolete"))
62 #define COMMAND_PAGEKEYWORDS            Doc::alias(QLatin1String("pagekeywords"))
63 #define COMMAND_PRELIMINARY             Doc::alias(QLatin1String("preliminary"))
64 #define COMMAND_INPUBLICGROUP           Doc::alias(QLatin1String("inpublicgroup"))
65 #define COMMAND_REENTRANT               Doc::alias(QLatin1String("reentrant"))
66 #define COMMAND_SINCE                   Doc::alias(QLatin1String("since"))
67 #define COMMAND_SUBTITLE                Doc::alias(QLatin1String("subtitle"))
68 #define COMMAND_THREADSAFE              Doc::alias(QLatin1String("threadsafe"))
69 #define COMMAND_TITLE                   Doc::alias(QLatin1String("title"))
70 
71 QList<CodeParser *> CodeParser::parsers;
72 bool CodeParser::showInternal = false;
73 QMap<QString,QString> CodeParser::nameToTitle;
74 
75 /*!
76   The constructor adds this code parser to the static
77   list of code parsers.
78  */
CodeParser()79 CodeParser::CodeParser()
80 {
81     parsers.prepend(this);
82 }
83 
84 /*!
85   The destructor removes this code parser from the static
86   list of code parsers.
87  */
~CodeParser()88 CodeParser::~CodeParser()
89 {
90     parsers.removeAll(this);
91 }
92 
93 /*!
94   Initialize the code parser base class.
95  */
initializeParser(const Config & config)96 void CodeParser::initializeParser(const Config& config)
97 {
98     showInternal = config.getBool(QLatin1String(CONFIG_SHOWINTERNAL));
99 }
100 
101 /*!
102   Terminating a code parser is trivial.
103  */
terminateParser()104 void CodeParser::terminateParser()
105 {
106     // nothing.
107 }
108 
headerFileNameFilter()109 QStringList CodeParser::headerFileNameFilter()
110 {
111     return sourceFileNameFilter();
112 }
113 
parseHeaderFile(const Location & location,const QString & filePath,Tree * tree)114 void CodeParser::parseHeaderFile(const Location& location,
115                                  const QString& filePath,
116                                  Tree *tree)
117 {
118     parseSourceFile(location, filePath, tree);
119 }
120 
doneParsingHeaderFiles(Tree * tree)121 void CodeParser::doneParsingHeaderFiles(Tree *tree)
122 {
123     doneParsingSourceFiles(tree);
124 }
125 
126 /*!
127   All the code parsers in the static list are initialized here,
128   after the qdoc configuration variables have been set.
129  */
initialize(const Config & config)130 void CodeParser::initialize(const Config& config)
131 {
132     QList<CodeParser *>::ConstIterator p = parsers.begin();
133     while (p != parsers.end()) {
134 	(*p)->initializeParser(config);
135 	++p;
136     }
137 }
138 
139 /*!
140   All the code parsers in the static list are terminated here.
141  */
terminate()142 void CodeParser::terminate()
143 {
144     QList<CodeParser *>::ConstIterator p = parsers.begin();
145     while (p != parsers.end()) {
146 	(*p)->terminateParser();
147 	++p;
148     }
149 }
150 
parserForLanguage(const QString & language)151 CodeParser *CodeParser::parserForLanguage(const QString& language)
152 {
153     QList<CodeParser *>::ConstIterator p = parsers.begin();
154     while (p != parsers.end()) {
155 	if ((*p)->language() == language)
156 	    return *p;
157 	++p;
158     }
159     return 0;
160 }
161 
parserForHeaderFile(const QString & filePath)162 CodeParser *CodeParser::parserForHeaderFile(const QString &filePath)
163 {
164     QString fileName = QFileInfo(filePath).fileName();
165 
166     QList<CodeParser *>::ConstIterator p = parsers.begin();
167     while (p != parsers.end()) {
168 
169 	QStringList headerPatterns = (*p)->headerFileNameFilter();
170 	foreach (QString pattern, headerPatterns) {
171             QRegExp re(pattern, Qt::CaseInsensitive, QRegExp::Wildcard);
172             if (re.exactMatch(fileName))
173                 return *p;
174         }
175 	++p;
176     }
177     return 0;
178 }
179 
parserForSourceFile(const QString & filePath)180 CodeParser *CodeParser::parserForSourceFile(const QString &filePath)
181 {
182     QString fileName = QFileInfo(filePath).fileName();
183 
184     QList<CodeParser *>::ConstIterator p = parsers.begin();
185     while (p != parsers.end()) {
186 
187 	QStringList sourcePatterns = (*p)->sourceFileNameFilter();
188 	foreach (QString pattern, sourcePatterns) {
189             QRegExp re(pattern, Qt::CaseInsensitive, QRegExp::Wildcard);
190             if (re.exactMatch(fileName))
191                 return *p;
192         }
193 	++p;
194     }
195     return 0;
196 }
197 
198 /*!
199   Returns the set of strings representing the common metacommands.
200  */
commonMetaCommands()201 QSet<QString> CodeParser::commonMetaCommands()
202 {
203     return QSet<QString>() << COMMAND_COMPAT
204                            << COMMAND_DEPENDS
205                            << COMMAND_DEPRECATED
206                            << COMMAND_INGROUP
207                            << COMMAND_INMODULE
208                            << COMMAND_INTERNAL
209                            << COMMAND_MAINCLASS
210                            << COMMAND_NONREENTRANT
211                            << COMMAND_OBSOLETE
212                            << COMMAND_PAGEKEYWORDS
213                            << COMMAND_PRELIMINARY
214                            << COMMAND_INPUBLICGROUP
215                            << COMMAND_REENTRANT
216                            << COMMAND_SINCE
217                            << COMMAND_SUBTITLE
218                            << COMMAND_THREADSAFE
219                            << COMMAND_TITLE;
220 }
221 
222 /*!
223   The topic command has been processed. Now process the other
224   metacommands that were found. These are not the text markup
225   commands.
226  */
processCommonMetaCommand(const Location & location,const QString & command,const QString & arg,Node * node,Tree * tree)227 void CodeParser::processCommonMetaCommand(const Location &location,
228                                           const QString &command,
229 					  const QString &arg,
230                                           Node *node,
231                                           Tree *tree)
232 {
233     if (command == COMMAND_COMPAT) {
234         node->setStatus(Node::Compat);
235     }
236     else if (command == COMMAND_DEPENDS) {
237 	node->addDependency(arg);
238     }
239     else if (command == COMMAND_DEPRECATED) {
240 	node->setStatus(Node::Deprecated);
241     }
242     else if (command == COMMAND_INGROUP) {
243 	tree->addToGroup(node, arg);
244     }
245     else if (command == COMMAND_INPUBLICGROUP) {
246         tree->addToPublicGroup(node, arg);
247     }
248     else if (command == COMMAND_INMODULE) {
249 	node->setModuleName(arg);
250     }
251     else if (command == COMMAND_MAINCLASS) {
252 	node->setStatus(Node::Main);
253     }
254     else if (command == COMMAND_OBSOLETE) {
255         if (node->status() != Node::Compat)
256             node->setStatus(Node::Obsolete);
257     }
258     else if (command == COMMAND_NONREENTRANT) {
259 	node->setThreadSafeness(Node::NonReentrant);
260     }
261     else if (command == COMMAND_PRELIMINARY) {
262 	node->setStatus(Node::Preliminary);
263     }
264     else if (command == COMMAND_INTERNAL) {
265         if (!showInternal) {
266             node->setAccess(Node::Private);
267             node->setStatus(Node::Internal);
268         }
269     }
270     else if (command == COMMAND_REENTRANT) {
271 	node->setThreadSafeness(Node::Reentrant);
272     }
273     else if (command == COMMAND_SINCE) {
274         node->setSince(arg);
275     }
276     else if (command == COMMAND_PAGEKEYWORDS) {
277         node->addPageKeywords(arg);
278     }
279     else if (command == COMMAND_SUBTITLE) {
280 	if (node->type() == Node::Fake) {
281 	    FakeNode *fake = static_cast<FakeNode *>(node);
282             fake->setSubTitle(arg);
283         }
284         else
285             location.warning(tr("Ignored '\\%1'").arg(COMMAND_SUBTITLE));
286     }
287     else if (command == COMMAND_THREADSAFE) {
288 	node->setThreadSafeness(Node::ThreadSafe);
289     }
290     else if (command == COMMAND_TITLE) {
291 	if (node->type() == Node::Fake) {
292 	    FakeNode *fake = static_cast<FakeNode *>(node);
293             fake->setTitle(arg);
294             nameToTitle.insert(fake->name(),arg);
295             if (fake->subType() == Node::Example) {
296 
297             }
298         }
299         else
300 	    location.warning(tr("Ignored '\\%1'").arg(COMMAND_TITLE));
301     }
302 }
303 
304 /*!
305   Find the page title given the page \a name and return it.
306  */
titleFromName(const QString & name)307 const QString CodeParser::titleFromName(const QString& name)
308 {
309     const QString t = nameToTitle.value(name);
310     return t;
311 }
312 
313 QT_END_NAMESPACE
314