1 /*****************************************************************************
5 * Copyright (C) 1997-2015 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.
17 %option never-interactive
18 %option prefix="commentcnvYY"
28 #include <qtextstream.h>
37 #include "condparser.h"
42 #define YY_NO_UNISTD_H 1
44 #define ADDCHAR(c) g_outBuf->addChar(c)
45 #define ADDARRAY(a,s) g_outBuf->addArray(a,s)
49 CondCtx(int line,QCString id,bool b)
50 : lineNr(line),sectionId(id), skip(b) {}
63 static BufStr * g_inBuf;
64 static BufStr * g_outBuf;
65 static int g_inBufPos;
67 static int g_blockHeadCol;
68 static bool g_mlBrief;
69 static int g_readLineCtx;
71 static QCString g_fileName;
74 static QStack<CondCtx> g_condStack;
75 static QStack<CommentCtx> g_commentStack;
76 static QCString g_blockName;
77 static int g_lastCommentContext;
78 static bool g_inSpecialComment;
79 static bool g_inRoseComment;
80 static int g_stringContext;
81 static int g_charContext;
82 static int g_javaBlock;
83 static bool g_specialComment;
85 static QCString g_aliasString;
86 static int g_blockCount;
87 static bool g_lastEscaped;
88 static int g_lastBlockContext;
89 static bool g_pythonDocString;
90 static int g_nestingCount;
92 static bool g_vhdl; // for VHDL old style --! comment
94 static SrcLangExt g_lang;
95 static bool isFixedForm; // For Fortran
97 static void replaceCommentMarker(const char *s,int len)
101 // copy leading blanks
102 while ((c=*p) && (c==' ' || c=='\t' || c=='\n'))
108 // replace start of comment marker by blanks and the last character by a *
110 while ((c=*p) && (c=='/' || c=='!' || c=='#'))
114 if (*p=='<') // comment-after-item marker
119 if (c=='!') // end after first !
131 if (blanks>1) ADDCHAR('*');
134 // copy comment line to output
135 ADDARRAY(p,len-(int)(p-s));
138 static inline int computeIndent(const char *s)
141 static int tabSize=Config_getInt(TAB_SIZE);
147 else if (c=='\t') col+=tabSize-(col%tabSize);
153 static inline void copyToOutput(const char *s,int len)
156 if (g_skip) // only add newlines.
163 //fprintf(stderr,"---> skip %d\n",g_lineNr);
171 static int tabSize=Config_getInt(TAB_SIZE);
177 //fprintf(stderr,"---> copy %d\n",g_lineNr);
179 case '\t': g_col+=tabSize-(g_col%tabSize); break;
180 default: g_col++; break;
186 static void startCondSection(const char *sectId)
188 //printf("startCondSection: skip=%d stack=%d\n",g_skip,g_condStack.count());
190 bool expResult = prs.parse(g_fileName,g_lineNr,sectId);
191 g_condStack.push(new CondCtx(g_lineNr,sectId,g_skip));
192 if (!expResult) // not enabled
198 static void endCondSection()
200 if (g_condStack.isEmpty())
202 warn(g_fileName,g_lineNr,"Found \\endcond command without matching \\cond");
207 CondCtx *ctx = g_condStack.pop();
210 //printf("endCondSection: skip=%d stack=%d\n",g_skip,g_condStack.count());
213 /** copies string \a s with length \a len to the output, while
214 * replacing any alias commands found in the string.
216 static void replaceAliases(const char *s)
218 QCString result = resolveAliasCmd(s);
219 //printf("replaceAliases(%s)->'%s'\n",s,result.data());
220 copyToOutput(result,result.length());
225 #define YY_INPUT(buf,result,max_size) result=yyread(buf,max_size);
227 static int yyread(char *buf,int max_size)
229 int bytesInBuf = g_inBuf->curPos()-g_inBufPos;
230 int bytesToCopy = QMIN(max_size,bytesInBuf);
231 memcpy(buf,g_inBuf->data()+g_inBufPos,bytesToCopy);
232 g_inBufPos+=bytesToCopy;
236 void replaceComment(int offset);
255 <Scan>[^"'!\/\n\\#,\-]* { /* eat anything that is not " / , or \n */
256 copyToOutput(yytext,(int)yyleng);
258 <Scan>[,] { /* eat , so we have a nice separator in long initialization lines */
259 copyToOutput(yytext,(int)yyleng);
261 <Scan>"\"\"\""! { /* start of python long comment */
262 if (g_lang!=SrcLangExt_Python)
268 g_pythonDocString = TRUE;
270 g_commentStack.clear(); /* to be on the save side */
271 copyToOutput(yytext,(int)yyleng);
273 g_commentStack.push(new CommentCtx(g_lineNr));
277 if (g_lang!=SrcLangExt_Fortran)
283 copyToOutput(yytext,(int)yyleng);
285 g_commentStack.clear(); /* to be on the save side */
287 g_commentStack.push(new CommentCtx(g_lineNr));
290 <Scan>[Cc\*][><!]/.*\n {
291 if (g_lang!=SrcLangExt_Fortran)
297 /* check for fixed format; we might have some conditional as part of multilene if like C<5 .and. & */
298 if (isFixedForm && (g_col == 0))
300 copyToOutput(yytext,(int)yyleng);
302 g_commentStack.clear(); /* to be on the save side */
304 g_commentStack.push(new CommentCtx(g_lineNr));
313 if (g_lang!=SrcLangExt_Fortran)
319 copyToOutput(yytext,(int)yyleng);
323 if (g_lang!=SrcLangExt_Fortran)
331 copyToOutput(yytext,(int)yyleng);
339 <Scan>"\"" { /* start of a string */
340 copyToOutput(yytext,(int)yyleng);
341 g_stringContext = YY_START;
345 copyToOutput(yytext,(int)yyleng);
346 g_charContext = YY_START;
347 if (g_lang!=SrcLangExt_VHDL)
352 <Scan>\n { /* new line */
353 copyToOutput(yytext,(int)yyleng);
355 <Scan>"//!"/.*\n[ \t]*"//"[\/!][^\/] | /* start C++ style special comment block */
356 <Scan>("///"[/]*)/[^/].*\n[ \t]*"//"[\/!][^\/] { /* start C++ style special comment block */
359 REJECT; // bail out if we do not need to convert
366 while (i<(int)yyleng && yytext[i]=='/') i++;
368 g_blockHeadCol=g_col;
369 copyToOutput("/**",3);
370 replaceAliases(yytext+i);
371 g_inSpecialComment=TRUE;
373 g_readLineCtx=SComment;
377 <Scan>"//##Documentation".*/\n { /* Start of Rational Rose ANSI C++ comment block */
378 if (g_mlBrief) REJECT;
379 int i=17; //=strlen("//##Documentation");
380 g_blockHeadCol=g_col;
381 copyToOutput("/**",3);
382 replaceAliases(yytext+i);
383 g_inRoseComment=TRUE;
386 <Scan>"//"[!\/]/.*\n[ \t]*"//"[|\/][ \t]*[@\\]"}" { // next line contains an end marker, see bug 752712
387 g_inSpecialComment=yytext[2]=='/' || yytext[2]=='!';
388 copyToOutput(yytext,(int)yyleng);
389 g_readLineCtx=YY_START;
392 <Scan>"//"/.*\n { /* one line C++ comment */
393 g_inSpecialComment=yytext[2]=='/' || yytext[2]=='!';
394 copyToOutput(yytext,(int)yyleng);
395 g_readLineCtx=YY_START;
398 <Scan>"/**/" { /* avoid matching next rule for empty C comment, see bug 711723 */
399 copyToOutput(yytext,(int)yyleng);
401 <Scan>"/*"[*!]? { /* start of a C comment */
402 g_specialComment=(int)yyleng==3;
404 g_commentStack.clear(); /* to be on the save side */
405 copyToOutput(yytext,(int)yyleng);
407 g_commentStack.push(new CommentCtx(g_lineNr));
410 if (g_lang!=SrcLangExt_Python)
416 copyToOutput(yytext,(int)yyleng);
418 g_commentStack.clear(); /* to be on the save side */
420 g_commentStack.push(new CommentCtx(g_lineNr));
424 if (g_lang!=SrcLangExt_VHDL)
431 copyToOutput(yytext,(int)yyleng);
433 g_commentStack.clear(); /* to be on the save side */
435 g_commentStack.push(new CommentCtx(g_lineNr));
439 if (g_lang!=SrcLangExt_Fortran)
445 copyToOutput(yytext,(int)yyleng);
447 g_commentStack.clear(); /* to be on the save side */
449 g_commentStack.push(new CommentCtx(g_lineNr));
452 <CComment>"{@code"/[ \t\n] {
453 copyToOutput("@code",5);
454 g_lastCommentContext = YY_START;
456 g_blockName=&yytext[1];
459 <CComment,ReadLine>[\\@]("dot"|"code"|"msc"|"startuml")/[^a-z_A-Z0-9] { /* start of a verbatim block */
460 copyToOutput(yytext,(int)yyleng);
461 g_lastCommentContext = YY_START;
463 if (qstrcmp(&yytext[1],"startuml")==0)
469 g_blockName=&yytext[1];
473 <CComment,ReadLine>[\\@]("f$"|"f["|"f{") {
474 copyToOutput(yytext,(int)yyleng);
475 g_blockName=&yytext[1];
476 if (g_blockName.at(1)=='[')
478 g_blockName.at(1)=']';
480 else if (g_blockName.at(1)=='{')
482 g_blockName.at(1)='}';
484 g_lastCommentContext = YY_START;
487 <CComment,ReadLine>[\\@]("verbatim"|"latexonly"|"htmlonly"|"xmlonly"|"docbookonly"|"rtfonly"|"manonly")/[^a-z_A-Z0-9] { /* start of a verbatim block */
488 copyToOutput(yytext,(int)yyleng);
489 g_blockName=&yytext[1];
490 g_lastCommentContext = YY_START;
493 <Scan>. { /* any ather character */
494 copyToOutput(yytext,(int)yyleng);
496 <Verbatim>[\\@]("endverbatim"|"endlatexonly"|"endhtmlonly"|"endxmlonly"|"enddocbookonly"|"endrtfonly"|"endmanonly"|"f$"|"f]"|"f}") { /* end of verbatim block */
497 copyToOutput(yytext,(int)yyleng);
498 if (&yytext[1]==g_blockName) // end of formula
500 BEGIN(g_lastCommentContext);
502 else if (&yytext[4]==g_blockName)
504 BEGIN(g_lastCommentContext);
515 copyToOutput(yytext,(int)yyleng);
528 copyToOutput(" @endcode ",10);
529 BEGIN(g_lastCommentContext);
533 copyToOutput(yytext,(int)yyleng);
537 <VerbatimCode>[\\@]("enddot"|"endcode"|"endmsc"|"enduml") { /* end of verbatim block */
538 copyToOutput(yytext,(int)yyleng);
539 if (&yytext[4]==g_blockName)
541 BEGIN(g_lastCommentContext);
544 <VerbatimCode>^[ \t]*"//"[\!\/]? { /* skip leading comments */
545 if (!g_inSpecialComment)
547 copyToOutput(yytext,(int)yyleng);
552 while (yytext[l]==' ' || yytext[l]=='\t')
556 copyToOutput(yytext,l);
557 if (yyleng-l==3) // ends with //! or ///
559 copyToOutput(" * ",3);
563 copyToOutput("//",2);
567 <Verbatim,VerbatimCode>[^@\/\\\n{}]* { /* any character not a backslash or new line or } */
568 copyToOutput(yytext,(int)yyleng);
570 <Verbatim,VerbatimCode>\n { /* new line in verbatim block */
571 copyToOutput(yytext,(int)yyleng);
573 <Verbatim>^[ \t]*"///" {
574 if (g_blockName=="dot" || g_blockName=="msc" || g_blockName=="uml" || g_blockName.at(0)=='f')
576 // see bug 487871, strip /// from dot images and formulas.
578 while (yytext[l]==' ' || yytext[l]=='\t')
582 copyToOutput(yytext,l);
585 else // even slashes are verbatim (e.g. \verbatim, \code)
590 <Verbatim,VerbatimCode>. { /* any other character */
591 copyToOutput(yytext,(int)yyleng);
593 <SkipString>\\. { /* escaped character in string */
594 copyToOutput(yytext,(int)yyleng);
596 <SkipString>"\"" { /* end of string */
597 copyToOutput(yytext,(int)yyleng);
598 BEGIN(g_stringContext);
600 <SkipString>. { /* any other string character */
601 copyToOutput(yytext,(int)yyleng);
603 <SkipString>\n { /* new line inside string (illegal for some compilers) */
604 copyToOutput(yytext,(int)yyleng);
606 <SkipChar>\\. { /* escaped character */
607 copyToOutput(yytext,(int)yyleng);
609 <SkipChar>' { /* end of character literal */
610 copyToOutput(yytext,(int)yyleng);
611 BEGIN(g_charContext);
613 <SkipChar>. { /* any other string character */
614 copyToOutput(yytext,(int)yyleng);
616 <SkipChar>\n { /* new line character */
617 copyToOutput(yytext,(int)yyleng);
620 <CComment>[^\\!@*\n{\"\/]* { /* anything that is not a '*' or command */
621 copyToOutput(yytext,(int)yyleng);
623 <CComment>"*"+[^*/\\@\n{\"]* { /* stars without slashes */
624 copyToOutput(yytext,(int)yyleng);
626 <CComment>"\"\"\"" { /* end of Python docstring */
627 if (g_lang!=SrcLangExt_Python)
633 g_pythonDocString = FALSE;
634 copyToOutput(yytext,(int)yyleng);
638 <CComment>\n { /* new line in comment */
639 copyToOutput(yytext,(int)yyleng);
640 /* in case of Fortran always end of comment */
641 if (g_lang==SrcLangExt_Fortran)
646 <CComment>"/"+"*" { /* nested C comment */
648 g_commentStack.push(new CommentCtx(g_lineNr));
649 copyToOutput(yytext,(int)yyleng);
651 <CComment>"*"+"/" { /* end of C comment */
652 if (g_lang==SrcLangExt_Python)
658 copyToOutput(yytext,(int)yyleng);
659 if (g_nestingCount<=0)
666 delete g_commentStack.pop();
670 /* Python an VHDL share CComment, so special attention for ending comments is required */
671 <CComment>"\n"/[ \t]*"#" {
672 if (g_lang!=SrcLangExt_VHDL)
678 if (g_vhdl) // inside --! comment
681 copyToOutput(yytext,(int)yyleng);
684 else // C-type comment
690 <CComment>"\n"/[ \t]*"-" {
691 if (g_lang!=SrcLangExt_Python || g_pythonDocString)
697 copyToOutput(yytext,(int)yyleng);
701 <CComment>"\n"/[ \t]*[^ \t#\-] {
702 if (g_lang==SrcLangExt_Python)
704 if (g_pythonDocString)
710 copyToOutput(yytext,(int)yyleng);
714 else if (g_lang==SrcLangExt_VHDL)
716 if (g_vhdl) // inside --! comment
719 copyToOutput(yytext,(int)yyleng);
722 else // C-type comment
732 /* removed for bug 674842 (bug was introduced in rev 768)
734 g_charContext = YY_START;
735 copyToOutput(yytext,(int)yyleng);
739 g_stringContext = YY_START;
740 copyToOutput(yytext,(int)yyleng);
745 copyToOutput(yytext,(int)yyleng);
747 <SComment>^[ \t]*"///"[\/]*/\n {
750 <SComment>\n[ \t]*"///"[\/]*/\n {
753 <SComment>^[ \t]*"///"[^\/\n]/.*\n {
755 g_readLineCtx=YY_START;
758 <SComment>\n[ \t]*"//"[\/!]("<")?[ \t]*[\\@]"}".*\n {
759 /* See Bug 752712: end the multiline comment when finding a @} or \} command */
760 copyToOutput(" */",3);
761 copyToOutput(yytext,(int)yyleng);
762 g_inSpecialComment=FALSE;
763 g_inRoseComment=FALSE;
766 <SComment>\n[ \t]*"///"[^\/\n]/.*\n {
768 g_readLineCtx=YY_START;
771 <SComment>^[ \t]*"//!" | // just //!
772 <SComment>^[ \t]*"//!<"/.*\n | // or //!< something
773 <SComment>^[ \t]*"//!"[^<]/.*\n { // or //!something
775 g_readLineCtx=YY_START;
778 <SComment>\n[ \t]*"//!" |
779 <SComment>\n[ \t]*"//!<"/.*\n |
780 <SComment>\n[ \t]*"//!"[^<\n]/.*\n {
782 g_readLineCtx=YY_START;
785 <SComment>^[ \t]*"//##"/.*\n {
786 if (!g_inRoseComment)
793 g_readLineCtx=YY_START;
797 <SComment>\n[ \t]*"//##"/.*\n {
798 if (!g_inRoseComment)
805 g_readLineCtx=YY_START;
809 <SComment>\n { /* end of special comment */
810 copyToOutput(" */",3);
811 copyToOutput(yytext,(int)yyleng);
812 g_inSpecialComment=FALSE;
813 g_inRoseComment=FALSE;
816 <ReadLine>[^\\@\n]*/\n {
817 copyToOutput(yytext,(int)yyleng);
818 BEGIN(g_readLineCtx);
820 <CComment,ReadLine>[\\@][\\@][~a-z_A-Z][a-z_A-Z0-9]*[ \t]* { // escaped command
821 copyToOutput(yytext,(int)yyleng);
823 <CComment,ReadLine>[\\@]"cond"/[^a-z_A-Z0-9] { // conditional section
824 g_condCtx = YY_START;
827 <CComment,ReadLine>[\\@]"endcond"/[^a-z_A-Z0-9] { // end of conditional section
830 if (YY_START==CComment && oldSkip && !g_skip)
832 //printf("** Adding start of comment!\n");
833 if (g_lang!=SrcLangExt_Python &&
834 g_lang!=SrcLangExt_VHDL &&
835 g_lang!=SrcLangExt_Markdown &&
836 g_lang!=SrcLangExt_Fortran)
840 if (g_specialComment)
847 <CondLine>[!()&| \ta-z_A-Z0-9.\-]+ {
849 startCondSection(yytext);
850 if ((g_condCtx==CComment || g_readLineCtx==SComment) &&
853 if (g_lang!=SrcLangExt_Python &&
854 g_lang!=SrcLangExt_VHDL &&
855 g_lang!=SrcLangExt_Markdown &&
856 g_lang!=SrcLangExt_Fortran)
862 if (g_readLineCtx==SComment)
872 <CComment,ReadLine>[\\@]"cond"[ \t\r]*/\n |
873 <CondLine>. { // forgot section id?
874 if (YY_START!=CondLine) g_condCtx=YY_START;
876 startCondSection(" "); // fake section id causing the section to be hidden unconditionally
877 if ((g_condCtx==CComment || g_readLineCtx==SComment) &&
880 //printf("** Adding terminator for comment!\n");
881 if (g_lang!=SrcLangExt_Python &&
882 g_lang!=SrcLangExt_VHDL)
888 if (*yytext=='\n') g_lineNr++;
889 if (g_readLineCtx==SComment)
898 <CComment,ReadLine>[\\@][a-z_A-Z][a-z_A-Z0-9]* { // expand alias without arguments
899 replaceAliases(yytext);
901 <CComment,ReadLine>[\\@][a-z_A-Z][a-z_A-Z0-9]*"{" { // expand alias with arguments
902 g_lastBlockContext=YY_START;
904 g_aliasString=yytext;
906 BEGIN( ReadAliasArgs );
908 <ReadAliasArgs>^[ \t]*"//"[/!]/[^\n]+ { // skip leading special comments (see bug 618079)
910 <ReadAliasArgs>"*/" { // oops, end of comment in the middle of an alias?
911 if (g_lang==SrcLangExt_Python)
915 else // abort the alias, restart scanning
917 copyToOutput(g_aliasString,g_aliasString.length());
918 copyToOutput(yytext,(int)yyleng);
922 <ReadAliasArgs>[^{}\n\\\*]+ {
923 g_aliasString+=yytext;
926 <ReadAliasArgs>"\\" {
927 if (g_lastEscaped) g_lastEscaped=FALSE;
928 else g_lastEscaped=TRUE;
929 g_aliasString+=yytext;
932 g_aliasString+=yytext;
937 g_aliasString+=yytext;
938 if (!g_lastEscaped) g_blockCount++;
942 g_aliasString+=yytext;
943 if (!g_lastEscaped) g_blockCount--;
946 replaceAliases(g_aliasString);
947 BEGIN( g_lastBlockContext );
952 g_aliasString+=yytext;
956 copyToOutput(yytext,(int)yyleng);
961 void replaceComment(int offset)
963 if (g_mlBrief || g_skip)
965 copyToOutput(yytext,(int)yyleng);
969 //printf("replaceComment(%s)\n",yytext);
970 int i=computeIndent(&yytext[offset]);
971 if (i==g_blockHeadCol)
973 replaceCommentMarker(yytext,(int)yyleng);
977 copyToOutput(" */",3);
978 int i;for (i=(int)yyleng-1;i>=0;i--) unput(yytext[i]);
979 g_inSpecialComment=FALSE;
985 // simplified way to know if this is fixed form
986 // duplicate in fortrancode.l
987 static bool recognizeFixedForm(const char* contents)
995 switch(contents[i]) {
1007 if(column==1) return TRUE;
1011 if(column>1 && column<7) return FALSE;
1016 if(column==7) return TRUE;
1024 /*! This function does three things:
1025 * -# It converts multi-line C++ style comment blocks (that are aligned)
1026 * to C style comment blocks (if MULTILINE_CPP_IS_BRIEF is set to NO).
1027 * -# It replaces aliases with their definition (see ALIASES)
1028 * -# It handles conditional sections (cond...endcond blocks)
1030 void convertCppComments(BufStr *inBuf,BufStr *outBuf,const char *fileName)
1032 //printf("convertCppComments(%s)\n",fileName);
1037 g_mlBrief = Config_getBool(MULTILINE_CPP_IS_BRIEF);
1039 g_fileName = fileName;
1040 g_lang = getLanguageFromFileName(fileName);
1041 g_pythonDocString = FALSE;
1043 g_condStack.clear();
1044 g_condStack.setAutoDelete(TRUE);
1045 g_commentStack.clear();
1046 g_commentStack.setAutoDelete(TRUE);
1049 printlex(yy_flex_debug, TRUE, __FILE__, fileName);
1050 isFixedForm = FALSE;
1051 if (g_lang==SrcLangExt_Fortran)
1053 isFixedForm = recognizeFixedForm(inBuf->data());
1056 if (g_lang==SrcLangExt_Markdown)
1060 g_commentStack.push(new CommentCtx(g_lineNr));
1067 while (!g_condStack.isEmpty())
1069 CondCtx *ctx = g_condStack.pop();
1070 QCString sectionInfo = " ";
1071 if (ctx->sectionId!=" ") sectionInfo.sprintf(" with label %s ",ctx->sectionId.data());
1072 warn(g_fileName,ctx->lineNr,"Conditional section%sdoes not have "
1073 "a corresponding \\endcond command within this file.",sectionInfo.data());
1075 if (g_nestingCount>0 && g_lang!=SrcLangExt_Markdown)
1077 QCString tmp= "(probable line reference: ";
1079 while (!g_commentStack.isEmpty())
1081 CommentCtx *ctx = g_commentStack.pop();
1082 if (!first) tmp += ", ";
1083 tmp += QCString().setNum(ctx->lineNr);
1088 warn(g_fileName,g_lineNr,"Reached end of file while still inside a (nested) comment. "
1089 "Nesting level %d %s",g_nestingCount+1,tmp.data()); // add one for "normal" expected end of comment
1091 g_commentStack.clear();
1093 if (Debug::isFlagSet(Debug::CommentCnv))
1095 g_outBuf->at(g_outBuf->curPos())='\0';
1096 msg("-------------\n%s\n-------------\n",g_outBuf->data());
1098 printlex(yy_flex_debug, FALSE, __FILE__, fileName);
1102 //----------------------------------------------------------------------------
1103 #if !defined(YY_FLEX_SUBMINOR_VERSION)
1104 extern "C" { // some bogus code to keep the compiler happy
1105 void commentcnvYYdummy() { yy_flex_realloc(0,0); }