1 /*****************************************************************************
5 * Copyright (C) 1997-2013 by Dimitri van Heesch.
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.
13 * Documents produced by Doxygen are derivative works derived from the
14 * input used in their production; they are not affected by this license.
20 #define YY_NEVER_INTERACTIVE 1
28 #include <qtextstream.h>
37 #include "condparser.h"
43 #define ADDCHAR(c) g_outBuf->addChar(c)
44 #define ADDARRAY(a,s) g_outBuf->addArray(a,s)
48 CondCtx(int line,QCString id,bool b)
49 : lineNr(line),sectionId(id), skip(b) {}
55 static BufStr * g_inBuf;
56 static BufStr * g_outBuf;
57 static int g_inBufPos;
59 static int g_blockHeadCol;
60 static bool g_mlBrief;
61 static int g_readLineCtx;
63 static QCString g_fileName;
66 static QStack<CondCtx> g_condStack;
67 static QCString g_blockName;
68 static int g_lastCommentContext;
69 static bool g_inSpecialComment;
70 static bool g_inRoseComment;
71 static int g_stringContext;
72 static int g_charContext;
73 static int g_javaBlock;
74 static bool g_specialComment;
76 static QCString g_aliasString;
77 static int g_blockCount;
78 static bool g_lastEscaped;
79 static int g_lastBlockContext;
80 static bool g_pythonDocString;
82 static SrcLangExt g_lang;
83 static bool isFixedForm; // For Fortran
85 static void replaceCommentMarker(const char *s,int len)
89 // copy leading blanks
90 while ((c=*p) && (c==' ' || c=='\t' || c=='\n'))
96 // replace start of comment marker by blanks and the last character by a *
98 while ((c=*p) && (c=='/' || c=='!' || c=='#'))
102 if (*p=='<') // comment-after-item marker
107 if (c=='!') // end after first !
119 if (blanks>1) ADDCHAR('*');
122 // copy comment line to output
123 ADDARRAY(p,len-(int)(p-s));
126 static inline int computeIndent(const char *s)
129 static int tabSize=Config_getInt("TAB_SIZE");
135 else if (c=='\t') col+=tabSize-(col%tabSize);
141 static inline void copyToOutput(const char *s,int len)
144 if (g_skip) // only add newlines.
151 //fprintf(stderr,"---> skip %d\n",g_lineNr);
159 static int tabSize=Config_getInt("TAB_SIZE");
165 //fprintf(stderr,"---> copy %d\n",g_lineNr);
167 case '\t': g_col+=tabSize-(g_col%tabSize); break;
168 default: g_col++; break;
174 static void startCondSection(const char *sectId)
176 //printf("startCondSection: skip=%d stack=%d\n",g_skip,g_condStack.count());
178 bool expResult = prs.parse(g_fileName,g_lineNr,sectId);
179 g_condStack.push(new CondCtx(g_lineNr,sectId,g_skip));
180 if (!expResult) // not enabled
186 static void endCondSection()
188 if (g_condStack.isEmpty())
190 warn(g_fileName,g_lineNr,"Found \\endcond command without matching \\cond");
195 CondCtx *ctx = g_condStack.pop();
198 //printf("endCondSection: skip=%d stack=%d\n",g_skip,g_condStack.count());
201 /** copies string \a s with length \a len to the output, while
202 * replacing any alias commands found in the string.
204 static void replaceAliases(const char *s)
206 QCString result = resolveAliasCmd(s);
207 //printf("replaceAliases(%s)->'%s'\n",s,result.data());
208 copyToOutput(result,result.length());
213 #define YY_INPUT(buf,result,max_size) result=yyread(buf,max_size);
215 static int yyread(char *buf,int max_size)
217 int bytesInBuf = g_inBuf->curPos()-g_inBufPos;
218 int bytesToCopy = QMIN(max_size,bytesInBuf);
219 memcpy(buf,g_inBuf->data()+g_inBufPos,bytesToCopy);
220 g_inBufPos+=bytesToCopy;
224 void replaceComment(int offset);
243 <Scan>[^"'!\/\n\\#\\-]* { /* eat anything that is not " / or \n */
244 copyToOutput(yytext,(int)yyleng);
246 <Scan>"\"\"\""! { /* start of python long comment */
247 if (g_lang!=SrcLangExt_Python)
253 g_pythonDocString = TRUE;
254 copyToOutput(yytext,(int)yyleng);
259 if (g_lang!=SrcLangExt_Fortran)
265 copyToOutput(yytext,(int)yyleng);
269 <Scan>[Cc\*][><!]/.*\n {
270 if (g_lang!=SrcLangExt_Fortran)
276 /* check for fixed format; we might have some conditional as part of multilene if like C<5 .and. & */
277 if (isFixedForm && (g_col == 0))
279 copyToOutput(yytext,(int)yyleng);
289 if (g_lang!=SrcLangExt_Fortran)
295 copyToOutput(yytext,(int)yyleng);
299 if (g_lang!=SrcLangExt_Fortran)
307 copyToOutput(yytext,(int)yyleng);
315 <Scan>"\"" { /* start of a string */
316 copyToOutput(yytext,(int)yyleng);
317 g_stringContext = YY_START;
321 copyToOutput(yytext,(int)yyleng);
322 g_charContext = YY_START;
323 if (g_lang!=SrcLangExt_VHDL)
328 <Scan>\n { /* new line */
329 copyToOutput(yytext,(int)yyleng);
331 <Scan>"//!"/.*\n[ \t]*"//"[\/!][^\/] | /* start C++ style special comment block */
332 <Scan>("///"[/]*)/[^/].*\n[ \t]*"//"[\/!][^\/] { /* start C++ style special comment block */
335 REJECT; // bail out if we do not need to convert
342 while (i<(int)yyleng && yytext[i]=='/') i++;
344 g_blockHeadCol=g_col;
345 copyToOutput("/**",3);
346 replaceAliases(yytext+i);
347 g_inSpecialComment=TRUE;
349 g_readLineCtx=SComment;
353 <Scan>"//##Documentation".*/\n { /* Start of Rational Rose ANSI C++ comment block */
354 if (g_mlBrief) REJECT;
355 int i=17; //=strlen("//##Documentation");
356 g_blockHeadCol=g_col;
357 copyToOutput("/**",3);
358 replaceAliases(yytext+i);
359 g_inRoseComment=TRUE;
362 <Scan>"//"/.*\n { /* one line C++ comment */
363 g_inSpecialComment=yytext[2]=='/' || yytext[2]=='!';
364 copyToOutput(yytext,(int)yyleng);
365 g_readLineCtx=YY_START;
368 <Scan>"/*"[*!]? { /* start of a C comment */
369 g_specialComment=(int)yyleng==3;
370 copyToOutput(yytext,(int)yyleng);
374 if (g_lang!=SrcLangExt_Python)
380 copyToOutput(yytext,(int)yyleng);
385 if (g_lang!=SrcLangExt_VHDL)
391 copyToOutput(yytext,(int)yyleng);
396 if (g_lang!=SrcLangExt_Fortran)
402 copyToOutput(yytext,(int)yyleng);
406 <CComment>"{@code"/[ \t\n] {
407 copyToOutput("@code",5);
408 g_lastCommentContext = YY_START;
410 g_blockName=&yytext[1];
413 <CComment,ReadLine>[\\@]("dot"|"code"|"msc")/[^a-z_A-Z0-9] { /* start of a verbatim block */
414 copyToOutput(yytext,(int)yyleng);
415 g_lastCommentContext = YY_START;
417 g_blockName=&yytext[1];
420 <CComment,ReadLine>[\\@]("f$"|"f["|"f{"[a-z]*) {
421 copyToOutput(yytext,(int)yyleng);
422 g_blockName=&yytext[1];
423 if (g_blockName.at(1)=='[')
425 g_blockName.at(1)=']';
427 else if (g_blockName.at(1)=='{')
429 g_blockName.at(1)='}';
431 g_lastCommentContext = YY_START;
434 <CComment,ReadLine>[\\@]("verbatim"|"latexonly"|"htmlonly"|"xmlonly"|"docbookonly"|"rtfonly"|"manonly")/[^a-z_A-Z0-9] { /* start of a verbatim block */
435 copyToOutput(yytext,(int)yyleng);
436 g_blockName=&yytext[1];
437 g_lastCommentContext = YY_START;
440 <Scan>. { /* any ather character */
441 copyToOutput(yytext,(int)yyleng);
443 <Verbatim>[\\@]("endverbatim"|"endlatexonly"|"endhtmlonly"|"endxmlonly"|"docbookonly"|"endrtfonly"|"endmanonly"|"f$"|"f]"|"f}") { /* end of verbatim block */
444 copyToOutput(yytext,(int)yyleng);
445 if (yytext[1]=='f') // end of formula
447 BEGIN(g_lastCommentContext);
449 else if (&yytext[4]==g_blockName)
451 BEGIN(g_lastCommentContext);
462 copyToOutput(yytext,(int)yyleng);
475 copyToOutput(" @endcode ",10);
476 BEGIN(g_lastCommentContext);
480 copyToOutput(yytext,(int)yyleng);
484 <VerbatimCode>[\\@]("enddot"|"endcode"|"endmsc") { /* end of verbatim block */
485 copyToOutput(yytext,(int)yyleng);
486 if (&yytext[4]==g_blockName)
488 BEGIN(g_lastCommentContext);
491 <VerbatimCode>^[ \t]*"//"[\!\/]? { /* skip leading comments */
492 if (!g_inSpecialComment)
494 copyToOutput(yytext,(int)yyleng);
499 while (yytext[l]==' ' || yytext[l]=='\t')
503 copyToOutput(yytext,l);
504 if (yyleng-l==3) // ends with //! or ///
506 copyToOutput(" * ",3);
510 copyToOutput("//",2);
514 <Verbatim,VerbatimCode>[^@\/\\\n{}]* { /* any character not a backslash or new line or } */
515 copyToOutput(yytext,(int)yyleng);
517 <Verbatim,VerbatimCode>\n { /* new line in verbatim block */
518 copyToOutput(yytext,(int)yyleng);
520 <Verbatim>^[ \t]*"///" {
521 if (g_blockName=="dot" || g_blockName=="msc" || g_blockName.at(0)=='f')
523 // see bug 487871, strip /// from dot images and formulas.
525 while (yytext[l]==' ' || yytext[l]=='\t')
529 copyToOutput(yytext,l);
532 else // even slashes are verbatim (e.g. \verbatim, \code)
537 <Verbatim,VerbatimCode>. { /* any other character */
538 copyToOutput(yytext,(int)yyleng);
540 <SkipString>\\. { /* escaped character in string */
541 copyToOutput(yytext,(int)yyleng);
543 <SkipString>"\"" { /* end of string */
544 copyToOutput(yytext,(int)yyleng);
545 BEGIN(g_stringContext);
547 <SkipString>. { /* any other string character */
548 copyToOutput(yytext,(int)yyleng);
550 <SkipString>\n { /* new line inside string (illegal for some compilers) */
551 copyToOutput(yytext,(int)yyleng);
553 <SkipChar>\\. { /* escaped character */
554 copyToOutput(yytext,(int)yyleng);
556 <SkipChar>' { /* end of character literal */
557 copyToOutput(yytext,(int)yyleng);
558 BEGIN(g_charContext);
560 <SkipChar>. { /* any other string character */
561 copyToOutput(yytext,(int)yyleng);
563 <SkipChar>\n { /* new line character */
564 copyToOutput(yytext,(int)yyleng);
567 <CComment>[^\\!@*\n{\"]* { /* anything that is not a '*' or command */
568 copyToOutput(yytext,(int)yyleng);
570 <CComment>"*"+[^*/\\@\n{\"]* { /* stars without slashes */
571 copyToOutput(yytext,(int)yyleng);
573 <CComment>"\"\"\"" { /* end of Python docstring */
574 if (g_lang!=SrcLangExt_Python)
580 g_pythonDocString = FALSE;
581 copyToOutput(yytext,(int)yyleng);
585 <CComment>\n { /* new line in comment */
586 copyToOutput(yytext,(int)yyleng);
587 /* in case of Fortran always end of comment */
588 if (g_lang==SrcLangExt_Fortran)
593 <CComment>"*"+"/" { /* end of C comment */
594 if (g_lang==SrcLangExt_Python)
600 copyToOutput(yytext,(int)yyleng);
604 <CComment>"\n"/[ \t]*[^#] { /* end of Python comment */
605 if (g_lang!=SrcLangExt_Python || g_pythonDocString)
611 copyToOutput(yytext,(int)yyleng);
615 <CComment>"\n"/[ \t]*[^\-] { /* end of VHDL comment */
616 if (g_lang!=SrcLangExt_VHDL)
622 copyToOutput(yytext,(int)yyleng);
626 /* removed for bug 674842 (bug was introduced in rev 768)
628 g_charContext = YY_START;
629 copyToOutput(yytext,(int)yyleng);
633 g_stringContext = YY_START;
634 copyToOutput(yytext,(int)yyleng);
639 copyToOutput(yytext,(int)yyleng);
641 <SComment>^[ \t]*"///"[\/]*/\n {
644 <SComment>\n[ \t]*"///"[\/]*/\n {
647 <SComment>^[ \t]*"///"[^\/\n]/.*\n {
649 g_readLineCtx=YY_START;
652 <SComment>\n[ \t]*"///"[^\/\n]/.*\n {
654 g_readLineCtx=YY_START;
657 <SComment>^[ \t]*"//!" | // just //!
658 <SComment>^[ \t]*"//!<"/.*\n | // or //!< something
659 <SComment>^[ \t]*"//!"[^<]/.*\n { // or //!something
661 g_readLineCtx=YY_START;
664 <SComment>\n[ \t]*"//!" |
665 <SComment>\n[ \t]*"//!<"/.*\n |
666 <SComment>\n[ \t]*"//!"[^<\n]/.*\n {
668 g_readLineCtx=YY_START;
671 <SComment>^[ \t]*"//##"/.*\n {
672 if (!g_inRoseComment)
679 g_readLineCtx=YY_START;
683 <SComment>\n[ \t]*"//##"/.*\n {
684 if (!g_inRoseComment)
691 g_readLineCtx=YY_START;
695 <SComment>\n { /* end of special comment */
696 copyToOutput(" */",3);
697 copyToOutput(yytext,(int)yyleng);
698 g_inSpecialComment=FALSE;
699 g_inRoseComment=FALSE;
702 <ReadLine>[^\\@\n]*/\n {
703 copyToOutput(yytext,(int)yyleng);
704 BEGIN(g_readLineCtx);
706 <CComment,ReadLine>[\\@][\\@][~a-z_A-Z][a-z_A-Z0-9]*[ \t]* { // escaped command
707 copyToOutput(yytext,(int)yyleng);
709 <CComment,ReadLine>[\\@]"cond"[ \t]+ { // conditional section
710 g_condCtx = YY_START;
713 <CComment,ReadLine>[\\@]"endcond"/[^a-z_A-Z0-9] { // end of conditional section
716 if (YY_START==CComment && oldSkip && !g_skip)
718 //printf("** Adding start of comment!\n");
719 if (g_lang!=SrcLangExt_Python &&
720 g_lang!=SrcLangExt_VHDL &&
721 g_lang!=SrcLangExt_Fortran)
725 if (g_specialComment)
732 <CondLine>[!()&| \ta-z_A-Z0-9.\-]+ {
734 startCondSection(yytext);
735 if ((g_condCtx==CComment || g_readLineCtx==SComment) &&
738 if (g_lang!=SrcLangExt_Python &&
739 g_lang!=SrcLangExt_VHDL &&
740 g_lang!=SrcLangExt_Fortran)
746 if (g_readLineCtx==SComment)
756 <CComment,ReadLine>[\\@]"cond"[ \t\r]*/\n |
757 <CondLine>. { // forgot section id?
758 if (YY_START!=CondLine) g_condCtx=YY_START;
760 startCondSection(" "); // fake section id causing the section to be hidden unconditionally
761 if ((g_condCtx==CComment || g_readLineCtx==SComment) &&
764 //printf("** Adding terminator for comment!\n");
765 if (g_lang!=SrcLangExt_Python &&
766 g_lang!=SrcLangExt_VHDL)
772 if (*yytext=='\n') g_lineNr++;
773 if (g_readLineCtx==SComment)
782 <CComment,ReadLine>[\\@][a-z_A-Z][a-z_A-Z0-9]* { // expand alias without arguments
783 replaceAliases(yytext);
785 <CComment,ReadLine>[\\@][a-z_A-Z][a-z_A-Z0-9]*"{" { // expand alias with arguments
786 g_lastBlockContext=YY_START;
788 g_aliasString=yytext;
790 BEGIN( ReadAliasArgs );
792 <ReadAliasArgs>^[ \t]*"//"[/!]/[^\n]+ { // skip leading special comments (see bug 618079)
794 <ReadAliasArgs>"*/" { // oops, end of comment in the middle of an alias?
795 if (g_lang==SrcLangExt_Python)
799 else // abort the alias, restart scanning
801 copyToOutput(g_aliasString,g_aliasString.length());
802 copyToOutput(yytext,(int)yyleng);
806 <ReadAliasArgs>[^{}\n\\\*]+ {
807 g_aliasString+=yytext;
810 <ReadAliasArgs>"\\" {
811 if (g_lastEscaped) g_lastEscaped=FALSE;
812 else g_lastEscaped=TRUE;
813 g_aliasString+=yytext;
816 g_aliasString+=yytext;
821 g_aliasString+=yytext;
822 if (!g_lastEscaped) g_blockCount++;
826 g_aliasString+=yytext;
827 if (!g_lastEscaped) g_blockCount--;
830 replaceAliases(g_aliasString);
831 BEGIN( g_lastBlockContext );
836 g_aliasString+=yytext;
840 copyToOutput(yytext,(int)yyleng);
845 void replaceComment(int offset)
847 if (g_mlBrief || g_skip)
849 copyToOutput(yytext,(int)yyleng);
853 //printf("replaceComment(%s)\n",yytext);
854 int i=computeIndent(&yytext[offset]);
855 if (i==g_blockHeadCol)
857 replaceCommentMarker(yytext,(int)yyleng);
861 copyToOutput(" */",3);
862 int i;for (i=(int)yyleng-1;i>=0;i--) unput(yytext[i]);
863 g_inSpecialComment=FALSE;
869 // simplified way to know if this is fixed form
870 // duplicate in fortrancode.l
871 static bool recognizeFixedForm(const char* contents)
879 switch(contents[i]) {
891 if(column==1) return TRUE;
895 if(column>1 && column<7) return FALSE;
900 if(column==7) return TRUE;
908 /*! This function does three things:
909 * -# It converts multi-line C++ style comment blocks (that are aligned)
910 * to C style comment blocks (if MULTILINE_CPP_IS_BRIEF is set to NO).
911 * -# It replaces aliases with their definition (see ALIASES)
912 * -# It handles conditional sections (cond...endcond blocks)
914 void convertCppComments(BufStr *inBuf,BufStr *outBuf,const char *fileName)
916 //printf("convertCppComments(%s)\n",fileName);
921 g_mlBrief = Config_getBool("MULTILINE_CPP_IS_BRIEF");
923 g_fileName = fileName;
924 g_lang = getLanguageFromFileName(fileName);
925 g_pythonDocString = FALSE;
928 g_condStack.setAutoDelete(TRUE);
931 if (g_lang==SrcLangExt_Fortran)
933 isFixedForm = recognizeFixedForm(inBuf->data());
936 if (g_lang==SrcLangExt_Markdown)
945 while (!g_condStack.isEmpty())
947 CondCtx *ctx = g_condStack.pop();
948 QCString sectionInfo = " ";
949 if (ctx->sectionId!=" ") sectionInfo.sprintf(" with label %s ",ctx->sectionId.data());
950 warn(g_fileName,ctx->lineNr,"Conditional section%sdoes not have "
951 "a corresponding \\endcond command within this file.",sectionInfo.data());
953 if (Debug::isFlagSet(Debug::CommentCnv))
955 g_outBuf->at(g_outBuf->curPos())='\0';
956 msg("-------------\n%s\n-------------\n",g_outBuf->data());
961 //----------------------------------------------------------------------------
962 #if !defined(YY_FLEX_SUBMINOR_VERSION)
963 extern "C" { // some bogus code to keep the compiler happy
964 void commentcnvYYdummy() { yy_flex_realloc(0,0); }