Imported Upstream version 1.8.11
[platform/upstream/doxygen.git] / src / commentcnv.l
1 /*****************************************************************************
2  *
3  * 
4  *
5  * Copyright (C) 1997-2015 by Dimitri van Heesch.
6  *
7  * Permission to use, copy, modify, and distribute this software and its
8  * documentation under the terms of the GNU General Public License is hereby 
9  * granted. No representations are made about the suitability of this software 
10  * for any purpose. It is provided "as is" without express or implied warranty.
11  * See the GNU General Public License for more details.
12  *
13  * Documents produced by Doxygen are derivative works derived from the
14  * input used in their production; they are not affected by this license.
15  *
16  */
17 %option never-interactive
18 %{
19
20   
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #include <qstack.h>
25 #include <qregexp.h>
26 #include <qtextstream.h>
27 #include <qglobal.h>
28
29 #include "bufstr.h"
30 #include "debug.h"
31 #include "message.h"
32 #include "config.h"
33 #include "doxygen.h"
34 #include "util.h"
35 #include "condparser.h"
36
37 #include <assert.h>
38
39 #define YY_NO_INPUT 1
40 #define YY_NO_UNISTD_H 1
41
42 #define ADDCHAR(c)    g_outBuf->addChar(c)
43 #define ADDARRAY(a,s) g_outBuf->addArray(a,s)
44   
45 struct CondCtx
46 {
47   CondCtx(int line,QCString id,bool b) 
48     : lineNr(line),sectionId(id), skip(b) {}
49   int lineNr;
50   QCString sectionId;
51   bool skip;
52 };
53   
54 struct CommentCtx
55 {
56   CommentCtx(int line) 
57     : lineNr(line) {}
58   int lineNr;
59 };
60   
61 static BufStr * g_inBuf;
62 static BufStr * g_outBuf;
63 static int      g_inBufPos;
64 static int      g_col;
65 static int      g_blockHeadCol;
66 static bool     g_mlBrief;
67 static int      g_readLineCtx;
68 static bool     g_skip;
69 static QCString g_fileName;
70 static int      g_lineNr;
71 static int      g_condCtx;
72 static QStack<CondCtx> g_condStack;
73 static QStack<CommentCtx> g_commentStack;
74 static QCString g_blockName;
75 static int      g_lastCommentContext;
76 static bool     g_inSpecialComment;
77 static bool     g_inRoseComment;
78 static int      g_stringContext;
79 static int      g_charContext;
80 static int      g_javaBlock;
81 static bool     g_specialComment;
82
83 static QCString g_aliasString;
84 static int      g_blockCount;
85 static bool     g_lastEscaped;
86 static int      g_lastBlockContext;
87 static bool     g_pythonDocString;
88 static int      g_nestingCount;
89
90 static SrcLangExt g_lang;
91 static bool       isFixedForm; // For Fortran
92
93 static void replaceCommentMarker(const char *s,int len)
94 {
95   const char *p=s;
96   char c;
97   // copy leading blanks
98   while ((c=*p) && (c==' ' || c=='\t' || c=='\n')) 
99   {
100     ADDCHAR(c);
101     g_lineNr += c=='\n';
102     p++;
103   }
104   // replace start of comment marker by blanks and the last character by a *
105   int blanks=0;
106   while ((c=*p) && (c=='/' || c=='!' || c=='#')) 
107   {
108     blanks++;
109     p++;
110     if (*p=='<') // comment-after-item marker 
111     { 
112       blanks++;
113       p++; 
114     }
115     if (c=='!') // end after first !
116     {
117       break;
118     }
119   }
120   if (blanks>0)
121   {
122     while (blanks>2)
123     {
124       ADDCHAR(' ');
125       blanks--;
126     }
127     if (blanks>1) ADDCHAR('*');
128     ADDCHAR(' ');
129   }
130   // copy comment line to output
131   ADDARRAY(p,len-(int)(p-s));
132 }
133
134 static inline int computeIndent(const char *s)
135 {
136   int col=0;
137   static int tabSize=Config_getInt("TAB_SIZE");
138   const char *p=s;
139   char c;
140   while ((c=*p++))
141   {
142     if (c==' ') col++;
143     else if (c=='\t') col+=tabSize-(col%tabSize); 
144     else break;
145   }
146   return col;
147 }
148
149 static inline void copyToOutput(const char *s,int len)
150 {
151   int i;
152   if (g_skip) // only add newlines.
153   {
154     for (i=0;i<len;i++) 
155     {
156       if (s[i]=='\n') 
157       {
158         ADDCHAR('\n');
159         //fprintf(stderr,"---> skip %d\n",g_lineNr);
160         g_lineNr++;
161       }
162     }
163   }
164   else if (len>0)
165   {
166     ADDARRAY(s,len);
167     static int tabSize=Config_getInt("TAB_SIZE");
168     for (i=0;i<len;i++) 
169     {
170       switch (s[i])
171       {
172         case '\n': g_col=0; 
173                    //fprintf(stderr,"---> copy %d\n",g_lineNr);
174                    g_lineNr++; break;
175         case '\t': g_col+=tabSize-(g_col%tabSize); break;
176         default:   g_col++; break;
177       }
178     }
179   }
180 }
181
182 static void startCondSection(const char *sectId)
183 {
184   //printf("startCondSection: skip=%d stack=%d\n",g_skip,g_condStack.count());
185   CondParser prs;
186   bool expResult = prs.parse(g_fileName,g_lineNr,sectId);
187   g_condStack.push(new CondCtx(g_lineNr,sectId,g_skip));
188   if (!expResult) // not enabled
189   {
190     g_skip=TRUE;
191   }
192 }
193
194 static void endCondSection()
195 {
196   if (g_condStack.isEmpty())
197   {
198     warn(g_fileName,g_lineNr,"Found \\endcond command without matching \\cond");
199     g_skip=FALSE;
200   }
201   else
202   {
203     CondCtx *ctx = g_condStack.pop();
204     g_skip=ctx->skip;
205   }
206   //printf("endCondSection: skip=%d stack=%d\n",g_skip,g_condStack.count());
207 }
208
209 /** copies string \a s with length \a len to the output, while 
210  *  replacing any alias commands found in the string.
211  */
212 static void replaceAliases(const char *s)
213 {
214   QCString result = resolveAliasCmd(s);
215   //printf("replaceAliases(%s)->'%s'\n",s,result.data());
216   copyToOutput(result,result.length());
217 }
218
219
220 #undef  YY_INPUT
221 #define YY_INPUT(buf,result,max_size) result=yyread(buf,max_size);
222
223 static int yyread(char *buf,int max_size)
224 {
225   int bytesInBuf = g_inBuf->curPos()-g_inBufPos;
226   int bytesToCopy = QMIN(max_size,bytesInBuf);
227   memcpy(buf,g_inBuf->data()+g_inBufPos,bytesToCopy);
228   g_inBufPos+=bytesToCopy;
229   return bytesToCopy;
230 }
231
232 void replaceComment(int offset);
233
234 %}
235
236 %option noyywrap
237
238 %x Scan
239 %x SkipString
240 %x SkipChar
241 %x SComment
242 %x CComment
243 %x Verbatim
244 %x VerbatimCode
245 %x ReadLine
246 %x CondLine
247 %x ReadAliasArgs
248
249 %%
250
251 <Scan>[^"'!\/\n\\#-,]*              { /* eat anything that is not " / , or \n */ 
252                                        copyToOutput(yytext,(int)yyleng);
253                                     }
254 <Scan>[,]                           { /* eat , so we have a nice separator in long initialization lines */ 
255                                        copyToOutput(yytext,(int)yyleng);
256                                     }
257 <Scan>"\"\"\""!                     { /* start of python long comment */
258                                      if (g_lang!=SrcLangExt_Python)
259                                      {
260                                        REJECT;
261                                      }
262                                      else
263                                      {
264                                        g_pythonDocString = TRUE;
265                                        g_nestingCount=0;
266                                        g_commentStack.clear(); /*  to be on the save side */
267                                        copyToOutput(yytext,(int)yyleng);
268                                        BEGIN(CComment);
269                                        g_commentStack.push(new CommentCtx(g_lineNr));
270                                      }
271                                    }
272 <Scan>![><!]/.*\n          {
273                                      if (g_lang!=SrcLangExt_Fortran)
274                                      {
275                                        REJECT;
276                                      }
277                                      else
278                                      {
279                                        copyToOutput(yytext,(int)yyleng); 
280                                        g_nestingCount=0;
281                                        g_commentStack.clear(); /*  to be on the save side */
282                                        BEGIN(CComment);
283                                        g_commentStack.push(new CommentCtx(g_lineNr));
284                                      }
285                                    }
286 <Scan>[Cc\*][><!]/.*\n     {
287                                      if (g_lang!=SrcLangExt_Fortran)
288                                      {
289                                        REJECT;
290                                      }
291                                      else
292                                      {
293                                        /* check for fixed format; we might have some conditional as part of multilene if like C<5 .and. & */
294                                        if (isFixedForm && (g_col == 0))
295                                        {
296                                          copyToOutput(yytext,(int)yyleng); 
297                                          g_nestingCount=0;
298                                          g_commentStack.clear(); /*  to be on the save side */
299                                          BEGIN(CComment);
300                                          g_commentStack.push(new CommentCtx(g_lineNr));
301                                        }
302                                        else
303                                        {
304                                          REJECT;
305                                        }
306                                      }
307                                    }
308 <Scan>!.*\n                {
309                                      if (g_lang!=SrcLangExt_Fortran)
310                                      {
311                                        REJECT;
312                                      }
313                                      else
314                                      {
315                                        copyToOutput(yytext,(int)yyleng); 
316                                      }
317                                    }
318 <Scan>[Cc\*].*\n                   {
319                                      if (g_lang!=SrcLangExt_Fortran)
320                                      {
321                                        REJECT;
322                                      }
323                                      else
324                                      {
325                                        if (g_col == 0)
326                                        {
327                                          copyToOutput(yytext,(int)yyleng); 
328                                        }
329                                        else
330                                        {
331                                          REJECT;
332                                        }
333                                      }
334                                    }
335 <Scan>"\""                         { /* start of a string */ 
336                                      copyToOutput(yytext,(int)yyleng); 
337                                      g_stringContext = YY_START;
338                                      BEGIN(SkipString); 
339                                    }
340 <Scan>'                            {
341                                      copyToOutput(yytext,(int)yyleng); 
342                                      g_charContext = YY_START;
343                                      if (g_lang!=SrcLangExt_VHDL)
344                                      {
345                                        BEGIN(SkipChar);
346                                      }
347                                    }
348 <Scan>\n                           { /* new line */ 
349                                      copyToOutput(yytext,(int)yyleng); 
350                                    }
351 <Scan>"//!"/.*\n[ \t]*"//"[\/!][^\/] | /* start C++ style special comment block */
352 <Scan>("///"[/]*)/[^/].*\n[ \t]*"//"[\/!][^\/] { /* start C++ style special comment block */
353                                      if (g_mlBrief) 
354                                      {
355                                        REJECT; // bail out if we do not need to convert
356                                      }
357                                      else
358                                      {
359                                        int i=3;
360                                        if (yytext[2]=='/')
361                                        {
362                                          while (i<(int)yyleng && yytext[i]=='/') i++;
363                                        }
364                                        g_blockHeadCol=g_col;
365                                        copyToOutput("/**",3); 
366                                        replaceAliases(yytext+i);
367                                        g_inSpecialComment=TRUE;
368                                        //BEGIN(SComment); 
369                                        g_readLineCtx=SComment;
370                                        BEGIN(ReadLine);
371                                      }
372                                    }
373 <Scan>"//##Documentation".*/\n     { /* Start of Rational Rose ANSI C++ comment block */
374                                      if (g_mlBrief) REJECT;
375                                      int i=17; //=strlen("//##Documentation");
376                                      g_blockHeadCol=g_col;
377                                      copyToOutput("/**",3);
378                                      replaceAliases(yytext+i);
379                                      g_inRoseComment=TRUE;
380                                      BEGIN(SComment);
381                                    }
382 <Scan>"//"/.*\n                    { /* one line C++ comment */ 
383                                      g_inSpecialComment=yytext[2]=='/' || yytext[2]=='!';
384                                      copyToOutput(yytext,(int)yyleng); 
385                                      g_readLineCtx=YY_START;
386                                      BEGIN(ReadLine);
387                                    }
388 <Scan>"/**/"                       { /* avoid matching next rule for empty C comment, see bug 711723 */
389                                      copyToOutput(yytext,(int)yyleng);
390                                    }
391 <Scan>"/*"[*!]?                    { /* start of a C comment */
392                                      g_specialComment=(int)yyleng==3;
393                                      g_nestingCount=0;
394                                      g_commentStack.clear(); /*  to be on the save side */
395                                      copyToOutput(yytext,(int)yyleng); 
396                                      BEGIN(CComment); 
397                                      g_commentStack.push(new CommentCtx(g_lineNr));
398                                    }
399 <Scan>"#"("#")?                    {
400                                      if (g_lang!=SrcLangExt_Python)
401                                      {
402                                        REJECT;
403                                      }
404                                      else
405                                      {
406                                        copyToOutput(yytext,(int)yyleng); 
407                                        g_nestingCount=0;
408                                        g_commentStack.clear(); /*  to be on the save side */
409                                        BEGIN(CComment);
410                                        g_commentStack.push(new CommentCtx(g_lineNr));
411                                      }
412                                    }
413 <Scan>"--!"                        {
414                                      if (g_lang!=SrcLangExt_VHDL)
415                                      {
416                                        REJECT;
417                                      }
418                                      else
419                                      {
420                                        copyToOutput(yytext,(int)yyleng); 
421                                        g_nestingCount=0;
422                                        g_commentStack.clear(); /*  to be on the save side */
423                                        BEGIN(CComment);
424                                        g_commentStack.push(new CommentCtx(g_lineNr));
425                                      }
426                                    }
427 <Scan>![><!]                       {
428                                      if (g_lang!=SrcLangExt_Fortran)
429                                      {
430                                        REJECT;
431                                      }
432                                      else
433                                      {
434                                        copyToOutput(yytext,(int)yyleng); 
435                                        g_nestingCount=0;
436                                        g_commentStack.clear(); /*  to be on the save side */
437                                        BEGIN(CComment);
438                                        g_commentStack.push(new CommentCtx(g_lineNr));
439                                      }
440                                    }
441 <CComment>"{@code"/[ \t\n]         {
442                                      copyToOutput("@code",5); 
443                                      g_lastCommentContext = YY_START;
444                                      g_javaBlock=1;
445                                      g_blockName=&yytext[1];
446                                      BEGIN(VerbatimCode);
447                                    }
448 <CComment,ReadLine>[\\@]("dot"|"code"|"msc"|"startuml")/[^a-z_A-Z0-9] { /* start of a verbatim block */
449                                      copyToOutput(yytext,(int)yyleng); 
450                                      g_lastCommentContext = YY_START;
451                                      g_javaBlock=0;
452                                      if (qstrcmp(&yytext[1],"startuml")==0)
453                                      {
454                                        g_blockName="uml";
455                                      }
456                                      else
457                                      {
458                                        g_blockName=&yytext[1];
459                                      }
460                                      BEGIN(VerbatimCode);
461                                    }
462 <CComment,ReadLine>[\\@]("f$"|"f["|"f{"[a-z]*) {
463                                      copyToOutput(yytext,(int)yyleng); 
464                                      g_blockName=&yytext[1];
465                                      if (g_blockName.at(1)=='[')
466                                      {
467                                        g_blockName.at(1)=']';
468                                      }
469                                      else if (g_blockName.at(1)=='{')
470                                      {
471                                        g_blockName.at(1)='}';
472                                      }
473                                      g_lastCommentContext = YY_START;
474                                      BEGIN(Verbatim);
475                                    }
476 <CComment,ReadLine>[\\@]("verbatim"|"latexonly"|"htmlonly"|"xmlonly"|"docbookonly"|"rtfonly"|"manonly")/[^a-z_A-Z0-9] { /* start of a verbatim block */
477                                      copyToOutput(yytext,(int)yyleng); 
478                                      g_blockName=&yytext[1];
479                                      g_lastCommentContext = YY_START;
480                                      BEGIN(Verbatim);
481                                    }
482 <Scan>.                            { /* any ather character */
483                                      copyToOutput(yytext,(int)yyleng); 
484                                    }
485 <Verbatim>[\\@]("endverbatim"|"endlatexonly"|"endhtmlonly"|"endxmlonly"|"docbookonly"|"endrtfonly"|"endmanonly"|"f$"|"f]"|"f}") { /* end of verbatim block */
486                                      copyToOutput(yytext,(int)yyleng);
487                                      if (yytext[1]=='f') // end of formula
488                                      {
489                                        BEGIN(g_lastCommentContext);
490                                      }
491                                      else if (&yytext[4]==g_blockName)
492                                      {
493                                        BEGIN(g_lastCommentContext);
494                                      }
495                                    }
496 <VerbatimCode>"{"                  {
497                                      if (g_javaBlock==0)
498                                      {
499                                        REJECT;
500                                      }
501                                      else
502                                      {
503                                        g_javaBlock++;
504                                        copyToOutput(yytext,(int)yyleng);
505                                      }
506                                    }
507 <VerbatimCode>"}"                  {
508                                      if (g_javaBlock==0)
509                                      {
510                                        REJECT;
511                                      }
512                                      else
513                                      {
514                                        g_javaBlock--;
515                                        if (g_javaBlock==0)
516                                        {
517                                          copyToOutput(" @endcode ",10);
518                                          BEGIN(g_lastCommentContext);
519                                        }
520                                        else
521                                        {
522                                          copyToOutput(yytext,(int)yyleng);
523                                        }
524                                      }
525                                    }
526 <VerbatimCode>[\\@]("enddot"|"endcode"|"endmsc"|"enduml") { /* end of verbatim block */
527                                      copyToOutput(yytext,(int)yyleng);
528                                      if (&yytext[4]==g_blockName)
529                                      {
530                                        BEGIN(g_lastCommentContext);
531                                      }
532                                    }
533 <VerbatimCode>^[ \t]*"//"[\!\/]?   { /* skip leading comments */
534                                      if (!g_inSpecialComment)
535                                      {
536                                        copyToOutput(yytext,(int)yyleng); 
537                                      }
538                                      else
539                                      {
540                                        int l=0;
541                                        while (yytext[l]==' ' || yytext[l]=='\t')
542                                        {
543                                          l++;
544                                        }
545                                        copyToOutput(yytext,l);
546                                        if (yyleng-l==3) // ends with //! or ///
547                                        {
548                                          copyToOutput(" * ",3);
549                                        }
550                                        else // ends with //
551                                        {
552                                          copyToOutput("//",2);
553                                        }
554                                      }
555                                    }
556 <Verbatim,VerbatimCode>[^@\/\\\n{}]* { /* any character not a backslash or new line or } */
557                                      copyToOutput(yytext,(int)yyleng); 
558                                    }
559 <Verbatim,VerbatimCode>\n          { /* new line in verbatim block */
560                                      copyToOutput(yytext,(int)yyleng); 
561                                    }
562 <Verbatim>^[ \t]*"///"             {
563                                      if (g_blockName=="dot" || g_blockName=="msc" || g_blockName=="uml" || g_blockName.at(0)=='f')
564                                      {
565                                        // see bug 487871, strip /// from dot images and formulas.
566                                        int l=0;
567                                        while (yytext[l]==' ' || yytext[l]=='\t')
568                                        {
569                                          l++;
570                                        }
571                                        copyToOutput(yytext,l);
572                                        copyToOutput("   ",3);
573                                      }
574                                      else // even slashes are verbatim (e.g. \verbatim, \code)
575                                      {
576                                        REJECT;
577                                      }
578                                    }
579 <Verbatim,VerbatimCode>.           { /* any other character */
580                                      copyToOutput(yytext,(int)yyleng); 
581                                    }
582 <SkipString>\\.                    { /* escaped character in string */
583                                      copyToOutput(yytext,(int)yyleng); 
584                                    }
585 <SkipString>"\""                   { /* end of string */ 
586                                      copyToOutput(yytext,(int)yyleng); 
587                                      BEGIN(g_stringContext); 
588                                    }
589 <SkipString>.                      { /* any other string character */ 
590                                      copyToOutput(yytext,(int)yyleng); 
591                                    }
592 <SkipString>\n                     { /* new line inside string (illegal for some compilers) */ 
593                                      copyToOutput(yytext,(int)yyleng); 
594                                    }
595 <SkipChar>\\.                      { /* escaped character */
596                                      copyToOutput(yytext,(int)yyleng); 
597                                    }
598 <SkipChar>'                        { /* end of character literal */ 
599                                      copyToOutput(yytext,(int)yyleng); 
600                                      BEGIN(g_charContext);
601                                    }
602 <SkipChar>.                        { /* any other string character */ 
603                                      copyToOutput(yytext,(int)yyleng); 
604                                    }
605 <SkipChar>\n                       { /* new line character */
606                                      copyToOutput(yytext,(int)yyleng); 
607                                    }
608
609 <CComment>[^\\!@*\n{\"\/]*           { /* anything that is not a '*' or command */ 
610                                      copyToOutput(yytext,(int)yyleng); 
611                                    }
612 <CComment>"*"+[^*/\\@\n{\"]*       { /* stars without slashes */
613                                      copyToOutput(yytext,(int)yyleng); 
614                                    }
615 <CComment>"\"\"\""                 { /* end of Python docstring */
616                                      if (g_lang!=SrcLangExt_Python)
617                                      {
618                                        REJECT;
619                                      }
620                                      else
621                                      {
622                                        g_pythonDocString = FALSE;
623                                        copyToOutput(yytext,(int)yyleng);
624                                        BEGIN(Scan);
625                                      }
626                                    }
627 <CComment>\n                       { /* new line in comment */
628                                      copyToOutput(yytext,(int)yyleng); 
629                                      /* in case of Fortran always end of comment */
630                                      if (g_lang==SrcLangExt_Fortran)
631                                      {
632                                        BEGIN(Scan);
633                                      }
634                                    }
635 <CComment>"/"+"*"                  { /* nested C comment */
636                                      g_nestingCount++;
637                                      g_commentStack.push(new CommentCtx(g_lineNr));
638                                      copyToOutput(yytext,(int)yyleng); 
639                                    }
640 <CComment>"*"+"/"                  { /* end of C comment */
641                                      if (g_lang==SrcLangExt_Python)
642                                      {
643                                        REJECT;
644                                      }
645                                      else
646                                      {
647                                        copyToOutput(yytext,(int)yyleng);
648                                        if (g_nestingCount<=0)
649                                        {
650                                          BEGIN(Scan);
651                                        }
652                                        else
653                                        {
654                                          g_nestingCount--;
655                                          delete g_commentStack.pop();
656                                        }
657                                      }
658                                    }
659 <CComment>"\n"/[ \t]*[^#]          { /* end of Python comment */
660                                      if (g_lang!=SrcLangExt_Python || g_pythonDocString)
661                                      {
662                                        REJECT;
663                                      }
664                                      else
665                                      {
666                                        copyToOutput(yytext,(int)yyleng);
667                                        BEGIN(Scan);
668                                      }
669                                    }
670 <CComment>"\n"/[ \t]*[^\-]         { /* end of VHDL comment */
671                                      if (g_lang!=SrcLangExt_VHDL)
672                                      {
673                                        REJECT;
674                                      }
675                                      else
676                                      {
677                                        copyToOutput(yytext,(int)yyleng);
678                                        BEGIN(Scan);
679                                      }
680                                    }
681    /* removed for bug 674842 (bug was introduced in rev 768)
682 <CComment>"'"                      {
683                                      g_charContext = YY_START;
684                                      copyToOutput(yytext,(int)yyleng);
685                                      BEGIN(SkipChar);
686                                    }
687 <CComment>"\""                     {
688                                      g_stringContext = YY_START;
689                                      copyToOutput(yytext,(int)yyleng);
690                                      BEGIN(SkipString);
691                                    }
692    */
693 <CComment>.                        {
694                                      copyToOutput(yytext,(int)yyleng); 
695                                    }
696 <SComment>^[ \t]*"///"[\/]*/\n     {
697                                      replaceComment(0);
698                                    }
699 <SComment>\n[ \t]*"///"[\/]*/\n    {
700                                      replaceComment(1); 
701                                    }
702 <SComment>^[ \t]*"///"[^\/\n]/.*\n { 
703                                      replaceComment(0);
704                                      g_readLineCtx=YY_START;
705                                      BEGIN(ReadLine);
706                                    }
707 <SComment>\n[ \t]*"//"[\/!]("<")?[ \t]*[\\@]"}".*\n {   
708                                      /* See Bug 752712: end the multiline comment when finding a @} or \} command */
709                                      copyToOutput(" */",3); 
710                                      copyToOutput(yytext,(int)yyleng); 
711                                      g_inSpecialComment=FALSE;
712                                      g_inRoseComment=FALSE;
713                                      BEGIN(Scan); 
714                                    }
715 <SComment>\n[ \t]*"///"[^\/\n]/.*\n  { 
716                                      replaceComment(1); 
717                                      g_readLineCtx=YY_START;
718                                      BEGIN(ReadLine);
719                                    }
720 <SComment>^[ \t]*"//!"             |    // just //!
721 <SComment>^[ \t]*"//!<"/.*\n       |    // or   //!< something
722 <SComment>^[ \t]*"//!"[^<]/.*\n    {    // or   //!something
723                                      replaceComment(0);
724                                      g_readLineCtx=YY_START;
725                                      BEGIN(ReadLine);
726                                    }
727 <SComment>\n[ \t]*"//!"            |
728 <SComment>\n[ \t]*"//!<"/.*\n      |
729 <SComment>\n[ \t]*"//!"[^<\n]/.*\n { 
730                                      replaceComment(1); 
731                                      g_readLineCtx=YY_START;
732                                      BEGIN(ReadLine);
733                                    }
734 <SComment>^[ \t]*"//##"/.*\n       {
735                                      if (!g_inRoseComment)
736                                      {
737                                        REJECT;
738                                      }
739                                      else
740                                      {
741                                        replaceComment(0);
742                                        g_readLineCtx=YY_START;
743                                        BEGIN(ReadLine);
744                                      }
745                                    }
746 <SComment>\n[ \t]*"//##"/.*\n      {
747                                      if (!g_inRoseComment)
748                                      {
749                                        REJECT;
750                                      }
751                                      else
752                                      {
753                                        replaceComment(1); 
754                                        g_readLineCtx=YY_START;
755                                        BEGIN(ReadLine);
756                                      }
757                                    }
758 <SComment>\n                       { /* end of special comment */
759                                      copyToOutput(" */",3); 
760                                      copyToOutput(yytext,(int)yyleng); 
761                                      g_inSpecialComment=FALSE;
762                                      g_inRoseComment=FALSE;
763                                      BEGIN(Scan); 
764                                    }
765 <ReadLine>[^\\@\n]*/\n             {
766                                      copyToOutput(yytext,(int)yyleng);
767                                      BEGIN(g_readLineCtx);
768                                    }
769 <CComment,ReadLine>[\\@][\\@][~a-z_A-Z][a-z_A-Z0-9]*[ \t]* { // escaped command
770                                      copyToOutput(yytext,(int)yyleng);
771                                    }
772 <CComment,ReadLine>[\\@]"cond"/[^a-z_A-Z0-9]       { // conditional section
773                                      g_condCtx = YY_START; 
774                                      BEGIN(CondLine);
775                                    }
776 <CComment,ReadLine>[\\@]"endcond"/[^a-z_A-Z0-9] { // end of conditional section
777                                      bool oldSkip=g_skip;
778                                      endCondSection();
779                                      if (YY_START==CComment && oldSkip && !g_skip) 
780                                      {
781                                        //printf("** Adding start of comment!\n");
782                                        if (g_lang!=SrcLangExt_Python &&
783                                            g_lang!=SrcLangExt_VHDL &&
784                                            g_lang!=SrcLangExt_Markdown &&
785                                            g_lang!=SrcLangExt_Fortran)
786                                        {
787                                          ADDCHAR('/');
788                                          ADDCHAR('*');
789                                          if (g_specialComment)
790                                          {
791                                            ADDCHAR('*');
792                                          }
793                                        }
794                                      }
795                                     }
796 <CondLine>[!()&| \ta-z_A-Z0-9.\-]+ {
797                                      bool oldSkip=g_skip;
798                                      startCondSection(yytext);
799                                      if ((g_condCtx==CComment || g_readLineCtx==SComment) && 
800                                          !oldSkip && g_skip) 
801                                      {
802                                        if (g_lang!=SrcLangExt_Python &&
803                                            g_lang!=SrcLangExt_VHDL &&
804                                            g_lang!=SrcLangExt_Markdown &&
805                                            g_lang!=SrcLangExt_Fortran)
806                                        {
807                                          ADDCHAR('*');
808                                          ADDCHAR('/');
809                                        }
810                                      }
811                                      if (g_readLineCtx==SComment)
812                                      {
813                                        BEGIN(SComment);
814                                      }
815                                      else
816                                      {
817                                        BEGIN(g_condCtx);
818                                      }
819                                    }
820 <CondLine>[ \t]*
821 <CComment,ReadLine>[\\@]"cond"[ \t\r]*/\n |
822 <CondLine>.                        { // forgot section id?
823                                      if (YY_START!=CondLine) g_condCtx=YY_START;
824                                      bool oldSkip=g_skip;
825                                      startCondSection(" "); // fake section id causing the section to be hidden unconditionally
826                                      if ((g_condCtx==CComment || g_readLineCtx==SComment) && 
827                                          !oldSkip && g_skip) 
828                                      {
829                                        //printf("** Adding terminator for comment!\n");
830                                        if (g_lang!=SrcLangExt_Python &&
831                                            g_lang!=SrcLangExt_VHDL)
832                                        {
833                                          ADDCHAR('*');
834                                          ADDCHAR('/');
835                                        }
836                                      }
837                                      if (*yytext=='\n') g_lineNr++;
838                                      if (g_readLineCtx==SComment)
839                                      {
840                                        BEGIN(SComment);
841                                      }
842                                      else
843                                      {
844                                        BEGIN(g_condCtx);
845                                      }
846                                    }
847 <CComment,ReadLine>[\\@][a-z_A-Z][a-z_A-Z0-9]*  { // expand alias without arguments
848                                      replaceAliases(yytext);
849                                    }
850 <CComment,ReadLine>[\\@][a-z_A-Z][a-z_A-Z0-9]*"{" { // expand alias with arguments
851                                      g_lastBlockContext=YY_START;
852                                      g_blockCount=1;
853                                      g_aliasString=yytext;
854                                      g_lastEscaped=0;
855                                      BEGIN( ReadAliasArgs );
856                                    }
857 <ReadAliasArgs>^[ \t]*"//"[/!]/[^\n]+   { // skip leading special comments (see bug 618079)
858                                    }
859 <ReadAliasArgs>"*/"                { // oops, end of comment in the middle of an alias?
860                                      if (g_lang==SrcLangExt_Python)
861                                      {
862                                        REJECT;
863                                      }
864                                      else // abort the alias, restart scanning
865                                      {
866                                        copyToOutput(g_aliasString,g_aliasString.length());
867                                        copyToOutput(yytext,(int)yyleng);
868                                        BEGIN(Scan);
869                                      }
870                                    }
871 <ReadAliasArgs>[^{}\n\\\*]+        {
872                                      g_aliasString+=yytext;
873                                      g_lastEscaped=FALSE;
874                                    }
875 <ReadAliasArgs>"\\"                {
876                                      if (g_lastEscaped)  g_lastEscaped=FALSE;
877                                      else                g_lastEscaped=TRUE;
878                                      g_aliasString+=yytext;
879                                    }
880 <ReadAliasArgs>\n                  {
881                                      g_aliasString+=yytext;
882                                      g_lineNr++;
883                                      g_lastEscaped=FALSE;
884                                    }
885 <ReadAliasArgs>"{"                 {
886                                      g_aliasString+=yytext;
887                                      if (!g_lastEscaped) g_blockCount++;
888                                      g_lastEscaped=FALSE;
889                                    }
890 <ReadAliasArgs>"}"                 {
891                                      g_aliasString+=yytext;
892                                      if (!g_lastEscaped) g_blockCount--;
893                                      if (g_blockCount==0)
894                                      {
895                                        replaceAliases(g_aliasString);
896                                        BEGIN( g_lastBlockContext );
897                                      }
898                                      g_lastEscaped=FALSE;
899                                    }
900 <ReadAliasArgs>.                   {
901                                      g_aliasString+=yytext;
902                                      g_lastEscaped=FALSE;
903                                    }
904 <ReadLine>.                        {
905                                      copyToOutput(yytext,(int)yyleng);
906                                    }
907
908 %%
909
910 void replaceComment(int offset)
911 {
912   if (g_mlBrief || g_skip)
913   {
914     copyToOutput(yytext,(int)yyleng);
915   }
916   else
917   {
918     //printf("replaceComment(%s)\n",yytext);
919     int i=computeIndent(&yytext[offset]);
920     if (i==g_blockHeadCol)
921     {
922       replaceCommentMarker(yytext,(int)yyleng);
923     }
924     else
925     {
926       copyToOutput(" */",3);
927       int i;for (i=(int)yyleng-1;i>=0;i--) unput(yytext[i]);
928       g_inSpecialComment=FALSE;
929       BEGIN(Scan);                                            
930     }                                                         
931   }
932 }
933
934 // simplified way to know if this is fixed form
935 // duplicate in fortrancode.l
936 static bool recognizeFixedForm(const char* contents)
937 {
938   int column=0;
939   bool skipLine=FALSE;
940
941   for(int i=0;;i++) {
942     column++;
943
944     switch(contents[i]) {
945       case '\n':
946         column=0;
947         skipLine=FALSE;
948         break;
949       case ' ':
950         break;
951       case '\000':
952         return FALSE;
953       case 'C':
954       case 'c':
955       case '*':
956         if(column==1) return TRUE;
957         if(skipLine) break;
958         return FALSE;
959       case '!':
960         if(column>1 && column<7) return FALSE;
961         skipLine=TRUE;
962         break;
963       default:
964         if(skipLine) break;
965         if(column==7) return TRUE;
966         return FALSE;
967     }
968   }
969   return FALSE;
970 }
971
972
973 /*! This function does three things:
974  *  -# It converts multi-line C++ style comment blocks (that are aligned)
975  *     to C style comment blocks (if MULTILINE_CPP_IS_BRIEF is set to NO).
976  *  -# It replaces aliases with their definition (see ALIASES)
977  *  -# It handles conditional sections (cond...endcond blocks)
978  */
979 void convertCppComments(BufStr *inBuf,BufStr *outBuf,const char *fileName)
980 {
981   //printf("convertCppComments(%s)\n",fileName);
982   g_inBuf    = inBuf;
983   g_outBuf   = outBuf;
984   g_inBufPos = 0;
985   g_col      = 0;
986   g_mlBrief = Config_getBool("MULTILINE_CPP_IS_BRIEF");
987   g_skip     = FALSE;
988   g_fileName = fileName;
989   g_lang = getLanguageFromFileName(fileName);
990   g_pythonDocString = FALSE;
991   g_lineNr   = 1;
992   g_condStack.clear();
993   g_condStack.setAutoDelete(TRUE);
994   g_commentStack.clear();
995   g_commentStack.setAutoDelete(TRUE);
996
997   printlex(yy_flex_debug, TRUE, __FILE__, fileName);
998   isFixedForm = FALSE;
999   if (g_lang==SrcLangExt_Fortran)
1000   {
1001     isFixedForm = recognizeFixedForm(inBuf->data());
1002   }
1003
1004   if (g_lang==SrcLangExt_Markdown)
1005   {
1006     g_nestingCount=0;
1007     BEGIN(CComment);
1008     g_commentStack.push(new CommentCtx(g_lineNr));
1009   }
1010   else
1011   {
1012     BEGIN(Scan);
1013   }
1014   yylex();
1015   while (!g_condStack.isEmpty())
1016   {
1017     CondCtx *ctx = g_condStack.pop();
1018     QCString sectionInfo = " ";
1019     if (ctx->sectionId!=" ") sectionInfo.sprintf(" with label %s ",ctx->sectionId.data()); 
1020     warn(g_fileName,ctx->lineNr,"Conditional section%sdoes not have "
1021         "a corresponding \\endcond command within this file.",sectionInfo.data());
1022   }
1023   if (g_nestingCount>0 && g_lang!=SrcLangExt_Markdown)
1024   {
1025     QCString tmp= "(probable line reference: ";
1026     bool first = TRUE;
1027     while (!g_commentStack.isEmpty())
1028     {
1029       CommentCtx *ctx = g_commentStack.pop();
1030       if (!first) tmp += ", ";
1031       tmp += QCString().setNum(ctx->lineNr);
1032       first = FALSE;
1033       delete ctx;
1034     }
1035     tmp += ")";
1036     warn(g_fileName,g_lineNr,"Reached end of file while still inside a (nested) comment. "
1037         "Nesting level %d %s",g_nestingCount+1,tmp.data()); // add one for "normal" expected end of comment
1038   }
1039   g_commentStack.clear();
1040   g_nestingCount = 0;
1041   if (Debug::isFlagSet(Debug::CommentCnv))
1042   {
1043     g_outBuf->at(g_outBuf->curPos())='\0';
1044     msg("-------------\n%s\n-------------\n",g_outBuf->data());
1045   }
1046   printlex(yy_flex_debug, FALSE, __FILE__, fileName);
1047 }
1048
1049
1050 //----------------------------------------------------------------------------
1051 #if !defined(YY_FLEX_SUBMINOR_VERSION) 
1052 extern "C" { // some bogus code to keep the compiler happy
1053     void commentcnvYYdummy() { yy_flex_realloc(0,0); } 
1054 }
1055 #endif
1056