From 2972f6ddc491f615f814b1277ec715b6f707c419 Mon Sep 17 00:00:00 2001 From: Dimitri van Heesch Date: Sun, 17 Nov 2019 13:15:09 +0100 Subject: Made commentcnv.l reentrant --- src/commentcnv.l | 959 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 493 insertions(+), 466 deletions(-) diff --git a/src/commentcnv.l b/src/commentcnv.l index 3e5faba..14abb8f 100644 --- a/src/commentcnv.l +++ b/src/commentcnv.l @@ -16,6 +16,8 @@ */ %option never-interactive %option prefix="commentcnvYY" +%option reentrant +%option extra-type="struct commentcnvYY_state *" %{ @@ -41,8 +43,8 @@ #define YY_NO_INPUT 1 #define YY_NO_UNISTD_H 1 -#define ADDCHAR(c) g_outBuf->addChar(c) -#define ADDARRAY(a,s) g_outBuf->addArray(a,s) +#define ADDCHAR(c) yyextra->outBuf->addChar(c) +#define ADDARRAY(a,s) yyextra->outBuf->addArray(a,s) struct CondCtx { @@ -60,182 +62,60 @@ struct CommentCtx int lineNr; }; -static BufStr * g_inBuf; -static BufStr * g_outBuf; -static int g_inBufPos; -static int g_col; -static int g_blockHeadCol; -static bool g_mlBrief; -static int g_readLineCtx; -static bool g_skip; -static QCString g_fileName; -static int g_lineNr; -static int g_condCtx; -static QStack g_condStack; -static QStack g_commentStack; -static QCString g_blockName; -static int g_lastCommentContext; -static bool g_inSpecialComment; -static bool g_inRoseComment; -static int g_stringContext; -static int g_charContext; -static int g_javaBlock; -static bool g_specialComment; +struct commentcnvYY_state +{ + BufStr * inBuf = 0; + BufStr * outBuf = 0; + int inBufPos = 0; + int col = 0; + int blockHeadCol = 0; + bool mlBrief = FALSE; + int readLineCtx = 0; + bool skip = FALSE; + QCString fileName; + int lineNr = 0; + int condCtx = 0; + QStack condStack; + QStack commentStack; + QCString blockName; + int lastCommentContext = 0; + bool inSpecialComment = FALSE; + bool inRoseComment= FALSE; + int stringContext = 0; + int charContext = 0; + int javaBlock = 0; + bool specialComment = FALSE; -static QCString g_aliasString; -static int g_blockCount; -static bool g_lastEscaped; -static int g_lastBlockContext; -static bool g_pythonDocString; -static int g_nestingCount; + QCString aliasString; + int blockCount = 0; + bool lastEscaped = FALSE; + int lastBlockContext= 0; + bool pythonDocString = FALSE; + int nestingCount= 0; -static bool g_vhdl; // for VHDL old style --! comment + bool vhdl = FALSE; // for VHDL old style --! comment -static SrcLangExt g_lang; -static bool isFixedForm; // For Fortran + SrcLangExt lang = SrcLangExt_Unknown; + bool isFixedForm = FALSE; // For Fortran +}; static const char *stateToString(int state); +static inline int computeIndent(const char *s); -static void replaceCommentMarker(const char *s,int len) -{ - const char *p=s; - char c; - // copy leading blanks - while ((c=*p) && (c==' ' || c=='\t' || c=='\n')) - { - ADDCHAR(c); - g_lineNr += c=='\n'; - p++; - } - // replace start of comment marker by blanks and the last character by a * - int blanks=0; - while ((c=*p) && (c=='/' || c=='!' || c=='#')) - { - blanks++; - p++; - if (*p=='<') // comment-after-item marker - { - blanks++; - p++; - } - if (c=='!') // end after first ! - { - break; - } - } - if (blanks>0) - { - while (blanks>2) - { - ADDCHAR(' '); - blanks--; - } - if (blanks>1) ADDCHAR('*'); - ADDCHAR(' '); - } - // copy comment line to output - ADDARRAY(p,len-(int)(p-s)); -} +static void replaceCommentMarker(yyscan_t yyscanner,const char *s,int len); +static inline void copyToOutput(yyscan_t yyscanner,const char *s,int len); +static void startCondSection(yyscan_t yyscanner,const char *sectId); +static void endCondSection(yyscan_t yyscanner); +static void replaceAliases(yyscan_t yyscanner,const char *s); +static int yyread(yyscan_t yyscanner,char *buf,int max_size); +static void replaceComment(yyscan_t yyscanner,int offset); -static inline int computeIndent(const char *s) -{ - int col=0; - static int tabSize=Config_getInt(TAB_SIZE); - const char *p=s; - char c; - while ((c=*p++)) - { - if (c==' ') col++; - else if (c=='\t') col+=tabSize-(col%tabSize); - else break; - } - return col; -} -static inline void copyToOutput(const char *s,int len) -{ - int i; - if (g_skip) // only add newlines. - { - for (i=0;i skip %d\n",g_lineNr); - g_lineNr++; - } - } - } - else if (len>0) - { - ADDARRAY(s,len); - static int tabSize=Config_getInt(TAB_SIZE); - for (i=0;i copy %d\n",g_lineNr); - g_lineNr++; break; - case '\t': g_col+=tabSize-(g_col%tabSize); break; - default: g_col++; break; - } - } - } -} - -static void startCondSection(const char *sectId) -{ - //printf("startCondSection: skip=%d stack=%d\n",g_skip,g_condStack.count()); - CondParser prs; - bool expResult = prs.parse(g_fileName,g_lineNr,sectId); - g_condStack.push(new CondCtx(g_lineNr,sectId,g_skip)); - if (!expResult) // not enabled - { - g_skip=TRUE; - } -} - -static void endCondSection() -{ - if (g_condStack.isEmpty()) - { - warn(g_fileName,g_lineNr,"Found \\endcond command without matching \\cond"); - g_skip=FALSE; - } - else - { - CondCtx *ctx = g_condStack.pop(); - g_skip=ctx->skip; - } - //printf("endCondSection: skip=%d stack=%d\n",g_skip,g_condStack.count()); -} - -/** copies string \a s with length \a len to the output, while - * replacing any alias commands found in the string. - */ -static void replaceAliases(const char *s) -{ - QCString result = resolveAliasCmd(s); - //printf("replaceAliases(%s)->'%s'\n",s,result.data()); - copyToOutput(result,result.length()); -} #undef YY_INPUT -#define YY_INPUT(buf,result,max_size) result=yyread(buf,max_size); +#define YY_INPUT(buf,result,max_size) result=yyread(yyscanner,buf,max_size); -static int yyread(char *buf,int max_size) -{ - int bytesInBuf = g_inBuf->curPos()-g_inBufPos; - int bytesToCopy = QMIN(max_size,bytesInBuf); - memcpy(buf,g_inBuf->data()+g_inBufPos,bytesToCopy); - g_inBufPos+=bytesToCopy; - return bytesToCopy; -} - -void replaceComment(int offset); %} @@ -255,55 +135,55 @@ void replaceComment(int offset); %% [^"'!\/\n\\#,\-]* { /* eat anything that is not " / , or \n */ - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); } [,] { /* eat , so we have a nice separator in long initialization lines */ - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); } "\"\"\""! { /* start of python long comment */ - if (g_lang!=SrcLangExt_Python) + if (yyextra->lang!=SrcLangExt_Python) { REJECT; } else { - g_pythonDocString = TRUE; - g_nestingCount=1; - g_commentStack.clear(); /* to be on the save side */ - copyToOutput(yytext,(int)yyleng); + yyextra->pythonDocString = TRUE; + yyextra->nestingCount=1; + yyextra->commentStack.clear(); /* to be on the save side */ + copyToOutput(yyscanner,yytext,(int)yyleng); BEGIN(CComment); - g_commentStack.push(new CommentCtx(g_lineNr)); + yyextra->commentStack.push(new CommentCtx(yyextra->lineNr)); } } ![>lang!=SrcLangExt_Fortran) { REJECT; } else { - copyToOutput(yytext,(int)yyleng); - g_nestingCount=0; // Fortran doesn't have an end comment - g_commentStack.clear(); /* to be on the save side */ + copyToOutput(yyscanner,yytext,(int)yyleng); + yyextra->nestingCount=0; // Fortran doesn't have an end comment + yyextra->commentStack.clear(); /* to be on the save side */ BEGIN(CComment); - g_commentStack.push(new CommentCtx(g_lineNr)); + yyextra->commentStack.push(new CommentCtx(yyextra->lineNr)); } } [Cc\*][>lang!=SrcLangExt_Fortran) { REJECT; } else { /* check for fixed format; we might have some conditional as part of multiline if like C<5 .and. & */ - if (isFixedForm && (g_col == 0)) + if (yyextra->isFixedForm && (yyextra->col == 0)) { - copyToOutput(yytext,(int)yyleng); - g_nestingCount=0; // Fortran doesn't have an end comment - g_commentStack.clear(); /* to be on the safe side */ + copyToOutput(yyscanner,yytext,(int)yyleng); + yyextra->nestingCount=0; // Fortran doesn't have an end comment + yyextra->commentStack.clear(); /* to be on the safe side */ BEGIN(CComment); - g_commentStack.push(new CommentCtx(g_lineNr)); + yyextra->commentStack.push(new CommentCtx(yyextra->lineNr)); } else { @@ -312,25 +192,25 @@ void replaceComment(int offset); } } !.*\n { - if (g_lang!=SrcLangExt_Fortran) + if (yyextra->lang!=SrcLangExt_Fortran) { REJECT; } else { - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); } } [Cc\*].*\n { - if (g_lang!=SrcLangExt_Fortran) + if (yyextra->lang!=SrcLangExt_Fortran) { REJECT; } else { - if (g_col == 0) + if (yyextra->col == 0) { - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); } else { @@ -339,24 +219,24 @@ void replaceComment(int offset); } } "\"" { /* start of a string */ - copyToOutput(yytext,(int)yyleng); - g_stringContext = YY_START; + copyToOutput(yyscanner,yytext,(int)yyleng); + yyextra->stringContext = YY_START; BEGIN(SkipString); } ' { - copyToOutput(yytext,(int)yyleng); - g_charContext = YY_START; - if (g_lang!=SrcLangExt_VHDL) + copyToOutput(yyscanner,yytext,(int)yyleng); + yyextra->charContext = YY_START; + if (yyextra->lang!=SrcLangExt_VHDL) { BEGIN(SkipChar); } } \n { /* new line */ - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); } "//!"/.*\n[ \t]*"//"[\/!][^\/] | /* start C++ style special comment block */ ("///"[/]*)/[^/].*\n[ \t]*"//"[\/!][^\/] { /* start C++ style special comment block */ - if (g_mlBrief) + if (yyextra->mlBrief) { REJECT; // bail out if we do not need to convert } @@ -367,190 +247,190 @@ void replaceComment(int offset); { while (i<(int)yyleng && yytext[i]=='/') i++; } - g_blockHeadCol=g_col; - copyToOutput("/**",3); - replaceAliases(yytext+i); - g_inSpecialComment=TRUE; + yyextra->blockHeadCol=yyextra->col; + copyToOutput(yyscanner,"/**",3); + replaceAliases(yyscanner,yytext+i); + yyextra->inSpecialComment=TRUE; //BEGIN(SComment); - g_readLineCtx=SComment; + yyextra->readLineCtx=SComment; BEGIN(ReadLine); } } "//##Documentation".*/\n { /* Start of Rational Rose ANSI C++ comment block */ - if (g_mlBrief) REJECT; + if (yyextra->mlBrief) REJECT; int i=17; //=strlen("//##Documentation"); - g_blockHeadCol=g_col; - copyToOutput("/**",3); - replaceAliases(yytext+i); - g_inRoseComment=TRUE; + yyextra->blockHeadCol=yyextra->col; + copyToOutput(yyscanner,"/**",3); + replaceAliases(yyscanner,yytext+i); + yyextra->inRoseComment=TRUE; BEGIN(SComment); } "//"[!\/]/.*\n[ \t]*"//"[|\/][ \t]*[@\\]"}" { // next line contains an end marker, see bug 752712 - g_inSpecialComment=yytext[2]=='/' || yytext[2]=='!'; - copyToOutput(yytext,(int)yyleng); - g_readLineCtx=YY_START; + yyextra->inSpecialComment=yytext[2]=='/' || yytext[2]=='!'; + copyToOutput(yyscanner,yytext,(int)yyleng); + yyextra->readLineCtx=YY_START; BEGIN(ReadLine); } "//"/.*\n { /* one line C++ comment */ - g_inSpecialComment=yytext[2]=='/' || yytext[2]=='!'; - copyToOutput(yytext,(int)yyleng); - g_readLineCtx=YY_START; + yyextra->inSpecialComment=yytext[2]=='/' || yytext[2]=='!'; + copyToOutput(yyscanner,yytext,(int)yyleng); + yyextra->readLineCtx=YY_START; BEGIN(ReadLine); } "/**/" { /* avoid matching next rule for empty C comment, see bug 711723 */ - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); } "/*"[*!]? { /* start of a C comment */ - if ((g_lang==SrcLangExt_Python) || (g_lang==SrcLangExt_Tcl)) + if ((yyextra->lang==SrcLangExt_Python) || (yyextra->lang==SrcLangExt_Tcl)) { REJECT; } - g_specialComment=(int)yyleng==3; - g_nestingCount=1; - g_commentStack.clear(); /* to be on the save side */ - copyToOutput(yytext,(int)yyleng); + yyextra->specialComment=(int)yyleng==3; + yyextra->nestingCount=1; + yyextra->commentStack.clear(); /* to be on the save side */ + copyToOutput(yyscanner,yytext,(int)yyleng); BEGIN(CComment); - g_commentStack.push(new CommentCtx(g_lineNr)); + yyextra->commentStack.push(new CommentCtx(yyextra->lineNr)); } "#"("#")? { - if (g_lang!=SrcLangExt_Python) + if (yyextra->lang!=SrcLangExt_Python) { REJECT; } else { - copyToOutput(yytext,(int)yyleng); - g_nestingCount=0; // Python doesn't have an end comment for # - g_commentStack.clear(); /* to be on the save side */ + copyToOutput(yyscanner,yytext,(int)yyleng); + yyextra->nestingCount=0; // Python doesn't have an end comment for # + yyextra->commentStack.clear(); /* to be on the save side */ BEGIN(CComment); - g_commentStack.push(new CommentCtx(g_lineNr)); + yyextra->commentStack.push(new CommentCtx(yyextra->lineNr)); } } "--!" { - if (g_lang!=SrcLangExt_VHDL) + if (yyextra->lang!=SrcLangExt_VHDL) { REJECT; } else { - g_vhdl = TRUE; - copyToOutput(yytext,(int)yyleng); - g_nestingCount=0; // VHDL doesn't have an end comment - g_commentStack.clear(); /* to be on the save side */ + yyextra->vhdl = TRUE; + copyToOutput(yyscanner,yytext,(int)yyleng); + yyextra->nestingCount=0; // VHDL doesn't have an end comment + yyextra->commentStack.clear(); /* to be on the save side */ BEGIN(CComment); - g_commentStack.push(new CommentCtx(g_lineNr)); + yyextra->commentStack.push(new CommentCtx(yyextra->lineNr)); } } ![>lang!=SrcLangExt_Fortran) { REJECT; } else { - copyToOutput(yytext,(int)yyleng); - g_nestingCount=0; // Fortran doesn't have an end comment - g_commentStack.clear(); /* to be on the save side */ + copyToOutput(yyscanner,yytext,(int)yyleng); + yyextra->nestingCount=0; // Fortran doesn't have an end comment + yyextra->commentStack.clear(); /* to be on the save side */ BEGIN(CComment); - g_commentStack.push(new CommentCtx(g_lineNr)); + yyextra->commentStack.push(new CommentCtx(yyextra->lineNr)); } } "{@code"/[ \t\n] { - copyToOutput("@code",5); - g_lastCommentContext = YY_START; - g_javaBlock=1; - g_blockName=&yytext[1]; + copyToOutput(yyscanner,"@code",5); + yyextra->lastCommentContext = YY_START; + yyextra->javaBlock=1; + yyextra->blockName=&yytext[1]; BEGIN(VerbatimCode); } [\\@]("dot"|"code"|"msc"|"startuml")/[^a-z_A-Z0-9] { /* start of a verbatim block */ - copyToOutput(yytext,(int)yyleng); - g_lastCommentContext = YY_START; - g_javaBlock=0; + copyToOutput(yyscanner,yytext,(int)yyleng); + yyextra->lastCommentContext = YY_START; + yyextra->javaBlock=0; if (qstrcmp(&yytext[1],"startuml")==0) { - g_blockName="uml"; + yyextra->blockName="uml"; } else { - g_blockName=&yytext[1]; + yyextra->blockName=&yytext[1]; } BEGIN(VerbatimCode); } [\\@]("f$"|"f["|"f{") { - copyToOutput(yytext,(int)yyleng); - g_blockName=&yytext[1]; - if (g_blockName.at(1)=='[') + copyToOutput(yyscanner,yytext,(int)yyleng); + yyextra->blockName=&yytext[1]; + if (yyextra->blockName.at(1)=='[') { - g_blockName.at(1)=']'; + yyextra->blockName.at(1)=']'; } - else if (g_blockName.at(1)=='{') + else if (yyextra->blockName.at(1)=='{') { - g_blockName.at(1)='}'; + yyextra->blockName.at(1)='}'; } - g_lastCommentContext = YY_START; + yyextra->lastCommentContext = YY_START; BEGIN(Verbatim); } [\\@]("verbatim"|"latexonly"|"htmlonly"|"xmlonly"|"docbookonly"|"rtfonly"|"manonly")/[^a-z_A-Z0-9] { /* start of a verbatim block */ - copyToOutput(yytext,(int)yyleng); - g_blockName=&yytext[1]; - g_lastCommentContext = YY_START; + copyToOutput(yyscanner,yytext,(int)yyleng); + yyextra->blockName=&yytext[1]; + yyextra->lastCommentContext = YY_START; BEGIN(Verbatim); } . { /* any other character */ - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); } [\\@]("endverbatim"|"endlatexonly"|"endhtmlonly"|"endxmlonly"|"enddocbookonly"|"endrtfonly"|"endmanonly"|"f$"|"f]"|"f}") { /* end of verbatim block */ - copyToOutput(yytext,(int)yyleng); - if (&yytext[1]==g_blockName) // end of formula + copyToOutput(yyscanner,yytext,(int)yyleng); + if (&yytext[1]==yyextra->blockName) // end of formula { - BEGIN(g_lastCommentContext); + BEGIN(yyextra->lastCommentContext); } - else if (&yytext[4]==g_blockName) + else if (&yytext[4]==yyextra->blockName) { - BEGIN(g_lastCommentContext); + BEGIN(yyextra->lastCommentContext); } } "{" { - if (g_javaBlock==0) + if (yyextra->javaBlock==0) { REJECT; } else { - g_javaBlock++; - copyToOutput(yytext,(int)yyleng); + yyextra->javaBlock++; + copyToOutput(yyscanner,yytext,(int)yyleng); } } "}" { - if (g_javaBlock==0) + if (yyextra->javaBlock==0) { REJECT; } else { - g_javaBlock--; - if (g_javaBlock==0) + yyextra->javaBlock--; + if (yyextra->javaBlock==0) { - copyToOutput(" @endcode ",10); - BEGIN(g_lastCommentContext); + copyToOutput(yyscanner," @endcode ",10); + BEGIN(yyextra->lastCommentContext); } else { - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); } } } [\\@]("enddot"|"endcode"|"endmsc"|"enduml") { /* end of verbatim block */ - copyToOutput(yytext,(int)yyleng); - if (&yytext[4]==g_blockName) + copyToOutput(yyscanner,yytext,(int)yyleng); + if (&yytext[4]==yyextra->blockName) { - BEGIN(g_lastCommentContext); + BEGIN(yyextra->lastCommentContext); } } ^[ \t]*"//"[\!\/]? { /* skip leading comments */ - if (!g_inSpecialComment) + if (!yyextra->inSpecialComment) { - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); } else { @@ -559,25 +439,25 @@ void replaceComment(int offset); { l++; } - copyToOutput(yytext,l); + copyToOutput(yyscanner,yytext,l); if (yyleng-l==3) // ends with //! or /// { - copyToOutput(" * ",3); + copyToOutput(yyscanner," * ",3); } else // ends with // { - copyToOutput("//",2); + copyToOutput(yyscanner,"//",2); } } } [^@\/\\\n{}]* { /* any character not a backslash or new line or } */ - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); } \n { /* new line in verbatim block */ - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); } ^[ \t]*"///" { - if (g_blockName=="dot" || g_blockName=="msc" || g_blockName=="uml" || g_blockName.at(0)=='f') + if (yyextra->blockName=="dot" || yyextra->blockName=="msc" || yyextra->blockName=="uml" || yyextra->blockName.at(0)=='f') { // see bug 487871, strip /// from dot images and formulas. int l=0; @@ -585,8 +465,8 @@ void replaceComment(int offset); { l++; } - copyToOutput(yytext,l); - copyToOutput(" ",3); + copyToOutput(yyscanner,yytext,l); + copyToOutput(yyscanner," ",3); } else // even slashes are verbatim (e.g. \verbatim, \code) { @@ -594,119 +474,119 @@ void replaceComment(int offset); } } . { /* any other character */ - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); } \\. { /* escaped character in string */ - if (g_lang==SrcLangExt_Fortran) + if (yyextra->lang==SrcLangExt_Fortran) { unput(yytext[1]); - copyToOutput(yytext,1); + copyToOutput(yyscanner,yytext,1); } else { - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); } } "\"" { /* end of string */ - copyToOutput(yytext,(int)yyleng); - BEGIN(g_stringContext); + copyToOutput(yyscanner,yytext,(int)yyleng); + BEGIN(yyextra->stringContext); } . { /* any other string character */ - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); } \n { /* new line inside string (illegal for some compilers) */ - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); } \\. { /* escaped character */ - if (g_lang==SrcLangExt_Fortran) + if (yyextra->lang==SrcLangExt_Fortran) { unput(yytext[1]); - copyToOutput(yytext,1); + copyToOutput(yyscanner,yytext,1); } else { - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); } } ' { /* end of character literal */ - copyToOutput(yytext,(int)yyleng); - BEGIN(g_charContext); + copyToOutput(yyscanner,yytext,(int)yyleng); + BEGIN(yyextra->charContext); } . { /* any other string character */ - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); } \n { /* new line character */ - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); } [^\\!@*\n{\"\/]* { /* anything that is not a '*' or command */ - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); } "*"+[^*/\\@\n{\"]* { /* stars without slashes */ - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); } "\"\"\"" { /* end of Python docstring */ - if (g_lang!=SrcLangExt_Python) + if (yyextra->lang!=SrcLangExt_Python) { REJECT; } else { - g_nestingCount--; - g_pythonDocString = FALSE; - copyToOutput(yytext,(int)yyleng); + yyextra->nestingCount--; + yyextra->pythonDocString = FALSE; + copyToOutput(yyscanner,yytext,(int)yyleng); BEGIN(Scan); } } \n { /* new line in comment */ - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); /* in case of Fortran always end of comment */ - if (g_lang==SrcLangExt_Fortran) + if (yyextra->lang==SrcLangExt_Fortran) { BEGIN(Scan); } } "/"+"*" { /* nested C comment */ - if ((g_lang==SrcLangExt_Python) || (g_lang==SrcLangExt_Tcl)) + if ((yyextra->lang==SrcLangExt_Python) || (yyextra->lang==SrcLangExt_Tcl)) { REJECT; } - g_nestingCount++; - g_commentStack.push(new CommentCtx(g_lineNr)); - copyToOutput(yytext,(int)yyleng); + yyextra->nestingCount++; + yyextra->commentStack.push(new CommentCtx(yyextra->lineNr)); + copyToOutput(yyscanner,yytext,(int)yyleng); } "*"+"/" { /* end of C comment */ - if ((g_lang==SrcLangExt_Python) || (g_lang==SrcLangExt_Tcl)) + if ((yyextra->lang==SrcLangExt_Python) || (yyextra->lang==SrcLangExt_Tcl)) { REJECT; } else { - copyToOutput(yytext,(int)yyleng); - g_nestingCount--; - if (g_nestingCount<=0) + copyToOutput(yyscanner,yytext,(int)yyleng); + yyextra->nestingCount--; + if (yyextra->nestingCount<=0) { BEGIN(Scan); } else { - //g_nestingCount--; - delete g_commentStack.pop(); + //yyextra->nestingCount--; + delete yyextra->commentStack.pop(); } } } /* Python an VHDL share CComment, so special attention for ending comments is required */ "\n"/[ \t]*"#" { - if (g_lang!=SrcLangExt_VHDL) + if (yyextra->lang!=SrcLangExt_VHDL) { REJECT; } else { - if (g_vhdl) // inside --! comment + if (yyextra->vhdl) // inside --! comment { - g_vhdl = FALSE; - copyToOutput(yytext,(int)yyleng); + yyextra->vhdl = FALSE; + copyToOutput(yyscanner,yytext,(int)yyleng); BEGIN(Scan); } else // C-type comment @@ -716,35 +596,35 @@ void replaceComment(int offset); } } "\n"/[ \t]*"-" { - if (g_lang!=SrcLangExt_Python || g_pythonDocString) + if (yyextra->lang!=SrcLangExt_Python || yyextra->pythonDocString) { REJECT; } else { - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); BEGIN(Scan); } } "\n"/[ \t]*[^ \t#\-] { - if (g_lang==SrcLangExt_Python) + if (yyextra->lang==SrcLangExt_Python) { - if (g_pythonDocString) + if (yyextra->pythonDocString) { REJECT; } else { - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); BEGIN(Scan); } } - else if (g_lang==SrcLangExt_VHDL) + else if (yyextra->lang==SrcLangExt_VHDL) { - if (g_vhdl) // inside --! comment + if (yyextra->vhdl) // inside --! comment { - g_vhdl = FALSE; - copyToOutput(yytext,(int)yyleng); + yyextra->vhdl = FALSE; + copyToOutput(yyscanner,yytext,(int)yyleng); BEGIN(Scan); } else // C-type comment @@ -759,122 +639,122 @@ void replaceComment(int offset); } /* removed for bug 674842 (bug was introduced in rev 768) "'" { - g_charContext = YY_START; - copyToOutput(yytext,(int)yyleng); + yyextra->charContext = YY_START; + copyToOutput(yyscanner,yytext,(int)yyleng); BEGIN(SkipChar); } "\"" { - g_stringContext = YY_START; - copyToOutput(yytext,(int)yyleng); + yyextra->stringContext = YY_START; + copyToOutput(yyscanner,yytext,(int)yyleng); BEGIN(SkipString); } */ . { - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); } ^[ \t]*"///"[\/]*/\n { - replaceComment(0); + replaceComment(yyscanner,0); } \n[ \t]*"///"[\/]*/\n { - replaceComment(1); + replaceComment(yyscanner,1); } ^[ \t]*"///"[^\/\n]/.*\n { - replaceComment(0); - g_readLineCtx=YY_START; + replaceComment(yyscanner,0); + yyextra->readLineCtx=YY_START; BEGIN(ReadLine); } \n[ \t]*"//"[\/!]("<")?[ \t]*[\\@]"}".*\n { /* See Bug 752712: end the multiline comment when finding a @} or \} command */ - copyToOutput(" */",3); - copyToOutput(yytext,(int)yyleng); - g_inSpecialComment=FALSE; - g_inRoseComment=FALSE; + copyToOutput(yyscanner," */",3); + copyToOutput(yyscanner,yytext,(int)yyleng); + yyextra->inSpecialComment=FALSE; + yyextra->inRoseComment=FALSE; BEGIN(Scan); } \n[ \t]*"///"[^\/\n]/.*\n { - replaceComment(1); - g_readLineCtx=YY_START; + replaceComment(yyscanner,1); + yyextra->readLineCtx=YY_START; BEGIN(ReadLine); } ^[ \t]*"//!" | // just //! ^[ \t]*"//!<"/.*\n | // or //!< something ^[ \t]*"//!"[^<]/.*\n { // or //!something - replaceComment(0); - g_readLineCtx=YY_START; + replaceComment(yyscanner,0); + yyextra->readLineCtx=YY_START; BEGIN(ReadLine); } \n[ \t]*"//!" | \n[ \t]*"//!<"/.*\n | \n[ \t]*"//!"[^<\n]/.*\n { - replaceComment(1); - g_readLineCtx=YY_START; + replaceComment(yyscanner,1); + yyextra->readLineCtx=YY_START; BEGIN(ReadLine); } ^[ \t]*"//##"/.*\n { - if (!g_inRoseComment) + if (!yyextra->inRoseComment) { REJECT; } else { - replaceComment(0); - g_readLineCtx=YY_START; + replaceComment(yyscanner,0); + yyextra->readLineCtx=YY_START; BEGIN(ReadLine); } } \n[ \t]*"//##"/.*\n { - if (!g_inRoseComment) + if (!yyextra->inRoseComment) { REJECT; } else { - replaceComment(1); - g_readLineCtx=YY_START; + replaceComment(yyscanner,1); + yyextra->readLineCtx=YY_START; BEGIN(ReadLine); } } \n { /* end of special comment */ - copyToOutput(" */",3); - copyToOutput(yytext,(int)yyleng); - g_inSpecialComment=FALSE; - g_inRoseComment=FALSE; + copyToOutput(yyscanner," */",3); + copyToOutput(yyscanner,yytext,(int)yyleng); + yyextra->inSpecialComment=FALSE; + yyextra->inRoseComment=FALSE; BEGIN(Scan); } "*/" { - copyToOutput("*‍/",7); + copyToOutput(yyscanner,"*‍/",7); } "*" { - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); } [^\\@\n\*]* { - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); } [^\\@\n\*]*/\n { - copyToOutput(yytext,(int)yyleng); - BEGIN(g_readLineCtx); + copyToOutput(yyscanner,yytext,(int)yyleng); + BEGIN(yyextra->readLineCtx); } [\\@][\\@][~a-z_A-Z][a-z_A-Z0-9]*[ \t]* { // escaped command - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); } [\\@]"cond"/[^a-z_A-Z0-9] { // conditional section - g_condCtx = YY_START; + yyextra->condCtx = YY_START; BEGIN(CondLine); } [\\@]"endcond"/[^a-z_A-Z0-9] { // end of conditional section - bool oldSkip=g_skip; - endCondSection(); - if (YY_START==CComment && oldSkip && !g_skip) + bool oldSkip=yyextra->skip; + endCondSection(yyscanner); + if (YY_START==CComment && oldSkip && !yyextra->skip) { //printf("** Adding start of comment!\n"); - if (g_lang!=SrcLangExt_Python && - g_lang!=SrcLangExt_VHDL && - g_lang!=SrcLangExt_Markdown && - g_lang!=SrcLangExt_Fortran) + if (yyextra->lang!=SrcLangExt_Python && + yyextra->lang!=SrcLangExt_VHDL && + yyextra->lang!=SrcLangExt_Markdown && + yyextra->lang!=SrcLangExt_Fortran) { ADDCHAR('/'); ADDCHAR('*'); - if (g_specialComment) + if (yyextra->specialComment) { ADDCHAR('*'); } @@ -882,142 +762,284 @@ void replaceComment(int offset); } } [!()&| \ta-z_A-Z0-9.\-]+ { - bool oldSkip=g_skip; - startCondSection(yytext); - if ((g_condCtx==CComment || g_readLineCtx==SComment) && - !oldSkip && g_skip) + bool oldSkip=yyextra->skip; + startCondSection(yyscanner,yytext); + if ((yyextra->condCtx==CComment || yyextra->readLineCtx==SComment) && + !oldSkip && yyextra->skip) { - if (g_lang!=SrcLangExt_Python && - g_lang!=SrcLangExt_VHDL && - g_lang!=SrcLangExt_Markdown && - g_lang!=SrcLangExt_Fortran) + if (yyextra->lang!=SrcLangExt_Python && + yyextra->lang!=SrcLangExt_VHDL && + yyextra->lang!=SrcLangExt_Markdown && + yyextra->lang!=SrcLangExt_Fortran) { ADDCHAR('*'); ADDCHAR('/'); } } - if (g_readLineCtx==SComment) + if (yyextra->readLineCtx==SComment) { BEGIN(SComment); } else { - BEGIN(g_condCtx); + BEGIN(yyextra->condCtx); } } [ \t]* [\\@]"cond"[ \t\r]*/\n | . { // forgot section id? - if (YY_START!=CondLine) g_condCtx=YY_START; - bool oldSkip=g_skip; - startCondSection(" "); // fake section id causing the section to be hidden unconditionally - if ((g_condCtx==CComment || g_readLineCtx==SComment) && - !oldSkip && g_skip) + if (YY_START!=CondLine) yyextra->condCtx=YY_START; + bool oldSkip=yyextra->skip; + startCondSection(yyscanner," "); // fake section id causing the section to be hidden unconditionally + if ((yyextra->condCtx==CComment || yyextra->readLineCtx==SComment) && + !oldSkip && yyextra->skip) { //printf("** Adding terminator for comment!\n"); - if (g_lang!=SrcLangExt_Python && - g_lang!=SrcLangExt_VHDL && - g_lang!=SrcLangExt_Markdown && - g_lang!=SrcLangExt_Fortran) + if (yyextra->lang!=SrcLangExt_Python && + yyextra->lang!=SrcLangExt_VHDL && + yyextra->lang!=SrcLangExt_Markdown && + yyextra->lang!=SrcLangExt_Fortran) { ADDCHAR('*'); ADDCHAR('/'); } } - if (*yytext=='\n') g_lineNr++; - if (g_readLineCtx==SComment) + if (*yytext=='\n') yyextra->lineNr++; + if (yyextra->readLineCtx==SComment) { BEGIN(SComment); } else { - BEGIN(g_condCtx); + BEGIN(yyextra->condCtx); } } [\\@][a-z_A-Z][a-z_A-Z0-9]* { // expand alias without arguments - replaceAliases(yytext); + replaceAliases(yyscanner,yytext); } [\\@][a-z_A-Z][a-z_A-Z0-9]*"{" { // expand alias with arguments - g_lastBlockContext=YY_START; - g_blockCount=1; - g_aliasString=yytext; - g_lastEscaped=0; + yyextra->lastBlockContext=YY_START; + yyextra->blockCount=1; + yyextra->aliasString=yytext; + yyextra->lastEscaped=0; BEGIN( ReadAliasArgs ); } ^[ \t]*"//"[/!]/[^\n]+ { // skip leading special comments (see bug 618079) } "*/" { // oops, end of comment in the middle of an alias? - if (g_lang==SrcLangExt_Python) + if (yyextra->lang==SrcLangExt_Python) { REJECT; } else // abort the alias, restart scanning { - copyToOutput(g_aliasString,g_aliasString.length()); - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yyextra->aliasString,yyextra->aliasString.length()); + copyToOutput(yyscanner,yytext,(int)yyleng); BEGIN(Scan); } } [^{}\n\\\*]+ { - g_aliasString+=yytext; - g_lastEscaped=FALSE; + yyextra->aliasString+=yytext; + yyextra->lastEscaped=FALSE; } "\\" { - if (g_lastEscaped) g_lastEscaped=FALSE; - else g_lastEscaped=TRUE; - g_aliasString+=yytext; + if (yyextra->lastEscaped) yyextra->lastEscaped=FALSE; + else yyextra->lastEscaped=TRUE; + yyextra->aliasString+=yytext; } \n { - g_aliasString+=yytext; - g_lineNr++; - g_lastEscaped=FALSE; + yyextra->aliasString+=yytext; + yyextra->lineNr++; + yyextra->lastEscaped=FALSE; } "{" { - g_aliasString+=yytext; - if (!g_lastEscaped) g_blockCount++; - g_lastEscaped=FALSE; + yyextra->aliasString+=yytext; + if (!yyextra->lastEscaped) yyextra->blockCount++; + yyextra->lastEscaped=FALSE; } "}" { - g_aliasString+=yytext; - if (!g_lastEscaped) g_blockCount--; - if (g_blockCount==0) + yyextra->aliasString+=yytext; + if (!yyextra->lastEscaped) yyextra->blockCount--; + if (yyextra->blockCount==0) { - replaceAliases(g_aliasString); - BEGIN( g_lastBlockContext ); + replaceAliases(yyscanner,yyextra->aliasString); + BEGIN( yyextra->lastBlockContext ); } - g_lastEscaped=FALSE; + yyextra->lastEscaped=FALSE; } . { - g_aliasString+=yytext; - g_lastEscaped=FALSE; + yyextra->aliasString+=yytext; + yyextra->lastEscaped=FALSE; } . { - copyToOutput(yytext,(int)yyleng); + copyToOutput(yyscanner,yytext,(int)yyleng); } %% -void replaceComment(int offset) +static void replaceCommentMarker(yyscan_t yyscanner,const char *s,int len) { - if (g_mlBrief || g_skip) + struct yyguts_t *yyg = (struct yyguts_t*)yyscanner; + const char *p=s; + char c; + // copy leading blanks + while ((c=*p) && (c==' ' || c=='\t' || c=='\n')) + { + ADDCHAR(c); + yyextra->lineNr += c=='\n'; + p++; + } + // replace start of comment marker by blanks and the last character by a * + int blanks=0; + while ((c=*p) && (c=='/' || c=='!' || c=='#')) + { + blanks++; + p++; + if (*p=='<') // comment-after-item marker + { + blanks++; + p++; + } + if (c=='!') // end after first ! + { + break; + } + } + if (blanks>0) + { + while (blanks>2) + { + ADDCHAR(' '); + blanks--; + } + if (blanks>1) ADDCHAR('*'); + ADDCHAR(' '); + } + // copy comment line to output + ADDARRAY(p,len-(int)(p-s)); +} + +static inline int computeIndent(const char *s) +{ + int col=0; + static int tabSize=Config_getInt(TAB_SIZE); + const char *p=s; + char c; + while ((c=*p++)) + { + if (c==' ') col++; + else if (c=='\t') col+=tabSize-(col%tabSize); + else break; + } + return col; +} + +static inline void copyToOutput(yyscan_t yyscanner,const char *s,int len) +{ + struct yyguts_t *yyg = (struct yyguts_t*)yyscanner; + int i; + if (yyextra->skip) // only add newlines. + { + for (i=0;i skip %d\n",g_lineNr); + yyextra->lineNr++; + } + } + } + else if (len>0) + { + ADDARRAY(s,len); + static int tabSize=Config_getInt(TAB_SIZE); + for (i=0;icol=0; + //fprintf(stderr,"---> copy %d\n",g_lineNr); + yyextra->lineNr++; break; + case '\t': yyextra->col+=tabSize-(yyextra->col%tabSize); break; + default: yyextra->col++; break; + } + } + } +} + +static void startCondSection(yyscan_t yyscanner,const char *sectId) +{ + struct yyguts_t *yyg = (struct yyguts_t*)yyscanner; + //printf("startCondSection: skip=%d stack=%d\n",g_skip,g_condStack.count()); + CondParser prs; + bool expResult = prs.parse(yyextra->fileName,yyextra->lineNr,sectId); + yyextra->condStack.push(new CondCtx(yyextra->lineNr,sectId,yyextra->skip)); + if (!expResult) // not enabled { - copyToOutput(yytext,(int)yyleng); + yyextra->skip=TRUE; + } +} + +static void endCondSection(yyscan_t yyscanner) +{ + struct yyguts_t *yyg = (struct yyguts_t*)yyscanner; + if (yyextra->condStack.isEmpty()) + { + warn(yyextra->fileName,yyextra->lineNr,"Found \\endcond command without matching \\cond"); + yyextra->skip=FALSE; + } + else + { + CondCtx *ctx = yyextra->condStack.pop(); + yyextra->skip=ctx->skip; + } + //printf("endCondSection: skip=%d stack=%d\n",g_skip,g_condStack.count()); +} + +/** copies string \a s with length \a len to the output, while + * replacing any alias commands found in the string. + */ +static void replaceAliases(yyscan_t yyscanner,const char *s) +{ + QCString result = resolveAliasCmd(s); + //printf("replaceAliases(%s)->'%s'\n",s,result.data()); + copyToOutput(yyscanner,result,result.length()); +} + + +static int yyread(yyscan_t yyscanner,char *buf,int max_size) +{ + struct yyguts_t *yyg = (struct yyguts_t*)yyscanner; + int bytesInBuf = yyextra->inBuf->curPos()-yyextra->inBufPos; + int bytesToCopy = QMIN(max_size,bytesInBuf); + memcpy(buf,yyextra->inBuf->data()+yyextra->inBufPos,bytesToCopy); + yyextra->inBufPos+=bytesToCopy; + return bytesToCopy; +} + +static void replaceComment(yyscan_t yyscanner,int offset) +{ + struct yyguts_t *yyg = (struct yyguts_t*)yyscanner; + if (yyextra->mlBrief || yyextra->skip) + { + copyToOutput(yyscanner,yytext,(int)yyleng); } else { //printf("replaceComment(%s)\n",yytext); int i=computeIndent(&yytext[offset]); - if (i==g_blockHeadCol) + if (i==yyextra->blockHeadCol) { - replaceCommentMarker(yytext,(int)yyleng); + replaceCommentMarker(yyscanner,yytext,(int)yyleng); } else { - copyToOutput(" */",3); + copyToOutput(yyscanner," */",3); int i;for (i=(int)yyleng-1;i>=0;i--) unput(yytext[i]); - g_inSpecialComment=FALSE; - BEGIN(Scan); - } + yyextra->inSpecialComment=FALSE; + BEGIN(Scan); + } } } @@ -1068,75 +1090,80 @@ static bool recognizeFixedForm(const char* contents) */ void convertCppComments(BufStr *inBuf,BufStr *outBuf,const char *fileName) { + yyscan_t yyscanner; + commentcnvYY_state extra; + commentcnvYYlex_init_extra(&extra,&yyscanner); + struct yyguts_t *yyg = (struct yyguts_t*)yyscanner; //printf("convertCppComments(%s)\n",fileName); - g_inBuf = inBuf; - g_outBuf = outBuf; - g_inBufPos = 0; - g_col = 0; - g_mlBrief = Config_getBool(MULTILINE_CPP_IS_BRIEF); - g_skip = FALSE; - g_fileName = fileName; - g_lang = getLanguageFromFileName(fileName); - g_pythonDocString = FALSE; - g_lineNr = 1; - g_condStack.clear(); - g_condStack.setAutoDelete(TRUE); - g_commentStack.clear(); - g_commentStack.setAutoDelete(TRUE); - g_vhdl = FALSE; + yyextra->inBuf = inBuf; + yyextra->outBuf = outBuf; + yyextra->inBufPos = 0; + yyextra->col = 0; + yyextra->mlBrief = Config_getBool(MULTILINE_CPP_IS_BRIEF); + yyextra->skip = FALSE; + yyextra->fileName = fileName; + yyextra->lang = getLanguageFromFileName(fileName); + yyextra->pythonDocString = FALSE; + yyextra->lineNr = 1; + yyextra->condStack.clear(); + yyextra->condStack.setAutoDelete(TRUE); + yyextra->commentStack.clear(); + yyextra->commentStack.setAutoDelete(TRUE); + yyextra->vhdl = FALSE; printlex(yy_flex_debug, TRUE, __FILE__, fileName); - isFixedForm = FALSE; - if (g_lang==SrcLangExt_Fortran) + yyextra->isFixedForm = FALSE; + if (yyextra->lang==SrcLangExt_Fortran) { - isFixedForm = recognizeFixedForm(inBuf->data()); + yyextra->isFixedForm = recognizeFixedForm(inBuf->data()); } - if (g_lang==SrcLangExt_Markdown) + if (yyextra->lang==SrcLangExt_Markdown) { - g_nestingCount=0; + yyextra->nestingCount=0; BEGIN(CComment); - g_commentStack.push(new CommentCtx(g_lineNr)); + yyextra->commentStack.push(new CommentCtx(yyextra->lineNr)); } else { BEGIN(Scan); } - yylex(); - while (!g_condStack.isEmpty()) + yylex(yyscanner); + while (!yyextra->condStack.isEmpty()) { - CondCtx *ctx = g_condStack.pop(); + CondCtx *ctx = yyextra->condStack.pop(); QCString sectionInfo = " "; if (ctx->sectionId!=" ") sectionInfo.sprintf(" with label '%s' ",ctx->sectionId.stripWhiteSpace().data()); - warn(g_fileName,ctx->lineNr,"Conditional section%sdoes not have " + warn(yyextra->fileName,ctx->lineNr,"Conditional section%sdoes not have " "a corresponding \\endcond command within this file.",sectionInfo.data()); } - if (g_nestingCount>0 && g_lang!=SrcLangExt_Markdown) + if (yyextra->nestingCount>0 && yyextra->lang!=SrcLangExt_Markdown) { QCString tmp= "(probable line reference: "; bool first = TRUE; - while (!g_commentStack.isEmpty()) + while (!yyextra->commentStack.isEmpty()) { - CommentCtx *ctx = g_commentStack.pop(); + CommentCtx *ctx = yyextra->commentStack.pop(); if (!first) tmp += ", "; tmp += QCString().setNum(ctx->lineNr); first = FALSE; delete ctx; } tmp += ")"; - warn(g_fileName,g_lineNr,"Reached end of file while still inside a (nested) comment. " - "Nesting level %d %s",g_nestingCount,tmp.data()); + warn(yyextra->fileName,yyextra->lineNr,"Reached end of file while still inside a (nested) comment. " + "Nesting level %d %s",yyextra->nestingCount,tmp.data()); } - g_commentStack.clear(); - g_nestingCount = 0; + yyextra->commentStack.clear(); + yyextra->nestingCount = 0; if (Debug::isFlagSet(Debug::CommentCnv)) { - g_outBuf->at(g_outBuf->curPos())='\0'; + yyextra->outBuf->at(yyextra->outBuf->curPos())='\0'; Debug::print(Debug::CommentCnv,0,"-----------\nCommentCnv: %s\n" - "output=[\n%s]\n-----------\n",fileName,g_outBuf->data() + "output=[\n%s]\n-----------\n",fileName,yyextra->outBuf->data() ); } printlex(yy_flex_debug, FALSE, __FILE__, fileName); + commentcnvYYlex_destroy(yyscanner); } -- cgit v0.12