|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the tools applications of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <qfileinfo.h> |
|
43 #include <qregexp.h> |
|
44 #include <qdebug.h> |
|
45 |
|
46 #include "quoter.h" |
|
47 |
|
48 QT_BEGIN_NAMESPACE |
|
49 |
|
50 static void replaceMultipleNewlines(QString &s) |
|
51 { |
|
52 const int n = s.size(); |
|
53 bool slurping = false; |
|
54 int j = -1; |
|
55 const QChar newLine = QLatin1Char('\n'); |
|
56 QChar *d = s.data(); |
|
57 for (int i = 0; i != n; ++i) { |
|
58 const QChar c = d[i]; |
|
59 bool hit = (c == newLine); |
|
60 if (slurping && hit) |
|
61 continue; |
|
62 d[++j] = c; |
|
63 slurping = hit; |
|
64 } |
|
65 s.resize(++j); |
|
66 } |
|
67 |
|
68 // This is equivalent to line.split( QRegExp("\n(?!\n|$)") ) but much faster |
|
69 static QStringList splitLines(const QString &line) |
|
70 { |
|
71 QStringList result; |
|
72 int i = line.size(); |
|
73 while (true) { |
|
74 int j = i - 1; |
|
75 while (j >= 0 && line.at(j) == QLatin1Char('\n')) |
|
76 --j; |
|
77 while (j >= 0 && line.at(j) != QLatin1Char('\n')) |
|
78 --j; |
|
79 result.prepend(line.mid(j + 1, i - j - 1)); |
|
80 if (j < 0) |
|
81 break; |
|
82 i = j; |
|
83 } |
|
84 return result; |
|
85 } |
|
86 |
|
87 /* |
|
88 Transforms 'int x = 3 + 4' into 'int x=3+4'. A white space is kept |
|
89 between 'int' and 'x' because it is meaningful in C++. |
|
90 */ |
|
91 static void trimWhiteSpace( QString& str ) |
|
92 { |
|
93 enum { Normal, MetAlnum, MetSpace } state = Normal; |
|
94 const int n = str.length(); |
|
95 |
|
96 int j = -1; |
|
97 QChar *d = str.data(); |
|
98 for ( int i = 0; i != n; ++i ) { |
|
99 const QChar c = d[i]; |
|
100 if ( c.isLetterOrNumber() ) { |
|
101 if ( state == Normal ) { |
|
102 state = MetAlnum; |
|
103 } else { |
|
104 if ( state == MetSpace ) |
|
105 str[++j] = c; |
|
106 state = Normal; |
|
107 } |
|
108 str[++j] = c; |
|
109 } else if ( c.isSpace() ) { |
|
110 if ( state == MetAlnum ) |
|
111 state = MetSpace; |
|
112 } else { |
|
113 state = Normal; |
|
114 str[++j] = c; |
|
115 } |
|
116 } |
|
117 str.resize(++j); |
|
118 } |
|
119 |
|
120 Quoter::Quoter() |
|
121 : silent( false ) |
|
122 { |
|
123 /* We're going to hard code these delimiters: |
|
124 * C++, Qt, Qt Script, Java: |
|
125 //! [<id>] |
|
126 * .pro files: |
|
127 #! [<id>] |
|
128 * .xq, .xml, .html files: |
|
129 <!-- [<id>] --> |
|
130 */ |
|
131 commentHash["pro"] = "#!"; |
|
132 commentHash["py"] = "#!"; |
|
133 commentHash["html"] = "<!--"; |
|
134 commentHash["qrc"] = "<!--"; |
|
135 commentHash["ui"] = "<!--"; |
|
136 commentHash["xml"] = "<!--"; |
|
137 commentHash["xq"] = "<!--"; |
|
138 } |
|
139 |
|
140 void Quoter::reset() |
|
141 { |
|
142 silent = false; |
|
143 plainLines.clear(); |
|
144 markedLines.clear(); |
|
145 codeLocation = Location::null; |
|
146 } |
|
147 |
|
148 void Quoter::quoteFromFile( const QString& userFriendlyFilePath, |
|
149 const QString& plainCode, |
|
150 const QString& markedCode ) |
|
151 { |
|
152 silent = false; |
|
153 |
|
154 /* |
|
155 Split the source code into logical lines. Empty lines are |
|
156 treated specially. Before: |
|
157 |
|
158 p->alpha(); |
|
159 p->beta(); |
|
160 |
|
161 p->gamma(); |
|
162 |
|
163 |
|
164 p->delta(); |
|
165 |
|
166 After: |
|
167 |
|
168 p->alpha(); |
|
169 p->beta();\n |
|
170 p->gamma();\n\n |
|
171 p->delta(); |
|
172 |
|
173 Newlines are preserved because they affect codeLocation. |
|
174 */ |
|
175 codeLocation = Location( userFriendlyFilePath ); |
|
176 |
|
177 plainLines = splitLines(plainCode); |
|
178 markedLines = splitLines(markedCode); |
|
179 if (markedLines.count() != plainLines.count()) { |
|
180 codeLocation.warning(tr("Something is wrong with qdoc's handling of marked code")); |
|
181 markedLines = plainLines; |
|
182 } |
|
183 |
|
184 /* |
|
185 Squeeze blanks (cat -s). |
|
186 */ |
|
187 QStringList::Iterator m = markedLines.begin(); |
|
188 while ( m != markedLines.end() ) { |
|
189 replaceMultipleNewlines( *m ); |
|
190 ++m; |
|
191 } |
|
192 codeLocation.start(); |
|
193 } |
|
194 |
|
195 QString Quoter::quoteLine( const Location& docLocation, const QString& command, |
|
196 const QString& pattern ) |
|
197 { |
|
198 if ( plainLines.isEmpty() ) { |
|
199 failedAtEnd( docLocation, command ); |
|
200 return QString(); |
|
201 } |
|
202 |
|
203 if ( pattern.isEmpty() ) { |
|
204 docLocation.warning( tr("Missing pattern after '\\%1'").arg(command) ); |
|
205 return QString(); |
|
206 } |
|
207 |
|
208 if ( match(docLocation, pattern, plainLines.first()) ) |
|
209 return getLine(); |
|
210 |
|
211 if ( !silent ) { |
|
212 docLocation.warning( tr("Command '\\%1' failed").arg(command) ); |
|
213 codeLocation.warning( tr("Pattern '%1' didn't match here") |
|
214 .arg(pattern) ); |
|
215 silent = true; |
|
216 } |
|
217 return QString(); |
|
218 } |
|
219 |
|
220 QString Quoter::quoteSnippet(const Location &docLocation, const QString &identifier) |
|
221 { |
|
222 QString comment = commentForCode(); |
|
223 QString delimiter = comment + QString(" [%1]").arg(identifier); |
|
224 QString t; |
|
225 |
|
226 while (!plainLines.isEmpty()) { |
|
227 if (match(docLocation, delimiter, plainLines.first())) { |
|
228 getLine(); |
|
229 break; |
|
230 } |
|
231 getLine(); |
|
232 } |
|
233 while (!plainLines.isEmpty()) { |
|
234 QString line = plainLines.first(); |
|
235 if (match(docLocation, delimiter, line)) { |
|
236 QString lastLine = getLine(); |
|
237 int dIndex = lastLine.indexOf(delimiter); |
|
238 if (dIndex > 0) { |
|
239 QString leading = lastLine.left(dIndex); |
|
240 dIndex = leading.indexOf(comment); |
|
241 if (dIndex != -1) |
|
242 leading = leading.left(dIndex); |
|
243 if (!leading.trimmed().isEmpty()) |
|
244 t += leading; |
|
245 } |
|
246 return t; |
|
247 } |
|
248 // Remove special macros to support Qt namespacing. |
|
249 if (line.startsWith("QT_BEGIN_NAMESPACE")) { |
|
250 getLine(); |
|
251 } else if (line.startsWith("QT_END_NAMESPACE")) { |
|
252 getLine(); |
|
253 t += QLatin1Char('\n'); |
|
254 } else if (!line.startsWith(comment)) { |
|
255 // Ordinary code |
|
256 t += getLine(); |
|
257 } else { |
|
258 // Normal comments |
|
259 if (line.contains(QLatin1Char('\n'))) |
|
260 t += QLatin1Char('\n'); |
|
261 getLine(); |
|
262 } |
|
263 } |
|
264 failedAtEnd(docLocation, QString("snippet (%1)").arg(delimiter)); |
|
265 return t; |
|
266 } |
|
267 |
|
268 QString Quoter::quoteTo( const Location& docLocation, const QString& command, |
|
269 const QString& pattern ) |
|
270 { |
|
271 QString t; |
|
272 QString comment = commentForCode(); |
|
273 |
|
274 if ( pattern.isEmpty() ) { |
|
275 while ( !plainLines.isEmpty() ) { |
|
276 QString line = plainLines.first(); |
|
277 // Remove special macros to support Qt namespacing. |
|
278 if (line.startsWith("QT_BEGIN_NAMESPACE")) { |
|
279 getLine(); |
|
280 } else if (line.startsWith("QT_END_NAMESPACE")) { |
|
281 getLine(); |
|
282 t += QLatin1Char('\n'); |
|
283 } else if (!line.startsWith(comment)) |
|
284 // Ordinary code |
|
285 t += getLine(); |
|
286 else { |
|
287 // Normal comments |
|
288 if (line.contains(QLatin1Char('\n'))) |
|
289 t += QLatin1Char('\n'); |
|
290 getLine(); |
|
291 } |
|
292 } |
|
293 } else { |
|
294 while ( !plainLines.isEmpty() ) { |
|
295 if ( match(docLocation, pattern, plainLines.first()) ) { |
|
296 return t; |
|
297 } |
|
298 t += getLine(); |
|
299 } |
|
300 failedAtEnd( docLocation, command ); |
|
301 } |
|
302 return t; |
|
303 } |
|
304 |
|
305 QString Quoter::quoteUntil( const Location& docLocation, const QString& command, |
|
306 const QString& pattern ) |
|
307 { |
|
308 QString t = quoteTo( docLocation, command, pattern ); |
|
309 t += getLine(); |
|
310 return t; |
|
311 } |
|
312 |
|
313 QString Quoter::getLine() |
|
314 { |
|
315 if ( plainLines.isEmpty() ) |
|
316 return QString(); |
|
317 |
|
318 plainLines.removeFirst(); |
|
319 |
|
320 QString t = markedLines.takeFirst(); |
|
321 t += QLatin1Char('\n'); |
|
322 codeLocation.advanceLines( t.count( QLatin1Char('\n') ) ); |
|
323 return t; |
|
324 } |
|
325 |
|
326 bool Quoter::match( const Location& docLocation, const QString& pattern0, |
|
327 const QString& line ) |
|
328 { |
|
329 QString str = line; |
|
330 while ( str.endsWith(QLatin1Char('\n')) ) |
|
331 str.truncate( str.length() - 1 ); |
|
332 |
|
333 QString pattern = pattern0; |
|
334 if ( pattern.startsWith(QLatin1Char('/')) |
|
335 && pattern.endsWith(QLatin1Char('/')) |
|
336 && pattern.length() > 2 ) { |
|
337 QRegExp rx( pattern.mid(1, pattern.length() - 2) ); |
|
338 if ( !silent && !rx.isValid() ) { |
|
339 docLocation.warning( tr("Invalid regular expression '%1'") |
|
340 .arg(rx.pattern()) ); |
|
341 silent = true; |
|
342 } |
|
343 return str.indexOf( rx ) != -1; |
|
344 } |
|
345 trimWhiteSpace(str); |
|
346 trimWhiteSpace(pattern); |
|
347 return str.indexOf(pattern) != -1; |
|
348 } |
|
349 |
|
350 void Quoter::failedAtEnd( const Location& docLocation, const QString& command ) |
|
351 { |
|
352 if (!silent && !command.isEmpty()) { |
|
353 if ( codeLocation.filePath().isEmpty() ) { |
|
354 docLocation.warning( tr("Unexpected '\\%1'").arg(command) ); |
|
355 } else { |
|
356 docLocation.warning( tr("Command '\\%1' failed at end of file '%2'") |
|
357 .arg(command).arg(codeLocation.filePath()) ); |
|
358 } |
|
359 silent = true; |
|
360 } |
|
361 } |
|
362 |
|
363 QString Quoter::commentForCode() const |
|
364 { |
|
365 QString suffix = QFileInfo(codeLocation.fileName()).suffix(); |
|
366 return commentHash.value(suffix, "//!"); |
|
367 } |
|
368 |
|
369 QT_END_NAMESPACE |