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
26 #include <qtextstream.h>
35 #include "condparser.h"
40 #define YY_NO_UNISTD_H 1
42 #define ADDCHAR(c) g_outBuf->addChar(c)
43 #define ADDARRAY(a,s) g_outBuf->addArray(a,s)
47 CondCtx(int line,QCString id,bool b)
48 : lineNr(line),sectionId(id), skip(b) {}
61 static BufStr * g_inBuf;
62 static BufStr * g_outBuf;
63 static int g_inBufPos;
65 static int g_blockHeadCol;
66 static bool g_mlBrief;
67 static int g_readLineCtx;
69 static QCString g_fileName;
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;
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;
90 static SrcLangExt g_lang;
91 static bool isFixedForm; // For Fortran
93 static void replaceCommentMarker(const char *s,int len)
97 // copy leading blanks
98 while ((c=*p) && (c==' ' || c=='\t' || c=='\n'))
104 // replace start of comment marker by blanks and the last character by a *
106 while ((c=*p) && (c=='/' || c=='!' || c=='#'))
110 if (*p=='<') // comment-after-item marker
115 if (c=='!') // end after first !
127 if (blanks>1) ADDCHAR('*');
130 // copy comment line to output
131 ADDARRAY(p,len-(int)(p-s));
134 static inline int computeIndent(const char *s)
137 static int tabSize=Config_getInt("TAB_SIZE");
143 else if (c=='\t') col+=tabSize-(col%tabSize);
149 static inline void copyToOutput(const char *s,int len)
152 if (g_skip) // only add newlines.
159 //fprintf(stderr,"---> skip %d\n",g_lineNr);
167 static int tabSize=Config_getInt("TAB_SIZE");
173 //fprintf(stderr,"---> copy %d\n",g_lineNr);
175 case '\t': g_col+=tabSize-(g_col%tabSize); break;
176 default: g_col++; break;
182 static void startCondSection(const char *sectId)
184 //printf("startCondSection: skip=%d stack=%d\n",g_skip,g_condStack.count());
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
194 static void endCondSection()
196 if (g_condStack.isEmpty())
198 warn(g_fileName,g_lineNr,"Found \\endcond command without matching \\cond");
203 CondCtx *ctx = g_condStack.pop();
206 //printf("endCondSection: skip=%d stack=%d\n",g_skip,g_condStack.count());
209 /** copies string \a s with length \a len to the output, while
210 * replacing any alias commands found in the string.
212 static void replaceAliases(const char *s)
214 QCString result = resolveAliasCmd(s);
215 //printf("replaceAliases(%s)->'%s'\n",s,result.data());
216 copyToOutput(result,result.length());
221 #define YY_INPUT(buf,result,max_size) result=yyread(buf,max_size);
223 static int yyread(char *buf,int max_size)
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;
232 void replaceComment(int offset);
251 <Scan>[^"'!\/\n\\#-,]* { /* eat anything that is not " / , or \n */
252 copyToOutput(yytext,(int)yyleng);
254 <Scan>[,] { /* eat , so we have a nice separator in long initialization lines */
255 copyToOutput(yytext,(int)yyleng);
257 <Scan>"\"\"\""! { /* start of python long comment */
258 if (g_lang!=SrcLangExt_Python)
264 g_pythonDocString = TRUE;
266 g_commentStack.clear(); /* to be on the save side */
267 copyToOutput(yytext,(int)yyleng);
269 g_commentStack.push(new CommentCtx(g_lineNr));
273 if (g_lang!=SrcLangExt_Fortran)
279 copyToOutput(yytext,(int)yyleng);
281 g_commentStack.clear(); /* to be on the save side */
283 g_commentStack.push(new CommentCtx(g_lineNr));
286 <Scan>[Cc\*][><!]/.*\n {
287 if (g_lang!=SrcLangExt_Fortran)
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))
296 copyToOutput(yytext,(int)yyleng);
298 g_commentStack.clear(); /* to be on the save side */
300 g_commentStack.push(new CommentCtx(g_lineNr));
309 if (g_lang!=SrcLangExt_Fortran)
315 copyToOutput(yytext,(int)yyleng);
319 if (g_lang!=SrcLangExt_Fortran)
327 copyToOutput(yytext,(int)yyleng);
335 <Scan>"\"" { /* start of a string */
336 copyToOutput(yytext,(int)yyleng);
337 g_stringContext = YY_START;
341 copyToOutput(yytext,(int)yyleng);
342 g_charContext = YY_START;
343 if (g_lang!=SrcLangExt_VHDL)
348 <Scan>\n { /* new line */
349 copyToOutput(yytext,(int)yyleng);
351 <Scan>"//!"/.*\n[ \t]*"//"[\/!][^\/] | /* start C++ style special comment block */
352 <Scan>("///"[/]*)/[^/].*\n[ \t]*"//"[\/!][^\/] { /* start C++ style special comment block */
355 REJECT; // bail out if we do not need to convert
362 while (i<(int)yyleng && yytext[i]=='/') i++;
364 g_blockHeadCol=g_col;
365 copyToOutput("/**",3);
366 replaceAliases(yytext+i);
367 g_inSpecialComment=TRUE;
369 g_readLineCtx=SComment;
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;
382 <Scan>"//"/.*\n { /* one line C++ comment */
383 g_inSpecialComment=yytext[2]=='/' || yytext[2]=='!';
384 copyToOutput(yytext,(int)yyleng);
385 g_readLineCtx=YY_START;
388 <Scan>"/**/" { /* avoid matching next rule for empty C comment, see bug 711723 */
389 copyToOutput(yytext,(int)yyleng);
391 <Scan>"/*"[*!]? { /* start of a C comment */
392 g_specialComment=(int)yyleng==3;
394 g_commentStack.clear(); /* to be on the save side */
395 copyToOutput(yytext,(int)yyleng);
397 g_commentStack.push(new CommentCtx(g_lineNr));
400 if (g_lang!=SrcLangExt_Python)
406 copyToOutput(yytext,(int)yyleng);
408 g_commentStack.clear(); /* to be on the save side */
410 g_commentStack.push(new CommentCtx(g_lineNr));
414 if (g_lang!=SrcLangExt_VHDL)
420 copyToOutput(yytext,(int)yyleng);
422 g_commentStack.clear(); /* to be on the save side */
424 g_commentStack.push(new CommentCtx(g_lineNr));
428 if (g_lang!=SrcLangExt_Fortran)
434 copyToOutput(yytext,(int)yyleng);
436 g_commentStack.clear(); /* to be on the save side */
438 g_commentStack.push(new CommentCtx(g_lineNr));
441 <CComment>"{@code"/[ \t\n] {
442 copyToOutput("@code",5);
443 g_lastCommentContext = YY_START;
445 g_blockName=&yytext[1];
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;
452 if (qstrcmp(&yytext[1],"startuml")==0)
458 g_blockName=&yytext[1];
462 <CComment,ReadLine>[\\@]("f$"|"f["|"f{"[a-z]*) {
463 copyToOutput(yytext,(int)yyleng);
464 g_blockName=&yytext[1];
465 if (g_blockName.at(1)=='[')
467 g_blockName.at(1)=']';
469 else if (g_blockName.at(1)=='{')
471 g_blockName.at(1)='}';
473 g_lastCommentContext = YY_START;
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;
482 <Scan>. { /* any ather character */
483 copyToOutput(yytext,(int)yyleng);
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
489 BEGIN(g_lastCommentContext);
491 else if (&yytext[4]==g_blockName)
493 BEGIN(g_lastCommentContext);
504 copyToOutput(yytext,(int)yyleng);
517 copyToOutput(" @endcode ",10);
518 BEGIN(g_lastCommentContext);
522 copyToOutput(yytext,(int)yyleng);
526 <VerbatimCode>[\\@]("enddot"|"endcode"|"endmsc"|"enduml") { /* end of verbatim block */
527 copyToOutput(yytext,(int)yyleng);
528 if (&yytext[4]==g_blockName)
530 BEGIN(g_lastCommentContext);
533 <VerbatimCode>^[ \t]*"//"[\!\/]? { /* skip leading comments */
534 if (!g_inSpecialComment)
536 copyToOutput(yytext,(int)yyleng);
541 while (yytext[l]==' ' || yytext[l]=='\t')
545 copyToOutput(yytext,l);
546 if (yyleng-l==3) // ends with //! or ///
548 copyToOutput(" * ",3);
552 copyToOutput("//",2);
556 <Verbatim,VerbatimCode>[^@\/\\\n{}]* { /* any character not a backslash or new line or } */
557 copyToOutput(yytext,(int)yyleng);
559 <Verbatim,VerbatimCode>\n { /* new line in verbatim block */
560 copyToOutput(yytext,(int)yyleng);
562 <Verbatim>^[ \t]*"///" {
563 if (g_blockName=="dot" || g_blockName=="msc" || g_blockName=="uml" || g_blockName.at(0)=='f')
565 // see bug 487871, strip /// from dot images and formulas.
567 while (yytext[l]==' ' || yytext[l]=='\t')
571 copyToOutput(yytext,l);
574 else // even slashes are verbatim (e.g. \verbatim, \code)
579 <Verbatim,VerbatimCode>. { /* any other character */
580 copyToOutput(yytext,(int)yyleng);
582 <SkipString>\\. { /* escaped character in string */
583 copyToOutput(yytext,(int)yyleng);
585 <SkipString>"\"" { /* end of string */
586 copyToOutput(yytext,(int)yyleng);
587 BEGIN(g_stringContext);
589 <SkipString>. { /* any other string character */
590 copyToOutput(yytext,(int)yyleng);
592 <SkipString>\n { /* new line inside string (illegal for some compilers) */
593 copyToOutput(yytext,(int)yyleng);
595 <SkipChar>\\. { /* escaped character */
596 copyToOutput(yytext,(int)yyleng);
598 <SkipChar>' { /* end of character literal */
599 copyToOutput(yytext,(int)yyleng);
600 BEGIN(g_charContext);
602 <SkipChar>. { /* any other string character */
603 copyToOutput(yytext,(int)yyleng);
605 <SkipChar>\n { /* new line character */
606 copyToOutput(yytext,(int)yyleng);
609 <CComment>[^\\!@*\n{\"\/]* { /* anything that is not a '*' or command */
610 copyToOutput(yytext,(int)yyleng);
612 <CComment>"*"+[^*/\\@\n{\"]* { /* stars without slashes */
613 copyToOutput(yytext,(int)yyleng);
615 <CComment>"\"\"\"" { /* end of Python docstring */
616 if (g_lang!=SrcLangExt_Python)
622 g_pythonDocString = FALSE;
623 copyToOutput(yytext,(int)yyleng);
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)
635 <CComment>"/"+"*" { /* nested C comment */
637 g_commentStack.push(new CommentCtx(g_lineNr));
638 copyToOutput(yytext,(int)yyleng);
640 <CComment>"*"+"/" { /* end of C comment */
641 if (g_lang==SrcLangExt_Python)
647 copyToOutput(yytext,(int)yyleng);
648 if (g_nestingCount<=0)
655 delete g_commentStack.pop();
659 <CComment>"\n"/[ \t]*[^#] { /* end of Python comment */
660 if (g_lang!=SrcLangExt_Python || g_pythonDocString)
666 copyToOutput(yytext,(int)yyleng);
670 <CComment>"\n"/[ \t]*[^\-] { /* end of VHDL comment */
671 if (g_lang!=SrcLangExt_VHDL)
677 copyToOutput(yytext,(int)yyleng);
681 /* removed for bug 674842 (bug was introduced in rev 768)
683 g_charContext = YY_START;
684 copyToOutput(yytext,(int)yyleng);
688 g_stringContext = YY_START;
689 copyToOutput(yytext,(int)yyleng);
694 copyToOutput(yytext,(int)yyleng);
696 <SComment>^[ \t]*"///"[\/]*/\n {
699 <SComment>\n[ \t]*"///"[\/]*/\n {
702 <SComment>^[ \t]*"///"[^\/\n]/.*\n {
704 g_readLineCtx=YY_START;
707 <SComment>\n[ \t]*"///"[^\/\n]/.*\n {
709 g_readLineCtx=YY_START;
712 <SComment>^[ \t]*"//!" | // just //!
713 <SComment>^[ \t]*"//!<"/.*\n | // or //!< something
714 <SComment>^[ \t]*"//!"[^<]/.*\n { // or //!something
716 g_readLineCtx=YY_START;
719 <SComment>\n[ \t]*"//!" |
720 <SComment>\n[ \t]*"//!<"/.*\n |
721 <SComment>\n[ \t]*"//!"[^<\n]/.*\n {
723 g_readLineCtx=YY_START;
726 <SComment>^[ \t]*"//##"/.*\n {
727 if (!g_inRoseComment)
734 g_readLineCtx=YY_START;
738 <SComment>\n[ \t]*"//##"/.*\n {
739 if (!g_inRoseComment)
746 g_readLineCtx=YY_START;
750 <SComment>\n { /* end of special comment */
751 copyToOutput(" */",3);
752 copyToOutput(yytext,(int)yyleng);
753 g_inSpecialComment=FALSE;
754 g_inRoseComment=FALSE;
757 <ReadLine>[^\\@\n]*/\n {
758 copyToOutput(yytext,(int)yyleng);
759 BEGIN(g_readLineCtx);
761 <CComment,ReadLine>[\\@][\\@][~a-z_A-Z][a-z_A-Z0-9]*[ \t]* { // escaped command
762 copyToOutput(yytext,(int)yyleng);
764 <CComment,ReadLine>[\\@]"cond"/[^a-z_A-Z0-9] { // conditional section
765 g_condCtx = YY_START;
768 <CComment,ReadLine>[\\@]"endcond"/[^a-z_A-Z0-9] { // end of conditional section
771 if (YY_START==CComment && oldSkip && !g_skip)
773 //printf("** Adding start of comment!\n");
774 if (g_lang!=SrcLangExt_Python &&
775 g_lang!=SrcLangExt_VHDL &&
776 g_lang!=SrcLangExt_Fortran)
780 if (g_specialComment)
787 <CondLine>[!()&| \ta-z_A-Z0-9.\-]+ {
789 startCondSection(yytext);
790 if ((g_condCtx==CComment || g_readLineCtx==SComment) &&
793 if (g_lang!=SrcLangExt_Python &&
794 g_lang!=SrcLangExt_VHDL &&
795 g_lang!=SrcLangExt_Fortran)
801 if (g_readLineCtx==SComment)
811 <CComment,ReadLine>[\\@]"cond"[ \t\r]*/\n |
812 <CondLine>. { // forgot section id?
813 if (YY_START!=CondLine) g_condCtx=YY_START;
815 startCondSection(" "); // fake section id causing the section to be hidden unconditionally
816 if ((g_condCtx==CComment || g_readLineCtx==SComment) &&
819 //printf("** Adding terminator for comment!\n");
820 if (g_lang!=SrcLangExt_Python &&
821 g_lang!=SrcLangExt_VHDL)
827 if (*yytext=='\n') g_lineNr++;
828 if (g_readLineCtx==SComment)
837 <CComment,ReadLine>[\\@][a-z_A-Z][a-z_A-Z0-9]* { // expand alias without arguments
838 replaceAliases(yytext);
840 <CComment,ReadLine>[\\@][a-z_A-Z][a-z_A-Z0-9]*"{" { // expand alias with arguments
841 g_lastBlockContext=YY_START;
843 g_aliasString=yytext;
845 BEGIN( ReadAliasArgs );
847 <ReadAliasArgs>^[ \t]*"//"[/!]/[^\n]+ { // skip leading special comments (see bug 618079)
849 <ReadAliasArgs>"*/" { // oops, end of comment in the middle of an alias?
850 if (g_lang==SrcLangExt_Python)
854 else // abort the alias, restart scanning
856 copyToOutput(g_aliasString,g_aliasString.length());
857 copyToOutput(yytext,(int)yyleng);
861 <ReadAliasArgs>[^{}\n\\\*]+ {
862 g_aliasString+=yytext;
865 <ReadAliasArgs>"\\" {
866 if (g_lastEscaped) g_lastEscaped=FALSE;
867 else g_lastEscaped=TRUE;
868 g_aliasString+=yytext;
871 g_aliasString+=yytext;
876 g_aliasString+=yytext;
877 if (!g_lastEscaped) g_blockCount++;
881 g_aliasString+=yytext;
882 if (!g_lastEscaped) g_blockCount--;
885 replaceAliases(g_aliasString);
886 BEGIN( g_lastBlockContext );
891 g_aliasString+=yytext;
895 copyToOutput(yytext,(int)yyleng);
900 void replaceComment(int offset)
902 if (g_mlBrief || g_skip)
904 copyToOutput(yytext,(int)yyleng);
908 //printf("replaceComment(%s)\n",yytext);
909 int i=computeIndent(&yytext[offset]);
910 if (i==g_blockHeadCol)
912 replaceCommentMarker(yytext,(int)yyleng);
916 copyToOutput(" */",3);
917 int i;for (i=(int)yyleng-1;i>=0;i--) unput(yytext[i]);
918 g_inSpecialComment=FALSE;
924 // simplified way to know if this is fixed form
925 // duplicate in fortrancode.l
926 static bool recognizeFixedForm(const char* contents)
934 switch(contents[i]) {
946 if(column==1) return TRUE;
950 if(column>1 && column<7) return FALSE;
955 if(column==7) return TRUE;
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)
969 void convertCppComments(BufStr *inBuf,BufStr *outBuf,const char *fileName)
971 //printf("convertCppComments(%s)\n",fileName);
976 g_mlBrief = Config_getBool("MULTILINE_CPP_IS_BRIEF");
978 g_fileName = fileName;
979 g_lang = getLanguageFromFileName(fileName);
980 g_pythonDocString = FALSE;
983 g_condStack.setAutoDelete(TRUE);
984 g_commentStack.clear();
985 g_commentStack.setAutoDelete(TRUE);
987 printlex(yy_flex_debug, TRUE, __FILE__, fileName);
989 if (g_lang==SrcLangExt_Fortran)
991 isFixedForm = recognizeFixedForm(inBuf->data());
994 if (g_lang==SrcLangExt_Markdown)
998 g_commentStack.push(new CommentCtx(g_lineNr));
1005 while (!g_condStack.isEmpty())
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());
1013 if (g_nestingCount>0 && g_lang!=SrcLangExt_Markdown)
1015 QCString tmp= "(probable line reference: ";
1017 while (!g_commentStack.isEmpty())
1019 CommentCtx *ctx = g_commentStack.pop();
1020 if (!first) tmp += ", ";
1021 tmp += QCString().setNum(ctx->lineNr);
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
1029 g_commentStack.clear();
1031 if (Debug::isFlagSet(Debug::CommentCnv))
1033 g_outBuf->at(g_outBuf->curPos())='\0';
1034 msg("-------------\n%s\n-------------\n",g_outBuf->data());
1036 printlex(yy_flex_debug, FALSE, __FILE__, fileName);
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); }