Imported Upstream version 1.8.10
[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]*"///"[^\/\n]/.*\n  { 
708                                      replaceComment(1); 
709                                      g_readLineCtx=YY_START;
710                                      BEGIN(ReadLine);
711                                    }
712 <SComment>^[ \t]*"//!"             |    // just //!
713 <SComment>^[ \t]*"//!<"/.*\n       |    // or   //!< something
714 <SComment>^[ \t]*"//!"[^<]/.*\n    {    // or   //!something
715                                      replaceComment(0);
716                                      g_readLineCtx=YY_START;
717                                      BEGIN(ReadLine);
718                                    }
719 <SComment>\n[ \t]*"//!"            |
720 <SComment>\n[ \t]*"//!<"/.*\n      |
721 <SComment>\n[ \t]*"//!"[^<\n]/.*\n { 
722                                      replaceComment(1); 
723                                      g_readLineCtx=YY_START;
724                                      BEGIN(ReadLine);
725                                    }
726 <SComment>^[ \t]*"//##"/.*\n       {
727                                      if (!g_inRoseComment)
728                                      {
729                                        REJECT;
730                                      }
731                                      else
732                                      {
733                                        replaceComment(0);
734                                        g_readLineCtx=YY_START;
735                                        BEGIN(ReadLine);
736                                      }
737                                    }
738 <SComment>\n[ \t]*"//##"/.*\n      {
739                                      if (!g_inRoseComment)
740                                      {
741                                        REJECT;
742                                      }
743                                      else
744                                      {
745                                        replaceComment(1); 
746                                        g_readLineCtx=YY_START;
747                                        BEGIN(ReadLine);
748                                      }
749                                    }
750 <SComment>\n                       { /* end of special comment */
751                                      copyToOutput(" */",3); 
752                                      copyToOutput(yytext,(int)yyleng); 
753                                      g_inSpecialComment=FALSE;
754                                      g_inRoseComment=FALSE;
755                                      BEGIN(Scan); 
756                                    }
757 <ReadLine>[^\\@\n]*/\n             {
758                                      copyToOutput(yytext,(int)yyleng);
759                                      BEGIN(g_readLineCtx);
760                                    }
761 <CComment,ReadLine>[\\@][\\@][~a-z_A-Z][a-z_A-Z0-9]*[ \t]* { // escaped command
762                                      copyToOutput(yytext,(int)yyleng);
763                                    }
764 <CComment,ReadLine>[\\@]"cond"/[^a-z_A-Z0-9]       { // conditional section
765                                      g_condCtx = YY_START; 
766                                      BEGIN(CondLine);
767                                    }
768 <CComment,ReadLine>[\\@]"endcond"/[^a-z_A-Z0-9] { // end of conditional section
769                                      bool oldSkip=g_skip;
770                                      endCondSection();
771                                      if (YY_START==CComment && oldSkip && !g_skip) 
772                                      {
773                                        //printf("** Adding start of comment!\n");
774                                        if (g_lang!=SrcLangExt_Python &&
775                                            g_lang!=SrcLangExt_VHDL &&
776                                            g_lang!=SrcLangExt_Fortran)
777                                        {
778                                          ADDCHAR('/');
779                                          ADDCHAR('*');
780                                          if (g_specialComment)
781                                          {
782                                            ADDCHAR('*');
783                                          }
784                                        }
785                                      }
786                                     }
787 <CondLine>[!()&| \ta-z_A-Z0-9.\-]+ {
788                                      bool oldSkip=g_skip;
789                                      startCondSection(yytext);
790                                      if ((g_condCtx==CComment || g_readLineCtx==SComment) && 
791                                          !oldSkip && g_skip) 
792                                      {
793                                        if (g_lang!=SrcLangExt_Python &&
794                                            g_lang!=SrcLangExt_VHDL &&
795                                            g_lang!=SrcLangExt_Fortran)
796                                        {
797                                          ADDCHAR('*');
798                                          ADDCHAR('/');
799                                        }
800                                      }
801                                      if (g_readLineCtx==SComment)
802                                      {
803                                        BEGIN(SComment);
804                                      }
805                                      else
806                                      {
807                                        BEGIN(g_condCtx);
808                                      }
809                                    }
810 <CondLine>[ \t]*
811 <CComment,ReadLine>[\\@]"cond"[ \t\r]*/\n |
812 <CondLine>.                        { // forgot section id?
813                                      if (YY_START!=CondLine) g_condCtx=YY_START;
814                                      bool oldSkip=g_skip;
815                                      startCondSection(" "); // fake section id causing the section to be hidden unconditionally
816                                      if ((g_condCtx==CComment || g_readLineCtx==SComment) && 
817                                          !oldSkip && g_skip) 
818                                      {
819                                        //printf("** Adding terminator for comment!\n");
820                                        if (g_lang!=SrcLangExt_Python &&
821                                            g_lang!=SrcLangExt_VHDL)
822                                        {
823                                          ADDCHAR('*');
824                                          ADDCHAR('/');
825                                        }
826                                      }
827                                      if (*yytext=='\n') g_lineNr++;
828                                      if (g_readLineCtx==SComment)
829                                      {
830                                        BEGIN(SComment);
831                                      }
832                                      else
833                                      {
834                                        BEGIN(g_condCtx);
835                                      }
836                                    }
837 <CComment,ReadLine>[\\@][a-z_A-Z][a-z_A-Z0-9]*  { // expand alias without arguments
838                                      replaceAliases(yytext);
839                                    }
840 <CComment,ReadLine>[\\@][a-z_A-Z][a-z_A-Z0-9]*"{" { // expand alias with arguments
841                                      g_lastBlockContext=YY_START;
842                                      g_blockCount=1;
843                                      g_aliasString=yytext;
844                                      g_lastEscaped=0;
845                                      BEGIN( ReadAliasArgs );
846                                    }
847 <ReadAliasArgs>^[ \t]*"//"[/!]/[^\n]+   { // skip leading special comments (see bug 618079)
848                                    }
849 <ReadAliasArgs>"*/"                { // oops, end of comment in the middle of an alias?
850                                      if (g_lang==SrcLangExt_Python)
851                                      {
852                                        REJECT;
853                                      }
854                                      else // abort the alias, restart scanning
855                                      {
856                                        copyToOutput(g_aliasString,g_aliasString.length());
857                                        copyToOutput(yytext,(int)yyleng);
858                                        BEGIN(Scan);
859                                      }
860                                    }
861 <ReadAliasArgs>[^{}\n\\\*]+        {
862                                      g_aliasString+=yytext;
863                                      g_lastEscaped=FALSE;
864                                    }
865 <ReadAliasArgs>"\\"                {
866                                      if (g_lastEscaped)  g_lastEscaped=FALSE;
867                                      else                g_lastEscaped=TRUE;
868                                      g_aliasString+=yytext;
869                                    }
870 <ReadAliasArgs>\n                  {
871                                      g_aliasString+=yytext;
872                                      g_lineNr++;
873                                      g_lastEscaped=FALSE;
874                                    }
875 <ReadAliasArgs>"{"                 {
876                                      g_aliasString+=yytext;
877                                      if (!g_lastEscaped) g_blockCount++;
878                                      g_lastEscaped=FALSE;
879                                    }
880 <ReadAliasArgs>"}"                 {
881                                      g_aliasString+=yytext;
882                                      if (!g_lastEscaped) g_blockCount--;
883                                      if (g_blockCount==0)
884                                      {
885                                        replaceAliases(g_aliasString);
886                                        BEGIN( g_lastBlockContext );
887                                      }
888                                      g_lastEscaped=FALSE;
889                                    }
890 <ReadAliasArgs>.                   {
891                                      g_aliasString+=yytext;
892                                      g_lastEscaped=FALSE;
893                                    }
894 <ReadLine>.                        {
895                                      copyToOutput(yytext,(int)yyleng);
896                                    }
897
898 %%
899
900 void replaceComment(int offset)
901 {
902   if (g_mlBrief || g_skip)
903   {
904     copyToOutput(yytext,(int)yyleng);
905   }
906   else
907   {
908     //printf("replaceComment(%s)\n",yytext);
909     int i=computeIndent(&yytext[offset]);
910     if (i==g_blockHeadCol)
911     {
912       replaceCommentMarker(yytext,(int)yyleng);
913     }
914     else
915     {
916       copyToOutput(" */",3);
917       int i;for (i=(int)yyleng-1;i>=0;i--) unput(yytext[i]);
918       g_inSpecialComment=FALSE;
919       BEGIN(Scan);                                            
920     }                                                         
921   }
922 }
923
924 // simplified way to know if this is fixed form
925 // duplicate in fortrancode.l
926 static bool recognizeFixedForm(const char* contents)
927 {
928   int column=0;
929   bool skipLine=FALSE;
930
931   for(int i=0;;i++) {
932     column++;
933
934     switch(contents[i]) {
935       case '\n':
936         column=0;
937         skipLine=FALSE;
938         break;
939       case ' ':
940         break;
941       case '\000':
942         return FALSE;
943       case 'C':
944       case 'c':
945       case '*':
946         if(column==1) return TRUE;
947         if(skipLine) break;
948         return FALSE;
949       case '!':
950         if(column>1 && column<7) return FALSE;
951         skipLine=TRUE;
952         break;
953       default:
954         if(skipLine) break;
955         if(column==7) return TRUE;
956         return FALSE;
957     }
958   }
959   return FALSE;
960 }
961
962
963 /*! This function does three things:
964  *  -# It converts multi-line C++ style comment blocks (that are aligned)
965  *     to C style comment blocks (if MULTILINE_CPP_IS_BRIEF is set to NO).
966  *  -# It replaces aliases with their definition (see ALIASES)
967  *  -# It handles conditional sections (cond...endcond blocks)
968  */
969 void convertCppComments(BufStr *inBuf,BufStr *outBuf,const char *fileName)
970 {
971   //printf("convertCppComments(%s)\n",fileName);
972   g_inBuf    = inBuf;
973   g_outBuf   = outBuf;
974   g_inBufPos = 0;
975   g_col      = 0;
976   g_mlBrief = Config_getBool("MULTILINE_CPP_IS_BRIEF");
977   g_skip     = FALSE;
978   g_fileName = fileName;
979   g_lang = getLanguageFromFileName(fileName);
980   g_pythonDocString = FALSE;
981   g_lineNr   = 1;
982   g_condStack.clear();
983   g_condStack.setAutoDelete(TRUE);
984   g_commentStack.clear();
985   g_commentStack.setAutoDelete(TRUE);
986
987   printlex(yy_flex_debug, TRUE, __FILE__, fileName);
988   isFixedForm = FALSE;
989   if (g_lang==SrcLangExt_Fortran)
990   {
991     isFixedForm = recognizeFixedForm(inBuf->data());
992   }
993
994   if (g_lang==SrcLangExt_Markdown)
995   {
996     g_nestingCount=0;
997     BEGIN(CComment);
998     g_commentStack.push(new CommentCtx(g_lineNr));
999   }
1000   else
1001   {
1002     BEGIN(Scan);
1003   }
1004   yylex();
1005   while (!g_condStack.isEmpty())
1006   {
1007     CondCtx *ctx = g_condStack.pop();
1008     QCString sectionInfo = " ";
1009     if (ctx->sectionId!=" ") sectionInfo.sprintf(" with label %s ",ctx->sectionId.data()); 
1010     warn(g_fileName,ctx->lineNr,"Conditional section%sdoes not have "
1011         "a corresponding \\endcond command within this file.",sectionInfo.data());
1012   }
1013   if (g_nestingCount>0 && g_lang!=SrcLangExt_Markdown)
1014   {
1015     QCString tmp= "(probable line reference: ";
1016     bool first = TRUE;
1017     while (!g_commentStack.isEmpty())
1018     {
1019       CommentCtx *ctx = g_commentStack.pop();
1020       if (!first) tmp += ", ";
1021       tmp += QCString().setNum(ctx->lineNr);
1022       first = FALSE;
1023       delete ctx;
1024     }
1025     tmp += ")";
1026     warn(g_fileName,g_lineNr,"Reached end of file while still inside a (nested) comment. "
1027         "Nesting level %d %s",g_nestingCount+1,tmp.data()); // add one for "normal" expected end of comment
1028   }
1029   g_commentStack.clear();
1030   g_nestingCount = 0;
1031   if (Debug::isFlagSet(Debug::CommentCnv))
1032   {
1033     g_outBuf->at(g_outBuf->curPos())='\0';
1034     msg("-------------\n%s\n-------------\n",g_outBuf->data());
1035   }
1036   printlex(yy_flex_debug, FALSE, __FILE__, fileName);
1037 }
1038
1039
1040 //----------------------------------------------------------------------------
1041 #if !defined(YY_FLEX_SUBMINOR_VERSION) 
1042 extern "C" { // some bogus code to keep the compiler happy
1043     void commentcnvYYdummy() { yy_flex_realloc(0,0); } 
1044 }
1045 #endif
1046