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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
|
/*****************************************************************************
*
*
*
* Copyright (C) 1997-2008 by Dimitri van Heesch.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation under the terms of the GNU General Public License is hereby
* granted. No representations are made about the suitability of this software
* for any purpose. It is provided "as is" without express or implied warranty.
* See the GNU General Public License for more details.
*
* Documents produced by Doxygen are derivative works derived from the
* input used in their production; they are not affected by this license.
*
*/
%{
#define YY_NEVER_INTERACTIVE 1
#include <stdio.h>
#include <stdlib.h>
#include <qstack.h>
#include <qregexp.h>
#include <qtextstream.h>
#include "bufstr.h"
#include "debug.h"
#include "message.h"
#include "config.h"
#include "doxygen.h"
#define ADDCHAR(c) g_outBuf->addChar(c)
#define ADDARRAY(a,s) g_outBuf->addArray(a,s)
struct CondCtx
{
CondCtx(int line,QCString id,bool b)
: lineNr(line),sectionId(id), skip(b) {}
int lineNr;
QCString sectionId;
bool skip;
};
static BufStr * g_inBuf;
static BufStr * g_outBuf;
static int g_inBufPos;
static int g_col;
static int g_blockHeadCol;
static bool g_mlBrief;
static int g_readLineCtx;
static bool g_skip;
static QCString g_fileName;
static int g_lineNr;
static int g_condCtx;
static QStack<CondCtx> g_condStack;
static QCString g_blockName;
static int g_lastCommentContext;
static bool g_inSpecialComment;
static QCString g_aliasString;
static int g_blockCount;
static int g_lastBlockContext;
static SrcLangExt g_lang;
static void replaceCommentMarker(const char *s,int len)
{
const char *p=s;
char c;
// copy blanks
while ((c=*p) && (c==' ' || c=='\t' || c=='\n'))
{
ADDCHAR(c);
g_lineNr += c=='\n';
p++;
}
// replace start of comment marker by spaces
while ((c=*p) && (c=='/' || c=='!' || c=='#'))
{
ADDCHAR(' ');
p++;
if (*p=='<') // comment-after-item marker
{
ADDCHAR(' ');
p++;
}
if (c=='!') // end after first !
{
break;
}
}
// copy comment line to output
ADDARRAY(p,len-(p-s));
}
static inline int computeIndent(const char *s)
{
int col=0;
static int tabSize=Config_getInt("TAB_SIZE");
const char *p=s;
char c;
while ((c=*p++))
{
if (c==' ') col++;
else if (c=='\t') col+=tabSize-(col%tabSize);
else break;
}
return col;
}
static inline void copyToOutput(const char *s,int len)
{
int i;
if (g_skip) // only add newlines.
{
for (i=0;i<len;i++)
{
if (s[i]=='\n')
{
ADDCHAR('\n');
g_lineNr++;
}
}
}
else
{
ADDARRAY(s,len);
static int tabSize=Config_getInt("TAB_SIZE");
for (i=0;i<len;i++)
{
switch (s[i])
{
case '\n': g_col=0; g_lineNr++; break;
case '\t': g_col+=tabSize-(g_col%tabSize); break;
default: g_col++; break;
}
}
}
}
static void startCondSection(const char *sectId)
{
g_condStack.push(new CondCtx(g_lineNr,sectId,g_skip));
if (Config_getList("ENABLED_SECTIONS").find(sectId)!=-1)
{
//printf("*** Section is enabled!\n");
}
else
{
//printf("*** Section is disabled!\n");
g_skip=TRUE;
}
}
static void endCondSection()
{
if (g_condStack.isEmpty())
{
warn(g_fileName,g_lineNr,"Found \\endcond command without matching \\cond");
g_skip=FALSE;
}
else
{
CondCtx *ctx = g_condStack.pop();
g_skip=ctx->skip;
}
}
#if 0
/** remove and executes cond and endcond commands in \a s */
static QCString handleCondCmdInAliases(const QCString &s)
{
QCString result;
//printf("handleCondCmdInAliases(%s)\n",s.data());
static QRegExp cmdPat("[\\\\@][a-z_A-Z][a-z_A-Z0-9]*");
int p=0,i,l;
while ((i=cmdPat.match(s,p,&l))!=-1)
{
result+=s.mid(p,i-p);
QCString cmd = s.mid(i+1,l-1);
//printf("Found command %s\n",cmd.data());
if (cmd=="cond")
{
int sp=i+l,ep;
const char *arg=s.data()+sp;
char c;
// skip spaces
while ((c=*arg) && (c==' ' || c=='\t')) arg++,sp++;
// read argument
if (*arg=='\n') // no arg
{
startCondSection(" ");
ep=sp;
}
else // get argument
{
ep=sp;
while ((c=*arg) && isId(c)) arg++,ep++;
if (ep>sp)
{
QCString id = s.mid(sp,ep-sp);
//printf("Found conditional section id %s\n",id.data());
startCondSection(id);
}
else // invalid identifier
{
}
}
p=ep;
}
else if (cmd=="endcond")
{
endCondSection();
p=i+l;
}
else
{
result+=s.mid(i,l);
p=i+l;
}
}
result+=s.right(s.length()-p);
return result;
}
#endif
/** copies string \a s with length \a len to the output, while
* replacing any alias commands found in the string.
*/
static void replaceAliases(const char *s)
{
QCString result = resolveAliasCmd(s);
//printf("replaceAliases(%s)->'%s'\n",s,result.data());
copyToOutput(result,result.length());
}
#undef YY_INPUT
#define YY_INPUT(buf,result,max_size) result=yyread(buf,max_size);
static int yyread(char *buf,int max_size)
{
int bytesInBuf = g_inBuf->curPos()-g_inBufPos;
int bytesToCopy = QMIN(max_size,bytesInBuf);
memcpy(buf,g_inBuf->data()+g_inBufPos,bytesToCopy);
g_inBufPos+=bytesToCopy;
return bytesToCopy;
}
void replaceComment(int offset);
%}
%option noyywrap
%x Scan
%x SkipString
%x SkipChar
%x SComment
%x CComment
%x Verbatim
%x VerbatimCode
%x ReadLine
%x CondLine
%x ReadAliasArgs
%%
<Scan>[^"'\/\n\\#\\-]* { /* eat anything that is not " / or \n */
copyToOutput(yytext,yyleng);
}
<Scan>"\"" { /* start of a string */
copyToOutput(yytext,yyleng);
BEGIN(SkipString);
}
<Scan>' {
copyToOutput(yytext,yyleng);
BEGIN(SkipChar);
}
<Scan>\n { /* new line */
copyToOutput(yytext,yyleng);
}
<Scan>("//!"|"///").*/\n[ \t]*"//"[\/!][^\/] { /* start C++ style special comment block */
if (g_mlBrief) REJECT; // bail out if we do not need to convert
int i=3;
if (yytext[2]=='/')
{
while (i<yyleng && yytext[i]=='/') i++;
}
g_blockHeadCol=g_col;
copyToOutput("/**",3);
replaceAliases(yytext+i);
g_inSpecialComment=TRUE;
BEGIN(SComment);
}
<Scan>"//##Documentation".*/\n { /* Start of Rational Rose ANSI C++ comment block */
if (g_mlBrief) REJECT;
int i=17; //=strlen("//##Documentation");
g_blockHeadCol=g_col;
copyToOutput("/**",3);
replaceAliases(yytext+i);
BEGIN(SComment);
}
<Scan>"//"/.*\n { /* one line C++ comment */
copyToOutput(yytext,yyleng);
g_readLineCtx=YY_START;
BEGIN(ReadLine);
}
<Scan>"/*" { /* start of a C comment */
copyToOutput(yytext,yyleng);
BEGIN(CComment);
}
<Scan>"##" {
if (g_lang!=SrcLangExt_Python)
{
REJECT;
}
else
{
copyToOutput(yytext,yyleng);
BEGIN(CComment);
}
}
<Scan>"--!" {
if (g_lang!=SrcLangExt_VHDL)
{
REJECT;
}
else
{
copyToOutput(yytext,yyleng);
BEGIN(CComment);
}
}
<CComment,ReadLine>[\\@]("dot"|"code"|"msc")/[^a-z_A-Z0-9] { /* start of a verbatim block */
copyToOutput(yytext,yyleng);
g_lastCommentContext = YY_START;
g_blockName=&yytext[1];
BEGIN(VerbatimCode);
}
<CComment,ReadLine>[\\@]("f$"|"f["|"f{"[a-z]*) {
copyToOutput(yytext,yyleng);
g_blockName=&yytext[1];
if (g_blockName.at(1)=='[')
{
g_blockName.at(1)=']';
}
else if (g_blockName.at(1)=='{')
{
g_blockName.at(1)='}';
}
g_lastCommentContext = YY_START;
BEGIN(Verbatim);
}
<CComment,ReadLine>[\\@]("verbatim"|"latexonly"|"htmlonly"|"xmlonly"|"rtfonly"|"manonly")/[^a-z_A-Z0-9] { /* start of a verbatim block */
copyToOutput(yytext,yyleng);
g_blockName=&yytext[1];
g_lastCommentContext = YY_START;
BEGIN(Verbatim);
}
<Scan>. { /* any other character */
copyToOutput(yytext,yyleng);
}
<Verbatim>[\\@]("endverbatim"|"endlatexonly"|"endhtmlonly"|"endxmlonly"|"endrtfonly"|"endmanonly"|"f$"|"f]"|"f}") { /* end of verbatim block */
copyToOutput(yytext,yyleng);
if (yytext[1]=='f') // end of formula
{
BEGIN(g_lastCommentContext);
}
else if (&yytext[4]==g_blockName)
{
BEGIN(g_lastCommentContext);
}
}
<VerbatimCode>[\\@]("enddot"|"endcode"|"endmsc") { /* end of verbatim block */
copyToOutput(yytext,yyleng);
if (&yytext[4]==g_blockName)
{
BEGIN(g_lastCommentContext);
}
}
<VerbatimCode>^[ \t]*"//"[\!\/]? { /* skip leading comments */
if (!g_inSpecialComment)
{
copyToOutput(yytext,yyleng);
}
}
<Verbatim,VerbatimCode>[^@\/\\\n]* { /* any character not a backslash or new line */
copyToOutput(yytext,yyleng);
}
<Verbatim,VerbatimCode>\n { /* new line in verbatim block */
copyToOutput(yytext,yyleng);
}
<Verbatim,VerbatimCode>. { /* any other character */
copyToOutput(yytext,yyleng);
}
<SkipString>\\. { /* escaped character in string */
copyToOutput(yytext,yyleng);
}
<SkipString>"\"" { /* end of string */
copyToOutput(yytext,yyleng);
BEGIN(Scan);
}
<SkipString>. { /* any other string character */
copyToOutput(yytext,yyleng);
}
<SkipString>\n { /* new line inside string (illegal for some compilers) */
copyToOutput(yytext,yyleng);
}
<SkipChar>\\. { /* escaped character */
copyToOutput(yytext,yyleng);
}
<SkipChar>' { /* end of character literal */
copyToOutput(yytext,yyleng);
BEGIN(Scan);
}
<SkipChar>. { /* any other string character */
copyToOutput(yytext,yyleng);
}
<SkipChar>\n { /* new line character */
copyToOutput(yytext,yyleng);
}
<CComment>[^\\@*\n]* { /* anything that is not a '*' or command */
copyToOutput(yytext,yyleng);
}
<CComment>"*"+[^*/\\@\n]* { /* stars without slashes */
copyToOutput(yytext,yyleng);
}
<CComment>\n { /* new line in comment */
copyToOutput(yytext,yyleng);
}
<CComment>"*"+"/" { /* end of C comment */
copyToOutput(yytext,yyleng);
BEGIN(Scan);
}
<CComment>"\n"/[ \t]*[^#] { /* end of Python comment */
if (g_lang!=SrcLangExt_Python)
{
REJECT;
}
else
{
copyToOutput(yytext,yyleng);
BEGIN(Scan);
}
}
<CComment>"\n"/[ \t]*[^\-] { /* end of VHDL comment */
if (g_lang!=SrcLangExt_VHDL)
{
REJECT;
}
else
{
copyToOutput(yytext,yyleng);
BEGIN(Scan);
}
}
<CComment>. {
copyToOutput(yytext,yyleng);
}
<SComment>^[ \t]*"///"[\/]*/\n {
replaceComment(0);
}
<SComment>\n[ \t]*"///"[\/]*/\n {
replaceComment(1);
}
<SComment>^[ \t]*"///"[^\/\n]/.*\n {
replaceComment(0);
g_readLineCtx=YY_START;
BEGIN(ReadLine);
}
<SComment>\n[ \t]*"///"[^\/\n]/.*\n {
replaceComment(1);
g_readLineCtx=YY_START;
BEGIN(ReadLine);
}
<SComment>^[ \t]*"//!" | // just //!
<SComment>^[ \t]*"//!<"/.*\n | // or //!< something
<SComment>^[ \t]*"//!"[^<]/.*\n { // or //!something
replaceComment(0);
g_readLineCtx=YY_START;
BEGIN(ReadLine);
}
<SComment>\n[ \t]*"//!" |
<SComment>\n[ \t]*"//!<"/.*\n |
<SComment>\n[ \t]*"//!"[^<\n]/.*\n {
replaceComment(1);
g_readLineCtx=YY_START;
BEGIN(ReadLine);
}
<SComment>^[ \t]*"//##"/.*\n {
replaceComment(0);
g_readLineCtx=YY_START;
BEGIN(ReadLine);
}
<SComment>\n[ \t]*"//##"/.*\n {
replaceComment(1);
g_readLineCtx=YY_START;
BEGIN(ReadLine);
}
<SComment>\n { /* end of special comment */
copyToOutput(" */",3);
copyToOutput(yytext,yyleng);
g_inSpecialComment=FALSE;
BEGIN(Scan);
}
<ReadLine>[^\\@\n]*/\n {
copyToOutput(yytext,yyleng);
BEGIN(g_readLineCtx);
}
<CComment,ReadLine>[\\@][\\@][~a-z_A-Z][a-z_A-Z0-9]*[ \t]* { // escaped command
copyToOutput(yytext,yyleng);
}
<CComment,ReadLine>[\\@]"cond"[ \t]+ { // conditional section
g_condCtx = YY_START;
BEGIN(CondLine);
}
<CComment,ReadLine>[\\@]"endcond"/[^a-z_A-Z0-9] { // end of conditional section
bool oldSkip=g_skip;
endCondSection();
if (YY_START==CComment && oldSkip && !g_skip)
{
//printf("** Adding start of comment!\n");
if (g_lang!=SrcLangExt_Python &&
g_lang!=SrcLangExt_VHDL)
{
ADDCHAR('/');
ADDCHAR('*');
}
}
}
<CondLine>[a-z_A-Z][a-z_A-Z0-9.\-]* {
bool oldSkip=g_skip;
startCondSection(yytext);
if (g_condCtx==CComment && !oldSkip && g_skip)
{
//printf("** Adding terminator for comment!\n");
if (g_lang!=SrcLangExt_Python &&
g_lang!=SrcLangExt_VHDL)
{
ADDCHAR('*');
ADDCHAR('/');
}
}
BEGIN(g_condCtx);
}
<CondLine>[ \t]*
<CComment,ReadLine>[\\@]"cond"[ \t\r]*\n |
<CondLine>. { // forgot section id?
if (YY_START!=CondLine) g_condCtx=YY_START;
bool oldSkip=g_skip;
startCondSection(" "); // fake section id causing the section to be hidden unconditionally
if (g_condCtx==CComment && !oldSkip && g_skip)
{
//printf("** Adding terminator for comment!\n");
if (g_lang!=SrcLangExt_Python &&
g_lang!=SrcLangExt_VHDL)
{
ADDCHAR('*');
ADDCHAR('/');
}
}
if (*yytext=='\n') g_lineNr++;
BEGIN(g_condCtx);
}
<CComment,ReadLine>[\\@][a-z_A-Z][a-z_A-Z0-9]* { // expand alias without arguments
replaceAliases(yytext);
}
<CComment,ReadLine>[\\@][a-z_A-Z][a-z_A-Z0-9]*"{" { // expand alias with arguments
g_lastBlockContext=YY_START;
g_blockCount=1;
g_aliasString=yytext;
BEGIN( ReadAliasArgs );
}
<ReadAliasArgs>[^{}\n\*]+ {
g_aliasString+=yytext;
}
<ReadAliasArgs>\n {
g_aliasString+=yytext;
g_lineNr++;
}
<ReadAliasArgs>"{" {
g_aliasString+=yytext;
g_blockCount++;
}
<ReadAliasArgs>"}" {
g_aliasString+=yytext;
g_blockCount--;
if (g_blockCount==0)
{
replaceAliases(g_aliasString);
BEGIN( g_lastBlockContext );
}
}
<ReadAliasArgs>. {
g_aliasString+=yytext;
}
<ReadLine>. {
copyToOutput(yytext,yyleng);
}
%%
void replaceComment(int offset)
{
if (g_mlBrief)
{
copyToOutput(yytext,yyleng);
}
else
{
//printf("replaceComment(%s)\n",yytext);
int i=computeIndent(&yytext[offset]);
if (i==g_blockHeadCol)
{
replaceCommentMarker(yytext,yyleng);
}
else
{
copyToOutput(" */",3);
int i;for (i=yyleng-1;i>=0;i--) unput(yytext[i]);
BEGIN(Scan);
}
}
}
/*! This function does three things:
* -# It converts multi-line C++ style comment blocks (that are aligned)
* to C style comment blocks (if MULTILINE_CPP_IS_BRIEF is set to NO).
* -# It replaces aliases with their definition (see ALIASES)
* -# It handles conditional sections (cond...endcond blocks)
*/
void convertCppComments(BufStr *inBuf,BufStr *outBuf,const char *fileName)
{
//printf("convertCppComments(%s)\n",fileName);
g_inBuf = inBuf;
g_outBuf = outBuf;
g_inBufPos = 0;
g_col = 0;
g_mlBrief = Config_getBool("MULTILINE_CPP_IS_BRIEF");
g_skip = FALSE;
g_fileName = fileName;
g_lang = getLanguageFromFileName(fileName);
g_lineNr = 0;
g_condStack.clear();
g_condStack.setAutoDelete(TRUE);
BEGIN(Scan);
yylex();
while (!g_condStack.isEmpty())
{
CondCtx *ctx = g_condStack.pop();
QCString sectionInfo = " ";
if (ctx->sectionId!=" ") sectionInfo.sprintf(" with label %s ",ctx->sectionId.data());
warn(g_fileName,ctx->lineNr,"Conditional section%sdoes not have "
"a corresponding \\endcond command within this file.",sectionInfo.data());
}
if (Debug::isFlagSet(Debug::CommentCnv))
{
g_outBuf->at(g_outBuf->curPos())='\0';
msg("-------------\n%s\n-------------\n",g_outBuf->data());
}
}
//----------------------------------------------------------------------------
#if !defined(YY_FLEX_SUBMINOR_VERSION)
extern "C" { // some bogus code to keep the compiler happy
void commentcnvYYdummy() { yy_flex_realloc(0,0); }
}
#endif
|