1 /*****************************************************************************
5 * Copyright (C) 1997-2014 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) {}
62 static BufStr * g_inBuf;
63 static BufStr * g_outBuf;
64 static int g_inBufPos;
66 static int g_blockHeadCol;
67 static bool g_mlBrief;
68 static int g_readLineCtx;
70 static QCString g_fileName;
73 static QStack<CondCtx> g_condStack;
74 static QStack<CommentCtx> g_commentStack;
75 static QCString g_blockName;
76 static int g_lastCommentContext;
77 static bool g_inSpecialComment;
78 static bool g_inRoseComment;
79 static int g_stringContext;
80 static int g_charContext;
81 static int g_javaBlock;
82 static bool g_specialComment;
84 static QCString g_aliasString;
85 static int g_blockCount;
86 static bool g_lastEscaped;
87 static int g_lastBlockContext;
88 static bool g_pythonDocString;
89 static int g_nestingCount;
91 static SrcLangExt g_lang;
92 static bool isFixedForm; // For Fortran
94 static void replaceCommentMarker(const char *s,int len)
98 // copy leading blanks
99 while ((c=*p) && (c==' ' || c=='\t' || c=='\n'))
105 // replace start of comment marker by blanks and the last character by a *
107 while ((c=*p) && (c=='/' || c=='!' || c=='#'))
111 if (*p=='<') // comment-after-item marker
116 if (c=='!') // end after first !
128 if (blanks>1) ADDCHAR('*');
131 // copy comment line to output
132 ADDARRAY(p,len-(int)(p-s));
135 static inline int computeIndent(const char *s)
138 static int tabSize=Config_getInt("TAB_SIZE");
144 else if (c=='\t') col+=tabSize-(col%tabSize);
150 static inline void copyToOutput(const char *s,int len)
153 if (g_skip) // only add newlines.
160 //fprintf(stderr,"---> skip %d\n",g_lineNr);
168 static int tabSize=Config_getInt("TAB_SIZE");
174 //fprintf(stderr,"---> copy %d\n",g_lineNr);
176 case '\t': g_col+=tabSize-(g_col%tabSize); break;
177 default: g_col++; break;
183 static void startCondSection(const char *sectId)
185 //printf("startCondSection: skip=%d stack=%d\n",g_skip,g_condStack.count());
187 bool expResult = prs.parse(g_fileName,g_lineNr,sectId);
188 g_condStack.push(new CondCtx(g_lineNr,sectId,g_skip));
189 if (!expResult) // not enabled
195 static void endCondSection()
197 if (g_condStack.isEmpty())
199 warn(g_fileName,g_lineNr,"Found \\endcond command without matching \\cond");
204 CondCtx *ctx = g_condStack.pop();
207 //printf("endCondSection: skip=%d stack=%d\n",g_skip,g_condStack.count());
210 /** copies string \a s with length \a len to the output, while
211 * replacing any alias commands found in the string.
213 static void replaceAliases(const char *s)
215 QCString result = resolveAliasCmd(s);
216 //printf("replaceAliases(%s)->'%s'\n",s,result.data());
217 copyToOutput(result,result.length());
222 #define YY_INPUT(buf,result,max_size) result=yyread(buf,max_size);
224 static int yyread(char *buf,int max_size)
226 int bytesInBuf = g_inBuf->curPos()-g_inBufPos;
227 int bytesToCopy = QMIN(max_size,bytesInBuf);
228 memcpy(buf,g_inBuf->data()+g_inBufPos,bytesToCopy);
229 g_inBufPos+=bytesToCopy;
233 void replaceComment(int offset);
252 <Scan>[^"'!\/\n\\#-,]* { /* eat anything that is not " / , or \n */
253 copyToOutput(yytext,(int)yyleng);
255 <Scan>[,] { /* eat , so we have a nice separator in long initialization lines */
256 copyToOutput(yytext,(int)yyleng);
258 <Scan>"\"\"\""! { /* start of python long comment */
259 if (g_lang!=SrcLangExt_Python)
265 g_pythonDocString = TRUE;
267 g_commentStack.clear(); /* to be on the save side */
268 copyToOutput(yytext,(int)yyleng);
270 g_commentStack.push(new CommentCtx(g_lineNr));
274 if (g_lang!=SrcLangExt_Fortran)
280 copyToOutput(yytext,(int)yyleng);
282 g_commentStack.clear(); /* to be on the save side */
284 g_commentStack.push(new CommentCtx(g_lineNr));
287 <Scan>[Cc\*][><!]/.*\n {
288 if (g_lang!=SrcLangExt_Fortran)
294 /* check for fixed format; we might have some conditional as part of multilene if like C<5 .and. & */
295 if (isFixedForm && (g_col == 0))
297 copyToOutput(yytext,(int)yyleng);
299 g_commentStack.clear(); /* to be on the save side */
301 g_commentStack.push(new CommentCtx(g_lineNr));
310 if (g_lang!=SrcLangExt_Fortran)
316 copyToOutput(yytext,(int)yyleng);
320 if (g_lang!=SrcLangExt_Fortran)
328 copyToOutput(yytext,(int)yyleng);
336 <Scan>"\"" { /* start of a string */
337 copyToOutput(yytext,(int)yyleng);
338 g_stringContext = YY_START;
342 copyToOutput(yytext,(int)yyleng);
343 g_charContext = YY_START;
344 if (g_lang!=SrcLangExt_VHDL)
349 <Scan>\n { /* new line */
350 copyToOutput(yytext,(int)yyleng);
352 <Scan>"//!"/.*\n[ \t]*"//"[\/!][^\/] | /* start C++ style special comment block */
353 <Scan>("///"[/]*)/[^/].*\n[ \t]*"//"[\/!][^\/] { /* start C++ style special comment block */
356 REJECT; // bail out if we do not need to convert
363 while (i<(int)yyleng && yytext[i]=='/') i++;
365 g_blockHeadCol=g_col;
366 copyToOutput("/**",3);
367 replaceAliases(yytext+i);
368 g_inSpecialComment=TRUE;
370 g_readLineCtx=SComment;
374 <Scan>"//##Documentation".*/\n { /* Start of Rational Rose ANSI C++ comment block */
375 if (g_mlBrief) REJECT;
376 int i=17; //=strlen("//##Documentation");
377 g_blockHeadCol=g_col;
378 copyToOutput("/**",3);
379 replaceAliases(yytext+i);
380 g_inRoseComment=TRUE;
383 <Scan>"//"/.*\n { /* one line C++ comment */
384 g_inSpecialComment=yytext[2]=='/' || yytext[2]=='!';
385 copyToOutput(yytext,(int)yyleng);
386 g_readLineCtx=YY_START;
389 <Scan>"/**/" { /* avoid matching next rule for empty C comment, see bug 711723 */
390 copyToOutput(yytext,(int)yyleng);
392 <Scan>"/*"[*!]? { /* start of a C comment */
393 g_specialComment=(int)yyleng==3;
395 g_commentStack.clear(); /* to be on the save side */
396 copyToOutput(yytext,(int)yyleng);
398 g_commentStack.push(new CommentCtx(g_lineNr));
401 if (g_lang!=SrcLangExt_Python)
407 copyToOutput(yytext,(int)yyleng);
409 g_commentStack.clear(); /* to be on the save side */
411 g_commentStack.push(new CommentCtx(g_lineNr));
415 if (g_lang!=SrcLangExt_VHDL)
421 copyToOutput(yytext,(int)yyleng);
423 g_commentStack.clear(); /* to be on the save side */
425 g_commentStack.push(new CommentCtx(g_lineNr));
429 if (g_lang!=SrcLangExt_Fortran)
435 copyToOutput(yytext,(int)yyleng);
437 g_commentStack.clear(); /* to be on the save side */
439 g_commentStack.push(new CommentCtx(g_lineNr));
442 <CComment>"{@code"/[ \t\n] {
443 copyToOutput("@code",5);
444 g_lastCommentContext = YY_START;
446 g_blockName=&yytext[1];
449 <CComment,ReadLine>[\\@]("dot"|"code"|"msc"|"startuml")/[^a-z_A-Z0-9] { /* start of a verbatim block */
450 copyToOutput(yytext,(int)yyleng);
451 g_lastCommentContext = YY_START;
453 g_blockName=&yytext[1];
456 <CComment,ReadLine>[\\@]("f$"|"f["|"f{"[a-z]*) {
457 copyToOutput(yytext,(int)yyleng);
458 g_blockName=&yytext[1];
459 if (g_blockName.at(1)=='[')
461 g_blockName.at(1)=']';
463 else if (g_blockName.at(1)=='{')
465 g_blockName.at(1)='}';
467 g_lastCommentContext = YY_START;
470 <CComment,ReadLine>[\\@]("verbatim"|"latexonly"|"htmlonly"|"xmlonly"|"docbookonly"|"rtfonly"|"manonly")/[^a-z_A-Z0-9] { /* start of a verbatim block */
471 copyToOutput(yytext,(int)yyleng);
472 g_blockName=&yytext[1];
473 g_lastCommentContext = YY_START;
476 <Scan>. { /* any ather character */
477 copyToOutput(yytext,(int)yyleng);
479 <Verbatim>[\\@]("endverbatim"|"endlatexonly"|"endhtmlonly"|"endxmlonly"|"docbookonly"|"endrtfonly"|"endmanonly"|"f$"|"f]"|"f}") { /* end of verbatim block */
480 copyToOutput(yytext,(int)yyleng);
481 if (yytext[1]=='f') // end of formula
483 BEGIN(g_lastCommentContext);
485 else if (&yytext[4]==g_blockName)
487 BEGIN(g_lastCommentContext);
498 copyToOutput(yytext,(int)yyleng);
511 copyToOutput(" @endcode ",10);
512 BEGIN(g_lastCommentContext);
516 copyToOutput(yytext,(int)yyleng);
520 <VerbatimCode>[\\@]("enddot"|"endcode"|"endmsc"|"enduml") { /* end of verbatim block */
521 copyToOutput(yytext,(int)yyleng);
522 if (&yytext[4]==g_blockName)
524 BEGIN(g_lastCommentContext);
527 <VerbatimCode>^[ \t]*"//"[\!\/]? { /* skip leading comments */
528 if (!g_inSpecialComment)
530 copyToOutput(yytext,(int)yyleng);
535 while (yytext[l]==' ' || yytext[l]=='\t')
539 copyToOutput(yytext,l);
540 if (yyleng-l==3) // ends with //! or ///
542 copyToOutput(" * ",3);
546 copyToOutput("//",2);
550 <Verbatim,VerbatimCode>[^@\/\\\n{}]* { /* any character not a backslash or new line or } */
551 copyToOutput(yytext,(int)yyleng);
553 <Verbatim,VerbatimCode>\n { /* new line in verbatim block */
554 copyToOutput(yytext,(int)yyleng);
556 <Verbatim>^[ \t]*"///" {
557 if (g_blockName=="dot" || g_blockName=="msc" || g_blockName=="startuml" || g_blockName.at(0)=='f')
559 // see bug 487871, strip /// from dot images and formulas.
561 while (yytext[l]==' ' || yytext[l]=='\t')
565 copyToOutput(yytext,l);
568 else // even slashes are verbatim (e.g. \verbatim, \code)
573 <Verbatim,VerbatimCode>. { /* any other character */
574 copyToOutput(yytext,(int)yyleng);
576 <SkipString>\\. { /* escaped character in string */
577 copyToOutput(yytext,(int)yyleng);
579 <SkipString>"\"" { /* end of string */
580 copyToOutput(yytext,(int)yyleng);
581 BEGIN(g_stringContext);
583 <SkipString>. { /* any other string character */
584 copyToOutput(yytext,(int)yyleng);
586 <SkipString>\n { /* new line inside string (illegal for some compilers) */
587 copyToOutput(yytext,(int)yyleng);
589 <SkipChar>\\. { /* escaped character */
590 copyToOutput(yytext,(int)yyleng);
592 <SkipChar>' { /* end of character literal */
593 copyToOutput(yytext,(int)yyleng);
594 BEGIN(g_charContext);
596 <SkipChar>. { /* any other string character */
597 copyToOutput(yytext,(int)yyleng);
599 <SkipChar>\n { /* new line character */
600 copyToOutput(yytext,(int)yyleng);
603 <CComment>[^\\!@*\n{\"\/]* { /* anything that is not a '*' or command */
604 copyToOutput(yytext,(int)yyleng);
606 <CComment>"*"+[^*/\\@\n{\"]* { /* stars without slashes */
607 copyToOutput(yytext,(int)yyleng);
609 <CComment>"\"\"\"" { /* end of Python docstring */
610 if (g_lang!=SrcLangExt_Python)
616 g_pythonDocString = FALSE;
617 copyToOutput(yytext,(int)yyleng);
621 <CComment>\n { /* new line in comment */
622 copyToOutput(yytext,(int)yyleng);
623 /* in case of Fortran always end of comment */
624 if (g_lang==SrcLangExt_Fortran)
629 <CComment>"/"+"*" { /* nested C comment */
631 g_commentStack.push(new CommentCtx(g_lineNr));
632 copyToOutput(yytext,(int)yyleng);
634 <CComment>"*"+"/" { /* end of C comment */
635 if (g_lang==SrcLangExt_Python)
641 copyToOutput(yytext,(int)yyleng);
642 if (g_nestingCount<=0)
649 delete g_commentStack.pop();
653 <CComment>"\n"/[ \t]*[^#] { /* end of Python comment */
654 if (g_lang!=SrcLangExt_Python || g_pythonDocString)
660 copyToOutput(yytext,(int)yyleng);
664 <CComment>"\n"/[ \t]*[^\-] { /* end of VHDL comment */
665 if (g_lang!=SrcLangExt_VHDL)
671 copyToOutput(yytext,(int)yyleng);
675 /* removed for bug 674842 (bug was introduced in rev 768)
677 g_charContext = YY_START;
678 copyToOutput(yytext,(int)yyleng);
682 g_stringContext = YY_START;
683 copyToOutput(yytext,(int)yyleng);
688 copyToOutput(yytext,(int)yyleng);
690 <SComment>^[ \t]*"///"[\/]*/\n {
693 <SComment>\n[ \t]*"///"[\/]*/\n {
696 <SComment>^[ \t]*"///"[^\/\n]/.*\n {
698 g_readLineCtx=YY_START;
701 <SComment>\n[ \t]*"///"[^\/\n]/.*\n {
703 g_readLineCtx=YY_START;
706 <SComment>^[ \t]*"//!" | // just //!
707 <SComment>^[ \t]*"//!<"/.*\n | // or //!< something
708 <SComment>^[ \t]*"//!"[^<]/.*\n { // or //!something
710 g_readLineCtx=YY_START;
713 <SComment>\n[ \t]*"//!" |
714 <SComment>\n[ \t]*"//!<"/.*\n |
715 <SComment>\n[ \t]*"//!"[^<\n]/.*\n {
717 g_readLineCtx=YY_START;
720 <SComment>^[ \t]*"//##"/.*\n {
721 if (!g_inRoseComment)
728 g_readLineCtx=YY_START;
732 <SComment>\n[ \t]*"//##"/.*\n {
733 if (!g_inRoseComment)
740 g_readLineCtx=YY_START;
744 <SComment>\n { /* end of special comment */
745 copyToOutput(" */",3);
746 copyToOutput(yytext,(int)yyleng);
747 g_inSpecialComment=FALSE;
748 g_inRoseComment=FALSE;
751 <ReadLine>[^\\@\n]*/\n {
752 copyToOutput(yytext,(int)yyleng);
753 BEGIN(g_readLineCtx);
755 <CComment,ReadLine>[\\@][\\@][~a-z_A-Z][a-z_A-Z0-9]*[ \t]* { // escaped command
756 copyToOutput(yytext,(int)yyleng);
758 <CComment,ReadLine>[\\@]"cond"/[^a-z_A-Z0-9] { // conditional section
759 g_condCtx = YY_START;
762 <CComment,ReadLine>[\\@]"endcond"/[^a-z_A-Z0-9] { // end of conditional section
765 if (YY_START==CComment && oldSkip && !g_skip)
767 //printf("** Adding start of comment!\n");
768 if (g_lang!=SrcLangExt_Python &&
769 g_lang!=SrcLangExt_VHDL &&
770 g_lang!=SrcLangExt_Fortran)
774 if (g_specialComment)
781 <CondLine>[!()&| \ta-z_A-Z0-9.\-]+ {
783 startCondSection(yytext);
784 if ((g_condCtx==CComment || g_readLineCtx==SComment) &&
787 if (g_lang!=SrcLangExt_Python &&
788 g_lang!=SrcLangExt_VHDL &&
789 g_lang!=SrcLangExt_Fortran)
795 if (g_readLineCtx==SComment)
805 <CComment,ReadLine>[\\@]"cond"[ \t\r]*/\n |
806 <CondLine>. { // forgot section id?
807 if (YY_START!=CondLine) g_condCtx=YY_START;
809 startCondSection(" "); // fake section id causing the section to be hidden unconditionally
810 if ((g_condCtx==CComment || g_readLineCtx==SComment) &&
813 //printf("** Adding terminator for comment!\n");
814 if (g_lang!=SrcLangExt_Python &&
815 g_lang!=SrcLangExt_VHDL)
821 if (*yytext=='\n') g_lineNr++;
822 if (g_readLineCtx==SComment)
831 <CComment,ReadLine>[\\@][a-z_A-Z][a-z_A-Z0-9]* { // expand alias without arguments
832 replaceAliases(yytext);
834 <CComment,ReadLine>[\\@][a-z_A-Z][a-z_A-Z0-9]*"{" { // expand alias with arguments
835 g_lastBlockContext=YY_START;
837 g_aliasString=yytext;
839 BEGIN( ReadAliasArgs );
841 <ReadAliasArgs>^[ \t]*"//"[/!]/[^\n]+ { // skip leading special comments (see bug 618079)
843 <ReadAliasArgs>"*/" { // oops, end of comment in the middle of an alias?
844 if (g_lang==SrcLangExt_Python)
848 else // abort the alias, restart scanning
850 copyToOutput(g_aliasString,g_aliasString.length());
851 copyToOutput(yytext,(int)yyleng);
855 <ReadAliasArgs>[^{}\n\\\*]+ {
856 g_aliasString+=yytext;
859 <ReadAliasArgs>"\\" {
860 if (g_lastEscaped) g_lastEscaped=FALSE;
861 else g_lastEscaped=TRUE;
862 g_aliasString+=yytext;
865 g_aliasString+=yytext;
870 g_aliasString+=yytext;
871 if (!g_lastEscaped) g_blockCount++;
875 g_aliasString+=yytext;
876 if (!g_lastEscaped) g_blockCount--;
879 replaceAliases(g_aliasString);
880 BEGIN( g_lastBlockContext );
885 g_aliasString+=yytext;
889 copyToOutput(yytext,(int)yyleng);
894 void replaceComment(int offset)
896 if (g_mlBrief || g_skip)
898 copyToOutput(yytext,(int)yyleng);
902 //printf("replaceComment(%s)\n",yytext);
903 int i=computeIndent(&yytext[offset]);
904 if (i==g_blockHeadCol)
906 replaceCommentMarker(yytext,(int)yyleng);
910 copyToOutput(" */",3);
911 int i;for (i=(int)yyleng-1;i>=0;i--) unput(yytext[i]);
912 g_inSpecialComment=FALSE;
918 // simplified way to know if this is fixed form
919 // duplicate in fortrancode.l
920 static bool recognizeFixedForm(const char* contents)
928 switch(contents[i]) {
940 if(column==1) return TRUE;
944 if(column>1 && column<7) return FALSE;
949 if(column==7) return TRUE;
957 /*! This function does three things:
958 * -# It converts multi-line C++ style comment blocks (that are aligned)
959 * to C style comment blocks (if MULTILINE_CPP_IS_BRIEF is set to NO).
960 * -# It replaces aliases with their definition (see ALIASES)
961 * -# It handles conditional sections (cond...endcond blocks)
963 void convertCppComments(BufStr *inBuf,BufStr *outBuf,const char *fileName)
965 //printf("convertCppComments(%s)\n",fileName);
970 g_mlBrief = Config_getBool("MULTILINE_CPP_IS_BRIEF");
972 g_fileName = fileName;
973 g_lang = getLanguageFromFileName(fileName);
974 g_pythonDocString = FALSE;
977 g_condStack.setAutoDelete(TRUE);
978 g_commentStack.clear();
979 g_commentStack.setAutoDelete(TRUE);
981 printlex(yy_flex_debug, TRUE, __FILE__, fileName);
983 if (g_lang==SrcLangExt_Fortran)
985 isFixedForm = recognizeFixedForm(inBuf->data());
988 if (g_lang==SrcLangExt_Markdown)
992 g_commentStack.push(new CommentCtx(g_lineNr));
999 while (!g_condStack.isEmpty())
1001 CondCtx *ctx = g_condStack.pop();
1002 QCString sectionInfo = " ";
1003 if (ctx->sectionId!=" ") sectionInfo.sprintf(" with label %s ",ctx->sectionId.data());
1004 warn(g_fileName,ctx->lineNr,"Conditional section%sdoes not have "
1005 "a corresponding \\endcond command within this file.",sectionInfo.data());
1007 if (g_nestingCount>0 || (YY_START==CComment && g_lang!=SrcLangExt_Markdown))
1009 QCString tmp= "(probable line reference: ";
1011 while (!g_commentStack.isEmpty())
1013 CommentCtx *ctx = g_commentStack.pop();
1014 if (!first) tmp += ", ";
1015 tmp += QCString().setNum(ctx->lineNr);
1020 warn(g_fileName,g_lineNr,"Reached end of file while still inside a (nested) comment. "
1021 "Nesting level %d %s",g_nestingCount+1,tmp.data()); // add one for "normal" expected end of comment
1023 g_commentStack.clear();
1024 if (Debug::isFlagSet(Debug::CommentCnv))
1026 g_outBuf->at(g_outBuf->curPos())='\0';
1027 msg("-------------\n%s\n-------------\n",g_outBuf->data());
1029 printlex(yy_flex_debug, FALSE, __FILE__, fileName);
1033 //----------------------------------------------------------------------------
1034 #if !defined(YY_FLEX_SUBMINOR_VERSION)
1035 extern "C" { // some bogus code to keep the compiler happy
1036 void commentcnvYYdummy() { yy_flex_realloc(0,0); }